3
|
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 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 #include "mycrypt.h" |
|
12 |
|
13 /* The Mask Generation Function (MGF1) for PKCS #1 -- Tom St Denis */ |
|
14 |
|
15 #ifdef PKCS_1 |
|
16 |
|
17 int pkcs_1_mgf1(const unsigned char *seed, unsigned long seedlen, |
|
18 int hash_idx, |
|
19 unsigned char *mask, unsigned long masklen) |
|
20 { |
|
21 unsigned long hLen, counter, x; |
|
22 int err; |
|
23 hash_state md; |
|
24 unsigned char buf[MAXBLOCKSIZE]; |
|
25 |
|
26 _ARGCHK(seed != NULL); |
|
27 _ARGCHK(mask != NULL); |
|
28 |
|
29 /* ensure valid hash */ |
|
30 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { |
|
31 return err; |
|
32 } |
|
33 |
|
34 /* get hash output size */ |
|
35 hLen = hash_descriptor[hash_idx].hashsize; |
|
36 |
|
37 /* start counter */ |
|
38 counter = 0; |
|
39 |
|
40 while (masklen > 0) { |
|
41 /* handle counter */ |
|
42 STORE32H(counter, buf); |
|
43 ++counter; |
|
44 |
|
45 /* get hash of seed || counter */ |
|
46 hash_descriptor[hash_idx].init(&md); |
|
47 if ((err = hash_descriptor[hash_idx].process(&md, seed, seedlen)) != CRYPT_OK) { |
|
48 return err; |
|
49 } |
|
50 if ((err = hash_descriptor[hash_idx].process(&md, buf, 4)) != CRYPT_OK) { |
|
51 return err; |
|
52 } |
|
53 if ((err = hash_descriptor[hash_idx].done(&md, buf)) != CRYPT_OK) { |
|
54 return err; |
|
55 } |
|
56 |
|
57 /* store it */ |
|
58 for (x = 0; x < hLen && masklen > 0; x++, masklen--) { |
|
59 *mask++ = buf[x]; |
|
60 } |
|
61 } |
|
62 |
|
63 return CRYPT_OK; |
|
64 } |
|
65 |
|
66 #endif /* PKCS_1 */ |