From bc7cb88cdfa3e2b1ed0dc8026ada32afc32aa4e1 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 27 Apr 2016 18:45:00 +0200 Subject: [PATCH] use little endian for toif, fix random --- Makefile | 2 +- assets/colorwheel.toif | Bin 21019 -> 21019 bytes assets/lock.toig | Bin 328 -> 328 bytes assets/satoshilabs.toif | Bin 6757 -> 6757 bytes assets/trezor.toig | Bin 1395 -> 1395 bytes docs/bootloader.md | 2 +- docs/toif.md | 2 +- extmod/modtrezorcrypto/rand.c | 66 +++++++++++++++++++++++ extmod/modtrezorcrypto/rand.h | 12 +++++ extmod/modtrezorui/modtrezorui-display.h | 12 ++--- src/playground/tap_64.toig | Bin 434 -> 434 bytes tools/png2toi | 33 ++++++++---- vendor/micropython | 2 +- 13 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 extmod/modtrezorcrypto/rand.c create mode 100644 extmod/modtrezorcrypto/rand.h diff --git a/Makefile b/Makefile index 7020159a45..8e51e7ee59 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ flash: ## flash firmware using st-flash st-flash write $(STMHAL_BUILD_DIR)/firmware1.bin 0x8020000 openocd: ## start openocd which connects to the device - openocd -f interface/stlink-v2.cfg -f target/stm32f4x_stlink.cfg + openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg gdb: ## start remote gdb session which connects to the openocd gdb $(STMHAL_BUILD_DIR)/firmware.elf -ex 'target remote localhost:3333' diff --git a/assets/colorwheel.toif b/assets/colorwheel.toif index 7ae41becfc5462ddb7b3e2baa1b061c6018f5098..bc4497be55b084720049934a7956cd16096632ab 100644 GIT binary patch delta 23 fcmbQegmLx~MxGFV&$JH=9~k(97#KG4@CN|^V-g2G delta 23 dcmbQegmLx~MxGFV&oqV)K*$ipzmbPO2moNS2R;A* diff --git a/assets/lock.toig b/assets/lock.toig index 720f0cdf0d90d41ca1cfb7841fec6f19266cb7ad..de81220c73cca35a8ed7ae5f719d2fa9634190cb 100644 GIT binary patch delta 21 ccmX@Xbb^T|#NRVrhrxlthLM3`BaaOu06kv>mjD0& delta 21 ccmX@Xbb^T|#NRWWL5IPCfq~IxBaaOu06abfmjD0& diff --git a/assets/satoshilabs.toif b/assets/satoshilabs.toif index 497f32f14b4b604c53bee3d9f39091705bb26a9a..84f8a52c1650a062af0422beb240fd8c84a55b82 100644 GIT binary patch delta 21 ccmaEA^3;SU#NRXR1H%V~NGS$}jXaT309!x@6aWAK delta 21 bcmaEA^3;SU#NRWG;R6sdNJVbsiIf5WSUU$4 diff --git a/assets/trezor.toig b/assets/trezor.toig index de183b8feb799dddb859b4eed3f06cbc159b5c41..8ab461e3b09a41c989b33ebe5850e64a7ba0a203 100644 GIT binary patch delta 21 ccmey&^_hz&#NRXh1H%V~bXEq2jXdeB09Pdj&j0`b delta 21 bcmey&^_hz&#NRWW;R6sdu%>V1NoNHBR0{^r diff --git a/docs/bootloader.md b/docs/bootloader.md index 634cd8a902..97da34d74a 100644 --- a/docs/bootloader.md +++ b/docs/bootloader.md @@ -1,6 +1,6 @@ #TREZOR OS Bootloader -All multibyte integer values are big endian! +All multibyte integer values are little endian! ##Firmware File Format diff --git a/docs/toif.md b/docs/toif.md index cd6b9e8466..0bba3b35b1 100644 --- a/docs/toif.md +++ b/docs/toif.md @@ -1,6 +1,6 @@ #TREZOR Optimized Image Format -All multibyte integer values are big endian! +All multibyte integer values are little endian! ##Header diff --git a/extmod/modtrezorcrypto/rand.c b/extmod/modtrezorcrypto/rand.c new file mode 100644 index 0000000000..ef7df098e1 --- /dev/null +++ b/extmod/modtrezorcrypto/rand.c @@ -0,0 +1,66 @@ +#include "rand.h" + +#ifdef UNIX +#include +#include +static FILE *frand = NULL; +#else +uint32_t rng_get(void); +#endif + +uint32_t random32(void) +{ +#ifdef UNIX + uint32_t r; + size_t len = sizeof(r); + if (!frand) { + frand = fopen("/dev/urandom", "r"); + } + size_t len_read = fread(&r, 1, len, frand); + (void)len_read; + assert(len_read == len); + return r; +#else + return rng_get(); +#endif +} + +uint32_t random_uniform(uint32_t n) +{ + uint32_t x, max = 0xFFFFFFFF - (0xFFFFFFFF % n); + while ((x = random32()) >= max); + return x / (max / n); +} + +void random_buffer(uint8_t *buf, size_t len) +{ +#ifdef UNIX + if (!frand) { + frand = fopen("/dev/urandom", "r"); + } + size_t len_read = fread(buf, 1, len, frand); + (void)len_read; + assert(len_read == len); +#else + size_t i; + uint32_t r = 0; + for (i = 0; i < len; i++) { + if (i % 4 == 0) { + r = random32(); + } + buf[i] = (r >> ((i % 4) * 8)) & 0xFF; + } +#endif +} + +void random_permute(char *str, size_t len) +{ + int i, j; + char t; + for (i = len - 1; i >= 1; i--) { + j = random_uniform(i + 1); + t = str[j]; + str[j] = str[i]; + str[i] = t; + } +} diff --git a/extmod/modtrezorcrypto/rand.h b/extmod/modtrezorcrypto/rand.h new file mode 100644 index 0000000000..4ad63b22e4 --- /dev/null +++ b/extmod/modtrezorcrypto/rand.h @@ -0,0 +1,12 @@ +#ifndef __RAND_H__ +#define __RAND_H__ + +#include +#include + +uint32_t random32(void); +uint32_t random_uniform(uint32_t n); +void random_buffer(uint8_t *buf, size_t len); +void random_permute(char *buf, size_t len); + +#endif diff --git a/extmod/modtrezorui/modtrezorui-display.h b/extmod/modtrezorui/modtrezorui-display.h index caeddbf3cb..a1f43f7bcc 100644 --- a/extmod/modtrezorui/modtrezorui-display.h +++ b/extmod/modtrezorui/modtrezorui-display.h @@ -206,9 +206,9 @@ STATIC mp_obj_t mod_TrezorUi_Display_image(size_t n_args, const mp_obj_t *args) if (bufinfo.len < 8 || memcmp(data, "TOIf", 4) != 0) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid image format")); } - mp_int_t w = (data[4] << 8) | data[5]; - mp_int_t h = (data[6] << 8) | data[7]; - mp_int_t datalen = (data[8] << 24) | (data[9] << 16) | (data[10] << 8) | data[11]; + mp_int_t w = *(uint16_t *)(data + 4); + mp_int_t h = *(uint16_t *)(data + 6); + mp_int_t datalen = *(uint32_t *)(data + 8); if ((x < 0) || (y < 0) || (x + w > RESX) || (y + h > RESY)) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Out of bounds")); } @@ -230,9 +230,9 @@ STATIC mp_obj_t mod_TrezorUi_Display_icon(size_t n_args, const mp_obj_t *args) { if (bufinfo.len < 8 || memcmp(data, "TOIg", 4) != 0) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid image format")); } - mp_int_t w = (data[4] << 8) | data[5]; - mp_int_t h = (data[6] << 8) | data[7]; - mp_int_t datalen = (data[8] << 24) | (data[9] << 16) | (data[10] << 8) | data[11]; + mp_int_t w = *(uint16_t *)(data + 4); + mp_int_t h = *(uint16_t *)(data + 6); + mp_int_t datalen = *(uint32_t *)(data + 8); if ((x < 0) || (y < 0) || (x + w > RESX) || (y + h > RESY)) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Out of bounds")); } diff --git a/src/playground/tap_64.toig b/src/playground/tap_64.toig index 9b3f1957dc9560d5530c052880a853d461264226..1bd987bd00f5dc7d397d2d87f1f99d675f9aa7af 100644 GIT binary patch delta 21 ccmdnQyos47#NRXBfx&@c86yM3MxJGi06!@ORR910 delta 21 bcmdnQyos47#NRWW!2t*v7?*A2S;hzeJhlZ@ diff --git a/tools/png2toi b/tools/png2toi index b1899e9702..8bd6cfe3f2 100755 --- a/tools/png2toi +++ b/tools/png2toi @@ -45,25 +45,36 @@ def process_image(ifn): if im.mode == 'RGB': ofn = '%s.toif' % ifn[:-4] + pixeldata = process_rgb(w, h, pix) else: ofn = '%s.toig' % ifn[:-4] + pixeldata = process_grayscale(w, h, pix) + z = zlib.compressobj(level=9, wbits=10) + zdata = z.compress(pixeldata) + z.flush() + zdata = zdata[2:-4] # strip header and checksum + with open(ofn, 'wb') as f: if im.mode == 'RGB': f.write(bytes('TOIf', 'ascii')) else: f.write(bytes('TOIg', 'ascii')) - f.write(struct.pack('>HH', w, h)) - if im.mode == 'RGB': - pixeldata = process_rgb(w, h, pix) - else: - pixeldata = process_grayscale(w, h, pix) - z = zlib.compressobj(level=9, wbits=10) - zdata = z.compress(pixeldata) + z.flush() - zdata = zdata[2:-4] # strip header and checksum - f.write(struct.pack('>I', len(zdata))) + f.write(struct.pack('