Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / pk11wrap / pk11mech.c
blob7f46459daa6fdfc7bfca5373f04ce457a1bd5670
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
12 * License.
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.
21 * Contributor(s):
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.
42 #include "seccomon.h"
43 #include "secmod.h"
44 #include "secmodi.h"
45 #include "pkcs11t.h"
46 #include "pk11func.h"
47 #include "secitem.h"
48 #include "secder.h"
49 #include "secasn1.h"
50 #include "secoid.h"
51 #include "secerr.h"
53 /*************************************************************
54 * local static and global data
55 *************************************************************/
58 * Tables used for Extended mechanism mapping (currently not used)
60 typedef struct {
61 CK_MECHANISM_TYPE keyGen;
62 CK_KEY_TYPE keyType;
63 CK_MECHANISM_TYPE type;
64 int blockSize;
65 int iv;
66 } pk11MechanismData;
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[] = {
79 CKM_DES3_ECB,
80 CKM_CAST5_ECB,
81 CKM_AES_ECB,
82 CKM_CAMELLIA_ECB,
83 CKM_CAST5_ECB,
84 CKM_DES_ECB,
85 CKM_KEY_WRAP_LYNKS,
86 CKM_IDEA_ECB,
87 CKM_CAST3_ECB,
88 CKM_CAST_ECB,
89 CKM_RC5_ECB,
90 CKM_RC2_ECB,
91 CKM_CDMF_ECB,
92 CKM_SKIPJACK_WRAP,
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
103 * default structure.
105 static pk11MechanismData *
106 pk11_lookup(CK_MECHANISM_TYPE type)
108 int i;
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.
120 CK_MECHANISM_TYPE
121 PK11_GetBestWrapMechanism(PK11SlotInfo *slot)
123 int i;
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.
140 void
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;
146 int entry = size++;
147 pk11MechanismData *old = pk11_MechanismTable;
148 pk11MechanismData *newt = pk11_MechanismTable;
151 if (size > tableSize) {
152 int oldTableSize = tableSize;
153 tableSize += 10;
154 newt = PORT_NewArray(pk11MechanismData, tableSize);
155 if (newt == NULL) return;
157 if (old) PORT_Memcpy(newt, old, oldTableSize*sizeof(*newt));
158 } else old = NULL;
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
175 CK_MECHANISM_TYPE
176 PK11_GetKeyMechanism(CK_KEY_TYPE type)
178 switch (type) {
179 case CKK_CAMELLIA:
180 return CKM_CAMELLIA_CBC;
181 case CKK_AES:
182 return CKM_AES_CBC;
183 case CKK_DES:
184 return CKM_DES_CBC;
185 case CKK_DES3:
186 return CKM_DES3_KEY_GEN;
187 case CKK_DES2:
188 return CKM_DES2_KEY_GEN;
189 case CKK_CDMF:
190 return CKM_CDMF_CBC;
191 case CKK_RC2:
192 return CKM_RC2_CBC;
193 case CKK_RC4:
194 return CKM_RC4;
195 case CKK_RC5:
196 return CKM_RC5_CBC;
197 case CKK_SKIPJACK:
198 return CKM_SKIPJACK_CBC64;
199 case CKK_BATON:
200 return CKM_BATON_CBC128;
201 case CKK_JUNIPER:
202 return CKM_JUNIPER_CBC128;
203 case CKK_IDEA:
204 return CKM_IDEA_CBC;
205 case CKK_CAST:
206 return CKM_CAST_CBC;
207 case CKK_CAST3:
208 return CKM_CAST3_CBC;
209 case CKK_CAST5:
210 return CKM_CAST5_CBC;
211 case CKK_RSA:
212 return CKM_RSA_PKCS;
213 case CKK_DSA:
214 return CKM_DSA;
215 case CKK_DH:
216 return CKM_DH_PKCS_DERIVE;
217 case CKK_KEA:
218 return CKM_KEA_KEY_DERIVE;
219 case CKK_EC: /* CKK_ECDSA is deprecated */
220 return CKM_ECDSA;
221 case CKK_GENERIC_SECRET:
222 default:
223 return CKM_SHA_1_HMAC;
228 * Get the key type needed for the given mechanism
230 CK_MECHANISM_TYPE
231 PK11_GetKeyType(CK_MECHANISM_TYPE type,unsigned long len)
233 switch (type) {
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:
240 return CKK_CAMELLIA;
241 case CKM_AES_ECB:
242 case CKM_AES_CBC:
243 case CKM_AES_MAC:
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:
249 return CKK_AES;
250 case CKM_DES_ECB:
251 case CKM_DES_CBC:
252 case CKM_DES_MAC:
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:
259 return CKK_DES;
260 case CKM_DES3_ECB:
261 case CKM_DES3_CBC:
262 case CKM_DES3_MAC:
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:
268 return CKK_DES2;
269 case CKM_PBE_SHA1_DES3_EDE_CBC:
270 case CKM_DES3_KEY_GEN:
271 return CKK_DES3;
272 case CKM_CDMF_ECB:
273 case CKM_CDMF_CBC:
274 case CKM_CDMF_MAC:
275 case CKM_CDMF_MAC_GENERAL:
276 case CKM_CDMF_CBC_PAD:
277 case CKM_CDMF_KEY_GEN:
278 return CKK_CDMF;
279 case CKM_RC2_ECB:
280 case CKM_RC2_CBC:
281 case CKM_RC2_MAC:
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:
287 return CKK_RC2;
288 case CKM_RC4:
289 case CKM_RC4_KEY_GEN:
290 return CKK_RC4;
291 case CKM_RC5_ECB:
292 case CKM_RC5_CBC:
293 case CKM_RC5_MAC:
294 case CKM_RC5_MAC_GENERAL:
295 case CKM_RC5_CBC_PAD:
296 case CKM_RC5_KEY_GEN:
297 return CKK_RC5;
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:
308 return CKK_SKIPJACK;
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:
314 case CKM_BATON_WRAP:
315 case CKM_BATON_KEY_GEN:
316 return CKK_BATON;
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:
323 return CKK_JUNIPER;
324 case CKM_IDEA_CBC:
325 case CKM_IDEA_ECB:
326 case CKM_IDEA_MAC:
327 case CKM_IDEA_MAC_GENERAL:
328 case CKM_IDEA_CBC_PAD:
329 case CKM_IDEA_KEY_GEN:
330 return CKK_IDEA;
331 case CKM_CAST_ECB:
332 case CKM_CAST_CBC:
333 case CKM_CAST_MAC:
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:
338 return CKK_CAST;
339 case CKM_CAST3_ECB:
340 case CKM_CAST3_CBC:
341 case CKM_CAST3_MAC:
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:
346 return CKK_CAST3;
347 case CKM_CAST5_ECB:
348 case CKM_CAST5_CBC:
349 case CKM_CAST5_MAC:
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:
354 return CKK_CAST5;
355 case CKM_RSA_PKCS:
356 case CKM_RSA_9796:
357 case CKM_RSA_X_509:
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:
367 return CKK_RSA;
368 case CKM_DSA:
369 case CKM_DSA_SHA1:
370 case CKM_DSA_KEY_PAIR_GEN:
371 return CKK_DSA;
372 case CKM_DH_PKCS_DERIVE:
373 case CKM_DH_PKCS_KEY_PAIR_GEN:
374 return CKK_DH;
375 case CKM_KEA_KEY_DERIVE:
376 case CKM_KEA_KEY_PAIR_GEN:
377 return CKK_KEA;
378 case CKM_ECDSA:
379 case CKM_ECDSA_SHA1:
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:
393 case CKM_SHA_1_HMAC:
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:
401 case CKM_MD2_HMAC:
402 case CKM_MD2_HMAC_GENERAL:
403 case CKM_MD5_HMAC:
404 case CKM_MD5_HMAC_GENERAL:
405 case CKM_TLS_PRF_GENERAL:
406 return CKK_GENERIC_SECRET;
407 default:
408 return pk11_lookup(type)->keyType;
413 * Get the Key Gen Mechanism needed for the given
414 * crypto mechanism
416 CK_MECHANISM_TYPE
417 PK11_GetKeyGen(CK_MECHANISM_TYPE type)
419 return PK11_GetKeyGenWithSize(type, 0);
422 CK_MECHANISM_TYPE
423 PK11_GetKeyGenWithSize(CK_MECHANISM_TYPE type, int size)
425 switch (type) {
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;
433 case CKM_AES_ECB:
434 case CKM_AES_CBC:
435 case CKM_AES_MAC:
436 case CKM_AES_MAC_GENERAL:
437 case CKM_AES_CBC_PAD:
438 case CKM_AES_KEY_GEN:
439 return CKM_AES_KEY_GEN;
440 case CKM_DES_ECB:
441 case CKM_DES_CBC:
442 case CKM_DES_MAC:
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;
448 case CKM_DES3_ECB:
449 case CKM_DES3_CBC:
450 case CKM_DES3_MAC:
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;
458 case CKM_CDMF_ECB:
459 case CKM_CDMF_CBC:
460 case CKM_CDMF_MAC:
461 case CKM_CDMF_MAC_GENERAL:
462 case CKM_CDMF_CBC_PAD:
463 case CKM_CDMF_KEY_GEN:
464 return CKM_CDMF_KEY_GEN;
465 case CKM_RC2_ECB:
466 case CKM_RC2_CBC:
467 case CKM_RC2_MAC:
468 case CKM_RC2_MAC_GENERAL:
469 case CKM_RC2_CBC_PAD:
470 case CKM_RC2_KEY_GEN:
471 return CKM_RC2_KEY_GEN;
472 case CKM_RC4:
473 case CKM_RC4_KEY_GEN:
474 return CKM_RC4_KEY_GEN;
475 case CKM_RC5_ECB:
476 case CKM_RC5_CBC:
477 case CKM_RC5_MAC:
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:
497 case CKM_BATON_WRAP:
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;
507 case CKM_IDEA_CBC:
508 case CKM_IDEA_ECB:
509 case CKM_IDEA_MAC:
510 case CKM_IDEA_MAC_GENERAL:
511 case CKM_IDEA_CBC_PAD:
512 case CKM_IDEA_KEY_GEN:
513 return CKM_IDEA_KEY_GEN;
514 case CKM_CAST_ECB:
515 case CKM_CAST_CBC:
516 case CKM_CAST_MAC:
517 case CKM_CAST_MAC_GENERAL:
518 case CKM_CAST_CBC_PAD:
519 case CKM_CAST_KEY_GEN:
520 return CKM_CAST_KEY_GEN;
521 case CKM_CAST3_ECB:
522 case CKM_CAST3_CBC:
523 case CKM_CAST3_MAC:
524 case CKM_CAST3_MAC_GENERAL:
525 case CKM_CAST3_CBC_PAD:
526 case CKM_CAST3_KEY_GEN:
527 return CKM_CAST3_KEY_GEN;
528 case CKM_CAST5_ECB:
529 case CKM_CAST5_CBC:
530 case CKM_CAST5_MAC:
531 case CKM_CAST5_MAC_GENERAL:
532 case CKM_CAST5_CBC_PAD:
533 case CKM_CAST5_KEY_GEN:
534 return CKM_CAST5_KEY_GEN;
535 case CKM_RSA_PKCS:
536 case CKM_RSA_9796:
537 case CKM_RSA_X_509:
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;
549 case CKM_DSA:
550 case CKM_DSA_SHA1:
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;
559 case CKM_ECDSA:
560 case CKM_ECDSA_SHA1:
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;
572 case CKM_SHA_1_HMAC:
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:
580 case CKM_MD2_HMAC:
581 case CKM_MD2_HMAC_GENERAL:
582 case CKM_MD5_HMAC:
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:
606 return type;
607 default:
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;
620 switch (type) {
621 case CKM_RC5_ECB:
622 if ((params) && (params->data)) {
623 rc5_params = (CK_RC5_PARAMS *) params->data;
624 return (rc5_params->ulWordsize)*2;
626 return 8;
627 case CKM_RC5_CBC:
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;
633 return 8;
634 case CKM_DES_ECB:
635 case CKM_DES3_ECB:
636 case CKM_RC2_ECB:
637 case CKM_IDEA_ECB:
638 case CKM_CAST_ECB:
639 case CKM_CAST3_ECB:
640 case CKM_CAST5_ECB:
641 case CKM_RC2_CBC:
642 case CKM_SKIPJACK_CBC64:
643 case CKM_SKIPJACK_ECB64:
644 case CKM_SKIPJACK_OFB64:
645 case CKM_SKIPJACK_CFB64:
646 case CKM_DES_CBC:
647 case CKM_DES3_CBC:
648 case CKM_IDEA_CBC:
649 case CKM_CAST_CBC:
650 case CKM_CAST3_CBC:
651 case CKM_CAST5_CBC:
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:
670 return 8;
671 case CKM_SKIPJACK_CFB32:
672 case CKM_SKIPJACK_CFB16:
673 case CKM_SKIPJACK_CFB8:
674 return 4;
675 case CKM_CAMELLIA_ECB:
676 case CKM_CAMELLIA_CBC:
677 case CKM_CAMELLIA_CBC_PAD:
678 case CKM_AES_ECB:
679 case CKM_AES_CBC:
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:
689 return 16;
690 case CKM_BATON_ECB96:
691 return 12;
692 case CKM_RC4:
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:
697 return 0;
698 case CKM_RSA_PKCS:
699 case CKM_RSA_9796:
700 case CKM_RSA_X_509:
701 /*actually it's the modulus length of the key!*/
702 return -1; /* failure */
703 default:
704 return pk11_lookup(type)->blockSize;
709 * get the iv length
712 PK11_GetIVLength(CK_MECHANISM_TYPE type)
714 switch (type) {
715 case CKM_CAMELLIA_ECB:
716 case CKM_AES_ECB:
717 case CKM_DES_ECB:
718 case CKM_DES3_ECB:
719 case CKM_RC2_ECB:
720 case CKM_IDEA_ECB:
721 case CKM_SKIPJACK_WRAP:
722 case CKM_BATON_WRAP:
723 case CKM_RC5_ECB:
724 case CKM_CAST_ECB:
725 case CKM_CAST3_ECB:
726 case CKM_CAST5_ECB:
727 return 0;
728 case CKM_RC2_CBC:
729 case CKM_DES_CBC:
730 case CKM_DES3_CBC:
731 case CKM_IDEA_CBC:
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:
743 case CKM_RC5_CBC:
744 case CKM_CAST_CBC:
745 case CKM_CAST3_CBC:
746 case CKM_CAST5_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:
755 return 8;
756 case CKM_CAMELLIA_CBC:
757 case CKM_CAMELLIA_CBC_PAD:
758 case CKM_AES_CBC:
759 case CKM_AES_CBC_PAD:
760 return 16;
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:
777 return 24;
778 case CKM_RC4:
779 case CKM_RSA_PKCS:
780 case CKM_RSA_9796:
781 case CKM_RSA_X_509:
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:
786 return 0;
787 default:
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.
797 SECItem *
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;
804 SECItem *param;
806 param = (SECItem *)PORT_Alloc(sizeof(SECItem));
807 if (param == NULL) return NULL;
808 param->data = NULL;
809 param->len = 0;
810 param->type = 0;
811 switch (type) {
812 case CKM_CAMELLIA_ECB:
813 case CKM_AES_ECB:
814 case CKM_DES_ECB:
815 case CKM_DES3_ECB:
816 case CKM_RSA_PKCS:
817 case CKM_RSA_X_509:
818 case CKM_RSA_9796:
819 case CKM_IDEA_ECB:
820 case CKM_CDMF_ECB:
821 case CKM_CAST_ECB:
822 case CKM_CAST3_ECB:
823 case CKM_CAST5_ECB:
824 case CKM_RC4:
825 break;
826 case CKM_RC2_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);
833 break;
834 case CKM_RC2_CBC:
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;
840 if (iv && iv->data)
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);
844 break;
845 case CKM_RC5_CBC:
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;
856 } else {
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);
864 break;
865 case CKM_RC5_ECB:
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;
870 } else {
871 rc5_params->ulWordsize = 4;
873 rc5_params->ulRounds = 16;
874 param->data = (unsigned char *) rc5_params;
875 param->len = sizeof(CK_RC5_PARAMS);
876 break;
877 case CKM_CAMELLIA_CBC:
878 case CKM_AES_CBC:
879 case CKM_DES_CBC:
880 case CKM_DES3_CBC:
881 case CKM_IDEA_CBC:
882 case CKM_CDMF_CBC:
883 case CKM_CAST_CBC:
884 case CKM_CAST3_CBC:
885 case CKM_CAST5_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;
917 break;
918 /* unknown mechanism, pass IV in if it's there */
919 default:
920 if (pk11_lookup(type)->iv == 0) {
921 break;
923 if ((iv == NULL) || (iv->data == NULL)) {
924 break;
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;
931 break;
933 return param;
936 unsigned char *
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;
942 *len = 0;
943 switch (type) {
944 case CKM_CAMELLIA_ECB:
945 case CKM_AES_ECB:
946 case CKM_DES_ECB:
947 case CKM_DES3_ECB:
948 case CKM_RSA_PKCS:
949 case CKM_RSA_X_509:
950 case CKM_RSA_9796:
951 case CKM_IDEA_ECB:
952 case CKM_CDMF_ECB:
953 case CKM_CAST_ECB:
954 case CKM_CAST3_ECB:
955 case CKM_CAST5_ECB:
956 case CKM_RC4:
957 return NULL;
958 case CKM_RC2_ECB:
959 return NULL;
960 case CKM_RC2_CBC:
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];
965 case CKM_RC5_CBC:
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:
971 case CKM_AES_CBC:
972 case CKM_DES_CBC:
973 case CKM_DES3_CBC:
974 case CKM_IDEA_CBC:
975 case CKM_CDMF_CBC:
976 case CKM_CAST_CBC:
977 case CKM_CAST3_CBC:
978 case CKM_CAST5_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:
1002 break;
1003 /* unknown mechanism, pass IV in if it's there */
1004 default:
1005 break;
1007 if (param->data) {
1008 *len = param->len;
1010 return param->data;
1013 typedef struct sec_rc5cbcParameterStr {
1014 SECItem version;
1015 SECItem rounds;
1016 SECItem blockSizeInBits;
1017 SECItem iv;
1018 } sec_rc5cbcParameter;
1020 static const SEC_ASN1Template sec_rc5ecb_parameter_template[] = {
1021 { SEC_ASN1_SEQUENCE,
1022 0, NULL, sizeof(sec_rc5cbcParameter) },
1023 { SEC_ASN1_INTEGER,
1024 offsetof(sec_rc5cbcParameter,version) },
1025 { SEC_ASN1_INTEGER,
1026 offsetof(sec_rc5cbcParameter,rounds) },
1027 { SEC_ASN1_INTEGER,
1028 offsetof(sec_rc5cbcParameter,blockSizeInBits) },
1029 { 0 }
1032 static const SEC_ASN1Template sec_rc5cbc_parameter_template[] = {
1033 { SEC_ASN1_SEQUENCE,
1034 0, NULL, sizeof(sec_rc5cbcParameter) },
1035 { SEC_ASN1_INTEGER,
1036 offsetof(sec_rc5cbcParameter,version) },
1037 { SEC_ASN1_INTEGER,
1038 offsetof(sec_rc5cbcParameter,rounds) },
1039 { SEC_ASN1_INTEGER,
1040 offsetof(sec_rc5cbcParameter,blockSizeInBits) },
1041 { SEC_ASN1_OCTET_STRING,
1042 offsetof(sec_rc5cbcParameter,iv) },
1043 { 0 }
1046 typedef struct sec_rc2cbcParameterStr {
1047 SECItem rc2ParameterVersion;
1048 SECItem iv;
1049 } sec_rc2cbcParameter;
1051 static const SEC_ASN1Template sec_rc2cbc_parameter_template[] = {
1052 { SEC_ASN1_SEQUENCE,
1053 0, NULL, sizeof(sec_rc2cbcParameter) },
1054 { SEC_ASN1_INTEGER,
1055 offsetof(sec_rc2cbcParameter,rc2ParameterVersion) },
1056 { SEC_ASN1_OCTET_STRING,
1057 offsetof(sec_rc2cbcParameter,iv) },
1058 { 0 }
1061 static const SEC_ASN1Template sec_rc2ecb_parameter_template[] = {
1062 { SEC_ASN1_SEQUENCE,
1063 0, NULL, sizeof(sec_rc2cbcParameter) },
1064 { SEC_ASN1_INTEGER,
1065 offsetof(sec_rc2cbcParameter,rc2ParameterVersion) },
1066 { 0 }
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)
1074 long x;
1076 x = DER_GetInteger(version);
1078 switch (x) {
1079 case 58: return 128;
1080 case 120: return 64;
1081 case 160: return 40;
1083 return 128;
1086 static unsigned long rc2_unmap(unsigned long x)
1088 switch (x) {
1089 case 128: return 58;
1090 case 64: return 120;
1091 case 40: return 160;
1093 return 58;
1098 /* Generate a mechaism param from a type, and iv. */
1099 SECItem *
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;
1108 SECOidTag algtag;
1109 SECStatus rv;
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);
1121 if (mech == NULL) {
1122 return NULL;
1124 mech->type = siBuffer;
1125 mech->data = NULL;
1126 mech->len = 0;
1128 arena = PORT_NewArena(1024);
1129 if (!arena) {
1130 goto loser;
1133 /* handle the complicated cases */
1134 switch (type) {
1135 case CKM_RC2_ECB:
1136 rv = SEC_ASN1DecodeItem(arena, &rc2 ,sec_rc2ecb_parameter_template,
1137 &(algid->parameters));
1138 if (rv != SECSuccess) {
1139 goto loser;
1141 rc2_ecb_params = PORT_New(CK_RC2_PARAMS);
1142 if (rc2_ecb_params == NULL) {
1143 goto loser;
1145 *rc2_ecb_params = rc2_map(&rc2.rc2ParameterVersion);
1146 mech->data = (unsigned char *) rc2_ecb_params;
1147 mech->len = sizeof *rc2_ecb_params;
1148 break;
1149 case CKM_RC2_CBC:
1150 case CKM_RC2_CBC_PAD:
1151 rv = SEC_ASN1DecodeItem(arena, &rc2 ,sec_rc2cbc_parameter_template,
1152 &(algid->parameters));
1153 if (rv != SECSuccess) {
1154 goto loser;
1156 rc2_cbc_params = PORT_New(CK_RC2_CBC_PARAMS);
1157 if (rc2_cbc_params == NULL) {
1158 goto loser;
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);
1165 goto loser;
1167 PORT_Memcpy(rc2_cbc_params->iv, rc2.iv.data, rc2.iv.len);
1168 break;
1169 case CKM_RC5_ECB:
1170 rv = SEC_ASN1DecodeItem(arena, &rc5 ,sec_rc5ecb_parameter_template,
1171 &(algid->parameters));
1172 if (rv != SECSuccess) {
1173 goto loser;
1175 rc5_ecb_params = PORT_New(CK_RC5_PARAMS);
1176 if (rc5_ecb_params == NULL) {
1177 goto loser;
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;
1183 break;
1184 case CKM_RC5_CBC:
1185 case CKM_RC5_CBC_PAD:
1186 rv = SEC_ASN1DecodeItem(arena, &rc5 ,sec_rc5cbc_parameter_template,
1187 &(algid->parameters));
1188 if (rv != SECSuccess) {
1189 goto loser;
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) {
1194 goto loser;
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);
1204 break;
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) {
1222 goto loser;
1224 break;
1225 case CKM_RC4:
1226 case CKM_CAMELLIA_ECB:
1227 case CKM_AES_ECB:
1228 case CKM_DES_ECB:
1229 case CKM_DES3_ECB:
1230 case CKM_IDEA_ECB:
1231 case CKM_CDMF_ECB:
1232 case CKM_CAST_ECB:
1233 case CKM_CAST3_ECB:
1234 case CKM_CAST5_ECB:
1235 break;
1237 default:
1238 if (pk11_lookup(type)->iv == 0) {
1239 break;
1241 /* FALL THROUGH */
1242 case CKM_CAMELLIA_CBC:
1243 case CKM_AES_CBC:
1244 case CKM_DES_CBC:
1245 case CKM_DES3_CBC:
1246 case CKM_IDEA_CBC:
1247 case CKM_CDMF_CBC:
1248 case CKM_CAST_CBC:
1249 case CKM_CAST3_CBC:
1250 case CKM_CAST5_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) {
1280 goto loser;
1282 /* XXX Should be some IV length sanity check here. */
1283 mech->data = (unsigned char*)PORT_Alloc(iv.len);
1284 if (mech->data == NULL) {
1285 goto loser;
1287 PORT_Memcpy(mech->data, iv.data, iv.len);
1288 mech->len = iv.len;
1289 break;
1291 PORT_FreeArena(arena, PR_FALSE);
1292 return mech;
1294 loser:
1295 if (arena)
1296 PORT_FreeArena(arena, PR_FALSE);
1297 SECITEM_FreeItem(mech,PR_TRUE);
1298 return NULL;
1302 * Generate an IV for the given mechanism
1304 static SECStatus
1305 pk11_GenIV(CK_MECHANISM_TYPE type, SECItem *iv) {
1306 int iv_size = PK11_GetIVLength(type);
1307 SECStatus rv;
1309 iv->len = iv_size;
1310 if (iv_size == 0) {
1311 iv->data = NULL;
1312 return SECSuccess;
1315 iv->data = (unsigned char *) PORT_Alloc(iv_size);
1316 if (iv->data == NULL) {
1317 iv->len = 0;
1318 return SECFailure;
1321 rv = PK11_GenerateRandom(iv->data,iv->len);
1322 if (rv != SECSuccess) {
1323 PORT_Free(iv->data);
1324 iv->data = NULL; iv->len = 0;
1325 return SECFailure;
1327 return SECSuccess;
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.
1335 SECItem *
1336 PK11_GenerateNewParam(CK_MECHANISM_TYPE type, PK11SymKey *key) {
1337 CK_RC2_CBC_PARAMS *rc2_params;
1338 CK_RC2_PARAMS *rc2_ecb_params;
1339 SECItem *mech;
1340 SECItem iv;
1341 SECStatus rv;
1344 mech = (SECItem *) PORT_Alloc(sizeof(SECItem));
1345 if (mech == NULL) return NULL;
1347 rv = SECSuccess;
1348 mech->type = siBuffer;
1349 switch (type) {
1350 case CKM_RC4:
1351 case CKM_CAMELLIA_ECB:
1352 case CKM_AES_ECB:
1353 case CKM_DES_ECB:
1354 case CKM_DES3_ECB:
1355 case CKM_IDEA_ECB:
1356 case CKM_CDMF_ECB:
1357 case CKM_CAST_ECB:
1358 case CKM_CAST3_ECB:
1359 case CKM_CAST5_ECB:
1360 mech->data = NULL;
1361 mech->len = 0;
1362 break;
1363 case CKM_RC2_ECB:
1364 rc2_ecb_params = (CK_RC2_PARAMS *)PORT_Alloc(sizeof(CK_RC2_PARAMS));
1365 if (rc2_ecb_params == NULL) {
1366 rv = SECFailure;
1367 break;
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);
1374 break;
1375 case CKM_RC2_CBC:
1376 case CKM_RC2_CBC_PAD:
1377 rv = pk11_GenIV(type,&iv);
1378 if (rv != SECSuccess) {
1379 break;
1381 rc2_params = (CK_RC2_CBC_PARAMS *)PORT_Alloc(sizeof(CK_RC2_CBC_PARAMS));
1382 if (rc2_params == NULL) {
1383 PORT_Free(iv.data);
1384 rv = SECFailure;
1385 break;
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;
1390 if (iv.data)
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);
1394 PORT_Free(iv.data);
1395 break;
1396 case CKM_RC5_ECB:
1397 PORT_Free(mech);
1398 return PK11_ParamFromIV(type,NULL);
1399 case CKM_RC5_CBC:
1400 case CKM_RC5_CBC_PAD:
1401 rv = pk11_GenIV(type,&iv);
1402 if (rv != SECSuccess) {
1403 break;
1405 PORT_Free(mech);
1406 return PK11_ParamFromIV(type,&iv);
1407 default:
1408 if (pk11_lookup(type)->iv == 0) {
1409 mech->data = NULL;
1410 mech->len = 0;
1411 break;
1413 case CKM_CAMELLIA_CBC:
1414 case CKM_AES_CBC:
1415 case CKM_DES_CBC:
1416 case CKM_DES3_CBC:
1417 case CKM_IDEA_CBC:
1418 case CKM_CDMF_CBC:
1419 case CKM_CAST_CBC:
1420 case CKM_CAST3_CBC:
1421 case CKM_CAST5_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) {
1447 break;
1449 mech->data = (unsigned char*)PORT_Alloc(iv.len);
1450 if (mech->data == NULL) {
1451 PORT_Free(iv.data);
1452 rv = SECFailure;
1453 break;
1455 PORT_Memcpy(mech->data,iv.data,iv.len);
1456 mech->len = iv.len;
1457 PORT_Free(iv.data);
1458 break;
1460 if (rv != SECSuccess) {
1461 SECITEM_FreeItem(mech,PR_TRUE);
1462 return NULL;
1464 return mech;
1468 #define RC5_V10 0x10
1470 /* turn a PKCS #11 parameter into a DER Encoded Algorithm ID */
1471 SECStatus
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;
1483 switch (type) {
1484 case CKM_RC4:
1485 case CKM_CAMELLIA_ECB:
1486 case CKM_AES_ECB:
1487 case CKM_DES_ECB:
1488 case CKM_DES3_ECB:
1489 case CKM_IDEA_ECB:
1490 case CKM_CDMF_ECB:
1491 case CKM_CAST_ECB:
1492 case CKM_CAST3_ECB:
1493 case CKM_CAST5_ECB:
1494 newParams = NULL;
1495 rv = SECSuccess;
1496 break;
1497 case CKM_RC2_ECB:
1498 break;
1499 case CKM_RC2_CBC:
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)
1505 break;
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)
1512 break;
1513 rv = SECSuccess;
1514 break;
1516 case CKM_RC5_ECB: /* well not really... */
1517 break;
1518 case CKM_RC5_CBC:
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)
1522 break;
1523 if (SEC_ASN1EncodeUnsignedInteger (NULL, &rc5.blockSizeInBits,
1524 rc5_params->ulWordsize*8) == NULL) {
1525 PORT_Free(rc5.version.data);
1526 break;
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);
1532 break;
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)
1542 break;
1543 rv = SECSuccess;
1544 break;
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);
1561 default:
1562 if (pk11_lookup(type)->iv == 0) {
1563 rv = SECSuccess;
1564 newParams = NULL;
1565 break;
1567 case CKM_CAMELLIA_CBC:
1568 case CKM_AES_CBC:
1569 case CKM_DES_CBC:
1570 case CKM_DES3_CBC:
1571 case CKM_IDEA_CBC:
1572 case CKM_CDMF_CBC:
1573 case CKM_CAST_CBC:
1574 case CKM_CAST3_CBC:
1575 case CKM_CAST5_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)
1602 break;
1603 rv = SECSuccess;
1604 break;
1607 if (rv != SECSuccess) {
1608 if (newParams) SECITEM_FreeItem(newParams,PR_TRUE);
1609 return rv;
1612 rv = SECOID_SetAlgorithmID(arena, algid, algTag, newParams);
1613 SECITEM_FreeItem(newParams,PR_TRUE);
1614 return rv;
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 */
1620 CK_MECHANISM_TYPE
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. */
1629 SECOidTag
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.
1641 CK_MECHANISM_TYPE
1642 PK11_GetPadMechanism(CK_MECHANISM_TYPE type) {
1643 switch(type) {
1644 case CKM_CAMELLIA_CBC:
1645 return CKM_CAMELLIA_CBC_PAD;
1646 case CKM_AES_CBC:
1647 return CKM_AES_CBC_PAD;
1648 case CKM_DES_CBC:
1649 return CKM_DES_CBC_PAD;
1650 case CKM_DES3_CBC:
1651 return CKM_DES3_CBC_PAD;
1652 case CKM_RC2_CBC:
1653 return CKM_RC2_CBC_PAD;
1654 case CKM_CDMF_CBC:
1655 return CKM_CDMF_CBC_PAD;
1656 case CKM_CAST_CBC:
1657 return CKM_CAST_CBC_PAD;
1658 case CKM_CAST3_CBC:
1659 return CKM_CAST3_CBC_PAD;
1660 case CKM_CAST5_CBC:
1661 return CKM_CAST5_CBC_PAD;
1662 case CKM_RC5_CBC:
1663 return CKM_RC5_CBC_PAD;
1664 case CKM_IDEA_CBC:
1665 return CKM_IDEA_CBC_PAD;
1666 default:
1667 break;
1670 return type;
1673 static PRBool
1674 pk11_isAllZero(unsigned char *data,int len) {
1675 while (len--) {
1676 if (*data++) {
1677 return PR_FALSE;
1680 return PR_TRUE;
1683 CK_RV
1684 PK11_MapPBEMechanismToCryptoMechanism(CK_MECHANISM_PTR pPBEMechanism,
1685 CK_MECHANISM_PTR pCryptoMechanism,
1686 SECItem *pbe_pwd, PRBool faulty3DES)
1688 int iv_len = 0;
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);
1700 if (iv_len) {
1701 if (pk11_isAllZero(pPBEparams->pInitVector,iv_len)) {
1702 SECItem param;
1703 PK11SymKey *symKey;
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, &param, 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),
1742 iv_len);
1743 break;
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;
1751 break;
1752 case CKM_NETSCAPE_PBE_SHA1_40_BIT_RC2_CBC:
1753 case CKM_PBE_SHA1_RC2_40_CBC:
1754 rc2_key_len = 40;
1755 goto have_key_len;
1756 case CKM_NETSCAPE_PBE_SHA1_128_BIT_RC2_CBC:
1757 rc2_key_len = 128;
1758 have_key_len:
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,
1770 iv_len);
1771 rc2_params->ulEffectiveBits = rc2_key_len;
1772 break;
1773 default:
1774 return CKR_MECHANISM_INVALID;
1777 return CKR_OK;
1780 /* Make a Key type to an appropriate signing/verification mechanism */
1781 CK_MECHANISM_TYPE
1782 PK11_MapSignKeyType(KeyType keyType)
1784 switch (keyType) {
1785 case rsaKey:
1786 return CKM_RSA_PKCS;
1787 case fortezzaKey:
1788 case dsaKey:
1789 return CKM_DSA;
1790 case ecKey:
1791 return CKM_ECDSA;
1792 case dhKey:
1793 default:
1794 break;
1796 return CKM_INVALID_MECHANISM;
1799 CK_MECHANISM_TYPE
1800 pk11_mapWrapKeyType(KeyType keyType)
1802 switch (keyType) {
1803 case rsaKey:
1804 return CKM_RSA_PKCS;
1805 /* Add fortezza?? */
1806 default:
1807 break;
1809 return CKM_INVALID_MECHANISM;
1812 SECOidTag
1813 PK11_FortezzaMapSig(SECOidTag algTag)
1815 switch (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;
1822 default:
1823 break;
1825 return algTag;