#include <crypto/kdf.h>

NTL_CLIENT


void KDF1 :: PrettyPrint(ostream& tofile)
{
	tofile << "Kdf1(Hash=";
	h.PrettyPrint(tofile);
	tofile << ")";
}

ostring KDF1 :: eval(const ostring& str, const long l)
{
	if (l < 0)
		throw General_error();
	int i;

	long k = (l + h.outlen - 1)/h.outlen;
	ostring ostr, x, y;

	ostr.reserve(l);

	if (l == 0)
		return ostr;

	for(i = 0; i < k-1; i++)
	{
		y = i2osp(to_ZZ(i),4);
		x = h.eval(str + y);
		ostr += x;
	}

	y = i2osp(to_ZZ(i),4);
	x = h.eval(str + y);
	ostr += x.substr(0, l-(k-1)*h.outlen);
	
	return ostr;
}


void KDF2 :: PrettyPrint(ostream& tofile)
{
	tofile << "Kdf2(Hash=";
	h.PrettyPrint(tofile);
	tofile << ")";
}

ostring KDF2 :: eval(const ostring& str, const long l)
{
        if (l < 0)
                throw General_error();
        int i;

        long k = (l + h.outlen - 1)/h.outlen;
        ostring ostr, x, y;

        ostr.reserve(l);

        if (l == 0)
                return ostr;

        for(i = 1; i < k; i++)
        {
                y = i2osp(to_ZZ(i),4);
                x = h.eval(str + y);
                ostr += x;
        }

        y = i2osp(to_ZZ(i),4);
        x = h.eval(str + y);
        ostr += x.substr(0, l-(k-1)*h.outlen);

        return ostr;
}

