mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-12 18:49:07 +00:00
extract some pbkdf2 constants into macros
This commit is contained in:
parent
352bc42be2
commit
97067c918e
27
pbkdf2.c
27
pbkdf2.c
@ -2,12 +2,15 @@
|
||||
#include "pbkdf2.h"
|
||||
#include "hmac.h"
|
||||
|
||||
#define HMACFUNC hmac_sha512
|
||||
#define HMACLEN (512/8)
|
||||
|
||||
void pbkdf2(const uint8_t *pass, int passlen, uint8_t *salt, int saltlen, uint32_t iterations, uint8_t *key, int keylen)
|
||||
{
|
||||
uint32_t i, j, k;
|
||||
uint8_t f[64], g[64];
|
||||
uint32_t blocks = keylen / 64; // SHA-512
|
||||
if (keylen & 63) {
|
||||
uint8_t f[HMACLEN], g[HMACLEN];
|
||||
uint32_t blocks = keylen / HMACLEN;
|
||||
if (keylen & (HMACLEN - 1)) {
|
||||
blocks++;
|
||||
}
|
||||
for (i = 1; i <= blocks; i++) {
|
||||
@ -15,22 +18,18 @@ void pbkdf2(const uint8_t *pass, int passlen, uint8_t *salt, int saltlen, uint32
|
||||
salt[saltlen + 1] = (i >> 16) & 0xFF;
|
||||
salt[saltlen + 2] = (i >> 8) & 0xFF;
|
||||
salt[saltlen + 3] = i & 0xFF;
|
||||
hmac_sha512(pass, passlen, salt, saltlen + 4, g);
|
||||
memcpy(f, g, 64);
|
||||
HMACFUNC(pass, passlen, salt, saltlen + 4, g);
|
||||
memcpy(f, g, HMACLEN);
|
||||
for (j = 1; j < iterations; j++) {
|
||||
hmac_sha512(pass, passlen, g, 64, g);
|
||||
for (k = 0; k < 64; k++) {
|
||||
HMACFUNC(pass, passlen, g, HMACLEN, g);
|
||||
for (k = 0; k < HMACLEN; k++) {
|
||||
f[k] ^= g[k];
|
||||
}
|
||||
}
|
||||
if (i == blocks - 1 && (keylen & 63)) {
|
||||
for (j = 0; j < (keylen & 63); j++) {
|
||||
key[64 * (i - 1) + j] = f[j];
|
||||
}
|
||||
if (i == blocks - 1 && (keylen & (HMACLEN - 1))) {
|
||||
memcpy(key + HMACLEN * (i - 1), f, keylen & (HMACLEN - 1));
|
||||
} else {
|
||||
for (j = 0; j < 64; j++) {
|
||||
key[64 * (i - 1) + j] = f[j];
|
||||
}
|
||||
memcpy(key + HMACLEN * (i - 1), f, HMACLEN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user