while(keylen) { if(keylen > SHA_DIGEST_LENGTH) cplen = SHA_DIGEST_LENGTH; else cplen = keylen;
while(tkeylen) { if(tkeylen > SHA_DIGEST_LENGTH) cplen = SHA_DIGEST_LENGTH; else cplen = tkeylen;
int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,			   unsigned char *salt, int saltlen, int iter,			   int keylen, unsigned char *out){	unsigned char digtmp[SHA_DIGEST_LENGTH], *p, itmp[4];	int cplen, j, k;	unsigned long i = 1;	HMAC_CTX hctx;	p = out;	if(passlen == -1) passlen = strlen(pass);	while(keylen) {		if(keylen > SHA_DIGEST_LENGTH) cplen = SHA_DIGEST_LENGTH;		else cplen = keylen;		/* We are unlikely to ever use more than 256 blocks (5120 bits!)		 * but just in case...		 */		itmp[0] = (i >> 24) & 0xff;		itmp[1] = (i >> 16) & 0xff;		itmp[2] = (i >> 8) & 0xff;		itmp[3] = i & 0xff;		HMAC_Init(&hctx, pass, passlen, EVP_sha1());		HMAC_Update(&hctx, salt, saltlen);		HMAC_Update(&hctx, itmp, 4);		HMAC_Final(&hctx, digtmp, NULL);		memcpy(p, digtmp, cplen);		for(j = 1; j < iter; j++) {			HMAC(EVP_sha1(), pass, passlen,				 digtmp, SHA_DIGEST_LENGTH, digtmp, NULL);			for(k = 0; k < cplen; k++) p[k] ^= digtmp[k];		}		keylen-= cplen;		i++;		p+= cplen;	}	HMAC_cleanup(&hctx);	return 1;}