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
13 The Mask Generation Function (MGF1) for PKCS #1, Tom St Denis
19 Perform PKCS #1 MGF1 (internal)
20 @param hash_idx The index of the hash desired
21 @param seed The seed for MGF1
22 @param seedlen The length of the seed
23 @param mask [out] The destination
24 @param masklen The length of the mask desired
25 @return CRYPT_OK if successful
27 int pkcs_1_mgf1(int hash_idx,
28 const unsigned char *seed, unsigned long seedlen,
29 unsigned char *mask, unsigned long masklen)
31 unsigned long hLen, x;
37 LTC_ARGCHK(seed != NULL);
38 LTC_ARGCHK(mask != NULL);
40 /* ensure valid hash */
41 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
45 /* get hash output size */
46 hLen = hash_descriptor[hash_idx].hashsize;
49 md = XMALLOC(sizeof(hash_state));
51 if (md == NULL || buf == NULL) {
66 STORE32H(counter, buf);
69 /* get hash of seed || counter */
70 if ((err = hash_descriptor[hash_idx].init(md)) != CRYPT_OK) {
73 if ((err = hash_descriptor[hash_idx].process(md, seed, seedlen)) != CRYPT_OK) {
76 if ((err = hash_descriptor[hash_idx].process(md, buf, 4)) != CRYPT_OK) {
79 if ((err = hash_descriptor[hash_idx].done(md, buf)) != CRYPT_OK) {
84 for (x = 0; x < hLen && masklen > 0; x++, masklen--) {
91 #ifdef LTC_CLEAN_STACK
93 zeromem(md, sizeof(hash_state));
102 #endif /* LTC_PKCS_1 */
104 /* ref: $Format:%D$ */
105 /* git commit: $Format:%H$ */
106 /* commit time: $Format:%ai$ */