1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00

validate mp_buffer_info_t usage for cases where len==0

This commit is contained in:
Pavol Rusnak 2016-10-04 18:53:32 +02:00
parent 1cb309260a
commit 27fb301b22
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
16 changed files with 84 additions and 19 deletions

View File

@ -90,9 +90,12 @@ STATIC mp_obj_t mod_TrezorCrypto_AES_make_new(const mp_obj_type_t *type, size_t
STATIC mp_obj_t mod_TrezorCrypto_AES_update(mp_obj_t self, mp_obj_t data) {
mp_buffer_info_t buf;
mp_get_buffer_raise(data, &buf, MP_BUFFER_READ);
mp_obj_AES_t *o = MP_OBJ_TO_PTR(self);
vstr_t vstr;
vstr_init_len(&vstr, buf.len);
if (buf.len == 0) {
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
mp_obj_AES_t *o = MP_OBJ_TO_PTR(self);
switch (o->mode & AESModeMask) {
case ECB:
if (buf.len & (AES_BLOCK_SIZE - 1)) {

View File

@ -62,7 +62,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Bip39_from_data_obj, mod_Trezo
STATIC mp_obj_t mod_TrezorCrypto_Bip39_check(mp_obj_t self, mp_obj_t mnemonic) {
mp_buffer_info_t text;
mp_get_buffer_raise(mnemonic, &text, MP_BUFFER_READ);
return mnemonic_check(text.buf) ? mp_const_true : mp_const_false;
return (text.len > 0 && mnemonic_check(text.buf)) ? mp_const_true : mp_const_false;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Bip39_check_obj, mod_TrezorCrypto_Bip39_check);
@ -77,7 +77,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Bip39_seed(mp_obj_t self, mp_obj_t mnemonic, mp
mp_get_buffer_raise(passphrase, &phrase, MP_BUFFER_READ);
vstr_t vstr;
vstr_init_len(&vstr, 64);
mnemonic_to_seed(mnemo.buf, phrase.buf, (uint8_t *)vstr.buf, NULL); // no callback for now
const char *pmnemonic = mnemo.len > 0 ? mnemo.buf : "";
const char *ppassphrase = phrase.len > 0 ? phrase.buf : "";
mnemonic_to_seed(pmnemonic, ppassphrase, (uint8_t *)vstr.buf, NULL); // no callback for now
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorCrypto_Bip39_seed_obj, mod_TrezorCrypto_Bip39_seed);

View File

@ -48,6 +48,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Ed25519_sign(mp_obj_t self, mp_obj_t secret_key
if (sk.len != 32) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of secret key"));
}
if (msg.len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Empty data to sign"));
}
ed25519_public_key pk;
ed25519_publickey(*(const ed25519_secret_key *)sk.buf, pk);
vstr_t vstr;
@ -73,6 +76,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Ed25519_verify(size_t n_args, const mp_obj_t *a
if (sig.len != 64) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of signature"));
}
if (msg.len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Empty data to verify"));
}
return (0 == ed25519_sign_open(msg.buf, msg.len, *(const ed25519_public_key *)pk.buf, *(const ed25519_signature *)sig.buf)) ? mp_const_true : mp_const_false;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Ed25519_verify_obj, 4, 4, mod_TrezorCrypto_Ed25519_verify);

View File

@ -55,6 +55,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_sign(mp_obj_t self, mp_obj_t secret_k
if (sk.len != 32) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of secret key"));
}
if (msg.len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Empty data to sign"));
}
vstr_t vstr;
vstr_init_len(&vstr, 65);
uint8_t pby;
@ -82,6 +85,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_verify(size_t n_args, const mp_obj_t
if (sig.len != 65) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of signature"));
}
if (msg.len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Empty data to verify"));
}
return mp_obj_new_bool(0 == ecdsa_verify(&nist256p1, (const uint8_t *)pk.buf, (const uint8_t *)sig.buf, (const uint8_t *)msg.buf, msg.len));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Nist256p1_verify_obj, 4, 4, mod_TrezorCrypto_Nist256p1_verify);

View File

@ -34,6 +34,13 @@ STATIC mp_obj_t mod_TrezorCrypto_Pbkdf2_make_new(const mp_obj_type_t *type, size
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 = "";
}
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);

View File

@ -43,7 +43,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_update(mp_obj_t self, mp_obj_t data)
mp_obj_Ripemd160_t *o = MP_OBJ_TO_PTR(self);
mp_buffer_info_t msg;
mp_get_buffer_raise(data, &msg, MP_BUFFER_READ);
ripemd160_Update(&(o->ctx), msg.buf, msg.len);
if (msg.len > 0) {
ripemd160_Update(&(o->ctx), msg.buf, msg.len);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Ripemd160_update_obj, mod_TrezorCrypto_Ripemd160_update);

View File

@ -55,6 +55,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_sign(mp_obj_t self, mp_obj_t secret_k
if (sk.len != 32) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of secret key"));
}
if (msg.len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Empty data to sign"));
}
vstr_t vstr;
vstr_init_len(&vstr, 65);
uint8_t pby;
@ -82,6 +85,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_verify(size_t n_args, const mp_obj_t
if (sig.len != 65) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid length of signature"));
}
if (msg.len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Empty data to verify"));
}
return mp_obj_new_bool(0 == ecdsa_verify(&secp256k1, (const uint8_t *)pk.buf, (const uint8_t *)sig.buf, (const uint8_t *)msg.buf, msg.len));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Secp256k1_verify_obj, 4, 4, mod_TrezorCrypto_Secp256k1_verify);

View File

@ -43,7 +43,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha256_update(mp_obj_t self, mp_obj_t data) {
mp_obj_Sha256_t *o = MP_OBJ_TO_PTR(self);
mp_buffer_info_t msg;
mp_get_buffer_raise(data, &msg, MP_BUFFER_READ);
sha256_Update(&(o->ctx), msg.buf, msg.len);
if (msg.len > 0) {
sha256_Update(&(o->ctx), msg.buf, msg.len);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha256_update_obj, mod_TrezorCrypto_Sha256_update);

View File

@ -43,7 +43,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha3_256_update(mp_obj_t self, mp_obj_t data) {
mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(self);
mp_buffer_info_t msg;
mp_get_buffer_raise(data, &msg, MP_BUFFER_READ);
sha3_Update(&(o->ctx), msg.buf, msg.len);
if (msg.len > 0) {
sha3_Update(&(o->ctx), msg.buf, msg.len);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha3_256_update_obj, mod_TrezorCrypto_Sha3_256_update);

View File

@ -43,7 +43,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha3_512_update(mp_obj_t self, mp_obj_t data) {
mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(self);
mp_buffer_info_t msg;
mp_get_buffer_raise(data, &msg, MP_BUFFER_READ);
sha3_Update(&(o->ctx), msg.buf, msg.len);
if (msg.len > 0) {
sha3_Update(&(o->ctx), msg.buf, msg.len);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha3_512_update_obj, mod_TrezorCrypto_Sha3_512_update);

View File

@ -42,7 +42,9 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha512_update(mp_obj_t self, mp_obj_t data) {
mp_obj_Sha512_t *o = MP_OBJ_TO_PTR(self);
mp_buffer_info_t msg;
mp_get_buffer_raise(data, &msg, MP_BUFFER_READ);
sha512_Update(&(o->ctx), msg.buf, msg.len);
if (msg.len > 0) {
sha512_Update(&(o->ctx), msg.buf, msg.len);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha512_update_obj, mod_TrezorCrypto_Sha512_update);

View File

@ -76,6 +76,9 @@ STATIC mp_obj_t mod_TrezorCrypto_SSSS_combine(mp_obj_t self, mp_obj_t shares) {
if (MP_OBJ_IS_TYPE(share[i], &mp_type_bytes)) {
mp_buffer_info_t s;
mp_get_buffer_raise(share[i], &s, MP_BUFFER_READ);
if (s.len != 32) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Length of share has to be 256 bits"));
}
bn_read_be(s.buf, &bnshares[n]);
} else {
memset(&bnshares[i], 0, sizeof(bignum256));

View File

@ -29,7 +29,9 @@ ssize_t msg_recv(uint8_t *iface, uint8_t *buf, size_t len)
ssize_t msg_send(uint8_t iface, const uint8_t *buf, size_t len)
{
(void)iface; // ignore interface for now
USBD_HID_SendReport(&hUSBDDevice, (uint8_t *)buf, len);
if (len > 0) {
USBD_HID_SendReport(&hUSBDDevice, (uint8_t *)buf, len);
}
return len;
}

View File

@ -54,8 +54,8 @@ ssize_t msg_recv(uint8_t *iface, uint8_t *buf, size_t len)
ssize_t msg_send(uint8_t iface, const uint8_t *buf, size_t len)
{
(void)iface; // ignore interface for UDP
ssize_t r = -1;
if (slen) {
ssize_t r = len;
if (slen > 0) {
r = sendto(s, buf, len, MSG_DONTWAIT, (const struct sockaddr *)&si_other, slen);
}
return r;

View File

@ -81,7 +81,9 @@ STATIC mp_obj_t mod_TrezorUi_Display_blit(size_t n_args, const mp_obj_t *args) {
if (data.len != 2 * w * h) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Wrong data size (got %d bytes, expected %d bytes)", data.len, 2 * w * h));
}
display_blit(x, y, w, h, data.buf, data.len);
if (w > 0 && h > 0) {
display_blit(x, y, w, h, data.buf, data.len);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_blit_obj, 6, 6, mod_TrezorUi_Display_blit);
@ -157,7 +159,9 @@ STATIC mp_obj_t mod_TrezorUi_Display_text(size_t n_args, const mp_obj_t *args) {
mp_int_t font = mp_obj_get_int(args[4]);
mp_int_t fgcolor = mp_obj_get_int(args[5]);
mp_int_t bgcolor = mp_obj_get_int(args[6]);
display_text(x, y, text.buf, text.len, font, fgcolor, bgcolor);
if (text.len > 0) {
display_text(x, y, text.buf, text.len, font, fgcolor, bgcolor);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_text_obj, 7, 7, mod_TrezorUi_Display_text);
@ -175,7 +179,9 @@ STATIC mp_obj_t mod_TrezorUi_Display_text_center(size_t n_args, const mp_obj_t *
mp_int_t font = mp_obj_get_int(args[4]);
mp_int_t fgcolor = mp_obj_get_int(args[5]);
mp_int_t bgcolor = mp_obj_get_int(args[6]);
display_text_center(x, y, text.buf, text.len, font, fgcolor, bgcolor);
if (text.len > 0) {
display_text_center(x, y, text.buf, text.len, font, fgcolor, bgcolor);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_text_center_obj, 7, 7, mod_TrezorUi_Display_text_center);
@ -193,7 +199,9 @@ STATIC mp_obj_t mod_TrezorUi_Display_text_right(size_t n_args, const mp_obj_t *a
mp_int_t font = mp_obj_get_int(args[4]);
mp_int_t fgcolor = mp_obj_get_int(args[5]);
mp_int_t bgcolor = mp_obj_get_int(args[6]);
display_text_right(x, y, text.buf, text.len, font, fgcolor, bgcolor);
if (text.len > 0) {
display_text_right(x, y, text.buf, text.len, font, fgcolor, bgcolor);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_text_right_obj, 7, 7, mod_TrezorUi_Display_text_right);
@ -206,7 +214,10 @@ STATIC mp_obj_t mod_TrezorUi_Display_text_width(mp_obj_t self, mp_obj_t text, mp
mp_buffer_info_t txt;
mp_get_buffer_raise(text, &txt, MP_BUFFER_READ);
mp_int_t f = mp_obj_get_int(font);
uint32_t w = display_text_width(txt.buf, txt.len, f);
uint32_t w = 0;
if (txt.len > 0) {
w = display_text_width(txt.buf, txt.len, f);
}
return MP_OBJ_NEW_SMALL_INT(w);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorUi_Display_text_width_obj, mod_TrezorUi_Display_text_width);
@ -225,7 +236,9 @@ STATIC mp_obj_t mod_TrezorUi_Display_qrcode(size_t n_args, const mp_obj_t *args)
}
mp_buffer_info_t data;
mp_get_buffer_raise(args[3], &data, MP_BUFFER_READ);
display_qrcode(x, y, data.buf, data.len, scale);
if (data.len > 0) {
display_qrcode(x, y, data.buf, data.len, scale);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_qrcode_obj, 5, 5, mod_TrezorUi_Display_qrcode);
@ -320,7 +333,9 @@ STATIC mp_obj_t mod_TrezorUi_Display_raw(mp_obj_t self, mp_obj_t reg, mp_obj_t d
mp_int_t r = mp_obj_get_int(reg);
mp_buffer_info_t raw;
mp_get_buffer_raise(data, &raw, MP_BUFFER_READ);
display_raw(r, raw.buf, raw.len);
if (raw.len > 0) {
display_raw(r, raw.buf, raw.len);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorUi_Display_raw_obj, mod_TrezorUi_Display_raw);

View File

@ -1,7 +1,12 @@
#!/bin/bash
results=()
error=0
for i in test_*.py; do
if [ -z "$1" ]; then
list="test_*.py"
else
list="$1"
fi
for i in $list; do
echo
if ../../vendor/micropython/unix/micropython $i; then
results+=("OK $i")