mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-17 19:00:58 +00:00
pbkdf2 now uses new hmac api
This commit is contained in:
parent
445e859450
commit
08219ea77a
36
pbkdf2.c
36
pbkdf2.c
@ -29,25 +29,25 @@
|
|||||||
void pbkdf2_hmac_sha256(const uint8_t *pass, int passlen, uint8_t *salt, int saltlen, uint32_t iterations, uint8_t *key, int keylen, void (*progress_callback)(uint32_t current, uint32_t total))
|
void pbkdf2_hmac_sha256(const uint8_t *pass, int passlen, uint8_t *salt, int saltlen, uint32_t iterations, uint8_t *key, int keylen, void (*progress_callback)(uint32_t current, uint32_t total))
|
||||||
{
|
{
|
||||||
const uint32_t HMACLEN = 256/8;
|
const uint32_t HMACLEN = 256/8;
|
||||||
uint32_t i, j, k;
|
|
||||||
uint8_t f[HMACLEN], g[HMACLEN];
|
uint8_t f[HMACLEN], g[HMACLEN];
|
||||||
uint32_t blocks = keylen / HMACLEN;
|
uint32_t blocks = keylen / HMACLEN;
|
||||||
if (keylen & (HMACLEN - 1)) {
|
if (keylen & (HMACLEN - 1)) {
|
||||||
blocks++;
|
blocks++;
|
||||||
}
|
}
|
||||||
for (i = 1; i <= blocks; i++) {
|
for (uint32_t i = 1; i <= blocks; i++) {
|
||||||
salt[saltlen ] = (i >> 24) & 0xFF;
|
HMAC_SHA256_CTX hctx;
|
||||||
salt[saltlen + 1] = (i >> 16) & 0xFF;
|
hmac_sha256_Init(&hctx, pass, passlen);
|
||||||
salt[saltlen + 2] = (i >> 8) & 0xFF;
|
hmac_sha256_Update(&hctx, salt, saltlen);
|
||||||
salt[saltlen + 3] = i & 0xFF;
|
uint32_t ii = ((i & 0xFF000000) >> 24) | ((i & 0x00FF0000) >> 8) | ((i & 0x0000FF00) << 8) | ((i & 0x000000FF) << 24);
|
||||||
hmac_sha256(pass, passlen, salt, saltlen + 4, g);
|
hmac_sha256_Update(&hctx, (const uint8_t *)&ii, sizeof(uint32_t));
|
||||||
|
hmac_sha256_Final(&hctx, g);
|
||||||
memcpy(f, g, HMACLEN);
|
memcpy(f, g, HMACLEN);
|
||||||
if (progress_callback) {
|
if (progress_callback) {
|
||||||
progress_callback(0, iterations);
|
progress_callback(0, iterations);
|
||||||
}
|
}
|
||||||
for (j = 1; j < iterations; j++) {
|
for (uint32_t j = 1; j < iterations; j++) {
|
||||||
hmac_sha256(pass, passlen, g, HMACLEN, g);
|
hmac_sha256(pass, passlen, g, HMACLEN, g);
|
||||||
for (k = 0; k < HMACLEN; k++) {
|
for (uint32_t k = 0; k < HMACLEN; k++) {
|
||||||
f[k] ^= g[k];
|
f[k] ^= g[k];
|
||||||
}
|
}
|
||||||
if (progress_callback && (j % 256 == 255)) {
|
if (progress_callback && (j % 256 == 255)) {
|
||||||
@ -67,25 +67,25 @@ void pbkdf2_hmac_sha256(const uint8_t *pass, int passlen, uint8_t *salt, int sal
|
|||||||
void pbkdf2_hmac_sha512(const uint8_t *pass, int passlen, uint8_t *salt, int saltlen, uint32_t iterations, uint8_t *key, int keylen, void (*progress_callback)(uint32_t current, uint32_t total))
|
void pbkdf2_hmac_sha512(const uint8_t *pass, int passlen, uint8_t *salt, int saltlen, uint32_t iterations, uint8_t *key, int keylen, void (*progress_callback)(uint32_t current, uint32_t total))
|
||||||
{
|
{
|
||||||
const uint32_t HMACLEN = 512/8;
|
const uint32_t HMACLEN = 512/8;
|
||||||
uint32_t i, j, k;
|
|
||||||
uint8_t f[HMACLEN], g[HMACLEN];
|
uint8_t f[HMACLEN], g[HMACLEN];
|
||||||
uint32_t blocks = keylen / HMACLEN;
|
uint32_t blocks = keylen / HMACLEN;
|
||||||
if (keylen & (HMACLEN - 1)) {
|
if (keylen & (HMACLEN - 1)) {
|
||||||
blocks++;
|
blocks++;
|
||||||
}
|
}
|
||||||
for (i = 1; i <= blocks; i++) {
|
for (uint32_t i = 1; i <= blocks; i++) {
|
||||||
salt[saltlen ] = (i >> 24) & 0xFF;
|
HMAC_SHA512_CTX hctx;
|
||||||
salt[saltlen + 1] = (i >> 16) & 0xFF;
|
hmac_sha512_Init(&hctx, pass, passlen);
|
||||||
salt[saltlen + 2] = (i >> 8) & 0xFF;
|
hmac_sha512_Update(&hctx, salt, saltlen);
|
||||||
salt[saltlen + 3] = i & 0xFF;
|
uint32_t ii = ((i & 0xFF000000) >> 24) | ((i & 0x00FF0000) >> 8) | ((i & 0x0000FF00) << 8) | ((i & 0x000000FF) << 24);
|
||||||
hmac_sha512(pass, passlen, salt, saltlen + 4, g);
|
hmac_sha512_Update(&hctx, (const uint8_t *)&ii, sizeof(uint32_t));
|
||||||
|
hmac_sha512_Final(&hctx, g);
|
||||||
memcpy(f, g, HMACLEN);
|
memcpy(f, g, HMACLEN);
|
||||||
if (progress_callback) {
|
if (progress_callback) {
|
||||||
progress_callback(0, iterations);
|
progress_callback(0, iterations);
|
||||||
}
|
}
|
||||||
for (j = 1; j < iterations; j++) {
|
for (uint32_t j = 1; j < iterations; j++) {
|
||||||
hmac_sha512(pass, passlen, g, HMACLEN, g);
|
hmac_sha512(pass, passlen, g, HMACLEN, g);
|
||||||
for (k = 0; k < HMACLEN; k++) {
|
for (uint32_t k = 0; k < HMACLEN; k++) {
|
||||||
f[k] ^= g[k];
|
f[k] ^= g[k];
|
||||||
}
|
}
|
||||||
if (progress_callback && (j % 256 == 255)) {
|
if (progress_callback && (j % 256 == 255)) {
|
||||||
|
2
pbkdf2.h
2
pbkdf2.h
@ -26,9 +26,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
// salt needs to have 4 extra bytes available beyond saltlen
|
|
||||||
void pbkdf2_hmac_sha256(const uint8_t *pass, int passlen, uint8_t *salt, int saltlen, uint32_t iterations, uint8_t *key, int keylen, void (*progress_callback)(uint32_t current, uint32_t total));
|
void pbkdf2_hmac_sha256(const uint8_t *pass, int passlen, uint8_t *salt, int saltlen, uint32_t iterations, uint8_t *key, int keylen, void (*progress_callback)(uint32_t current, uint32_t total));
|
||||||
// salt needs to have 4 extra bytes available beyond saltlen
|
|
||||||
void pbkdf2_hmac_sha512(const uint8_t *pass, int passlen, uint8_t *salt, int saltlen, uint32_t iterations, uint8_t *key, int keylen, void (*progress_callback)(uint32_t current, uint32_t total));
|
void pbkdf2_hmac_sha512(const uint8_t *pass, int passlen, uint8_t *salt, int saltlen, uint32_t iterations, uint8_t *key, int keylen, void (*progress_callback)(uint32_t current, uint32_t total));
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user