Skip to content

Commit b3f483a

Browse files
karel-msjaeckel
authored andcommitted
add pkcs12_kdf()
1 parent 98ad88b commit b3f483a

5 files changed

Lines changed: 98 additions & 0 deletions

File tree

src/headers/tomcrypt_custom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@
463463

464464
#define LTC_PKCS_1
465465
#define LTC_PKCS_5
466+
#define LTC_PKCS_12
466467

467468
/* Include ASN.1 DER (required by DSA/RSA) */
468469
#define LTC_DER

src/headers/tomcrypt_pkcs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
103103
int pkcs_5_test (void);
104104
#endif /* LTC_PKCS_5 */
105105

106+
106107
/* ref: $Format:%D$ */
107108
/* git commit: $Format:%H$ */
108109
/* commit time: $Format:%ai$ */

src/headers/tomcrypt_private.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,18 @@ int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long i
305305

306306
#endif /* LTC_DER */
307307

308+
/* tomcrypt_pkcs.h */
309+
310+
#ifdef LTC_PKCS_12
311+
312+
int pkcs12_kdf( int hash_id,
313+
const unsigned char *pw, unsigned long pwlen,
314+
const unsigned char *salt, unsigned long saltlen,
315+
unsigned int iterations, unsigned char purpose,
316+
unsigned char *out, unsigned long outlen);
317+
318+
#endif /* LTC_PKCS_12 */
319+
308320
/* tomcrypt_prng.h */
309321

310322
#define _LTC_PRNG_EXPORT(which) \

src/misc/crypt/crypt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ const char *crypt_build_settings =
436436
#if defined(LTC_PKCS_5)
437437
" PKCS#5 "
438438
#endif
439+
#if defined(LTC_PKCS_12)
440+
" PKCS#12 "
441+
#endif
439442
#if defined(LTC_PADDING)
440443
" PADDING "
441444
#endif

src/misc/pkcs12/pkcs12_kdf.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2+
*
3+
* LibTomCrypt is a library that provides various cryptographic
4+
* algorithms in a highly modular and flexible manner.
5+
*
6+
* The library is free for all purposes without any express
7+
* guarantee it works.
8+
*/
9+
10+
#include "tomcrypt_private.h"
11+
12+
#ifdef LTC_PKCS_12
13+
14+
int pkcs12_kdf( int hash_id,
15+
const unsigned char *pw, unsigned long pwlen,
16+
const unsigned char *salt, unsigned long saltlen,
17+
unsigned int iterations, unsigned char purpose,
18+
unsigned char *out, unsigned long outlen)
19+
{
20+
unsigned long u = hash_descriptor[hash_id].hashsize;
21+
unsigned long v = hash_descriptor[hash_id].blocksize;
22+
unsigned long c = (outlen + u - 1) / u;
23+
unsigned long Slen = ((saltlen + v - 1) / v) * v;
24+
unsigned long Plen = ((pwlen + v - 1) / v) * v;
25+
unsigned long k = (Plen + Slen) / v;
26+
unsigned long Alen, keylen = 0;
27+
unsigned int tmp, i, j, n;
28+
unsigned char ch;
29+
unsigned char D[MAXBLOCKSIZE], A[MAXBLOCKSIZE], B[MAXBLOCKSIZE];
30+
unsigned char *I = NULL, *key = NULL;
31+
int err = CRYPT_ERROR;
32+
33+
key = XMALLOC(u * c);
34+
I = XMALLOC(Plen + Slen);
35+
if (key == NULL || I == NULL) goto DONE;
36+
zeromem(key, u * c);
37+
38+
for (i = 0; i < v; i++) D[i] = purpose; /* D - diversifier */
39+
for (i = 0; i < Slen; i++) I[i] = salt[i % saltlen];
40+
for (i = 0; i < Plen; i++) I[Slen + i] = pw[i % pwlen]; /* I = Salt || Pass */
41+
42+
for (i = 0; i < c; i++) {
43+
Alen = u; /* hash size */
44+
err = hash_memory_multi(hash_id, A, &Alen, D, v, I, Slen + Plen, NULL); /* A = HASH(D || I) */
45+
if (err != CRYPT_OK) goto DONE;
46+
for (j = 1; j < iterations; j++) {
47+
err = hash_memory(hash_id, A, Alen, A, &Alen); /* A = HASH(A) */
48+
if (err != CRYPT_OK) goto DONE;
49+
}
50+
/* fill buffer B with A */
51+
for (j = 0; j < v; j++) B[j] = A[j % Alen];
52+
/* B += 1 */
53+
for (j = v; j > 0; j--) {
54+
if (++B[j - 1] != 0) break;
55+
}
56+
/* I_n += B */
57+
for (n = 0; n < k; n++) {
58+
ch = 0;
59+
for (j = v; j > 0; j--) {
60+
tmp = I[n * v + j - 1] + B[j - 1] + ch;
61+
ch = (unsigned char)((tmp >> 8) & 0xFF);
62+
I[n * v + j - 1] = (unsigned char)(tmp & 0xFF);
63+
}
64+
}
65+
/* store derived key block */
66+
for (j = 0; j < Alen; j++) key[keylen++] = A[j];
67+
}
68+
69+
for (i = 0; i < outlen; i++) out[i] = key[i];
70+
err = CRYPT_OK;
71+
DONE:
72+
if (I) XFREE(I);
73+
if (key) XFREE(key);
74+
return err;
75+
}
76+
77+
#endif
78+
79+
/* ref: $Format:%D$ */
80+
/* git commit: $Format:%H$ */
81+
/* commit time: $Format:%ai$ */

0 commit comments

Comments
 (0)