/* * Copyright 2004 Clemens Fruhwirth * Implementation of PBKDF2-HMAC-SHA1 according to RFC 2898. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include "hmac_sha1.h" #include "XORblock.h" #include static unsigned int *__PBKDF2_global_j; static unsigned int __PBKDF2_performance=0; void PBKDF2_HMAC_SHA1(const char *password, size_t passwordLen, const char *salt, size_t saltLen, unsigned int iterations, char *dKey, size_t dKeyLen) { uint32_t i=1; unsigned int j; /* U_n is the buffer for U_n values */ unsigned char U_n[SHA1_DIGEST_SIZE]; /* F_buf is the XOR buffer for F function */ char F_buf[SHA1_DIGEST_SIZE]; hmac_ctx templateCtx; /* We need a global pointer for signal handlers */ __PBKDF2_global_j = &j; /* Make a template context initialized with password as key */ hmac_sha_begin(&templateCtx); hmac_sha_key((unsigned char *) password,passwordLen,&templateCtx); #define HMAC_REINIT(__ctx) memcpy(&__ctx,&templateCtx,sizeof(__ctx)) /* The first hash iteration is done different, therefor we reduce iterations to conveniently use it as a loop counter */ assert(iterations != 0); iterations--; while(dKeyLen > 0) { hmac_ctx ctx; uint32_t iNetworkOrdered; unsigned int blocksize = dKeyLen