1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-09 06:50:58 +00:00

extract some pbkdf2 constants into macros

This commit is contained in:
Pavol Rusnak 2013-12-09 18:16:33 +01:00
parent 352bc42be2
commit 97067c918e

View File

@ -2,12 +2,15 @@
#include "pbkdf2.h" #include "pbkdf2.h"
#include "hmac.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) 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; uint32_t i, j, k;
uint8_t f[64], g[64]; uint8_t f[HMACLEN], g[HMACLEN];
uint32_t blocks = keylen / 64; // SHA-512 uint32_t blocks = keylen / HMACLEN;
if (keylen & 63) { if (keylen & (HMACLEN - 1)) {
blocks++; blocks++;
} }
for (i = 1; i <= blocks; i++) { 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 + 1] = (i >> 16) & 0xFF;
salt[saltlen + 2] = (i >> 8) & 0xFF; salt[saltlen + 2] = (i >> 8) & 0xFF;
salt[saltlen + 3] = i & 0xFF; salt[saltlen + 3] = i & 0xFF;
hmac_sha512(pass, passlen, salt, saltlen + 4, g); HMACFUNC(pass, passlen, salt, saltlen + 4, g);
memcpy(f, g, 64); memcpy(f, g, HMACLEN);
for (j = 1; j < iterations; j++) { for (j = 1; j < iterations; j++) {
hmac_sha512(pass, passlen, g, 64, g); HMACFUNC(pass, passlen, g, HMACLEN, g);
for (k = 0; k < 64; k++) { for (k = 0; k < HMACLEN; k++) {
f[k] ^= g[k]; f[k] ^= g[k];
} }
} }
if (i == blocks - 1 && (keylen & 63)) { if (i == blocks - 1 && (keylen & (HMACLEN - 1))) {
for (j = 0; j < (keylen & 63); j++) { memcpy(key + HMACLEN * (i - 1), f, keylen & (HMACLEN - 1));
key[64 * (i - 1) + j] = f[j];
}
} else { } else {
for (j = 0; j < 64; j++) { memcpy(key + HMACLEN * (i - 1), f, HMACLEN);
key[64 * (i - 1) + j] = f[j];
}
} }
} }
} }