From 8b4280b296040a33b781552b635ee61b2b10bf1a Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 15 Apr 2016 19:44:26 +0200 Subject: [PATCH] remove trezor-crypto, use mbedtls, use lowercase for trezor modules --- .gitmodules | 3 - extmod/modTrezorCrypto/trezor-crypto | 1 - extmod/modtrezorcrypto/base58.c | 161 ++++++++++++++++++ extmod/modtrezorcrypto/base58.h | 33 ++++ .../mbedtls | 0 .../modtrezorcrypto-base58.h} | 2 +- .../modtrezorcrypto-ripemd160.h} | 0 .../modtrezorcrypto-sha256.h} | 0 .../modtrezorcrypto-sha512.h} | 0 .../modtrezorcrypto.c} | 8 +- .../modtrezormsg.c} | 0 .../modtrezorprotobuf.c} | 0 .../modtrezorui-display.h} | 8 +- .../modtrezorui-font_roboto_bold.h} | 0 .../modtrezorui-font_roboto_regular.h} | 0 .../modtrezorui-font_robotomono_regular.h} | 0 .../modtrezorui-inflate.h} | 0 .../modtrezorui-stmhal.h} | 0 .../modtrezorui-touch.h} | 0 .../modtrezorui-unix.h} | 0 .../modtrezorui.c} | 8 +- .../{modTrezorUi => modtrezorui}/trezor-qrenc | 0 tools/ttf2c | 2 +- vendor/micropython | 2 +- vendor/trezor-crypto | 1 - 25 files changed, 209 insertions(+), 20 deletions(-) delete mode 120000 extmod/modTrezorCrypto/trezor-crypto create mode 100644 extmod/modtrezorcrypto/base58.c create mode 100644 extmod/modtrezorcrypto/base58.h rename extmod/{modTrezorCrypto => modtrezorcrypto}/mbedtls (100%) rename extmod/{modTrezorCrypto/modTrezorCrypto-base58.h => modtrezorcrypto/modtrezorcrypto-base58.h} (98%) rename extmod/{modTrezorCrypto/modTrezorCrypto-ripemd160.h => modtrezorcrypto/modtrezorcrypto-ripemd160.h} (100%) rename extmod/{modTrezorCrypto/modTrezorCrypto-sha256.h => modtrezorcrypto/modtrezorcrypto-sha256.h} (100%) rename extmod/{modTrezorCrypto/modTrezorCrypto-sha512.h => modtrezorcrypto/modtrezorcrypto-sha512.h} (100%) rename extmod/{modTrezorCrypto/modTrezorCrypto.c => modtrezorcrypto/modtrezorcrypto.c} (88%) rename extmod/{modTrezorMsg/modTrezorMsg.c => modtrezormsg/modtrezormsg.c} (100%) rename extmod/{modTrezorProtobuf/modTrezorProtobuf.c => modtrezorprotobuf/modtrezorprotobuf.c} (100%) rename extmod/{modTrezorUi/modTrezorUi-display.h => modtrezorui/modtrezorui-display.h} (98%) rename extmod/{modTrezorUi/modTrezorUi-font_Roboto_Bold.h => modtrezorui/modtrezorui-font_roboto_bold.h} (100%) rename extmod/{modTrezorUi/modTrezorUi-font_Roboto_Regular.h => modtrezorui/modtrezorui-font_roboto_regular.h} (100%) rename extmod/{modTrezorUi/modTrezorUi-font_RobotoMono_Regular.h => modtrezorui/modtrezorui-font_robotomono_regular.h} (100%) rename extmod/{modTrezorUi/modTrezorUi-inflate.h => modtrezorui/modtrezorui-inflate.h} (100%) rename extmod/{modTrezorUi/modTrezorUi-stmhal.h => modtrezorui/modtrezorui-stmhal.h} (100%) rename extmod/{modTrezorUi/modTrezorUi-touch.h => modtrezorui/modtrezorui-touch.h} (100%) rename extmod/{modTrezorUi/modTrezorUi-unix.h => modtrezorui/modtrezorui-unix.h} (100%) rename extmod/{modTrezorUi/modTrezorUi.c => modtrezorui/modtrezorui.c} (89%) rename extmod/{modTrezorUi => modtrezorui}/trezor-qrenc (100%) delete mode 160000 vendor/trezor-crypto diff --git a/.gitmodules b/.gitmodules index 5e92ecbf04..dc37e96569 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,9 +4,6 @@ [submodule "vendor/trezor-qrenc"] path = vendor/trezor-qrenc url = https://github.com/trezor/trezor-qrenc.git -[submodule "vendor/trezor-crypto"] - path = vendor/trezor-crypto - url = https://github.com/trezor/trezor-crypto.git [submodule "vendor/mbedtls"] path = vendor/mbedtls url = https://github.com/trezor/mbedtls.git diff --git a/extmod/modTrezorCrypto/trezor-crypto b/extmod/modTrezorCrypto/trezor-crypto deleted file mode 120000 index e533d16294..0000000000 --- a/extmod/modTrezorCrypto/trezor-crypto +++ /dev/null @@ -1 +0,0 @@ -../../vendor/trezor-crypto \ No newline at end of file diff --git a/extmod/modtrezorcrypto/base58.c b/extmod/modtrezorcrypto/base58.c new file mode 100644 index 0000000000..de83c133b4 --- /dev/null +++ b/extmod/modtrezorcrypto/base58.c @@ -0,0 +1,161 @@ +/** + * Copyright (c) 2012-2014 Luke Dashjr + * Copyright (c) 2013-2014 Pavol Rusnak + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include "base58.h" + +static const int8_t b58digits_map[] = { + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1, + -1, 9,10,11,12,13,14,15,16,-1,17,18,19,20,21,-1, + 22,23,24,25,26,27,28,29,30,31,32,-1,-1,-1,-1,-1, + -1,33,34,35,36,37,38,39,40,41,42,43,-1,44,45,46, + 47,48,49,50,51,52,53,54,55,56,57,-1,-1,-1,-1,-1, +}; + +bool b58tobin(void *bin, size_t *binszp, const char *b58) +{ + size_t binsz = *binszp; + const unsigned char *b58u = (void*)b58; + unsigned char *binu = bin; + size_t outisz = (binsz + 3) / 4; + uint32_t outi[outisz]; + uint64_t t; + uint32_t c; + size_t i, j; + uint8_t bytesleft = binsz % 4; + uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0; + unsigned zerocount = 0; + size_t b58sz; + + b58sz = strlen(b58); + + memset(outi, 0, outisz * sizeof(*outi)); + + // Leading zeros, just count + for (i = 0; i < b58sz && !b58digits_map[b58u[i]]; ++i) + ++zerocount; + + for ( ; i < b58sz; ++i) + { + if (b58u[i] & 0x80) + // High-bit set on invalid digit + return false; + if (b58digits_map[b58u[i]] == -1) + // Invalid base58 digit + return false; + c = (unsigned)b58digits_map[b58u[i]]; + for (j = outisz; j--; ) + { + t = ((uint64_t)outi[j]) * 58 + c; + c = (t & 0x3f00000000) >> 32; + outi[j] = t & 0xffffffff; + } + if (c) + // Output number too big (carry to the next int32) + return false; + if (outi[0] & zeromask) + // Output number too big (last int32 filled too far) + return false; + } + + j = 0; + switch (bytesleft) { + case 3: + *(binu++) = (outi[0] & 0xff0000) >> 16; + case 2: + *(binu++) = (outi[0] & 0xff00) >> 8; + case 1: + *(binu++) = (outi[0] & 0xff); + ++j; + default: + break; + } + + for (; j < outisz; ++j) + { + *(binu++) = (outi[j] >> 0x18) & 0xff; + *(binu++) = (outi[j] >> 0x10) & 0xff; + *(binu++) = (outi[j] >> 8) & 0xff; + *(binu++) = (outi[j] >> 0) & 0xff; + } + + // Count canonical base58 byte count + binu = bin; + for (i = 0; i < binsz; ++i) + { + if (binu[i]) + break; + --*binszp; + } + *binszp += zerocount; + + return true; +} + +static const char b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; + +bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) +{ + const uint8_t *bin = data; + int carry; + ssize_t i, j, high, zcount = 0; + size_t size; + + while (zcount < (ssize_t)binsz && !bin[zcount]) + ++zcount; + + size = (binsz - zcount) * 138 / 100 + 1; + uint8_t buf[size]; + memset(buf, 0, size); + + for (i = zcount, high = size - 1; i < (ssize_t)binsz; ++i, high = j) + { + for (carry = bin[i], j = size - 1; (j > high) || carry; --j) + { + carry += 256 * buf[j]; + buf[j] = carry % 58; + carry /= 58; + } + } + + for (j = 0; j < (ssize_t)size && !buf[j]; ++j); + + if (*b58sz <= zcount + size - j) + { + *b58sz = zcount + size - j + 1; + return false; + } + + if (zcount) + memset(b58, '1', zcount); + for (i = zcount; j < (ssize_t)size; ++i, ++j) + b58[i] = b58digits_ordered[buf[j]]; + b58[i] = '\0'; + *b58sz = i + 1; + + return true; +} diff --git a/extmod/modtrezorcrypto/base58.h b/extmod/modtrezorcrypto/base58.h new file mode 100644 index 0000000000..aa78a0b42e --- /dev/null +++ b/extmod/modtrezorcrypto/base58.h @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2013-2014 Tomas Dzetkulic + * Copyright (c) 2013-2014 Pavol Rusnak + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __BASE58_H__ +#define __BASE58_H__ + +#include +#include + +bool b58tobin(void *bin, size_t *binszp, const char *b58); +bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz); + +#endif diff --git a/extmod/modTrezorCrypto/mbedtls b/extmod/modtrezorcrypto/mbedtls similarity index 100% rename from extmod/modTrezorCrypto/mbedtls rename to extmod/modtrezorcrypto/mbedtls diff --git a/extmod/modTrezorCrypto/modTrezorCrypto-base58.h b/extmod/modtrezorcrypto/modtrezorcrypto-base58.h similarity index 98% rename from extmod/modTrezorCrypto/modTrezorCrypto-base58.h rename to extmod/modtrezorcrypto/modtrezorcrypto-base58.h index 255f79f2bd..334fb2100f 100644 --- a/extmod/modTrezorCrypto/modTrezorCrypto-base58.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-base58.h @@ -5,7 +5,7 @@ * see LICENSE.md file for details */ -#include "trezor-crypto/base58.h" +#include "base58.h" // class Base58(object): typedef struct _mp_obj_Base58_t { diff --git a/extmod/modTrezorCrypto/modTrezorCrypto-ripemd160.h b/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h similarity index 100% rename from extmod/modTrezorCrypto/modTrezorCrypto-ripemd160.h rename to extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h diff --git a/extmod/modTrezorCrypto/modTrezorCrypto-sha256.h b/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h similarity index 100% rename from extmod/modTrezorCrypto/modTrezorCrypto-sha256.h rename to extmod/modtrezorcrypto/modtrezorcrypto-sha256.h diff --git a/extmod/modTrezorCrypto/modTrezorCrypto-sha512.h b/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h similarity index 100% rename from extmod/modTrezorCrypto/modTrezorCrypto-sha512.h rename to extmod/modtrezorcrypto/modtrezorcrypto-sha512.h diff --git a/extmod/modTrezorCrypto/modTrezorCrypto.c b/extmod/modtrezorcrypto/modtrezorcrypto.c similarity index 88% rename from extmod/modTrezorCrypto/modTrezorCrypto.c rename to extmod/modtrezorcrypto/modtrezorcrypto.c index 776939095d..d4ffca04fe 100644 --- a/extmod/modTrezorCrypto/modTrezorCrypto.c +++ b/extmod/modtrezorcrypto/modtrezorcrypto.c @@ -15,10 +15,10 @@ #if MICROPY_PY_TREZORCRYPTO -#include "modTrezorCrypto-base58.h" -#include "modTrezorCrypto-ripemd160.h" -#include "modTrezorCrypto-sha256.h" -#include "modTrezorCrypto-sha512.h" +#include "modtrezorcrypto-base58.h" +#include "modtrezorcrypto-ripemd160.h" +#include "modtrezorcrypto-sha256.h" +#include "modtrezorcrypto-sha512.h" // module stuff diff --git a/extmod/modTrezorMsg/modTrezorMsg.c b/extmod/modtrezormsg/modtrezormsg.c similarity index 100% rename from extmod/modTrezorMsg/modTrezorMsg.c rename to extmod/modtrezormsg/modtrezormsg.c diff --git a/extmod/modTrezorProtobuf/modTrezorProtobuf.c b/extmod/modtrezorprotobuf/modtrezorprotobuf.c similarity index 100% rename from extmod/modTrezorProtobuf/modTrezorProtobuf.c rename to extmod/modtrezorprotobuf/modtrezorprotobuf.c diff --git a/extmod/modTrezorUi/modTrezorUi-display.h b/extmod/modtrezorui/modtrezorui-display.h similarity index 98% rename from extmod/modTrezorUi/modTrezorUi-display.h rename to extmod/modtrezorui/modtrezorui-display.h index 4cbc28a788..f87f7e5456 100644 --- a/extmod/modTrezorUi/modTrezorUi-display.h +++ b/extmod/modtrezorui/modtrezorui-display.h @@ -5,10 +5,10 @@ * see LICENSE.md file for details */ -#include "modTrezorUi-inflate.h" -#include "modTrezorUi-font_RobotoMono_Regular.h" -#include "modTrezorUi-font_Roboto_Regular.h" -#include "modTrezorUi-font_Roboto_Bold.h" +#include "modtrezorui-inflate.h" +#include "modtrezorui-font_robotomono_regular.h" +#include "modtrezorui-font_roboto_regular.h" +#include "modtrezorui-font_roboto_bold.h" #include "trezor-qrenc/qr_encode.h" diff --git a/extmod/modTrezorUi/modTrezorUi-font_Roboto_Bold.h b/extmod/modtrezorui/modtrezorui-font_roboto_bold.h similarity index 100% rename from extmod/modTrezorUi/modTrezorUi-font_Roboto_Bold.h rename to extmod/modtrezorui/modtrezorui-font_roboto_bold.h diff --git a/extmod/modTrezorUi/modTrezorUi-font_Roboto_Regular.h b/extmod/modtrezorui/modtrezorui-font_roboto_regular.h similarity index 100% rename from extmod/modTrezorUi/modTrezorUi-font_Roboto_Regular.h rename to extmod/modtrezorui/modtrezorui-font_roboto_regular.h diff --git a/extmod/modTrezorUi/modTrezorUi-font_RobotoMono_Regular.h b/extmod/modtrezorui/modtrezorui-font_robotomono_regular.h similarity index 100% rename from extmod/modTrezorUi/modTrezorUi-font_RobotoMono_Regular.h rename to extmod/modtrezorui/modtrezorui-font_robotomono_regular.h diff --git a/extmod/modTrezorUi/modTrezorUi-inflate.h b/extmod/modtrezorui/modtrezorui-inflate.h similarity index 100% rename from extmod/modTrezorUi/modTrezorUi-inflate.h rename to extmod/modtrezorui/modtrezorui-inflate.h diff --git a/extmod/modTrezorUi/modTrezorUi-stmhal.h b/extmod/modtrezorui/modtrezorui-stmhal.h similarity index 100% rename from extmod/modTrezorUi/modTrezorUi-stmhal.h rename to extmod/modtrezorui/modtrezorui-stmhal.h diff --git a/extmod/modTrezorUi/modTrezorUi-touch.h b/extmod/modtrezorui/modtrezorui-touch.h similarity index 100% rename from extmod/modTrezorUi/modTrezorUi-touch.h rename to extmod/modtrezorui/modtrezorui-touch.h diff --git a/extmod/modTrezorUi/modTrezorUi-unix.h b/extmod/modtrezorui/modtrezorui-unix.h similarity index 100% rename from extmod/modTrezorUi/modTrezorUi-unix.h rename to extmod/modtrezorui/modtrezorui-unix.h diff --git a/extmod/modTrezorUi/modTrezorUi.c b/extmod/modtrezorui/modtrezorui.c similarity index 89% rename from extmod/modTrezorUi/modTrezorUi.c rename to extmod/modtrezorui/modtrezorui.c index 3de7964fc9..67bb0adfca 100644 --- a/extmod/modTrezorUi/modTrezorUi.c +++ b/extmod/modtrezorui/modtrezorui.c @@ -19,16 +19,16 @@ #define RESY 240 #if defined STM32_HAL_H -#include "modTrezorUi-stmhal.h" +#include "modtrezorui-stmhal.h" #elif defined UNIX -#include "modTrezorUi-unix.h" +#include "modtrezorui-unix.h" #else #error Unsupported port. Only STMHAL and UNIX ports are supported. #endif -#include "modTrezorUi-display.h" +#include "modtrezorui-display.h" -#include "modTrezorUi-touch.h" +#include "modtrezorui-touch.h" // module stuff diff --git a/extmod/modTrezorUi/trezor-qrenc b/extmod/modtrezorui/trezor-qrenc similarity index 100% rename from extmod/modTrezorUi/trezor-qrenc rename to extmod/modtrezorui/trezor-qrenc diff --git a/tools/ttf2c b/tools/ttf2c index 8d0fbca5b8..034b497fd2 100755 --- a/tools/ttf2c +++ b/tools/ttf2c @@ -7,7 +7,7 @@ MAX_GLYPH = ord('~') def process_face(name, style, size): face = freetype.Face('/usr/share/fonts/truetype/%s-%s.ttf' % (name, style)) face.set_pixel_sizes(0, size) - with open('%s-%s-%s.c' % (name, style, size), 'wt') as f: + with open('%s-%s-%s.c' % (name.lower(), style.lower(), size), 'wt') as f: f.write('// first two bytes are width and height of the glyph\n') f.write('// third, fourth and fifth bytes are advance, bearingX and bearingY of the horizontal metrics of the glyph\n') f.write('// rest is packed 4-bit glyph data\n\n') diff --git a/vendor/micropython b/vendor/micropython index fe1e443bd4..c83524cb4a 160000 --- a/vendor/micropython +++ b/vendor/micropython @@ -1 +1 @@ -Subproject commit fe1e443bd439be3e0c7942f0ed3f7cbfd3efd5dc +Subproject commit c83524cb4a55d862f348c63d28341485c6920398 diff --git a/vendor/trezor-crypto b/vendor/trezor-crypto deleted file mode 160000 index f4dd151eb9..0000000000 --- a/vendor/trezor-crypto +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f4dd151eb9ef989b88dc79218a2b0115934e4268