1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-12 01:18:14 +00:00
trezor-firmware/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h

134 lines
4.5 KiB
C
Raw Normal View History

2016-05-01 00:03:31 +00:00
/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
2016-05-28 12:37:32 +00:00
* Licensed under TREZOR License
* see LICENSE file for details
2016-05-01 00:03:31 +00:00
*/
#include "py/objstr.h"
#include "pbkdf2.h"
#include "memzero.h"
2016-05-01 00:03:31 +00:00
/// class Pbkdf2:
/// '''
/// PBKDF2 context.
/// '''
2016-05-01 00:03:31 +00:00
typedef struct _mp_obj_Pbkdf2_t {
mp_obj_base_t base;
union {
PBKDF2_HMAC_SHA256_CTX ctx256;
PBKDF2_HMAC_SHA512_CTX ctx512;
};
int prf;
} mp_obj_Pbkdf2_t;
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_Pbkdf2_update(mp_obj_t self, mp_obj_t data);
2016-05-01 00:03:31 +00:00
/// def __init__(self, prf: str, password: bytes, salt: bytes, iterations: int = None) -> None:
2016-10-07 11:27:43 +00:00
/// '''
/// Create a PBKDF2 context.
2016-10-07 11:27:43 +00:00
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_Pbkdf2_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
2016-05-01 00:03:31 +00:00
mp_arg_check_num(n_args, n_kw, 3, 4, false);
mp_obj_Pbkdf2_t *o = m_new_obj(mp_obj_Pbkdf2_t);
o->base.type = type;
mp_buffer_info_t prf;
mp_get_buffer_raise(args[0], &prf, 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);
if (password.len == 0) {
password.buf = "";
}
if (salt.len == 0) {
salt.buf = "";
}
2016-05-01 00:03:31 +00:00
o->prf = 0;
if (prf.len == 11 && memcmp(prf.buf, "hmac-sha256", prf.len) == 0) {
pbkdf2_hmac_sha256_Init(&(o->ctx256), password.buf, password.len, salt.buf, salt.len);
o->prf = 256;
} else
if (prf.len == 11 && memcmp(prf.buf, "hmac-sha512", prf.len) == 0) {
pbkdf2_hmac_sha512_Init(&(o->ctx512), password.buf, password.len, salt.buf, salt.len);
o->prf = 512;
} else
if (o->prf == 0) {
mp_raise_ValueError("Invalid PRF");
2016-05-01 00:03:31 +00:00
}
// constructor called with iterations as fourth parameter
if (n_args > 3) {
2017-06-14 16:47:38 +00:00
mod_trezorcrypto_Pbkdf2_update(MP_OBJ_FROM_PTR(o), args[3]);
2016-05-01 00:03:31 +00:00
}
return MP_OBJ_FROM_PTR(o);
}
/// def update(self, iterations: int) -> None:
2016-10-07 11:27:43 +00:00
/// '''
/// Update a PBKDF2 context.
2016-10-07 11:27:43 +00:00
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_Pbkdf2_update(mp_obj_t self, mp_obj_t iterations) {
2016-05-01 00:03:31 +00:00
mp_obj_Pbkdf2_t *o = MP_OBJ_TO_PTR(self);
uint32_t iter = mp_obj_get_int(iterations);
if (o->prf == 256) {
pbkdf2_hmac_sha256_Update(&(o->ctx256), iter);
}
if (o->prf == 512) {
pbkdf2_hmac_sha512_Update(&(o->ctx512), iter);
}
return mp_const_none;
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Pbkdf2_update_obj, mod_trezorcrypto_Pbkdf2_update);
2016-05-01 00:03:31 +00:00
/// def key(self) -> bytes:
2016-10-07 11:27:43 +00:00
/// '''
/// Retrieve derived key.
2016-10-07 11:27:43 +00:00
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_Pbkdf2_key(mp_obj_t self) {
2016-05-01 00:03:31 +00:00
mp_obj_Pbkdf2_t *o = MP_OBJ_TO_PTR(self);
if (o->prf == 256) {
2016-10-07 11:41:25 +00:00
PBKDF2_HMAC_SHA256_CTX ctx;
memcpy(&ctx, &(o->ctx256), sizeof(PBKDF2_HMAC_SHA256_CTX));
uint8_t out[SHA256_DIGEST_LENGTH];
pbkdf2_hmac_sha256_Final(&ctx, out);
2016-10-07 11:41:25 +00:00
memset(&ctx, 0, sizeof(PBKDF2_HMAC_SHA256_CTX));
return mp_obj_new_bytes(out, sizeof(out));
2016-05-01 00:03:31 +00:00
}
if (o->prf == 512) {
2016-10-07 11:41:25 +00:00
PBKDF2_HMAC_SHA512_CTX ctx;
memcpy(&ctx, &(o->ctx512), sizeof(PBKDF2_HMAC_SHA512_CTX));
uint8_t out[SHA512_DIGEST_LENGTH];
pbkdf2_hmac_sha512_Final(&ctx, out);
2016-10-07 11:41:25 +00:00
memset(&ctx, 0, sizeof(PBKDF2_HMAC_SHA512_CTX));
return mp_obj_new_bytes(out, sizeof(out));
2016-05-01 00:03:31 +00:00
}
return mp_const_none;
2016-05-01 00:03:31 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Pbkdf2_key_obj, mod_trezorcrypto_Pbkdf2_key);
2016-05-01 00:03:31 +00:00
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_Pbkdf2___del__(mp_obj_t self) {
2016-05-01 00:03:31 +00:00
mp_obj_Pbkdf2_t *o = MP_OBJ_TO_PTR(self);
memzero(&(o->ctx256), sizeof(PBKDF2_HMAC_SHA256_CTX));
memzero(&(o->ctx512), sizeof(PBKDF2_HMAC_SHA512_CTX));
2016-05-01 00:03:31 +00:00
return mp_const_none;
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Pbkdf2___del___obj, mod_trezorcrypto_Pbkdf2___del__);
2016-05-01 00:03:31 +00:00
2017-06-14 16:47:38 +00:00
STATIC const mp_rom_map_elem_t mod_trezorcrypto_Pbkdf2_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_trezorcrypto_Pbkdf2_update_obj) },
{ MP_ROM_QSTR(MP_QSTR_key), MP_ROM_PTR(&mod_trezorcrypto_Pbkdf2_key_obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_trezorcrypto_Pbkdf2___del___obj) },
2016-05-01 00:03:31 +00:00
};
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_Pbkdf2_locals_dict, mod_trezorcrypto_Pbkdf2_locals_dict_table);
2016-05-01 00:03:31 +00:00
2017-06-14 16:47:38 +00:00
STATIC const mp_obj_type_t mod_trezorcrypto_Pbkdf2_type = {
2016-05-01 00:03:31 +00:00
{ &mp_type_type },
.name = MP_QSTR_Pbkdf2,
2017-06-14 16:47:38 +00:00
.make_new = mod_trezorcrypto_Pbkdf2_make_new,
.locals_dict = (void*)&mod_trezorcrypto_Pbkdf2_locals_dict,
2016-05-01 00:03:31 +00:00
};