2004-05-12 20:42:16 +00:00
|
|
|
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
|
|
|
*
|
|
|
|
* LibTomCrypt is a library that provides various cryptographic
|
|
|
|
* algorithms in a highly modular and flexible manner.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* guarantee it works.
|
|
|
|
*
|
2005-04-17 11:37:13 +00:00
|
|
|
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
|
2004-05-12 20:42:16 +00:00
|
|
|
*/
|
2004-12-30 23:55:53 +00:00
|
|
|
#include <tomcrypt.h>
|
2004-05-12 20:42:16 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
/**
|
|
|
|
@file pkcs_5_1.c
|
|
|
|
PKCS #5, Algorithm #1, Tom St Denis
|
|
|
|
*/
|
2004-05-12 20:42:16 +00:00
|
|
|
#ifdef PKCS_5
|
2004-12-30 23:55:53 +00:00
|
|
|
/**
|
|
|
|
Execute PKCS #5 v1
|
|
|
|
@param password The password (or key)
|
|
|
|
@param password_len The length of the password (octet)
|
|
|
|
@param salt The salt (or nonce) which is 8 octets long
|
|
|
|
@param iteration_count The PKCS #5 v1 iteration count
|
|
|
|
@param hash_idx The index of the hash desired
|
|
|
|
@param out [out] The destination for this algorithm
|
|
|
|
@param outlen [in/out] The max size and resulting size of the algorithm output
|
|
|
|
@return CRYPT_OK if successful
|
|
|
|
*/
|
2004-05-12 20:42:16 +00:00
|
|
|
int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
|
|
|
|
const unsigned char *salt,
|
|
|
|
int iteration_count, int hash_idx,
|
|
|
|
unsigned char *out, unsigned long *outlen)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
unsigned long x;
|
2004-06-20 02:41:49 +00:00
|
|
|
hash_state *md;
|
|
|
|
unsigned char *buf;
|
2004-05-12 20:42:16 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
LTC_ARGCHK(password != NULL);
|
|
|
|
LTC_ARGCHK(salt != NULL);
|
|
|
|
LTC_ARGCHK(out != NULL);
|
|
|
|
LTC_ARGCHK(outlen != NULL);
|
2004-05-12 20:42:16 +00:00
|
|
|
|
|
|
|
/* test hash IDX */
|
|
|
|
if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2004-06-20 02:41:49 +00:00
|
|
|
/* allocate memory */
|
|
|
|
md = XMALLOC(sizeof(hash_state));
|
|
|
|
buf = XMALLOC(MAXBLOCKSIZE);
|
|
|
|
if (md == NULL || buf == NULL) {
|
|
|
|
if (md != NULL) {
|
|
|
|
XFREE(md);
|
|
|
|
}
|
|
|
|
if (buf != NULL) {
|
|
|
|
XFREE(buf);
|
|
|
|
}
|
|
|
|
return CRYPT_MEM;
|
|
|
|
}
|
|
|
|
|
2004-05-12 20:42:16 +00:00
|
|
|
/* hash initial password + salt */
|
2004-10-30 03:00:26 +00:00
|
|
|
if ((err = hash_descriptor[hash_idx].init(md)) != CRYPT_OK) {
|
2004-12-30 23:55:53 +00:00
|
|
|
goto LBL_ERR;
|
2004-10-30 03:00:26 +00:00
|
|
|
}
|
2004-06-20 02:41:49 +00:00
|
|
|
if ((err = hash_descriptor[hash_idx].process(md, password, password_len)) != CRYPT_OK) {
|
2004-12-30 23:55:53 +00:00
|
|
|
goto LBL_ERR;
|
2004-06-20 02:41:49 +00:00
|
|
|
}
|
|
|
|
if ((err = hash_descriptor[hash_idx].process(md, salt, 8)) != CRYPT_OK) {
|
2004-12-30 23:55:53 +00:00
|
|
|
goto LBL_ERR;
|
2004-06-20 02:41:49 +00:00
|
|
|
}
|
|
|
|
if ((err = hash_descriptor[hash_idx].done(md, buf)) != CRYPT_OK) {
|
2004-12-30 23:55:53 +00:00
|
|
|
goto LBL_ERR;
|
2004-06-20 02:41:49 +00:00
|
|
|
}
|
2004-05-12 20:42:16 +00:00
|
|
|
|
|
|
|
while (--iteration_count) {
|
2005-04-17 11:37:13 +00:00
|
|
|
/* code goes here. */
|
2004-06-20 02:41:49 +00:00
|
|
|
x = MAXBLOCKSIZE;
|
2004-05-12 20:42:16 +00:00
|
|
|
if ((err = hash_memory(hash_idx, buf, hash_descriptor[hash_idx].hashsize, buf, &x)) != CRYPT_OK) {
|
2004-12-30 23:55:53 +00:00
|
|
|
goto LBL_ERR;
|
2004-05-12 20:42:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy upto outlen bytes */
|
|
|
|
for (x = 0; x < hash_descriptor[hash_idx].hashsize && x < *outlen; x++) {
|
|
|
|
out[x] = buf[x];
|
|
|
|
}
|
|
|
|
*outlen = x;
|
2004-06-20 02:41:49 +00:00
|
|
|
err = CRYPT_OK;
|
2004-12-30 23:55:53 +00:00
|
|
|
LBL_ERR:
|
|
|
|
#ifdef LTC_CLEAN_STACK
|
2004-06-20 02:41:49 +00:00
|
|
|
zeromem(buf, MAXBLOCKSIZE);
|
|
|
|
zeromem(md, sizeof(hash_state));
|
2004-05-12 20:42:16 +00:00
|
|
|
#endif
|
|
|
|
|
2004-06-20 02:41:49 +00:00
|
|
|
XFREE(buf);
|
|
|
|
XFREE(md);
|
|
|
|
|
|
|
|
return err;
|
2004-05-12 20:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|