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

trezor.crypto: add keccak option to Sha3_*.digest()

This commit is contained in:
Pavol Rusnak 2016-11-06 01:49:55 +01:00
parent 192fc5f1ef
commit 8b0d7be52a
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
7 changed files with 41 additions and 20 deletions

View File

@ -1,9 +1,10 @@
.PHONY: vendor
STMHAL_BUILD_DIR=vendor/micropython/stmhal/build-TREZORV2
JOBS=4
MAKE=make -j $(JOBS)
BOARD=TREZORV2
STMHAL_BUILD_DIR=vendor/micropython/stmhal/build-$(BOARD)
help: ## show this help
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9_-]+:.*?## / {printf "\033[36mmake %-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
@ -17,13 +18,13 @@ res: ## update resources
build: build_stmhal build_unix build_cross ## build stmhal, unix and mpy-cross micropython ports
build_stmhal: vendor ## build stmhal port
$(MAKE) -C vendor/micropython/stmhal
$(MAKE) -C vendor/micropython/stmhal BOARD=$(BOARD)
build_stmhal_debug: vendor ## build stmhal port with debug symbols
$(MAKE) -C vendor/micropython/stmhal
$(MAKE) -C vendor/micropython/stmhal BOARD=$(BOARD)
build_stmhal_frozen: vendor res build_cross ## build stmhal port with frozen modules (from /src)
$(MAKE) -C vendor/micropython/stmhal FROZEN_MPY_DIR=../../../src
$(MAKE) -C vendor/micropython/stmhal FROZEN_MPY_DIR=../../../src BOARD=$(BOARD)
build_unix: vendor ## build unix port
$(MAKE) -C vendor/micropython/unix MICROPY_FORCE_32BIT=1
@ -38,7 +39,7 @@ build_cross: vendor ## build mpy-cross port
$(MAKE) -C vendor/micropython/mpy-cross MICROPY_FORCE_32BIT=1
build_bootloader: vendor ## build bootloader
$(MAKE) -C vendor/micropython/stmhal -f Makefile.bootloader
$(MAKE) -C vendor/micropython/stmhal -f Makefile.bootloader BOARD=$(BOARD)
run: ## run unix port
cd src ; ../vendor/micropython/unix/micropython
@ -49,7 +50,7 @@ emu: ## run emulator
clean: clean_stmhal clean_unix clean_cross ## clean all builds
clean_stmhal: ## clean stmhal build
$(MAKE) -C vendor/micropython/stmhal clean
$(MAKE) -C vendor/micropython/stmhal clean BOARD=$(BOARD)
clean_unix: ## clean unix build
$(MAKE) -C vendor/micropython/unix clean

View File

@ -50,21 +50,25 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha3_256_update(mp_obj_t self, mp_obj_t data) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha3_256_update_obj, mod_TrezorCrypto_Sha3_256_update);
/// def trezor.crypto.hashlib.Sha3_256.digest(self) -> bytes:
/// def trezor.crypto.hashlib.Sha3_256.digest(self, keccak: bool=False) -> bytes:
/// '''
/// Returns the digest of hashed data.
/// '''
STATIC mp_obj_t mod_TrezorCrypto_Sha3_256_digest(mp_obj_t self) {
mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(self);
STATIC mp_obj_t mod_TrezorCrypto_Sha3_256_digest(size_t n_args, const mp_obj_t *args) {
mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(args[0]);
vstr_t vstr;
vstr_init_len(&vstr, HASH_SHA3_256_DIGEST_SIZE);
SHA3_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(SHA3_CTX));
if (n_args >= 1 && args[1] == mp_const_true) {
keccak_Final(&ctx, (uint8_t *)vstr.buf);
} else {
sha3_Final(&ctx, (uint8_t *)vstr.buf);
}
memset(&ctx, 0, sizeof(SHA3_CTX));
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha3_256_digest_obj, mod_TrezorCrypto_Sha3_256_digest);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Sha3_256_digest_obj, 1, 2, mod_TrezorCrypto_Sha3_256_digest);
STATIC mp_obj_t mod_TrezorCrypto_Sha3_256___del__(mp_obj_t self) {
mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(self);

View File

@ -50,21 +50,25 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha3_512_update(mp_obj_t self, mp_obj_t data) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha3_512_update_obj, mod_TrezorCrypto_Sha3_512_update);
/// def trezor.crypto.hashlib.Sha3_512.digest(self) -> bytes:
/// def trezor.crypto.hashlib.Sha3_512.digest(self, keccak: bool=False) -> bytes:
/// '''
/// Returns the digest of hashed data.
/// '''
STATIC mp_obj_t mod_TrezorCrypto_Sha3_512_digest(mp_obj_t self) {
mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(self);
STATIC mp_obj_t mod_TrezorCrypto_Sha3_512_digest(size_t n_args, const mp_obj_t *args) {
mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(args[0]);
vstr_t vstr;
vstr_init_len(&vstr, HASH_SHA3_512_DIGEST_SIZE);
SHA3_CTX ctx;
memcpy(&ctx, &(o->ctx), sizeof(SHA3_CTX));
if (n_args >= 1 && args[1] == mp_const_true) {
keccak_Final(&ctx, (uint8_t *)vstr.buf);
} else {
sha3_Final(&ctx, (uint8_t *)vstr.buf);
}
memset(&ctx, 0, sizeof(SHA3_CTX));
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha3_512_digest_obj, mod_TrezorCrypto_Sha3_512_digest);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Sha3_512_digest_obj, 1, 2, mod_TrezorCrypto_Sha3_512_digest);
STATIC mp_obj_t mod_TrezorCrypto_Sha3_512___del__(mp_obj_t self) {
mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(self);

View File

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

View File

@ -16,6 +16,12 @@ class TestCryptoSha3_256(unittest.TestCase):
self.assertEqual(hashlib.sha3_256(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), unhexlify('41c0dba2a9d6240849100376a8235e2c82e1b9998a999e21db32dd97496d3376'))
self.assertEqual(hashlib.sha3_256(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), unhexlify('916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18'))
def test_digest_keccak(self):
self.assertEqual(hashlib.sha3_256(b'').digest(True), unhexlify('c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'))
self.assertEqual(hashlib.sha3_256(b'abc').digest(True), unhexlify('4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45'))
self.assertEqual(hashlib.sha3_256(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(True), unhexlify('45d3b367a6904e6e8d502ee04999a7c27647f91fa845d456525fd352ae3d7371'))
self.assertEqual(hashlib.sha3_256(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(True), unhexlify('f519747ed599024f3882238e5ab43960132572b7345fbeb9a90769dafd21ad67'))
def test_update(self):
x = hashlib.sha3_256()
self.assertEqual(x.digest(), unhexlify('a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a'))

View File

@ -16,6 +16,12 @@ class TestCryptoSha3_512(unittest.TestCase):
self.assertEqual(hashlib.sha3_512(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), unhexlify('04a371e84ecfb5b8b77cb48610fca8182dd457ce6f326a0fd3d7ec2f1e91636dee691fbe0c985302ba1b0d8dc78c086346b533b49c030d99a27daf1139d6e75e'))
self.assertEqual(hashlib.sha3_512(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), unhexlify('afebb2ef542e6579c50cad06d2e578f9f8dd6881d7dc824d26360feebf18a4fa73e3261122948efcfd492e74e82e2189ed0fb440d187f382270cb455f21dd185'))
def test_digest_keccak(self):
self.assertEqual(hashlib.sha3_512(b'').digest(True), unhexlify('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e'))
self.assertEqual(hashlib.sha3_512(b'abc').digest(True), unhexlify('18587dc2ea106b9a1563e32b3312421ca164c7f1f07bc922a9c83d77cea3a1e5d0c69910739025372dc14ac9642629379540c17e2a65b19d77aa511a9d00bb96'))
self.assertEqual(hashlib.sha3_512(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(True), unhexlify('6aa6d3669597df6d5a007b00d09c20795b5c4218234e1698a944757a488ecdc09965435d97ca32c3cfed7201ff30e070cd947f1fc12b9d9214c467d342bcba5d'))
self.assertEqual(hashlib.sha3_512(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(True), unhexlify('ac2fb35251825d3aa48468a9948c0a91b8256f6d97d8fa4160faff2dd9dfcc24f3f1db7a983dad13d53439ccac0b37e24037e7b95f80f59f37a2f683c4ba4682'))
def test_update(self):
x = hashlib.sha3_512()
self.assertEqual(x.digest(), unhexlify('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26'))

2
vendor/micropython vendored

@ -1 +1 @@
Subproject commit 460380bd44d10a4e192881fe9f895724e67916fd
Subproject commit 3e4519fed0021be62d61383b48b4c71c91cb48fa