Path: blob/master/libs/tomcrypt/src/pk/pkcs1/pkcs_1_mgf1.c
4396 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file pkcs_1_mgf1.c12The Mask Generation Function (MGF1) for PKCS #1, Tom St Denis13*/1415#ifdef LTC_PKCS_11617/**18Perform PKCS #1 MGF1 (internal)19@param hash_idx The index of the hash desired20@param seed The seed for MGF121@param seedlen The length of the seed22@param mask [out] The destination23@param masklen The length of the mask desired24@return CRYPT_OK if successful25*/26int pkcs_1_mgf1(int hash_idx,27const unsigned char *seed, unsigned long seedlen,28unsigned char *mask, unsigned long masklen)29{30unsigned long hLen, x;31ulong32 counter;32int err;33hash_state *md;34unsigned char *buf;3536LTC_ARGCHK(seed != NULL);37LTC_ARGCHK(mask != NULL);3839/* ensure valid hash */40if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {41return err;42}4344/* get hash output size */45hLen = hash_descriptor[hash_idx].hashsize;4647/* allocate memory */48md = XMALLOC(sizeof(hash_state));49buf = XMALLOC(hLen);50if (md == NULL || buf == NULL) {51if (md != NULL) {52XFREE(md);53}54if (buf != NULL) {55XFREE(buf);56}57return CRYPT_MEM;58}5960/* start counter */61counter = 0;6263while (masklen > 0) {64/* handle counter */65STORE32H(counter, buf);66++counter;6768/* get hash of seed || counter */69if ((err = hash_descriptor[hash_idx].init(md)) != CRYPT_OK) {70goto LBL_ERR;71}72if ((err = hash_descriptor[hash_idx].process(md, seed, seedlen)) != CRYPT_OK) {73goto LBL_ERR;74}75if ((err = hash_descriptor[hash_idx].process(md, buf, 4)) != CRYPT_OK) {76goto LBL_ERR;77}78if ((err = hash_descriptor[hash_idx].done(md, buf)) != CRYPT_OK) {79goto LBL_ERR;80}8182/* store it */83for (x = 0; x < hLen && masklen > 0; x++, masklen--) {84*mask++ = buf[x];85}86}8788err = CRYPT_OK;89LBL_ERR:90#ifdef LTC_CLEAN_STACK91zeromem(buf, hLen);92zeromem(md, sizeof(hash_state));93#endif9495XFREE(buf);96XFREE(md);9798return err;99}100101#endif /* LTC_PKCS_1 */102103104