Changeset View
Changeset View
Standalone View
Standalone View
head/sys/geom/eli/pkcs5v2.c
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | |||||
void | void | ||||
pkcs5v2_genkey(uint8_t *key, unsigned keylen, const uint8_t *salt, | pkcs5v2_genkey(uint8_t *key, unsigned keylen, const uint8_t *salt, | ||||
size_t saltsize, const char *passphrase, u_int iterations) | size_t saltsize, const char *passphrase, u_int iterations) | ||||
{ | { | ||||
uint8_t md[SHA512_MDLEN], saltcount[saltsize + sizeof(uint32_t)]; | uint8_t md[SHA512_MDLEN], saltcount[saltsize + sizeof(uint32_t)]; | ||||
uint8_t *counter, *keyp; | uint8_t *counter, *keyp; | ||||
u_int i, bsize, passlen; | u_int i, bsize, passlen; | ||||
uint32_t count; | uint32_t count; | ||||
struct hmac_ctx startpoint, ctx; | |||||
passlen = strlen(passphrase); | passlen = strlen(passphrase); | ||||
bzero(key, keylen); | bzero(key, keylen); | ||||
bcopy(salt, saltcount, saltsize); | bcopy(salt, saltcount, saltsize); | ||||
counter = saltcount + saltsize; | counter = saltcount + saltsize; | ||||
keyp = key; | keyp = key; | ||||
for (count = 1; keylen > 0; count++, keylen -= bsize, keyp += bsize) { | for (count = 1; keylen > 0; count++, keylen -= bsize, keyp += bsize) { | ||||
bsize = MIN(keylen, sizeof(md)); | bsize = MIN(keylen, sizeof(md)); | ||||
counter[0] = (count >> 24) & 0xff; | be32enc(counter, count); | ||||
counter[1] = (count >> 16) & 0xff; | |||||
counter[2] = (count >> 8) & 0xff; | g_eli_crypto_hmac_init(&startpoint, passphrase, passlen); | ||||
counter[3] = count & 0xff; | ctx = startpoint; | ||||
g_eli_crypto_hmac(passphrase, passlen, saltcount, | g_eli_crypto_hmac_update(&ctx, saltcount, sizeof(saltcount)); | ||||
sizeof(saltcount), md, 0); | g_eli_crypto_hmac_final(&ctx, md, sizeof(md)); | ||||
xor(keyp, md, bsize); | xor(keyp, md, bsize); | ||||
for(i = 1; i < iterations; i++) { | for(i = 1; i < iterations; i++) { | ||||
g_eli_crypto_hmac(passphrase, passlen, md, sizeof(md), | ctx = startpoint; | ||||
md, 0); | g_eli_crypto_hmac_update(&ctx, md, sizeof(md)); | ||||
g_eli_crypto_hmac_final(&ctx, md, sizeof(md)); | |||||
xor(keyp, md, bsize); | xor(keyp, md, bsize); | ||||
} | } | ||||
} | } | ||||
explicit_bzero(&startpoint, sizeof(startpoint)); | |||||
explicit_bzero(&ctx, sizeof(ctx)); | |||||
} | } | ||||
#ifndef _KERNEL | #ifndef _KERNEL | ||||
#ifndef _STAND | #ifndef _STAND | ||||
/* | /* | ||||
* Return the number of microseconds needed for 'interations' iterations. | * Return the number of microseconds needed for 'interations' iterations. | ||||
*/ | */ | ||||
static int | static int | ||||
Show All 35 Lines |