1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-10-11 18:39:05 +00:00
trezor-firmware/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2_hmac.h

47 lines
1.7 KiB
C
Raw Normal View History

2016-04-15 21:21:30 +00:00
/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
* Licensed under Microsoft Reference Source License (Ms-RSL)
* see LICENSE.md file for details
*/
#include "py/objstr.h"
2016-04-26 15:48:58 +00:00
#include "trezor-crypto/pbkdf2.h"
2016-04-15 21:21:30 +00:00
// def pbkdf2_hmac(hash_name: str, password: bytes, salt: bytes, iterations: int, dklen:int=None) -> bytes
STATIC mp_obj_t mod_TrezorCrypto_pbkdf2_hmac(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t hash_name;
mp_get_buffer_raise(args[0], &hash_name, MP_BUFFER_READ);
mp_buffer_info_t password;
mp_get_buffer_raise(args[1], &password, MP_BUFFER_READ);
mp_buffer_info_t salt;
mp_get_buffer_raise(args[2], &salt, MP_BUFFER_READ);
mp_int_t iterations = mp_obj_get_int(args[3]);
mp_int_t dklen = (n_args > 4) ? mp_obj_get_int(args[4]) : 0;
2016-04-26 15:48:58 +00:00
int digestsize = 0;
2016-04-15 21:21:30 +00:00
if (hash_name.len == 6 && memcmp(hash_name.buf, "sha256", hash_name.len) == 0) {
2016-04-26 15:48:58 +00:00
digestsize = 32;
2016-04-15 21:21:30 +00:00
} else
if (hash_name.len == 6 && memcmp(hash_name.buf, "sha512", hash_name.len) == 0) {
2016-04-26 15:48:58 +00:00
digestsize = 64;
2016-04-15 21:21:30 +00:00
}
2016-04-26 15:48:58 +00:00
if (digestsize == 0) {
2016-04-15 21:21:30 +00:00
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid hash_name"));
}
if (dklen == 0) {
2016-04-26 15:48:58 +00:00
dklen = digestsize;
}
vstr_t vstr;
vstr_init_len(&vstr, dklen);
if (digestsize == 32) {
pbkdf2_hmac_sha256(password.buf, password.len, salt.buf, salt.len, iterations, (uint8_t *)vstr.buf, dklen, NULL);
2016-04-15 21:21:30 +00:00
}
2016-04-26 15:48:58 +00:00
if (digestsize == 64) {
pbkdf2_hmac_sha512(password.buf, password.len, salt.buf, salt.len, iterations, (uint8_t *)vstr.buf, dklen, NULL);
2016-04-15 21:21:30 +00:00
}
2016-04-26 15:48:58 +00:00
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
2016-04-15 21:21:30 +00:00
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_pbkdf2_hmac_obj, 4, 5, mod_TrezorCrypto_pbkdf2_hmac);