From e92440dfc813fe24b0370812614d5f28ab3c9f5e Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Thu, 18 Jan 2018 15:26:22 +0100 Subject: [PATCH] introduce and use memzero instead of explicit_bzero --- SConscript.boardloader | 1 + SConscript.bootloader | 1 + SConscript.firmware | 1 + SConscript.unix | 1 + embed/extmod/modtrezorcrypto/modtrezorcrypto-aes.h | 5 +++-- embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h | 11 ++++++----- .../extmod/modtrezorcrypto/modtrezorcrypto-blake256.h | 3 ++- .../extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h | 3 ++- .../extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h | 3 ++- .../modtrezorcrypto-chacha20poly1305.h | 3 ++- embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h | 5 +++-- .../modtrezorcrypto/modtrezorcrypto-ripemd160.h | 3 ++- embed/extmod/modtrezorcrypto/modtrezorcrypto-sha1.h | 3 ++- embed/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h | 3 ++- .../extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h | 3 ++- .../extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h | 3 ++- embed/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h | 3 ++- vendor/trezor-crypto | 2 +- 18 files changed, 37 insertions(+), 20 deletions(-) diff --git a/SConscript.boardloader b/SConscript.boardloader index 7afaeb7494..dc96a28b4a 100644 --- a/SConscript.boardloader +++ b/SConscript.boardloader @@ -26,6 +26,7 @@ SOURCE_MOD += [ 'vendor/trezor-crypto/ed25519-donna/ed25519-donna-32bit-tables.c', 'vendor/trezor-crypto/ed25519-donna/ed25519-donna-impl-base.c', 'vendor/trezor-crypto/ed25519-donna/modm-donna-32bit.c', + 'vendor/trezor-crypto/memzero.c', 'vendor/trezor-crypto/sha2.c', ] diff --git a/SConscript.bootloader b/SConscript.bootloader index 232764a3fe..6df6d660cf 100644 --- a/SConscript.bootloader +++ b/SConscript.bootloader @@ -26,6 +26,7 @@ SOURCE_MOD += [ 'vendor/trezor-crypto/ed25519-donna/ed25519-donna-32bit-tables.c', 'vendor/trezor-crypto/ed25519-donna/ed25519-donna-impl-base.c', 'vendor/trezor-crypto/ed25519-donna/modm-donna-32bit.c', + 'vendor/trezor-crypto/memzero.c', 'vendor/trezor-crypto/sha2.c', ] diff --git a/SConscript.firmware b/SConscript.firmware index 71cda68973..f993db5afe 100644 --- a/SConscript.firmware +++ b/SConscript.firmware @@ -60,6 +60,7 @@ SOURCE_MOD += [ 'vendor/trezor-crypto/ed25519-donna/modm-donna-32bit.c', 'vendor/trezor-crypto/hasher.c', 'vendor/trezor-crypto/hmac.c', + 'vendor/trezor-crypto/memzero.c', 'vendor/trezor-crypto/nist256p1.c', 'vendor/trezor-crypto/pbkdf2.c', 'vendor/trezor-crypto/rand.c', diff --git a/SConscript.unix b/SConscript.unix index 735daf9755..292f03c3f6 100644 --- a/SConscript.unix +++ b/SConscript.unix @@ -59,6 +59,7 @@ SOURCE_MOD += [ 'vendor/trezor-crypto/ed25519-donna/modm-donna-32bit.c', 'vendor/trezor-crypto/hasher.c', 'vendor/trezor-crypto/hmac.c', + 'vendor/trezor-crypto/memzero.c', 'vendor/trezor-crypto/nist256p1.c', 'vendor/trezor-crypto/pbkdf2.c', 'vendor/trezor-crypto/rand.c', diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-aes.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-aes.h index e9c74ff8f1..207569d1a8 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-aes.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-aes.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "aes/aes.h" +#include "memzero.h" /// class AES: /// ''' @@ -145,8 +146,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_AES_update_obj, mod_trezorcryp STATIC mp_obj_t mod_trezorcrypto_AES___del__(mp_obj_t self) { mp_obj_AES_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx), sizeof(aes_encrypt_ctx)); - explicit_bzero(o->iv, AES_BLOCK_SIZE); + memzero(&(o->ctx), sizeof(aes_encrypt_ctx)); + memzero(o->iv, AES_BLOCK_SIZE); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_AES___del___obj, mod_trezorcrypto_AES___del__); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h index 7b327a8031..6ff5809b69 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h @@ -9,6 +9,7 @@ #include "bip32.h" #include "curves.h" +#include "memzero.h" /// class HDNode: /// ''' @@ -93,17 +94,17 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_make_new(const mp_obj_type_t *type, size if (NULL != chain_code.buf && 32 == chain_code.len) { memcpy(o->hdnode.chain_code, chain_code.buf, 32); } else { - explicit_bzero(o->hdnode.chain_code, 32); + memzero(o->hdnode.chain_code, 32); } if (NULL != private_key.buf && 32 == private_key.len) { memcpy(o->hdnode.private_key, private_key.buf, 32); } else { - explicit_bzero(o->hdnode.private_key, 32); + memzero(o->hdnode.private_key, 32); } if (NULL != public_key.buf && 33 == public_key.len) { memcpy(o->hdnode.public_key, public_key.buf, 33); } else { - explicit_bzero(o->hdnode.public_key, 33); + memzero(o->hdnode.public_key, 33); } o->hdnode.curve = curve; @@ -120,7 +121,7 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_derive(mp_obj_t self, mp_obj_t index) { uint32_t fp = hdnode_fingerprint(&o->hdnode); if (!hdnode_private_ckd(&o->hdnode, i)) { - explicit_bzero(&o->hdnode, sizeof(o->hdnode)); + memzero(&o->hdnode, sizeof(o->hdnode)); mp_raise_ValueError("Failed to derive"); } o->fingerprint = fp; @@ -157,7 +158,7 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_derive_path(mp_obj_t self, mp_obj_t path if (!hdnode_private_ckd_cached(&o->hdnode, pints, plen, &o->fingerprint)) { // derivation failed, reset the state and raise o->fingerprint = 0; - explicit_bzero(&o->hdnode, sizeof(o->hdnode)); + memzero(&o->hdnode, sizeof(o->hdnode)); mp_raise_ValueError("Failed to derive path"); } diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake256.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake256.h index e60668b05c..a0fe241608 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake256.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake256.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "blake256.h" +#include "memzero.h" /// class Blake256: /// ''' @@ -68,7 +69,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Blake256_digest_obj, mod_trezo STATIC mp_obj_t mod_trezorcrypto_Blake256___del__(mp_obj_t self) { mp_obj_Blake256_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx), sizeof(BLAKE256_CTX)); + memzero(&(o->ctx), sizeof(BLAKE256_CTX)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Blake256___del___obj, mod_trezorcrypto_Blake256___del__); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h index fd02af69d7..bccb1b875a 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "blake2b.h" +#include "memzero.h" /// class Blake2b: /// ''' @@ -75,7 +76,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Blake2b_digest_obj, mod_trezor STATIC mp_obj_t mod_trezorcrypto_Blake2b___del__(mp_obj_t self) { mp_obj_Blake2b_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx), sizeof(BLAKE2B_CTX)); + memzero(&(o->ctx), sizeof(BLAKE2B_CTX)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Blake2b___del___obj, mod_trezorcrypto_Blake2b___del__); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h index 2c277c716d..1b9a042514 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "blake2s.h" +#include "memzero.h" /// class Blake2s: /// ''' @@ -75,7 +76,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Blake2s_digest_obj, mod_trezor STATIC mp_obj_t mod_trezorcrypto_Blake2s___del__(mp_obj_t self) { mp_obj_Blake2s_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx), sizeof(BLAKE2S_CTX)); + memzero(&(o->ctx), sizeof(BLAKE2S_CTX)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Blake2s___del___obj, mod_trezorcrypto_Blake2s___del__); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.h index 2be22902be..891b7610d8 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "chacha20poly1305/rfc7539.h" +#include "memzero.h" /// class ChaCha20Poly1305: /// ''' @@ -105,7 +106,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_ChaCha20Poly1305_finish_obj, m STATIC mp_obj_t mod_trezorcrypto_ChaCha20Poly1305___del__(mp_obj_t self) { mp_obj_ChaCha20Poly1305_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx), sizeof(chacha20poly1305_ctx)); + memzero(&(o->ctx), sizeof(chacha20poly1305_ctx)); o->alen = 0; o->plen = 0; return mp_const_none; diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h index 4c551fa3db..b529867745 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "pbkdf2.h" +#include "memzero.h" /// class Pbkdf2: /// ''' @@ -111,8 +112,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Pbkdf2_key_obj, mod_trezorcryp STATIC mp_obj_t mod_trezorcrypto_Pbkdf2___del__(mp_obj_t self) { mp_obj_Pbkdf2_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx256), sizeof(PBKDF2_HMAC_SHA256_CTX)); - explicit_bzero(&(o->ctx512), sizeof(PBKDF2_HMAC_SHA512_CTX)); + memzero(&(o->ctx256), sizeof(PBKDF2_HMAC_SHA256_CTX)); + memzero(&(o->ctx512), sizeof(PBKDF2_HMAC_SHA512_CTX)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Pbkdf2___del___obj, mod_trezorcrypto_Pbkdf2___del__); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h index 5b54aca730..59418c78dc 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "ripemd160.h" +#include "memzero.h" /// class Ripemd160: /// ''' @@ -68,7 +69,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Ripemd160_digest_obj, mod_trez STATIC mp_obj_t mod_trezorcrypto_Ripemd160___del__(mp_obj_t self) { mp_obj_Ripemd160_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx), sizeof(RIPEMD160_CTX)); + memzero(&(o->ctx), sizeof(RIPEMD160_CTX)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Ripemd160___del___obj, mod_trezorcrypto_Ripemd160___del__); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha1.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha1.h index 49f51285c8..2824f5ade6 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha1.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha1.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "sha2.h" +#include "memzero.h" /// class Sha1: /// ''' @@ -68,7 +69,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha1_digest_obj, mod_trezorcry STATIC mp_obj_t mod_trezorcrypto_Sha1___del__(mp_obj_t self) { mp_obj_Sha1_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx), sizeof(SHA1_CTX)); + memzero(&(o->ctx), sizeof(SHA1_CTX)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha1___del___obj, mod_trezorcrypto_Sha1___del__); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h index da26a63a98..c665e77684 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "sha2.h" +#include "memzero.h" /// class Sha256: /// ''' @@ -68,7 +69,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha256_digest_obj, mod_trezorc STATIC mp_obj_t mod_trezorcrypto_Sha256___del__(mp_obj_t self) { mp_obj_Sha256_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx), sizeof(SHA256_CTX)); + memzero(&(o->ctx), sizeof(SHA256_CTX)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha256___del___obj, mod_trezorcrypto_Sha256___del__); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h index 92d92a7e29..6aad9b400e 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "sha3.h" +#include "memzero.h" /// class Sha3_256: /// ''' @@ -72,7 +73,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Sha3_256_digest_obj, STATIC mp_obj_t mod_trezorcrypto_Sha3_256___del__(mp_obj_t self) { mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx), sizeof(SHA3_CTX)); + memzero(&(o->ctx), sizeof(SHA3_CTX)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha3_256___del___obj, mod_trezorcrypto_Sha3_256___del__); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h index a0f366964e..28eb4bcb70 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "sha3.h" +#include "memzero.h" /// class Sha3_512: /// ''' @@ -72,7 +73,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Sha3_512_digest_obj, STATIC mp_obj_t mod_trezorcrypto_Sha3_512___del__(mp_obj_t self) { mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx), sizeof(SHA3_CTX)); + memzero(&(o->ctx), sizeof(SHA3_CTX)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha3_512___del___obj, mod_trezorcrypto_Sha3_512___del__); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h index 27ea40cd61..a6445aaeb2 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h @@ -8,6 +8,7 @@ #include "py/objstr.h" #include "sha2.h" +#include "memzero.h" /// class Sha512: /// ''' @@ -67,7 +68,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha512_digest_obj, mod_trezorc STATIC mp_obj_t mod_trezorcrypto_Sha512___del__(mp_obj_t self) { mp_obj_Sha512_t *o = MP_OBJ_TO_PTR(self); - explicit_bzero(&(o->ctx), sizeof(SHA512_CTX)); + memzero(&(o->ctx), sizeof(SHA512_CTX)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha512___del___obj, mod_trezorcrypto_Sha512___del__); diff --git a/vendor/trezor-crypto b/vendor/trezor-crypto index b7f73ee3ff..bb4c3d0525 160000 --- a/vendor/trezor-crypto +++ b/vendor/trezor-crypto @@ -1 +1 @@ -Subproject commit b7f73ee3ff78e09c266a30dbc31407558d471615 +Subproject commit bb4c3d052561bd31856a03d975ca226571f6a893