1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
22 * Dr Stephen Henson <stephen.henson@gemplus.com>
23 * Dr Vipul Gupta <vipul.gupta@sun.com>, Sun Microsystems Laboratories
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 * This file maps various PKCS #11 Mechanisms to related mechanisms, key
40 * types, and ASN.1 encodings.
53 /*************************************************************
54 * local static and global data
55 *************************************************************/
58 * Tables used for Extended mechanism mapping (currently not used)
61 CK_MECHANISM_TYPE keyGen
;
63 CK_MECHANISM_TYPE type
;
68 static pk11MechanismData pk11_default
=
69 { CKM_GENERIC_SECRET_KEY_GEN
, CKK_GENERIC_SECRET
, CKM_FAKE_RANDOM
, 8, 8 };
70 static pk11MechanismData
*pk11_MechanismTable
= NULL
;
71 static int pk11_MechTableSize
= 0;
72 static int pk11_MechEntrySize
= 0;
75 * list of mechanisms we're willing to wrap secret keys with.
76 * This list is ordered by preference.
78 CK_MECHANISM_TYPE wrapMechanismList
[] = {
95 int wrapMechanismCount
= sizeof(wrapMechanismList
)/sizeof(wrapMechanismList
[0]);
97 /*********************************************************************
98 * Mechanism Mapping functions
99 *********************************************************************/
102 * lookup an entry in the mechanism table. If none found, return the
105 static pk11MechanismData
*
106 pk11_lookup(CK_MECHANISM_TYPE type
)
109 for (i
=0; i
< pk11_MechEntrySize
; i
++) {
110 if (pk11_MechanismTable
[i
].type
== type
) {
111 return (&pk11_MechanismTable
[i
]);
114 return &pk11_default
;
118 * find the best key wrap mechanism for this slot.
121 PK11_GetBestWrapMechanism(PK11SlotInfo
*slot
)
124 for (i
=0; i
< wrapMechanismCount
; i
++) {
125 if (PK11_DoesMechanism(slot
,wrapMechanismList
[i
])) {
126 return wrapMechanismList
[i
];
129 return CKM_INVALID_MECHANISM
;
133 * NOTE: This is not thread safe. Called at init time, and when loading
134 * a new Entry. It is reasonably safe as long as it is not re-entered
135 * (readers will always see a consistant table)
137 * This routine is called to add entries to the mechanism table, once there,
138 * they can not be removed.
141 PK11_AddMechanismEntry(CK_MECHANISM_TYPE type
, CK_KEY_TYPE key
,
142 CK_MECHANISM_TYPE keyGen
, int ivLen
, int blockSize
)
144 int tableSize
= pk11_MechTableSize
;
145 int size
= pk11_MechEntrySize
;
147 pk11MechanismData
*old
= pk11_MechanismTable
;
148 pk11MechanismData
*newt
= pk11_MechanismTable
;
151 if (size
> tableSize
) {
152 int oldTableSize
= tableSize
;
154 newt
= PORT_NewArray(pk11MechanismData
, tableSize
);
155 if (newt
== NULL
) return;
157 if (old
) PORT_Memcpy(newt
, old
, oldTableSize
*sizeof(*newt
));
160 newt
[entry
].type
= type
;
161 newt
[entry
].keyType
= key
;
162 newt
[entry
].keyGen
= keyGen
;
163 newt
[entry
].iv
= ivLen
;
164 newt
[entry
].blockSize
= blockSize
;
166 pk11_MechanismTable
= newt
;
167 pk11_MechTableSize
= tableSize
;
168 pk11_MechEntrySize
= size
;
169 if (old
) PORT_Free(old
);
173 * Get the key type needed for the given mechanism
176 PK11_GetKeyMechanism(CK_KEY_TYPE type
)
180 return CKM_CAMELLIA_CBC
;
186 return CKM_DES3_KEY_GEN
;
188 return CKM_DES2_KEY_GEN
;
198 return CKM_SKIPJACK_CBC64
;
200 return CKM_BATON_CBC128
;
202 return CKM_JUNIPER_CBC128
;
208 return CKM_CAST3_CBC
;
210 return CKM_CAST5_CBC
;
216 return CKM_DH_PKCS_DERIVE
;
218 return CKM_KEA_KEY_DERIVE
;
219 case CKK_EC
: /* CKK_ECDSA is deprecated */
221 case CKK_GENERIC_SECRET
:
223 return CKM_SHA_1_HMAC
;
228 * Get the key type needed for the given mechanism
231 PK11_GetKeyType(CK_MECHANISM_TYPE type
,unsigned long len
)
234 case CKM_CAMELLIA_ECB
:
235 case CKM_CAMELLIA_CBC
:
236 case CKM_CAMELLIA_MAC
:
237 case CKM_CAMELLIA_MAC_GENERAL
:
238 case CKM_CAMELLIA_CBC_PAD
:
239 case CKM_CAMELLIA_KEY_GEN
:
244 case CKM_AES_MAC_GENERAL
:
245 case CKM_AES_CBC_PAD
:
246 case CKM_AES_KEY_GEN
:
247 case CKM_NETSCAPE_AES_KEY_WRAP
:
248 case CKM_NETSCAPE_AES_KEY_WRAP_PAD
:
253 case CKM_DES_MAC_GENERAL
:
254 case CKM_DES_CBC_PAD
:
255 case CKM_DES_KEY_GEN
:
256 case CKM_KEY_WRAP_LYNKS
:
257 case CKM_PBE_MD2_DES_CBC
:
258 case CKM_PBE_MD5_DES_CBC
:
263 case CKM_DES3_MAC_GENERAL
:
264 case CKM_DES3_CBC_PAD
:
265 return (len
== 16) ? CKK_DES2
: CKK_DES3
;
266 case CKM_DES2_KEY_GEN
:
267 case CKM_PBE_SHA1_DES2_EDE_CBC
:
269 case CKM_PBE_SHA1_DES3_EDE_CBC
:
270 case CKM_DES3_KEY_GEN
:
275 case CKM_CDMF_MAC_GENERAL
:
276 case CKM_CDMF_CBC_PAD
:
277 case CKM_CDMF_KEY_GEN
:
282 case CKM_RC2_MAC_GENERAL
:
283 case CKM_RC2_CBC_PAD
:
284 case CKM_RC2_KEY_GEN
:
285 case CKM_PBE_SHA1_RC2_128_CBC
:
286 case CKM_PBE_SHA1_RC2_40_CBC
:
289 case CKM_RC4_KEY_GEN
:
294 case CKM_RC5_MAC_GENERAL
:
295 case CKM_RC5_CBC_PAD
:
296 case CKM_RC5_KEY_GEN
:
298 case CKM_SKIPJACK_CBC64
:
299 case CKM_SKIPJACK_ECB64
:
300 case CKM_SKIPJACK_OFB64
:
301 case CKM_SKIPJACK_CFB64
:
302 case CKM_SKIPJACK_CFB32
:
303 case CKM_SKIPJACK_CFB16
:
304 case CKM_SKIPJACK_CFB8
:
305 case CKM_SKIPJACK_KEY_GEN
:
306 case CKM_SKIPJACK_WRAP
:
307 case CKM_SKIPJACK_PRIVATE_WRAP
:
309 case CKM_BATON_ECB128
:
310 case CKM_BATON_ECB96
:
311 case CKM_BATON_CBC128
:
312 case CKM_BATON_COUNTER
:
313 case CKM_BATON_SHUFFLE
:
315 case CKM_BATON_KEY_GEN
:
317 case CKM_JUNIPER_ECB128
:
318 case CKM_JUNIPER_CBC128
:
319 case CKM_JUNIPER_COUNTER
:
320 case CKM_JUNIPER_SHUFFLE
:
321 case CKM_JUNIPER_WRAP
:
322 case CKM_JUNIPER_KEY_GEN
:
327 case CKM_IDEA_MAC_GENERAL
:
328 case CKM_IDEA_CBC_PAD
:
329 case CKM_IDEA_KEY_GEN
:
334 case CKM_CAST_MAC_GENERAL
:
335 case CKM_CAST_CBC_PAD
:
336 case CKM_CAST_KEY_GEN
:
337 case CKM_PBE_MD5_CAST_CBC
:
342 case CKM_CAST3_MAC_GENERAL
:
343 case CKM_CAST3_CBC_PAD
:
344 case CKM_CAST3_KEY_GEN
:
345 case CKM_PBE_MD5_CAST3_CBC
:
350 case CKM_CAST5_MAC_GENERAL
:
351 case CKM_CAST5_CBC_PAD
:
352 case CKM_CAST5_KEY_GEN
:
353 case CKM_PBE_MD5_CAST5_CBC
:
358 case CKM_MD2_RSA_PKCS
:
359 case CKM_MD5_RSA_PKCS
:
360 case CKM_SHA1_RSA_PKCS
:
361 case CKM_SHA256_RSA_PKCS
:
362 case CKM_SHA384_RSA_PKCS
:
363 case CKM_SHA512_RSA_PKCS
:
364 case CKM_KEY_WRAP_SET_OAEP
:
365 case CKM_RSA_PKCS_KEY_PAIR_GEN
:
366 case CKM_RSA_X9_31_KEY_PAIR_GEN
:
370 case CKM_DSA_KEY_PAIR_GEN
:
372 case CKM_DH_PKCS_DERIVE
:
373 case CKM_DH_PKCS_KEY_PAIR_GEN
:
375 case CKM_KEA_KEY_DERIVE
:
376 case CKM_KEA_KEY_PAIR_GEN
:
380 case CKM_EC_KEY_PAIR_GEN
: /* aka CKM_ECDSA_KEY_PAIR_GEN */
381 case CKM_ECDH1_DERIVE
:
382 return CKK_EC
; /* CKK_ECDSA is deprecated */
383 case CKM_SSL3_PRE_MASTER_KEY_GEN
:
384 case CKM_GENERIC_SECRET_KEY_GEN
:
385 case CKM_SSL3_MASTER_KEY_DERIVE
:
386 case CKM_SSL3_MASTER_KEY_DERIVE_DH
:
387 case CKM_SSL3_KEY_AND_MAC_DERIVE
:
388 case CKM_SSL3_SHA1_MAC
:
389 case CKM_SSL3_MD5_MAC
:
390 case CKM_TLS_MASTER_KEY_DERIVE
:
391 case CKM_TLS_MASTER_KEY_DERIVE_DH
:
392 case CKM_TLS_KEY_AND_MAC_DERIVE
:
394 case CKM_SHA_1_HMAC_GENERAL
:
395 case CKM_SHA256_HMAC
:
396 case CKM_SHA256_HMAC_GENERAL
:
397 case CKM_SHA384_HMAC
:
398 case CKM_SHA384_HMAC_GENERAL
:
399 case CKM_SHA512_HMAC
:
400 case CKM_SHA512_HMAC_GENERAL
:
402 case CKM_MD2_HMAC_GENERAL
:
404 case CKM_MD5_HMAC_GENERAL
:
405 case CKM_TLS_PRF_GENERAL
:
406 return CKK_GENERIC_SECRET
;
408 return pk11_lookup(type
)->keyType
;
413 * Get the Key Gen Mechanism needed for the given
417 PK11_GetKeyGen(CK_MECHANISM_TYPE type
)
419 return PK11_GetKeyGenWithSize(type
, 0);
423 PK11_GetKeyGenWithSize(CK_MECHANISM_TYPE type
, int size
)
426 case CKM_CAMELLIA_ECB
:
427 case CKM_CAMELLIA_CBC
:
428 case CKM_CAMELLIA_MAC
:
429 case CKM_CAMELLIA_MAC_GENERAL
:
430 case CKM_CAMELLIA_CBC_PAD
:
431 case CKM_CAMELLIA_KEY_GEN
:
432 return CKM_CAMELLIA_KEY_GEN
;
436 case CKM_AES_MAC_GENERAL
:
437 case CKM_AES_CBC_PAD
:
438 case CKM_AES_KEY_GEN
:
439 return CKM_AES_KEY_GEN
;
443 case CKM_DES_MAC_GENERAL
:
444 case CKM_KEY_WRAP_LYNKS
:
445 case CKM_DES_CBC_PAD
:
446 case CKM_DES_KEY_GEN
:
447 return CKM_DES_KEY_GEN
;
451 case CKM_DES3_MAC_GENERAL
:
452 case CKM_DES3_CBC_PAD
:
453 return (size
== 16) ? CKM_DES2_KEY_GEN
: CKM_DES3_KEY_GEN
;
454 case CKM_DES3_KEY_GEN
:
455 return CKM_DES3_KEY_GEN
;
456 case CKM_DES2_KEY_GEN
:
457 return CKM_DES2_KEY_GEN
;
461 case CKM_CDMF_MAC_GENERAL
:
462 case CKM_CDMF_CBC_PAD
:
463 case CKM_CDMF_KEY_GEN
:
464 return CKM_CDMF_KEY_GEN
;
468 case CKM_RC2_MAC_GENERAL
:
469 case CKM_RC2_CBC_PAD
:
470 case CKM_RC2_KEY_GEN
:
471 return CKM_RC2_KEY_GEN
;
473 case CKM_RC4_KEY_GEN
:
474 return CKM_RC4_KEY_GEN
;
478 case CKM_RC5_MAC_GENERAL
:
479 case CKM_RC5_CBC_PAD
:
480 case CKM_RC5_KEY_GEN
:
481 return CKM_RC5_KEY_GEN
;
482 case CKM_SKIPJACK_CBC64
:
483 case CKM_SKIPJACK_ECB64
:
484 case CKM_SKIPJACK_OFB64
:
485 case CKM_SKIPJACK_CFB64
:
486 case CKM_SKIPJACK_CFB32
:
487 case CKM_SKIPJACK_CFB16
:
488 case CKM_SKIPJACK_CFB8
:
489 case CKM_SKIPJACK_WRAP
:
490 case CKM_SKIPJACK_KEY_GEN
:
491 return CKM_SKIPJACK_KEY_GEN
;
492 case CKM_BATON_ECB128
:
493 case CKM_BATON_ECB96
:
494 case CKM_BATON_CBC128
:
495 case CKM_BATON_COUNTER
:
496 case CKM_BATON_SHUFFLE
:
498 case CKM_BATON_KEY_GEN
:
499 return CKM_BATON_KEY_GEN
;
500 case CKM_JUNIPER_ECB128
:
501 case CKM_JUNIPER_CBC128
:
502 case CKM_JUNIPER_COUNTER
:
503 case CKM_JUNIPER_SHUFFLE
:
504 case CKM_JUNIPER_WRAP
:
505 case CKM_JUNIPER_KEY_GEN
:
506 return CKM_JUNIPER_KEY_GEN
;
510 case CKM_IDEA_MAC_GENERAL
:
511 case CKM_IDEA_CBC_PAD
:
512 case CKM_IDEA_KEY_GEN
:
513 return CKM_IDEA_KEY_GEN
;
517 case CKM_CAST_MAC_GENERAL
:
518 case CKM_CAST_CBC_PAD
:
519 case CKM_CAST_KEY_GEN
:
520 return CKM_CAST_KEY_GEN
;
524 case CKM_CAST3_MAC_GENERAL
:
525 case CKM_CAST3_CBC_PAD
:
526 case CKM_CAST3_KEY_GEN
:
527 return CKM_CAST3_KEY_GEN
;
531 case CKM_CAST5_MAC_GENERAL
:
532 case CKM_CAST5_CBC_PAD
:
533 case CKM_CAST5_KEY_GEN
:
534 return CKM_CAST5_KEY_GEN
;
538 case CKM_MD2_RSA_PKCS
:
539 case CKM_MD5_RSA_PKCS
:
540 case CKM_SHA1_RSA_PKCS
:
541 case CKM_SHA256_RSA_PKCS
:
542 case CKM_SHA384_RSA_PKCS
:
543 case CKM_SHA512_RSA_PKCS
:
544 case CKM_KEY_WRAP_SET_OAEP
:
545 case CKM_RSA_PKCS_KEY_PAIR_GEN
:
546 return CKM_RSA_PKCS_KEY_PAIR_GEN
;
547 case CKM_RSA_X9_31_KEY_PAIR_GEN
:
548 return CKM_RSA_X9_31_KEY_PAIR_GEN
;
551 case CKM_DSA_KEY_PAIR_GEN
:
552 return CKM_DSA_KEY_PAIR_GEN
;
553 case CKM_DH_PKCS_DERIVE
:
554 case CKM_DH_PKCS_KEY_PAIR_GEN
:
555 return CKM_DH_PKCS_KEY_PAIR_GEN
;
556 case CKM_KEA_KEY_DERIVE
:
557 case CKM_KEA_KEY_PAIR_GEN
:
558 return CKM_KEA_KEY_PAIR_GEN
;
561 case CKM_EC_KEY_PAIR_GEN
: /* aka CKM_ECDSA_KEY_PAIR_GEN */
562 case CKM_ECDH1_DERIVE
:
563 return CKM_EC_KEY_PAIR_GEN
;
564 case CKM_SSL3_PRE_MASTER_KEY_GEN
:
565 case CKM_SSL3_MASTER_KEY_DERIVE
:
566 case CKM_SSL3_KEY_AND_MAC_DERIVE
:
567 case CKM_SSL3_SHA1_MAC
:
568 case CKM_SSL3_MD5_MAC
:
569 case CKM_TLS_MASTER_KEY_DERIVE
:
570 case CKM_TLS_KEY_AND_MAC_DERIVE
:
571 return CKM_SSL3_PRE_MASTER_KEY_GEN
;
573 case CKM_SHA_1_HMAC_GENERAL
:
574 case CKM_SHA256_HMAC
:
575 case CKM_SHA256_HMAC_GENERAL
:
576 case CKM_SHA384_HMAC
:
577 case CKM_SHA384_HMAC_GENERAL
:
578 case CKM_SHA512_HMAC
:
579 case CKM_SHA512_HMAC_GENERAL
:
581 case CKM_MD2_HMAC_GENERAL
:
583 case CKM_MD5_HMAC_GENERAL
:
584 case CKM_TLS_PRF_GENERAL
:
585 case CKM_GENERIC_SECRET_KEY_GEN
:
586 return CKM_GENERIC_SECRET_KEY_GEN
;
587 case CKM_PBE_MD2_DES_CBC
:
588 case CKM_PBE_MD5_DES_CBC
:
589 case CKM_PBA_SHA1_WITH_SHA1_HMAC
:
590 case CKM_NETSCAPE_PBE_SHA1_HMAC_KEY_GEN
:
591 case CKM_NETSCAPE_PBE_MD5_HMAC_KEY_GEN
:
592 case CKM_NETSCAPE_PBE_MD2_HMAC_KEY_GEN
:
593 case CKM_NETSCAPE_PBE_SHA1_DES_CBC
:
594 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC
:
595 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC
:
596 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4
:
597 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4
:
598 case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC
:
599 case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC
:
600 case CKM_PBE_SHA1_RC2_40_CBC
:
601 case CKM_PBE_SHA1_RC2_128_CBC
:
602 case CKM_PBE_SHA1_RC4_40
:
603 case CKM_PBE_SHA1_RC4_128
:
604 case CKM_PBE_SHA1_DES3_EDE_CBC
:
605 case CKM_PBE_SHA1_DES2_EDE_CBC
:
608 return pk11_lookup(type
)->keyGen
;
613 * get the mechanism block size
616 PK11_GetBlockSize(CK_MECHANISM_TYPE type
,SECItem
*params
)
618 CK_RC5_PARAMS
*rc5_params
;
619 CK_RC5_CBC_PARAMS
*rc5_cbc_params
;
622 if ((params
) && (params
->data
)) {
623 rc5_params
= (CK_RC5_PARAMS
*) params
->data
;
624 return (rc5_params
->ulWordsize
)*2;
628 case CKM_RC5_CBC_PAD
:
629 if ((params
) && (params
->data
)) {
630 rc5_cbc_params
= (CK_RC5_CBC_PARAMS
*) params
->data
;
631 return (rc5_cbc_params
->ulWordsize
)*2;
642 case CKM_SKIPJACK_CBC64
:
643 case CKM_SKIPJACK_ECB64
:
644 case CKM_SKIPJACK_OFB64
:
645 case CKM_SKIPJACK_CFB64
:
652 case CKM_DES_CBC_PAD
:
653 case CKM_DES3_CBC_PAD
:
654 case CKM_RC2_CBC_PAD
:
655 case CKM_IDEA_CBC_PAD
:
656 case CKM_CAST_CBC_PAD
:
657 case CKM_CAST3_CBC_PAD
:
658 case CKM_CAST5_CBC_PAD
:
659 case CKM_PBE_MD2_DES_CBC
:
660 case CKM_PBE_MD5_DES_CBC
:
661 case CKM_NETSCAPE_PBE_SHA1_DES_CBC
:
662 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC
:
663 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC
:
664 case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC
:
665 case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC
:
666 case CKM_PBE_SHA1_RC2_40_CBC
:
667 case CKM_PBE_SHA1_RC2_128_CBC
:
668 case CKM_PBE_SHA1_DES3_EDE_CBC
:
669 case CKM_PBE_SHA1_DES2_EDE_CBC
:
671 case CKM_SKIPJACK_CFB32
:
672 case CKM_SKIPJACK_CFB16
:
673 case CKM_SKIPJACK_CFB8
:
675 case CKM_CAMELLIA_ECB
:
676 case CKM_CAMELLIA_CBC
:
677 case CKM_CAMELLIA_CBC_PAD
:
680 case CKM_AES_CBC_PAD
:
681 case CKM_BATON_ECB128
:
682 case CKM_BATON_CBC128
:
683 case CKM_BATON_COUNTER
:
684 case CKM_BATON_SHUFFLE
:
685 case CKM_JUNIPER_ECB128
:
686 case CKM_JUNIPER_CBC128
:
687 case CKM_JUNIPER_COUNTER
:
688 case CKM_JUNIPER_SHUFFLE
:
690 case CKM_BATON_ECB96
:
693 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4
:
694 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4
:
695 case CKM_PBE_SHA1_RC4_40
:
696 case CKM_PBE_SHA1_RC4_128
:
701 /*actually it's the modulus length of the key!*/
702 return -1; /* failure */
704 return pk11_lookup(type
)->blockSize
;
712 PK11_GetIVLength(CK_MECHANISM_TYPE type
)
715 case CKM_CAMELLIA_ECB
:
721 case CKM_SKIPJACK_WRAP
:
732 case CKM_PBE_MD2_DES_CBC
:
733 case CKM_PBE_MD5_DES_CBC
:
734 case CKM_NETSCAPE_PBE_SHA1_DES_CBC
:
735 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC
:
736 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC
:
737 case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC
:
738 case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC
:
739 case CKM_PBE_SHA1_RC2_40_CBC
:
740 case CKM_PBE_SHA1_RC2_128_CBC
:
741 case CKM_PBE_SHA1_DES3_EDE_CBC
:
742 case CKM_PBE_SHA1_DES2_EDE_CBC
:
747 case CKM_RC2_CBC_PAD
:
748 case CKM_DES_CBC_PAD
:
749 case CKM_DES3_CBC_PAD
:
750 case CKM_IDEA_CBC_PAD
:
751 case CKM_RC5_CBC_PAD
:
752 case CKM_CAST_CBC_PAD
:
753 case CKM_CAST3_CBC_PAD
:
754 case CKM_CAST5_CBC_PAD
:
756 case CKM_CAMELLIA_CBC
:
757 case CKM_CAMELLIA_CBC_PAD
:
759 case CKM_AES_CBC_PAD
:
761 case CKM_SKIPJACK_CBC64
:
762 case CKM_SKIPJACK_ECB64
:
763 case CKM_SKIPJACK_OFB64
:
764 case CKM_SKIPJACK_CFB64
:
765 case CKM_SKIPJACK_CFB32
:
766 case CKM_SKIPJACK_CFB16
:
767 case CKM_SKIPJACK_CFB8
:
768 case CKM_BATON_ECB128
:
769 case CKM_BATON_ECB96
:
770 case CKM_BATON_CBC128
:
771 case CKM_BATON_COUNTER
:
772 case CKM_BATON_SHUFFLE
:
773 case CKM_JUNIPER_ECB128
:
774 case CKM_JUNIPER_CBC128
:
775 case CKM_JUNIPER_COUNTER
:
776 case CKM_JUNIPER_SHUFFLE
:
782 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4
:
783 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4
:
784 case CKM_PBE_SHA1_RC4_40
:
785 case CKM_PBE_SHA1_RC4_128
:
788 return pk11_lookup(type
)->iv
;
793 /* These next two utilities are here to help facilitate future
794 * Dynamic Encrypt/Decrypt symetric key mechanisms, and to allow functions
795 * like SSL and S-MIME to automatically add them.
798 PK11_ParamFromIV(CK_MECHANISM_TYPE type
,SECItem
*iv
)
800 CK_RC2_CBC_PARAMS
*rc2_params
= NULL
;
801 CK_RC2_PARAMS
*rc2_ecb_params
= NULL
;
802 CK_RC5_PARAMS
*rc5_params
= NULL
;
803 CK_RC5_CBC_PARAMS
*rc5_cbc_params
= NULL
;
806 param
= (SECItem
*)PORT_Alloc(sizeof(SECItem
));
807 if (param
== NULL
) return NULL
;
812 case CKM_CAMELLIA_ECB
:
827 rc2_ecb_params
= (CK_RC2_PARAMS
*)PORT_Alloc(sizeof(CK_RC2_PARAMS
));
828 if (rc2_ecb_params
== NULL
) break;
829 /* Maybe we should pass the key size in too to get this value? */
830 *rc2_ecb_params
= 128;
831 param
->data
= (unsigned char *) rc2_ecb_params
;
832 param
->len
= sizeof(CK_RC2_PARAMS
);
835 case CKM_RC2_CBC_PAD
:
836 rc2_params
= (CK_RC2_CBC_PARAMS
*)PORT_Alloc(sizeof(CK_RC2_CBC_PARAMS
));
837 if (rc2_params
== NULL
) break;
838 /* Maybe we should pass the key size in too to get this value? */
839 rc2_params
->ulEffectiveBits
= 128;
841 PORT_Memcpy(rc2_params
->iv
,iv
->data
,sizeof(rc2_params
->iv
));
842 param
->data
= (unsigned char *) rc2_params
;
843 param
->len
= sizeof(CK_RC2_CBC_PARAMS
);
846 case CKM_RC5_CBC_PAD
:
847 rc5_cbc_params
= (CK_RC5_CBC_PARAMS
*)
848 PORT_Alloc(sizeof(CK_RC5_CBC_PARAMS
) + ((iv
) ? iv
->len
: 0));
849 if (rc5_cbc_params
== NULL
) break;
850 if (iv
&& iv
->data
&& iv
->len
) {
851 rc5_cbc_params
->pIv
= ((CK_BYTE_PTR
) rc5_cbc_params
)
852 + sizeof(CK_RC5_CBC_PARAMS
);
853 PORT_Memcpy(rc5_cbc_params
->pIv
,iv
->data
,iv
->len
);
854 rc5_cbc_params
->ulIvLen
= iv
->len
;
855 rc5_cbc_params
->ulWordsize
= iv
->len
/2;
857 rc5_cbc_params
->ulWordsize
= 4;
858 rc5_cbc_params
->pIv
= NULL
;
859 rc5_cbc_params
->ulIvLen
= 0;
861 rc5_cbc_params
->ulRounds
= 16;
862 param
->data
= (unsigned char *) rc5_cbc_params
;
863 param
->len
= sizeof(CK_RC5_CBC_PARAMS
);
866 rc5_params
= (CK_RC5_PARAMS
*)PORT_Alloc(sizeof(CK_RC5_PARAMS
));
867 if (rc5_params
== NULL
) break;
868 if (iv
&& iv
->data
&& iv
->len
) {
869 rc5_params
->ulWordsize
= iv
->len
/2;
871 rc5_params
->ulWordsize
= 4;
873 rc5_params
->ulRounds
= 16;
874 param
->data
= (unsigned char *) rc5_params
;
875 param
->len
= sizeof(CK_RC5_PARAMS
);
877 case CKM_CAMELLIA_CBC
:
886 case CKM_CAMELLIA_CBC_PAD
:
887 case CKM_AES_CBC_PAD
:
888 case CKM_DES_CBC_PAD
:
889 case CKM_DES3_CBC_PAD
:
890 case CKM_IDEA_CBC_PAD
:
891 case CKM_CDMF_CBC_PAD
:
892 case CKM_CAST_CBC_PAD
:
893 case CKM_CAST3_CBC_PAD
:
894 case CKM_CAST5_CBC_PAD
:
895 case CKM_SKIPJACK_CBC64
:
896 case CKM_SKIPJACK_ECB64
:
897 case CKM_SKIPJACK_OFB64
:
898 case CKM_SKIPJACK_CFB64
:
899 case CKM_SKIPJACK_CFB32
:
900 case CKM_SKIPJACK_CFB16
:
901 case CKM_SKIPJACK_CFB8
:
902 case CKM_BATON_ECB128
:
903 case CKM_BATON_ECB96
:
904 case CKM_BATON_CBC128
:
905 case CKM_BATON_COUNTER
:
906 case CKM_BATON_SHUFFLE
:
907 case CKM_JUNIPER_ECB128
:
908 case CKM_JUNIPER_CBC128
:
909 case CKM_JUNIPER_COUNTER
:
910 case CKM_JUNIPER_SHUFFLE
:
911 if ((iv
== NULL
) || (iv
->data
== NULL
)) break;
912 param
->data
= (unsigned char*)PORT_Alloc(iv
->len
);
913 if (param
->data
!= NULL
) {
914 PORT_Memcpy(param
->data
,iv
->data
,iv
->len
);
915 param
->len
= iv
->len
;
918 /* unknown mechanism, pass IV in if it's there */
920 if (pk11_lookup(type
)->iv
== 0) {
923 if ((iv
== NULL
) || (iv
->data
== NULL
)) {
926 param
->data
= (unsigned char*)PORT_Alloc(iv
->len
);
927 if (param
->data
!= NULL
) {
928 PORT_Memcpy(param
->data
,iv
->data
,iv
->len
);
929 param
->len
= iv
->len
;
937 PK11_IVFromParam(CK_MECHANISM_TYPE type
,SECItem
*param
,int *len
)
939 CK_RC2_CBC_PARAMS
*rc2_params
;
940 CK_RC5_CBC_PARAMS
*rc5_cbc_params
;
944 case CKM_CAMELLIA_ECB
:
961 case CKM_RC2_CBC_PAD
:
962 rc2_params
= (CK_RC2_CBC_PARAMS
*)param
->data
;
963 *len
= sizeof(rc2_params
->iv
);
964 return &rc2_params
->iv
[0];
966 case CKM_RC5_CBC_PAD
:
967 rc5_cbc_params
= (CK_RC5_CBC_PARAMS
*) param
->data
;
968 *len
= rc5_cbc_params
->ulIvLen
;
969 return rc5_cbc_params
->pIv
;
970 case CKM_CAMELLIA_CBC
:
979 case CKM_DES_CBC_PAD
:
980 case CKM_DES3_CBC_PAD
:
981 case CKM_IDEA_CBC_PAD
:
982 case CKM_CDMF_CBC_PAD
:
983 case CKM_CAST_CBC_PAD
:
984 case CKM_CAST3_CBC_PAD
:
985 case CKM_CAST5_CBC_PAD
:
986 case CKM_SKIPJACK_CBC64
:
987 case CKM_SKIPJACK_ECB64
:
988 case CKM_SKIPJACK_OFB64
:
989 case CKM_SKIPJACK_CFB64
:
990 case CKM_SKIPJACK_CFB32
:
991 case CKM_SKIPJACK_CFB16
:
992 case CKM_SKIPJACK_CFB8
:
993 case CKM_BATON_ECB128
:
994 case CKM_BATON_ECB96
:
995 case CKM_BATON_CBC128
:
996 case CKM_BATON_COUNTER
:
997 case CKM_BATON_SHUFFLE
:
998 case CKM_JUNIPER_ECB128
:
999 case CKM_JUNIPER_CBC128
:
1000 case CKM_JUNIPER_COUNTER
:
1001 case CKM_JUNIPER_SHUFFLE
:
1003 /* unknown mechanism, pass IV in if it's there */
1013 typedef struct sec_rc5cbcParameterStr
{
1016 SECItem blockSizeInBits
;
1018 } sec_rc5cbcParameter
;
1020 static const SEC_ASN1Template sec_rc5ecb_parameter_template
[] = {
1021 { SEC_ASN1_SEQUENCE
,
1022 0, NULL
, sizeof(sec_rc5cbcParameter
) },
1024 offsetof(sec_rc5cbcParameter
,version
) },
1026 offsetof(sec_rc5cbcParameter
,rounds
) },
1028 offsetof(sec_rc5cbcParameter
,blockSizeInBits
) },
1032 static const SEC_ASN1Template sec_rc5cbc_parameter_template
[] = {
1033 { SEC_ASN1_SEQUENCE
,
1034 0, NULL
, sizeof(sec_rc5cbcParameter
) },
1036 offsetof(sec_rc5cbcParameter
,version
) },
1038 offsetof(sec_rc5cbcParameter
,rounds
) },
1040 offsetof(sec_rc5cbcParameter
,blockSizeInBits
) },
1041 { SEC_ASN1_OCTET_STRING
,
1042 offsetof(sec_rc5cbcParameter
,iv
) },
1046 typedef struct sec_rc2cbcParameterStr
{
1047 SECItem rc2ParameterVersion
;
1049 } sec_rc2cbcParameter
;
1051 static const SEC_ASN1Template sec_rc2cbc_parameter_template
[] = {
1052 { SEC_ASN1_SEQUENCE
,
1053 0, NULL
, sizeof(sec_rc2cbcParameter
) },
1055 offsetof(sec_rc2cbcParameter
,rc2ParameterVersion
) },
1056 { SEC_ASN1_OCTET_STRING
,
1057 offsetof(sec_rc2cbcParameter
,iv
) },
1061 static const SEC_ASN1Template sec_rc2ecb_parameter_template
[] = {
1062 { SEC_ASN1_SEQUENCE
,
1063 0, NULL
, sizeof(sec_rc2cbcParameter
) },
1065 offsetof(sec_rc2cbcParameter
,rc2ParameterVersion
) },
1069 /* S/MIME picked id values to represent differnt keysizes */
1070 /* I do have a formula, but it ain't pretty, and it only works because you
1071 * can always match three points to a parabola:) */
1072 static unsigned char rc2_map(SECItem
*version
)
1076 x
= DER_GetInteger(version
);
1079 case 58: return 128;
1080 case 120: return 64;
1081 case 160: return 40;
1086 static unsigned long rc2_unmap(unsigned long x
)
1089 case 128: return 58;
1090 case 64: return 120;
1091 case 40: return 160;
1098 /* Generate a mechaism param from a type, and iv. */
1100 PK11_ParamFromAlgid(SECAlgorithmID
*algid
)
1102 CK_RC2_CBC_PARAMS
* rc2_cbc_params
= NULL
;
1103 CK_RC2_PARAMS
* rc2_ecb_params
= NULL
;
1104 CK_RC5_CBC_PARAMS
* rc5_cbc_params
= NULL
;
1105 CK_RC5_PARAMS
* rc5_ecb_params
= NULL
;
1106 PRArenaPool
* arena
= NULL
;
1107 SECItem
* mech
= NULL
;
1110 CK_MECHANISM_TYPE type
;
1111 /* initialize these to prevent UMRs in the ASN1 decoder. */
1112 SECItem iv
= {siBuffer
, NULL
, 0};
1113 sec_rc2cbcParameter rc2
= { {siBuffer
, NULL
, 0}, {siBuffer
, NULL
, 0} };
1114 sec_rc5cbcParameter rc5
= { {siBuffer
, NULL
, 0}, {siBuffer
, NULL
, 0},
1115 {siBuffer
, NULL
, 0}, {siBuffer
, NULL
, 0} };
1117 algtag
= SECOID_GetAlgorithmTag(algid
);
1118 type
= PK11_AlgtagToMechanism(algtag
);
1120 mech
= PORT_New(SECItem
);
1124 mech
->type
= siBuffer
;
1128 arena
= PORT_NewArena(1024);
1133 /* handle the complicated cases */
1136 rv
= SEC_ASN1DecodeItem(arena
, &rc2
,sec_rc2ecb_parameter_template
,
1137 &(algid
->parameters
));
1138 if (rv
!= SECSuccess
) {
1141 rc2_ecb_params
= PORT_New(CK_RC2_PARAMS
);
1142 if (rc2_ecb_params
== NULL
) {
1145 *rc2_ecb_params
= rc2_map(&rc2
.rc2ParameterVersion
);
1146 mech
->data
= (unsigned char *) rc2_ecb_params
;
1147 mech
->len
= sizeof *rc2_ecb_params
;
1150 case CKM_RC2_CBC_PAD
:
1151 rv
= SEC_ASN1DecodeItem(arena
, &rc2
,sec_rc2cbc_parameter_template
,
1152 &(algid
->parameters
));
1153 if (rv
!= SECSuccess
) {
1156 rc2_cbc_params
= PORT_New(CK_RC2_CBC_PARAMS
);
1157 if (rc2_cbc_params
== NULL
) {
1160 mech
->data
= (unsigned char *) rc2_cbc_params
;
1161 mech
->len
= sizeof *rc2_cbc_params
;
1162 rc2_cbc_params
->ulEffectiveBits
= rc2_map(&rc2
.rc2ParameterVersion
);
1163 if (rc2
.iv
.len
!= sizeof rc2_cbc_params
->iv
) {
1164 PORT_SetError(SEC_ERROR_INPUT_LEN
);
1167 PORT_Memcpy(rc2_cbc_params
->iv
, rc2
.iv
.data
, rc2
.iv
.len
);
1170 rv
= SEC_ASN1DecodeItem(arena
, &rc5
,sec_rc5ecb_parameter_template
,
1171 &(algid
->parameters
));
1172 if (rv
!= SECSuccess
) {
1175 rc5_ecb_params
= PORT_New(CK_RC5_PARAMS
);
1176 if (rc5_ecb_params
== NULL
) {
1179 rc5_ecb_params
->ulRounds
= DER_GetInteger(&rc5
.rounds
);
1180 rc5_ecb_params
->ulWordsize
= DER_GetInteger(&rc5
.blockSizeInBits
)/8;
1181 mech
->data
= (unsigned char *) rc5_ecb_params
;
1182 mech
->len
= sizeof *rc5_ecb_params
;
1185 case CKM_RC5_CBC_PAD
:
1186 rv
= SEC_ASN1DecodeItem(arena
, &rc5
,sec_rc5cbc_parameter_template
,
1187 &(algid
->parameters
));
1188 if (rv
!= SECSuccess
) {
1191 rc5_cbc_params
= (CK_RC5_CBC_PARAMS
*)
1192 PORT_Alloc(sizeof(CK_RC5_CBC_PARAMS
) + rc5
.iv
.len
);
1193 if (rc5_cbc_params
== NULL
) {
1196 mech
->data
= (unsigned char *) rc5_cbc_params
;
1197 mech
->len
= sizeof *rc5_cbc_params
;
1198 rc5_cbc_params
->ulRounds
= DER_GetInteger(&rc5
.rounds
);
1199 rc5_cbc_params
->ulWordsize
= DER_GetInteger(&rc5
.blockSizeInBits
)/8;
1200 rc5_cbc_params
->pIv
= ((CK_BYTE_PTR
)rc5_cbc_params
)
1201 + sizeof(CK_RC5_CBC_PARAMS
);
1202 rc5_cbc_params
->ulIvLen
= rc5
.iv
.len
;
1203 PORT_Memcpy(rc5_cbc_params
->pIv
, rc5
.iv
.data
, rc5
.iv
.len
);
1205 case CKM_PBE_MD2_DES_CBC
:
1206 case CKM_PBE_MD5_DES_CBC
:
1207 case CKM_NETSCAPE_PBE_SHA1_DES_CBC
:
1208 case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC
:
1209 case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC
:
1210 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC
:
1211 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC
:
1212 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4
:
1213 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4
:
1214 case CKM_PBE_SHA1_DES2_EDE_CBC
:
1215 case CKM_PBE_SHA1_DES3_EDE_CBC
:
1216 case CKM_PBE_SHA1_RC2_40_CBC
:
1217 case CKM_PBE_SHA1_RC2_128_CBC
:
1218 case CKM_PBE_SHA1_RC4_40
:
1219 case CKM_PBE_SHA1_RC4_128
:
1220 rv
= pbe_PK11AlgidToParam(algid
,mech
);
1221 if (rv
!= SECSuccess
) {
1226 case CKM_CAMELLIA_ECB
:
1238 if (pk11_lookup(type
)->iv
== 0) {
1242 case CKM_CAMELLIA_CBC
:
1251 case CKM_CAMELLIA_CBC_PAD
:
1252 case CKM_AES_CBC_PAD
:
1253 case CKM_DES_CBC_PAD
:
1254 case CKM_DES3_CBC_PAD
:
1255 case CKM_IDEA_CBC_PAD
:
1256 case CKM_CDMF_CBC_PAD
:
1257 case CKM_CAST_CBC_PAD
:
1258 case CKM_CAST3_CBC_PAD
:
1259 case CKM_CAST5_CBC_PAD
:
1260 case CKM_SKIPJACK_CBC64
:
1261 case CKM_SKIPJACK_ECB64
:
1262 case CKM_SKIPJACK_OFB64
:
1263 case CKM_SKIPJACK_CFB64
:
1264 case CKM_SKIPJACK_CFB32
:
1265 case CKM_SKIPJACK_CFB16
:
1266 case CKM_SKIPJACK_CFB8
:
1267 case CKM_BATON_ECB128
:
1268 case CKM_BATON_ECB96
:
1269 case CKM_BATON_CBC128
:
1270 case CKM_BATON_COUNTER
:
1271 case CKM_BATON_SHUFFLE
:
1272 case CKM_JUNIPER_ECB128
:
1273 case CKM_JUNIPER_CBC128
:
1274 case CKM_JUNIPER_COUNTER
:
1275 case CKM_JUNIPER_SHUFFLE
:
1276 /* simple cases are simply octet string encoded IVs */
1277 rv
= SEC_ASN1DecodeItem(arena
, &iv
, SEC_OctetStringTemplate
,
1278 &(algid
->parameters
));
1279 if (rv
!= SECSuccess
|| iv
.data
== NULL
) {
1282 /* XXX Should be some IV length sanity check here. */
1283 mech
->data
= (unsigned char*)PORT_Alloc(iv
.len
);
1284 if (mech
->data
== NULL
) {
1287 PORT_Memcpy(mech
->data
, iv
.data
, iv
.len
);
1291 PORT_FreeArena(arena
, PR_FALSE
);
1296 PORT_FreeArena(arena
, PR_FALSE
);
1297 SECITEM_FreeItem(mech
,PR_TRUE
);
1302 * Generate an IV for the given mechanism
1305 pk11_GenIV(CK_MECHANISM_TYPE type
, SECItem
*iv
) {
1306 int iv_size
= PK11_GetIVLength(type
);
1315 iv
->data
= (unsigned char *) PORT_Alloc(iv_size
);
1316 if (iv
->data
== NULL
) {
1321 rv
= PK11_GenerateRandom(iv
->data
,iv
->len
);
1322 if (rv
!= SECSuccess
) {
1323 PORT_Free(iv
->data
);
1324 iv
->data
= NULL
; iv
->len
= 0;
1332 * create a new paramter block from the passed in MECHANISM and the
1333 * key. Use Netscape's S/MIME Rules for the New param block.
1336 PK11_GenerateNewParam(CK_MECHANISM_TYPE type
, PK11SymKey
*key
) {
1337 CK_RC2_CBC_PARAMS
*rc2_params
;
1338 CK_RC2_PARAMS
*rc2_ecb_params
;
1344 mech
= (SECItem
*) PORT_Alloc(sizeof(SECItem
));
1345 if (mech
== NULL
) return NULL
;
1348 mech
->type
= siBuffer
;
1351 case CKM_CAMELLIA_ECB
:
1364 rc2_ecb_params
= (CK_RC2_PARAMS
*)PORT_Alloc(sizeof(CK_RC2_PARAMS
));
1365 if (rc2_ecb_params
== NULL
) {
1369 /* NOTE PK11_GetKeyLength can return -1 if the key isn't and RC2, RC5,
1370 * or RC4 key. Of course that wouldn't happen here doing RC2:).*/
1371 *rc2_ecb_params
= PK11_GetKeyLength(key
)*8;
1372 mech
->data
= (unsigned char *) rc2_ecb_params
;
1373 mech
->len
= sizeof(CK_RC2_PARAMS
);
1376 case CKM_RC2_CBC_PAD
:
1377 rv
= pk11_GenIV(type
,&iv
);
1378 if (rv
!= SECSuccess
) {
1381 rc2_params
= (CK_RC2_CBC_PARAMS
*)PORT_Alloc(sizeof(CK_RC2_CBC_PARAMS
));
1382 if (rc2_params
== NULL
) {
1387 /* NOTE PK11_GetKeyLength can return -1 if the key isn't and RC2, RC5,
1388 * or RC4 key. Of course that wouldn't happen here doing RC2:).*/
1389 rc2_params
->ulEffectiveBits
= PK11_GetKeyLength(key
)*8;
1391 PORT_Memcpy(rc2_params
->iv
,iv
.data
,sizeof(rc2_params
->iv
));
1392 mech
->data
= (unsigned char *) rc2_params
;
1393 mech
->len
= sizeof(CK_RC2_CBC_PARAMS
);
1398 return PK11_ParamFromIV(type
,NULL
);
1400 case CKM_RC5_CBC_PAD
:
1401 rv
= pk11_GenIV(type
,&iv
);
1402 if (rv
!= SECSuccess
) {
1406 return PK11_ParamFromIV(type
,&iv
);
1408 if (pk11_lookup(type
)->iv
== 0) {
1413 case CKM_CAMELLIA_CBC
:
1422 case CKM_DES_CBC_PAD
:
1423 case CKM_DES3_CBC_PAD
:
1424 case CKM_IDEA_CBC_PAD
:
1425 case CKM_CDMF_CBC_PAD
:
1426 case CKM_CAST_CBC_PAD
:
1427 case CKM_CAST3_CBC_PAD
:
1428 case CKM_CAST5_CBC_PAD
:
1429 case CKM_SKIPJACK_CBC64
:
1430 case CKM_SKIPJACK_ECB64
:
1431 case CKM_SKIPJACK_OFB64
:
1432 case CKM_SKIPJACK_CFB64
:
1433 case CKM_SKIPJACK_CFB32
:
1434 case CKM_SKIPJACK_CFB16
:
1435 case CKM_SKIPJACK_CFB8
:
1436 case CKM_BATON_ECB128
:
1437 case CKM_BATON_ECB96
:
1438 case CKM_BATON_CBC128
:
1439 case CKM_BATON_COUNTER
:
1440 case CKM_BATON_SHUFFLE
:
1441 case CKM_JUNIPER_ECB128
:
1442 case CKM_JUNIPER_CBC128
:
1443 case CKM_JUNIPER_COUNTER
:
1444 case CKM_JUNIPER_SHUFFLE
:
1445 rv
= pk11_GenIV(type
,&iv
);
1446 if (rv
!= SECSuccess
) {
1449 mech
->data
= (unsigned char*)PORT_Alloc(iv
.len
);
1450 if (mech
->data
== NULL
) {
1455 PORT_Memcpy(mech
->data
,iv
.data
,iv
.len
);
1460 if (rv
!= SECSuccess
) {
1461 SECITEM_FreeItem(mech
,PR_TRUE
);
1468 #define RC5_V10 0x10
1470 /* turn a PKCS #11 parameter into a DER Encoded Algorithm ID */
1472 PK11_ParamToAlgid(SECOidTag algTag
, SECItem
*param
,
1473 PRArenaPool
*arena
, SECAlgorithmID
*algid
) {
1474 CK_RC2_CBC_PARAMS
*rc2_params
;
1475 sec_rc2cbcParameter rc2
;
1476 CK_RC5_CBC_PARAMS
*rc5_params
;
1477 sec_rc5cbcParameter rc5
;
1478 CK_MECHANISM_TYPE type
= PK11_AlgtagToMechanism(algTag
);
1479 SECItem
*newParams
= NULL
;
1480 SECStatus rv
= SECFailure
;
1481 unsigned long rc2version
;
1485 case CKM_CAMELLIA_ECB
:
1500 case CKM_RC2_CBC_PAD
:
1501 rc2_params
= (CK_RC2_CBC_PARAMS
*)param
->data
;
1502 rc2version
= rc2_unmap(rc2_params
->ulEffectiveBits
);
1503 if (SEC_ASN1EncodeUnsignedInteger (NULL
, &(rc2
.rc2ParameterVersion
),
1504 rc2version
) == NULL
)
1506 rc2
.iv
.data
= rc2_params
->iv
;
1507 rc2
.iv
.len
= sizeof(rc2_params
->iv
);
1508 newParams
= SEC_ASN1EncodeItem (NULL
, NULL
, &rc2
,
1509 sec_rc2cbc_parameter_template
);
1510 PORT_Free(rc2
.rc2ParameterVersion
.data
);
1511 if (newParams
== NULL
)
1516 case CKM_RC5_ECB
: /* well not really... */
1519 case CKM_RC5_CBC_PAD
:
1520 rc5_params
= (CK_RC5_CBC_PARAMS
*)param
->data
;
1521 if (SEC_ASN1EncodeUnsignedInteger (NULL
, &rc5
.version
, RC5_V10
) == NULL
)
1523 if (SEC_ASN1EncodeUnsignedInteger (NULL
, &rc5
.blockSizeInBits
,
1524 rc5_params
->ulWordsize
*8) == NULL
) {
1525 PORT_Free(rc5
.version
.data
);
1528 if (SEC_ASN1EncodeUnsignedInteger (NULL
, &rc5
.rounds
,
1529 rc5_params
->ulWordsize
*8) == NULL
) {
1530 PORT_Free(rc5
.blockSizeInBits
.data
);
1531 PORT_Free(rc5
.version
.data
);
1534 rc5
.iv
.data
= rc5_params
->pIv
;
1535 rc5
.iv
.len
= rc5_params
->ulIvLen
;
1536 newParams
= SEC_ASN1EncodeItem (NULL
, NULL
, &rc5
,
1537 sec_rc5cbc_parameter_template
);
1538 PORT_Free(rc5
.version
.data
);
1539 PORT_Free(rc5
.blockSizeInBits
.data
);
1540 PORT_Free(rc5
.rounds
.data
);
1541 if (newParams
== NULL
)
1545 case CKM_PBE_MD2_DES_CBC
:
1546 case CKM_PBE_MD5_DES_CBC
:
1547 case CKM_NETSCAPE_PBE_SHA1_DES_CBC
:
1548 case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC
:
1549 case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC
:
1550 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC
:
1551 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC
:
1552 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4
:
1553 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4
:
1554 case CKM_PBE_SHA1_DES3_EDE_CBC
:
1555 case CKM_PBE_SHA1_DES2_EDE_CBC
:
1556 case CKM_PBE_SHA1_RC2_40_CBC
:
1557 case CKM_PBE_SHA1_RC2_128_CBC
:
1558 case CKM_PBE_SHA1_RC4_40
:
1559 case CKM_PBE_SHA1_RC4_128
:
1560 return PBE_PK11ParamToAlgid(algTag
, param
, arena
, algid
);
1562 if (pk11_lookup(type
)->iv
== 0) {
1567 case CKM_CAMELLIA_CBC
:
1576 case CKM_DES_CBC_PAD
:
1577 case CKM_DES3_CBC_PAD
:
1578 case CKM_IDEA_CBC_PAD
:
1579 case CKM_CDMF_CBC_PAD
:
1580 case CKM_CAST_CBC_PAD
:
1581 case CKM_CAST3_CBC_PAD
:
1582 case CKM_CAST5_CBC_PAD
:
1583 case CKM_SKIPJACK_CBC64
:
1584 case CKM_SKIPJACK_ECB64
:
1585 case CKM_SKIPJACK_OFB64
:
1586 case CKM_SKIPJACK_CFB64
:
1587 case CKM_SKIPJACK_CFB32
:
1588 case CKM_SKIPJACK_CFB16
:
1589 case CKM_SKIPJACK_CFB8
:
1590 case CKM_BATON_ECB128
:
1591 case CKM_BATON_ECB96
:
1592 case CKM_BATON_CBC128
:
1593 case CKM_BATON_COUNTER
:
1594 case CKM_BATON_SHUFFLE
:
1595 case CKM_JUNIPER_ECB128
:
1596 case CKM_JUNIPER_CBC128
:
1597 case CKM_JUNIPER_COUNTER
:
1598 case CKM_JUNIPER_SHUFFLE
:
1599 newParams
= SEC_ASN1EncodeItem(NULL
,NULL
,param
,
1600 SEC_OctetStringTemplate
);
1601 if (newParams
== NULL
)
1607 if (rv
!= SECSuccess
) {
1608 if (newParams
) SECITEM_FreeItem(newParams
,PR_TRUE
);
1612 rv
= SECOID_SetAlgorithmID(arena
, algid
, algTag
, newParams
);
1613 SECITEM_FreeItem(newParams
,PR_TRUE
);
1617 /* turn an OID algorithm tag into a PKCS #11 mechanism. This allows us to
1618 * map OID's directly into the PKCS #11 mechanism we want to call. We find
1619 * this mapping in our standard OID table */
1621 PK11_AlgtagToMechanism(SECOidTag algTag
) {
1622 SECOidData
*oid
= SECOID_FindOIDByTag(algTag
);
1624 if (oid
) return (CK_MECHANISM_TYPE
) oid
->mechanism
;
1625 return CKM_INVALID_MECHANISM
;
1628 /* turn a mechanism into an oid. */
1630 PK11_MechanismToAlgtag(CK_MECHANISM_TYPE type
) {
1631 SECOidData
*oid
= SECOID_FindOIDByMechanism((unsigned long)type
);
1633 if (oid
) return oid
->offset
;
1634 return SEC_OID_UNKNOWN
;
1637 /* Determine appropriate blocking mechanism, used when wrapping private keys
1638 * which require PKCS padding. If the mechanism does not map to a padding
1639 * mechanism, we simply return the mechanism.
1642 PK11_GetPadMechanism(CK_MECHANISM_TYPE type
) {
1644 case CKM_CAMELLIA_CBC
:
1645 return CKM_CAMELLIA_CBC_PAD
;
1647 return CKM_AES_CBC_PAD
;
1649 return CKM_DES_CBC_PAD
;
1651 return CKM_DES3_CBC_PAD
;
1653 return CKM_RC2_CBC_PAD
;
1655 return CKM_CDMF_CBC_PAD
;
1657 return CKM_CAST_CBC_PAD
;
1659 return CKM_CAST3_CBC_PAD
;
1661 return CKM_CAST5_CBC_PAD
;
1663 return CKM_RC5_CBC_PAD
;
1665 return CKM_IDEA_CBC_PAD
;
1674 pk11_isAllZero(unsigned char *data
,int len
) {
1684 PK11_MapPBEMechanismToCryptoMechanism(CK_MECHANISM_PTR pPBEMechanism
,
1685 CK_MECHANISM_PTR pCryptoMechanism
,
1686 SECItem
*pbe_pwd
, PRBool faulty3DES
)
1689 CK_PBE_PARAMS_PTR pPBEparams
;
1690 CK_RC2_CBC_PARAMS_PTR rc2_params
;
1691 CK_ULONG rc2_key_len
;
1693 if((pPBEMechanism
== CK_NULL_PTR
) || (pCryptoMechanism
== CK_NULL_PTR
)) {
1694 return CKR_HOST_MEMORY
;
1697 pPBEparams
= (CK_PBE_PARAMS_PTR
)pPBEMechanism
->pParameter
;
1698 iv_len
= PK11_GetIVLength(pPBEMechanism
->mechanism
);
1701 if (pk11_isAllZero(pPBEparams
->pInitVector
,iv_len
)) {
1704 PK11SlotInfo
*intSlot
= PK11_GetInternalSlot();
1706 if (intSlot
== NULL
) {
1707 return CKR_DEVICE_ERROR
;
1710 param
.data
= pPBEMechanism
->pParameter
;
1711 param
.len
= pPBEMechanism
->ulParameterLen
;
1713 symKey
= PK11_RawPBEKeyGen(intSlot
,
1714 pPBEMechanism
->mechanism
, ¶m
, pbe_pwd
, faulty3DES
, NULL
);
1715 PK11_FreeSlot(intSlot
);
1716 if (symKey
== NULL
) {
1717 return CKR_DEVICE_ERROR
; /* sigh */
1719 PK11_FreeSymKey(symKey
);
1723 switch(pPBEMechanism
->mechanism
) {
1724 case CKM_PBE_MD2_DES_CBC
:
1725 case CKM_PBE_MD5_DES_CBC
:
1726 case CKM_NETSCAPE_PBE_SHA1_DES_CBC
:
1727 pCryptoMechanism
->mechanism
= CKM_DES_CBC
;
1728 goto have_crypto_mechanism
;
1729 case CKM_NETSCAPE_PBE_SHA1_TRIPLE_DES_CBC
:
1730 case CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC
:
1731 case CKM_PBE_SHA1_DES3_EDE_CBC
:
1732 case CKM_PBE_SHA1_DES2_EDE_CBC
:
1733 pCryptoMechanism
->mechanism
= CKM_DES3_CBC
;
1734 have_crypto_mechanism
:
1735 pCryptoMechanism
->pParameter
= PORT_Alloc(iv_len
);
1736 pCryptoMechanism
->ulParameterLen
= (CK_ULONG
)iv_len
;
1737 if(pCryptoMechanism
->pParameter
== NULL
) {
1738 return CKR_HOST_MEMORY
;
1740 PORT_Memcpy((unsigned char *)(pCryptoMechanism
->pParameter
),
1741 (unsigned char *)(pPBEparams
->pInitVector
),
1744 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC4
:
1745 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC4
:
1746 case CKM_PBE_SHA1_RC4_40
:
1747 case CKM_PBE_SHA1_RC4_128
:
1748 pCryptoMechanism
->mechanism
= CKM_RC4
;
1749 pCryptoMechanism
->ulParameterLen
= 0;
1750 pCryptoMechanism
->pParameter
= CK_NULL_PTR
;
1752 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC
:
1753 case CKM_PBE_SHA1_RC2_40_CBC
:
1756 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC
:
1759 pCryptoMechanism
->mechanism
= CKM_RC2_CBC
;
1760 pCryptoMechanism
->ulParameterLen
= (CK_ULONG
)
1761 sizeof(CK_RC2_CBC_PARAMS
);
1762 pCryptoMechanism
->pParameter
= (CK_RC2_CBC_PARAMS_PTR
)
1763 PORT_ZAlloc(sizeof(CK_RC2_CBC_PARAMS
));
1764 if(pCryptoMechanism
->pParameter
== NULL
) {
1765 return CKR_HOST_MEMORY
;
1767 rc2_params
= (CK_RC2_CBC_PARAMS_PTR
)pCryptoMechanism
->pParameter
;
1768 PORT_Memcpy((unsigned char *)rc2_params
->iv
,
1769 (unsigned char *)pPBEparams
->pInitVector
,
1771 rc2_params
->ulEffectiveBits
= rc2_key_len
;
1774 return CKR_MECHANISM_INVALID
;
1780 /* Make a Key type to an appropriate signing/verification mechanism */
1782 PK11_MapSignKeyType(KeyType keyType
)
1786 return CKM_RSA_PKCS
;
1796 return CKM_INVALID_MECHANISM
;
1800 pk11_mapWrapKeyType(KeyType keyType
)
1804 return CKM_RSA_PKCS
;
1805 /* Add fortezza?? */
1809 return CKM_INVALID_MECHANISM
;
1813 PK11_FortezzaMapSig(SECOidTag algTag
)
1816 case SEC_OID_MISSI_KEA_DSS
:
1817 case SEC_OID_MISSI_DSS
:
1818 case SEC_OID_MISSI_DSS_OLD
:
1819 case SEC_OID_MISSI_KEA_DSS_OLD
:
1820 case SEC_OID_BOGUS_DSA_SIGNATURE_WITH_SHA1_DIGEST
:
1821 return SEC_OID_ANSIX9_DSA_SIGNATURE
;