1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
15 The Mask Generation Function (MGF1) for PKCS #1, Tom St Denis
21 Perform PKCS #1 MGF1 (internal)
22 @param seed The seed for MGF1
23 @param seedlen The length of the seed
24 @param hash_idx The index of the hash desired
25 @param mask [out] The destination
26 @param masklen The length of the mask desired
27 @return CRYPT_OK if successful
29 int pkcs_1_mgf1(int hash_idx,
30 const unsigned char *seed, unsigned long seedlen,
31 unsigned char *mask, unsigned long masklen)
33 unsigned long hLen, x;
39 LTC_ARGCHK(seed != NULL);
40 LTC_ARGCHK(mask != NULL);
42 /* ensure valid hash */
43 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
47 /* get hash output size */
48 hLen = hash_descriptor[hash_idx].hashsize;
51 md = XMALLOC(sizeof(hash_state));
53 if (md == NULL || buf == NULL) {
68 STORE32H(counter, buf);
71 /* get hash of seed || counter */
72 if ((err = hash_descriptor[hash_idx].init(md)) != CRYPT_OK) {
75 if ((err = hash_descriptor[hash_idx].process(md, seed, seedlen)) != CRYPT_OK) {
78 if ((err = hash_descriptor[hash_idx].process(md, buf, 4)) != CRYPT_OK) {
81 if ((err = hash_descriptor[hash_idx].done(md, buf)) != CRYPT_OK) {
86 for (x = 0; x < hLen && masklen > 0; x++, masklen--) {
93 #ifdef LTC_CLEAN_STACK
95 zeromem(md, sizeof(hash_state));
106 /* $Source: /cvs/libtom/libtomcrypt/src/pk/pkcs1/pkcs_1_mgf1.c,v $ */
107 /* $Revision: 1.6 $ */
108 /* $Date: 2006/03/31 14:15:35 $ */