/
lowpbe.c
1739 lines (1530 loc) · 51.1 KB
/
lowpbe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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/. */
#include "plarena.h"
#include "seccomon.h"
#include "secitem.h"
#include "secport.h"
#include "hasht.h"
#include "pkcs11t.h"
#include "blapi.h"
#include "hasht.h"
#include "secasn1.h"
#include "secder.h"
#include "lowpbe.h"
#include "secoid.h"
#include "alghmac.h"
#include "softoken.h"
#include "secerr.h"
#include "pkcs11i.h"
SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate)
/* how much a crypto encrypt/decryption may expand a buffer */
#define MAX_CRYPTO_EXPANSION 64
/* template for PKCS 5 PBE Parameter. This template has been expanded
* based upon the additions in PKCS 12. This should eventually be moved
* if RSA updates PKCS 5.
*/
static const SEC_ASN1Template NSSPKCS5PBEParameterTemplate[] =
{
{ SEC_ASN1_SEQUENCE,
0, NULL, sizeof(NSSPKCS5PBEParameter) },
{ SEC_ASN1_OCTET_STRING,
offsetof(NSSPKCS5PBEParameter, salt) },
{ SEC_ASN1_INTEGER,
offsetof(NSSPKCS5PBEParameter, iteration) },
{ 0 }
};
static const SEC_ASN1Template NSSPKCS5PKCS12V2PBEParameterTemplate[] =
{
{ SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSSPKCS5PBEParameter) },
{ SEC_ASN1_OCTET_STRING, offsetof(NSSPKCS5PBEParameter, salt) },
{ SEC_ASN1_INTEGER, offsetof(NSSPKCS5PBEParameter, iteration) },
{ 0 }
};
/* PKCS5 v2 */
struct nsspkcs5V2PBEParameterStr {
SECAlgorithmID keyParams; /* parameters of the key generation */
SECAlgorithmID algParams; /* parameters for the encryption or mac op */
};
typedef struct nsspkcs5V2PBEParameterStr nsspkcs5V2PBEParameter;
static const SEC_ASN1Template NSSPKCS5V2PBES2ParameterTemplate[] =
{
{ SEC_ASN1_SEQUENCE, 0, NULL, sizeof(nsspkcs5V2PBEParameter) },
{ SEC_ASN1_INLINE | SEC_ASN1_XTRN,
offsetof(nsspkcs5V2PBEParameter, keyParams),
SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
{ SEC_ASN1_INLINE | SEC_ASN1_XTRN,
offsetof(nsspkcs5V2PBEParameter, algParams),
SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
{ 0 }
};
static const SEC_ASN1Template NSSPKCS5V2PBEParameterTemplate[] =
{
{ SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSSPKCS5PBEParameter) },
/* this is really a choice, but since we don't understand any other
* choice, just inline it. */
{ SEC_ASN1_OCTET_STRING, offsetof(NSSPKCS5PBEParameter, salt) },
{ SEC_ASN1_INTEGER, offsetof(NSSPKCS5PBEParameter, iteration) },
{ SEC_ASN1_INTEGER, offsetof(NSSPKCS5PBEParameter, keyLength) },
{ SEC_ASN1_INLINE | SEC_ASN1_XTRN,
offsetof(NSSPKCS5PBEParameter, prfAlg),
SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
{ 0 }
};
SECStatus
nsspkcs5_HashBuf(const SECHashObject *hashObj, unsigned char *dest,
unsigned char *src, int len)
{
void *ctx;
unsigned int retLen;
ctx = hashObj->create();
if (ctx == NULL) {
return SECFailure;
}
hashObj->begin(ctx);
hashObj->update(ctx, src, len);
hashObj->end(ctx, dest, &retLen, hashObj->length);
hashObj->destroy(ctx, PR_TRUE);
return SECSuccess;
}
/* generate bits using any hash
*/
static SECItem *
nsspkcs5_PBKDF1(const SECHashObject *hashObj, SECItem *salt, SECItem *pwd,
int iter, PRBool faulty3DES)
{
SECItem *hash = NULL, *pre_hash = NULL;
SECStatus rv = SECFailure;
if ((salt == NULL) || (pwd == NULL) || (iter < 0)) {
return NULL;
}
hash = (SECItem *)PORT_ZAlloc(sizeof(SECItem));
pre_hash = (SECItem *)PORT_ZAlloc(sizeof(SECItem));
if ((hash != NULL) && (pre_hash != NULL)) {
int i, ph_len;
ph_len = hashObj->length;
if ((salt->len + pwd->len) > hashObj->length) {
ph_len = salt->len + pwd->len;
}
rv = SECFailure;
/* allocate buffers */
hash->len = hashObj->length;
hash->data = (unsigned char *)PORT_ZAlloc(hash->len);
pre_hash->data = (unsigned char *)PORT_ZAlloc(ph_len);
/* in pbeSHA1TripleDESCBC there was an allocation error that made
* it into the caller. We do not want to propagate those errors
* further, so we are doing it correctly, but reading the old method.
*/
if (faulty3DES) {
pre_hash->len = ph_len;
} else {
pre_hash->len = salt->len + pwd->len;
}
/* preform hash */
if ((hash->data != NULL) && (pre_hash->data != NULL)) {
rv = SECSuccess;
/* check for 0 length password */
if (pwd->len > 0) {
PORT_Memcpy(pre_hash->data, pwd->data, pwd->len);
}
if (salt->len > 0) {
PORT_Memcpy((pre_hash->data + pwd->len), salt->data, salt->len);
}
for (i = 0; ((i < iter) && (rv == SECSuccess)); i++) {
rv = nsspkcs5_HashBuf(hashObj, hash->data,
pre_hash->data, pre_hash->len);
if (rv != SECFailure) {
pre_hash->len = hashObj->length;
PORT_Memcpy(pre_hash->data, hash->data, hashObj->length);
}
}
}
}
if (pre_hash != NULL) {
SECITEM_FreeItem(pre_hash, PR_TRUE);
}
if ((rv != SECSuccess) && (hash != NULL)) {
SECITEM_FreeItem(hash, PR_TRUE);
hash = NULL;
}
return hash;
}
/* this bit generation routine is described in PKCS 12 and the proposed
* extensions to PKCS 5. an initial hash is generated following the
* instructions laid out in PKCS 5. If the number of bits generated is
* insufficient, then the method discussed in the proposed extensions to
* PKCS 5 in PKCS 12 are used. This extension makes use of the HMAC
* function. And the P_Hash function from the TLS standard.
*/
static SECItem *
nsspkcs5_PFXPBE(const SECHashObject *hashObj, NSSPKCS5PBEParameter *pbe_param,
SECItem *init_hash, unsigned int bytes_needed)
{
SECItem *ret_bits = NULL;
int hash_size = 0;
unsigned int i;
unsigned int hash_iter;
unsigned int dig_len;
SECStatus rv = SECFailure;
unsigned char *state = NULL;
unsigned int state_len;
HMACContext *cx = NULL;
hash_size = hashObj->length;
hash_iter = (bytes_needed + (unsigned int)hash_size - 1) / hash_size;
/* allocate return buffer */
ret_bits = (SECItem *)PORT_ZAlloc(sizeof(SECItem));
if (ret_bits == NULL)
return NULL;
ret_bits->data = (unsigned char *)PORT_ZAlloc((hash_iter * hash_size) + 1);
ret_bits->len = (hash_iter * hash_size);
if (ret_bits->data == NULL) {
PORT_Free(ret_bits);
return NULL;
}
/* allocate intermediate hash buffer. 8 is for the 8 bytes of
* data which are added based on iteration number
*/
if ((unsigned int)hash_size > pbe_param->salt.len) {
state_len = hash_size;
} else {
state_len = pbe_param->salt.len;
}
state = (unsigned char *)PORT_ZAlloc(state_len);
if (state == NULL) {
rv = SECFailure;
goto loser;
}
if (pbe_param->salt.len > 0) {
PORT_Memcpy(state, pbe_param->salt.data, pbe_param->salt.len);
}
cx = HMAC_Create(hashObj, init_hash->data, init_hash->len, PR_TRUE);
if (cx == NULL) {
rv = SECFailure;
goto loser;
}
for (i = 0; i < hash_iter; i++) {
/* generate output bits */
HMAC_Begin(cx);
HMAC_Update(cx, state, state_len);
HMAC_Update(cx, pbe_param->salt.data, pbe_param->salt.len);
rv = HMAC_Finish(cx, ret_bits->data + (i * hash_size),
&dig_len, hash_size);
if (rv != SECSuccess)
goto loser;
PORT_Assert((unsigned int)hash_size == dig_len);
/* generate new state */
HMAC_Begin(cx);
HMAC_Update(cx, state, state_len);
rv = HMAC_Finish(cx, state, &state_len, state_len);
if (rv != SECSuccess)
goto loser;
PORT_Assert(state_len == dig_len);
}
loser:
if (state != NULL)
PORT_ZFree(state, state_len);
HMAC_Destroy(cx, PR_TRUE);
if (rv != SECSuccess) {
SECITEM_ZfreeItem(ret_bits, PR_TRUE);
ret_bits = NULL;
}
return ret_bits;
}
/* generate bits for the key and iv determination. if enough bits
* are not generated using PKCS 5, then we need to generate more bits
* based on the extension proposed in PKCS 12
*/
static SECItem *
nsspkcs5_PBKDF1Extended(const SECHashObject *hashObj,
NSSPKCS5PBEParameter *pbe_param, SECItem *pwitem, PRBool faulty3DES)
{
SECItem *hash = NULL;
SECItem *newHash = NULL;
int bytes_needed;
int bytes_available;
bytes_needed = pbe_param->ivLen + pbe_param->keyLen;
bytes_available = hashObj->length;
hash = nsspkcs5_PBKDF1(hashObj, &pbe_param->salt, pwitem,
pbe_param->iter, faulty3DES);
if (hash == NULL) {
return NULL;
}
if (bytes_needed <= bytes_available) {
return hash;
}
newHash = nsspkcs5_PFXPBE(hashObj, pbe_param, hash, bytes_needed);
if (hash != newHash)
SECITEM_FreeItem(hash, PR_TRUE);
return newHash;
}
/*
* PBDKDF2 is PKCS #5 v2.0 it's currently not used by NSS
*/
static void
do_xor(unsigned char *dest, unsigned char *src, int len)
{
/* use byt xor, not all platforms are happy about inaligned
* integer fetches */
while (len--) {
*dest = *dest ^ *src;
dest++;
src++;
}
}
static SECStatus
nsspkcs5_PBKDF2_F(const SECHashObject *hashobj, SECItem *pwitem, SECItem *salt,
int iterations, unsigned int i, unsigned char *T)
{
int j;
HMACContext *cx = NULL;
unsigned int hLen = hashobj->length;
SECStatus rv = SECFailure;
unsigned char *last = NULL;
unsigned int lastLength = salt->len + 4;
unsigned int lastBufLength;
cx = HMAC_Create(hashobj, pwitem->data, pwitem->len, PR_FALSE);
if (cx == NULL) {
goto loser;
}
PORT_Memset(T, 0, hLen);
lastBufLength = PR_MAX(lastLength, hLen);
last = PORT_Alloc(lastBufLength);
if (last == NULL) {
goto loser;
}
PORT_Memcpy(last, salt->data, salt->len);
last[salt->len] = (i >> 24) & 0xff;
last[salt->len + 1] = (i >> 16) & 0xff;
last[salt->len + 2] = (i >> 8) & 0xff;
last[salt->len + 3] = i & 0xff;
/* NOTE: we need at least one iteration to return success! */
for (j = 0; j < iterations; j++) {
HMAC_Begin(cx);
HMAC_Update(cx, last, lastLength);
rv = HMAC_Finish(cx, last, &lastLength, hLen);
if (rv != SECSuccess) {
break;
}
do_xor(T, last, hLen);
}
loser:
if (cx) {
HMAC_Destroy(cx, PR_TRUE);
}
if (last) {
PORT_ZFree(last, lastBufLength);
}
return rv;
}
static SECItem *
nsspkcs5_PBKDF2(const SECHashObject *hashobj, NSSPKCS5PBEParameter *pbe_param,
SECItem *pwitem)
{
int iterations = pbe_param->iter;
int bytesNeeded = pbe_param->keyLen;
unsigned int dkLen = bytesNeeded;
unsigned int hLen = hashobj->length;
unsigned int nblocks = (dkLen + hLen - 1) / hLen;
unsigned int i;
unsigned char *rp;
unsigned char *T = NULL;
SECItem *result = NULL;
SECItem *salt = &pbe_param->salt;
SECStatus rv = SECFailure;
result = SECITEM_AllocItem(NULL, NULL, nblocks * hLen);
if (result == NULL) {
return NULL;
}
T = PORT_Alloc(hLen);
if (T == NULL) {
goto loser;
}
for (i = 1, rp = result->data; i <= nblocks; i++, rp += hLen) {
rv = nsspkcs5_PBKDF2_F(hashobj, pwitem, salt, iterations, i, T);
if (rv != SECSuccess) {
break;
}
PORT_Memcpy(rp, T, hLen);
}
loser:
if (T) {
PORT_ZFree(T, hLen);
}
if (rv != SECSuccess) {
SECITEM_FreeItem(result, PR_TRUE);
result = NULL;
} else {
result->len = dkLen;
}
return result;
}
#define NSSPBE_ROUNDUP(x, y) ((((x) + ((y)-1)) / (y)) * (y))
#define NSSPBE_MIN(x, y) ((x) < (y) ? (x) : (y))
/*
* This is the extended PBE function defined by the final PKCS #12 spec.
*/
static SECItem *
nsspkcs5_PKCS12PBE(const SECHashObject *hashObject,
NSSPKCS5PBEParameter *pbe_param, SECItem *pwitem,
PBEBitGenID bitGenPurpose, unsigned int bytesNeeded)
{
PLArenaPool *arena = NULL;
unsigned int SLen, PLen;
unsigned int hashLength = hashObject->length;
unsigned char *S, *P;
SECItem *A = NULL, B, D, I;
SECItem *salt = &pbe_param->salt;
unsigned int c, i = 0;
unsigned int hashLen;
int iter;
unsigned char *iterBuf;
void *hash = NULL;
unsigned int bufferLength;
arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (!arena) {
return NULL;
}
/* how many hash object lengths are needed */
c = (bytesNeeded + (hashLength - 1)) / hashLength;
/* 64 if 0 < hashLength <= 32, 128 if 32 < hashLength <= 64 */
bufferLength = NSSPBE_ROUNDUP(hashLength * 2, 64);
/* initialize our buffers */
D.len = bufferLength;
/* B and D are the same length, use one alloc go get both */
D.data = (unsigned char *)PORT_ArenaZAlloc(arena, D.len * 2);
B.len = D.len;
B.data = D.data + D.len;
/* if all goes well, A will be returned, so don't use our temp arena */
A = SECITEM_AllocItem(NULL, NULL, c * hashLength);
if (A == NULL) {
goto loser;
}
SLen = NSSPBE_ROUNDUP(salt->len, bufferLength);
PLen = NSSPBE_ROUNDUP(pwitem->len, bufferLength);
I.len = SLen + PLen;
I.data = (unsigned char *)PORT_ArenaZAlloc(arena, I.len);
if (I.data == NULL) {
goto loser;
}
/* S & P are only used to initialize I */
S = I.data;
P = S + SLen;
PORT_Memset(D.data, (char)bitGenPurpose, D.len);
if (SLen) {
for (i = 0; i < SLen; i += salt->len) {
PORT_Memcpy(S + i, salt->data, NSSPBE_MIN(SLen - i, salt->len));
}
}
if (PLen) {
for (i = 0; i < PLen; i += pwitem->len) {
PORT_Memcpy(P + i, pwitem->data, NSSPBE_MIN(PLen - i, pwitem->len));
}
}
iterBuf = (unsigned char *)PORT_ArenaZAlloc(arena, hashLength);
if (iterBuf == NULL) {
goto loser;
}
hash = hashObject->create();
if (!hash) {
goto loser;
}
/* calculate the PBE now */
for (i = 0; i < c; i++) {
int Bidx; /* must be signed or the for loop won't terminate */
unsigned int k, j;
unsigned char *Ai = A->data + i * hashLength;
for (iter = 0; iter < pbe_param->iter; iter++) {
hashObject->begin(hash);
if (iter) {
hashObject->update(hash, iterBuf, hashLen);
} else {
hashObject->update(hash, D.data, D.len);
hashObject->update(hash, I.data, I.len);
}
hashObject->end(hash, iterBuf, &hashLen, hashObject->length);
if (hashLen != hashObject->length) {
break;
}
}
PORT_Memcpy(Ai, iterBuf, hashLength);
for (Bidx = 0; Bidx < (int)B.len; Bidx += hashLength) {
PORT_Memcpy(B.data + Bidx, iterBuf, NSSPBE_MIN(B.len - Bidx, hashLength));
}
k = I.len / B.len;
for (j = 0; j < k; j++) {
unsigned int q, carryBit;
unsigned char *Ij = I.data + j * B.len;
/* (Ij = Ij+B+1) */
for (Bidx = (B.len - 1), q = 1, carryBit = 0; Bidx >= 0; Bidx--, q = 0) {
q += (unsigned int)Ij[Bidx];
q += (unsigned int)B.data[Bidx];
q += carryBit;
carryBit = (q > 0xff);
Ij[Bidx] = (unsigned char)(q & 0xff);
}
}
}
loser:
if (hash) {
hashObject->destroy(hash, PR_TRUE);
}
if (arena) {
PORT_FreeArena(arena, PR_TRUE);
}
if (A) {
/* if i != c, then we didn't complete the loop above and must of failed
* somwhere along the way */
if (i != c) {
SECITEM_ZfreeItem(A, PR_TRUE);
A = NULL;
} else {
A->len = bytesNeeded;
}
}
return A;
}
struct KDFCacheItemStr {
SECItem *hash;
SECItem *salt;
SECItem *pwItem;
HASH_HashType hashType;
int iterations;
int keyLen;
};
typedef struct KDFCacheItemStr KDFCacheItem;
/* Bug 1606992 - Cache the hash result for the common case that we're
* asked to repeatedly compute the key for the same password item,
* hash, iterations and salt. */
static struct {
PZLock *lock;
struct {
KDFCacheItem common;
int ivLen;
PRBool faulty3DES;
} cacheKDF1;
struct {
KDFCacheItem common;
} cacheKDF2;
} PBECache;
void
sftk_PBELockInit(void)
{
if (!PBECache.lock) {
PBECache.lock = PZ_NewLock(nssIPBECacheLock);
}
}
static void
sftk_clearPBECommonCacheItemsLocked(KDFCacheItem *item)
{
if (item->hash) {
SECITEM_ZfreeItem(item->hash, PR_TRUE);
item->hash = NULL;
}
if (item->salt) {
SECITEM_FreeItem(item->salt, PR_TRUE);
item->salt = NULL;
}
if (item->pwItem) {
SECITEM_ZfreeItem(item->pwItem, PR_TRUE);
item->pwItem = NULL;
}
}
static void
sftk_setPBECommonCacheItemsKDFLocked(KDFCacheItem *cacheItem,
const SECItem *hash,
const NSSPKCS5PBEParameter *pbe_param,
const SECItem *pwItem)
{
cacheItem->hash = SECITEM_DupItem(hash);
cacheItem->hashType = pbe_param->hashType;
cacheItem->iterations = pbe_param->iter;
cacheItem->keyLen = pbe_param->keyLen;
cacheItem->salt = SECITEM_DupItem(&pbe_param->salt);
cacheItem->pwItem = SECITEM_DupItem(pwItem);
}
static void
sftk_setPBECacheKDF2(const SECItem *hash,
const NSSPKCS5PBEParameter *pbe_param,
const SECItem *pwItem)
{
PZ_Lock(PBECache.lock);
sftk_clearPBECommonCacheItemsLocked(&PBECache.cacheKDF2.common);
sftk_setPBECommonCacheItemsKDFLocked(&PBECache.cacheKDF2.common,
hash, pbe_param, pwItem);
PZ_Unlock(PBECache.lock);
}
static void
sftk_setPBECacheKDF1(const SECItem *hash,
const NSSPKCS5PBEParameter *pbe_param,
const SECItem *pwItem,
PRBool faulty3DES)
{
PZ_Lock(PBECache.lock);
sftk_clearPBECommonCacheItemsLocked(&PBECache.cacheKDF1.common);
sftk_setPBECommonCacheItemsKDFLocked(&PBECache.cacheKDF1.common,
hash, pbe_param, pwItem);
PBECache.cacheKDF1.faulty3DES = faulty3DES;
PBECache.cacheKDF1.ivLen = pbe_param->ivLen;
PZ_Unlock(PBECache.lock);
}
static PRBool
sftk_comparePBECommonCacheItemLocked(const KDFCacheItem *cacheItem,
const NSSPKCS5PBEParameter *pbe_param,
const SECItem *pwItem)
{
return (cacheItem->hash &&
cacheItem->salt &&
cacheItem->pwItem &&
pbe_param->hashType == cacheItem->hashType &&
pbe_param->iter == cacheItem->iterations &&
pbe_param->keyLen == cacheItem->keyLen &&
SECITEM_ItemsAreEqual(&pbe_param->salt, cacheItem->salt) &&
SECITEM_ItemsAreEqual(pwItem, cacheItem->pwItem));
}
static SECItem *
sftk_getPBECacheKDF2(const NSSPKCS5PBEParameter *pbe_param,
const SECItem *pwItem)
{
SECItem *result = NULL;
const KDFCacheItem *cacheItem = &PBECache.cacheKDF2.common;
PZ_Lock(PBECache.lock);
if (sftk_comparePBECommonCacheItemLocked(cacheItem, pbe_param, pwItem)) {
result = SECITEM_DupItem(cacheItem->hash);
}
PZ_Unlock(PBECache.lock);
return result;
}
static SECItem *
sftk_getPBECacheKDF1(const NSSPKCS5PBEParameter *pbe_param,
const SECItem *pwItem,
PRBool faulty3DES)
{
SECItem *result = NULL;
const KDFCacheItem *cacheItem = &PBECache.cacheKDF1.common;
PZ_Lock(PBECache.lock);
if (sftk_comparePBECommonCacheItemLocked(cacheItem, pbe_param, pwItem) &&
PBECache.cacheKDF1.faulty3DES == faulty3DES &&
PBECache.cacheKDF1.ivLen == pbe_param->ivLen) {
result = SECITEM_DupItem(cacheItem->hash);
}
PZ_Unlock(PBECache.lock);
return result;
}
void
sftk_PBELockShutdown(void)
{
if (PBECache.lock) {
PZ_DestroyLock(PBECache.lock);
PBECache.lock = 0;
}
sftk_clearPBECommonCacheItemsLocked(&PBECache.cacheKDF1.common);
sftk_clearPBECommonCacheItemsLocked(&PBECache.cacheKDF2.common);
}
/*
* generate key as per PKCS 5
*/
SECItem *
nsspkcs5_ComputeKeyAndIV(NSSPKCS5PBEParameter *pbe_param, SECItem *pwitem,
SECItem *iv, PRBool faulty3DES)
{
SECItem *hash = NULL, *key = NULL;
const SECHashObject *hashObj;
PRBool getIV = PR_FALSE;
if ((pbe_param == NULL) || (pwitem == NULL)) {
return NULL;
}
key = SECITEM_AllocItem(NULL, NULL, pbe_param->keyLen);
if (key == NULL) {
return NULL;
}
if (iv && (pbe_param->ivLen) && (iv->data == NULL)) {
getIV = PR_TRUE;
iv->data = (unsigned char *)PORT_Alloc(pbe_param->ivLen);
if (iv->data == NULL) {
goto loser;
}
iv->len = pbe_param->ivLen;
}
hashObj = HASH_GetRawHashObject(pbe_param->hashType);
switch (pbe_param->pbeType) {
case NSSPKCS5_PBKDF1:
hash = sftk_getPBECacheKDF1(pbe_param, pwitem, faulty3DES);
if (!hash) {
hash = nsspkcs5_PBKDF1Extended(hashObj, pbe_param, pwitem, faulty3DES);
sftk_setPBECacheKDF1(hash, pbe_param, pwitem, faulty3DES);
}
if (hash == NULL) {
goto loser;
}
PORT_Assert(hash->len >= key->len + (getIV ? iv->len : 0));
if (getIV) {
PORT_Memcpy(iv->data, hash->data + (hash->len - iv->len), iv->len);
}
break;
case NSSPKCS5_PBKDF2:
hash = sftk_getPBECacheKDF2(pbe_param, pwitem);
if (!hash) {
hash = nsspkcs5_PBKDF2(hashObj, pbe_param, pwitem);
sftk_setPBECacheKDF2(hash, pbe_param, pwitem);
}
if (getIV) {
PORT_Memcpy(iv->data, pbe_param->ivData, iv->len);
}
break;
case NSSPKCS5_PKCS12_V2:
if (getIV) {
hash = nsspkcs5_PKCS12PBE(hashObj, pbe_param, pwitem,
pbeBitGenCipherIV, iv->len);
if (hash == NULL) {
goto loser;
}
PORT_Memcpy(iv->data, hash->data, iv->len);
SECITEM_ZfreeItem(hash, PR_TRUE);
hash = NULL;
}
hash = nsspkcs5_PKCS12PBE(hashObj, pbe_param, pwitem,
pbe_param->keyID, key->len);
default:
break;
}
if (hash == NULL) {
goto loser;
}
if (pbe_param->is2KeyDES) {
PORT_Memcpy(key->data, hash->data, (key->len * 2) / 3);
PORT_Memcpy(&(key->data[(key->len * 2) / 3]), key->data,
key->len / 3);
} else {
PORT_Memcpy(key->data, hash->data, key->len);
}
SECITEM_ZfreeItem(hash, PR_TRUE);
return key;
loser:
if (getIV && iv->data) {
PORT_ZFree(iv->data, iv->len);
iv->data = NULL;
}
SECITEM_ZfreeItem(key, PR_TRUE);
return NULL;
}
#define MAX_IV_LENGTH 64
/* get a random IV into the parameters */
static SECStatus
nsspkcs5_SetIVParam(NSSPKCS5PBEParameter *pbe_param, int ivLen)
{
SECStatus rv;
SECItem derIV;
SECItem iv;
SECItem *dummy = NULL;
unsigned char ivData[MAX_IV_LENGTH];
PORT_Assert(ivLen <= MAX_IV_LENGTH);
/* Because of a bug in the decode section, the IV's not are expected
* to be der encoded, but still need to parse as if they were der data.
* because we want to be compatible with existing versions of nss that
* have that bug, create an IV that looks like der data. That still
* leaves 14 bytes of entropy in the IV */
rv = RNG_GenerateGlobalRandomBytes(ivData, ivLen - 2);
if (rv != SECSuccess) {
return SECFailure;
}
derIV.data = NULL;
derIV.len = 0;
iv.data = ivData;
iv.len = ivLen - 2;
dummy = SEC_ASN1EncodeItem(pbe_param->poolp, &derIV, &iv,
SEC_ASN1_GET(SEC_OctetStringTemplate));
if (dummy == NULL) {
return SECFailure;
}
pbe_param->ivData = derIV.data;
pbe_param->ivLen = derIV.len;
PORT_Assert(pbe_param->ivLen == ivLen);
return SECSuccess;
}
static SECStatus
nsspkcs5_FillInParam(SECOidTag algorithm, HASH_HashType hashType,
NSSPKCS5PBEParameter *pbe_param)
{
PRBool skipType = PR_FALSE;
SECStatus rv;
pbe_param->keyLen = 5;
pbe_param->ivLen = 8;
pbe_param->hashType = hashType;
pbe_param->pbeType = NSSPKCS5_PBKDF1;
pbe_param->encAlg = SEC_OID_RC2_CBC;
pbe_param->is2KeyDES = PR_FALSE;
switch (algorithm) {
/* DES3 Algorithms */
case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_2KEY_TRIPLE_DES_CBC:
pbe_param->is2KeyDES = PR_TRUE;
/* fall through */
case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC:
pbe_param->pbeType = NSSPKCS5_PKCS12_V2;
/* fall through */
case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_TRIPLE_DES_CBC:
pbe_param->keyLen = 24;
pbe_param->encAlg = SEC_OID_DES_EDE3_CBC;
break;
/* DES Algorithms */
case SEC_OID_PKCS5_PBE_WITH_MD2_AND_DES_CBC:
pbe_param->hashType = HASH_AlgMD2;
goto finish_des;
case SEC_OID_PKCS5_PBE_WITH_MD5_AND_DES_CBC:
pbe_param->hashType = HASH_AlgMD5;
/* fall through */
case SEC_OID_PKCS5_PBE_WITH_SHA1_AND_DES_CBC:
finish_des:
pbe_param->keyLen = 8;
pbe_param->encAlg = SEC_OID_DES_CBC;
break;
/* RC2 Algorithms */
case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC:
pbe_param->keyLen = 16;
/* fall through */
case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC:
pbe_param->pbeType = NSSPKCS5_PKCS12_V2;
break;
case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC:
pbe_param->keyLen = 16;
/* fall through */
case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC:
break;
/* RC4 algorithms */
case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_128_BIT_RC4:
skipType = PR_TRUE;
/* fall through */
case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC4:
pbe_param->keyLen = 16;
/* fall through */
case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC4:
if (!skipType) {
pbe_param->pbeType = NSSPKCS5_PKCS12_V2;
}
/* fall through */
case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_40_BIT_RC4:
pbe_param->ivLen = 0;
pbe_param->encAlg = SEC_OID_RC4;
break;
case SEC_OID_PKCS5_PBKDF2:
case SEC_OID_PKCS5_PBES2:
case SEC_OID_PKCS5_PBMAC1:
/* everything else will be filled in by the template */
pbe_param->ivLen = 0;
pbe_param->pbeType = NSSPKCS5_PBKDF2;
pbe_param->encAlg = SEC_OID_PKCS5_PBKDF2;
pbe_param->keyLen = 0; /* needs to be set by caller after return */
break;
/* AES uses PBKDF2 */
case SEC_OID_AES_128_CBC:
rv = nsspkcs5_SetIVParam(pbe_param, 16);
if (rv != SECSuccess) {
return rv;
}
pbe_param->ivLen = 16;
pbe_param->pbeType = NSSPKCS5_PBKDF2;
pbe_param->encAlg = algorithm;
pbe_param->keyLen = 128 / 8;
break;
case SEC_OID_AES_192_CBC:
rv = nsspkcs5_SetIVParam(pbe_param, 16);
if (rv != SECSuccess) {
return rv;
}
pbe_param->pbeType = NSSPKCS5_PBKDF2;
pbe_param->encAlg = algorithm;
pbe_param->keyLen = 192 / 8;
break;
case SEC_OID_AES_256_CBC:
rv = nsspkcs5_SetIVParam(pbe_param, 16);
if (rv != SECSuccess) {
return rv;
}
pbe_param->pbeType = NSSPKCS5_PBKDF2;
pbe_param->encAlg = algorithm;
pbe_param->keyLen = 256 / 8;
break;
case SEC_OID_AES_128_KEY_WRAP:
pbe_param->ivLen = 0;
pbe_param->pbeType = NSSPKCS5_PBKDF2;
pbe_param->encAlg = algorithm;
pbe_param->keyLen = 128 / 8;
break;
case SEC_OID_AES_192_KEY_WRAP:
pbe_param->ivLen = 0;
pbe_param->pbeType = NSSPKCS5_PBKDF2;
pbe_param->encAlg = algorithm;
pbe_param->keyLen = 192 / 8;
break;
case SEC_OID_AES_256_KEY_WRAP:
pbe_param->ivLen = 0;
pbe_param->pbeType = NSSPKCS5_PBKDF2;
pbe_param->encAlg = algorithm;
pbe_param->keyLen = 256 / 8;
break;
default:
return SECFailure;
}
if (pbe_param->pbeType == NSSPKCS5_PBKDF2) {
SECOidTag prfAlg = HASH_HMACOidFromHash(pbe_param->hashType);
if (prfAlg == SEC_OID_UNKNOWN) {
return SECFailure;
}
rv = SECOID_SetAlgorithmID(pbe_param->poolp, &pbe_param->prfAlg,
prfAlg, NULL);
if (rv != SECSuccess) {
return rv;
}
}
return SECSuccess;
}
/* decode the algid and generate a PKCS 5 parameter from it
*/
NSSPKCS5PBEParameter *
nsspkcs5_NewParam(SECOidTag alg, HASH_HashType hashType, SECItem *salt,
int iterationCount)