From 3d8f8a659a8cbd17ed20630622a662abcf61ea6c Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Thu, 5 May 2016 21:41:03 +0200 Subject: [PATCH] rework documentation, proof of concept api docu generator --- docs/api-ideas.md | 131 ------------ docs/api.md | 197 +++++++++++++----- docs/api.py | 37 ++++ docs/api.template.md | 45 ++++ .../modtrezorcrypto/modtrezorcrypto-ed25519.h | 10 +- .../modtrezorcrypto-nist256p1.h | 12 +- .../modtrezorcrypto/modtrezorcrypto-pbkdf2.h | 10 +- .../modtrezorcrypto-ripemd160.h | 10 +- .../modtrezorcrypto-secp256k1.h | 12 +- .../modtrezorcrypto/modtrezorcrypto-sha256.h | 10 +- .../modtrezorcrypto-sha3-256.h | 10 +- .../modtrezorcrypto-sha3-512.h | 10 +- .../modtrezorcrypto/modtrezorcrypto-sha512.h | 10 +- extmod/modtrezorcrypto/modtrezorcrypto.c | 2 - extmod/modtrezorcrypto/todo.txt | 55 +++++ extmod/modtrezormsg/modtrezormsg.c | 10 +- extmod/modtrezorui/modtrezorui-display.h | 30 ++- extmod/modtrezorui/modtrezorui.c | 2 - extmod/modtrezorutils/modtrezorutils.c | 8 +- src/trezor/crypto/base58.py | 38 ++-- src/trezor/crypto/hmac.py | 6 +- 21 files changed, 352 insertions(+), 303 deletions(-) delete mode 100644 docs/api-ideas.md create mode 100755 docs/api.py create mode 100644 docs/api.template.md create mode 100644 extmod/modtrezorcrypto/todo.txt diff --git a/docs/api-ideas.md b/docs/api-ideas.md deleted file mode 100644 index d09dbe9bc..000000000 --- a/docs/api-ideas.md +++ /dev/null @@ -1,131 +0,0 @@ -##trezor.crypto - -###trezor.crypto.aes - -``` python -AES_CTX = object # AES context - -def aes_encrypt_key128(key: bytes16, iv: bytes16 = None) -> AES_CTX: # context - -def aes_encrypt_key192(key: bytes24, iv: bytes16 = None) -> AES_CTX: # context - -def aes_encrypt_key256(key: bytes32, iv: bytes16 = None) -> AES_CTX: # context - -def aes_decrypt_key128(key: bytes16, iv: bytes16 = None) -> AES_CTX: # context - -def aes_decrypt_key192(key: bytes24, iv: bytes16 = None) -> AES_CTX: # context - -def aes_decrypt_key256(key: bytes32, iv: bytes16 = None) -> AES_CTX: # context - -def aes_ecb_encrypt(ctx: AES_CTX, data: bytes) -> bytes: # encrypted - -def aes_cbc_encrypt(ctx: AES_CTX, data: bytes) -> bytes: # encrypted - -def aes_cfb_encrypt(ctx: AES_CTX, data: bytes) -> bytes: # encrypted - -def aes_ofb_encrypt(ctx: AES_CTX, data: bytes) -> bytes: # encrypted - -def aes_ctr_encrypt(ctx: AES_CTX, data: bytes) -> bytes: # encrypted - -def aes_ecb_decrypt(ctx: AES_CTX, data: bytes) -> bytes: # decrypted - -def aes_cbc_decrypt(ctx: AES_CTX, data: bytes) -> bytes: # decrypted - -def aes_cfb_decrypt(ctx: AES_CTX, data: bytes) -> bytes: # decrypted - -def aes_ofb_decrypt(ctx: AES_CTX, data: bytes) -> bytes: # decrypted - -def aes_ctr_decrypt(ctx: AES_CTX, data: bytes) -> bytes: # decrypted -``` - -###trezor.crypto.base58 - -``` python -def encode(data: bytes) -> bytes: # encoded - -def decode(data: bytes) -> bytes: # decoded - -def encode_check(data: bytes) -> bytes: # encoded - -def decode_check(data: bytes) -> bytes: # decoded -``` - -###trezor.crypto.ed25519 - -``` python -def to_public(secret_key: bytes32) -> bytes32: # public_key - -def sign(message: bytes, secret_key: bytes32, public_key: bytes32 = None) -> bytes64: # signature - -def verify(message: bytes, public_key: bytes32, signature: bytes64) -> bool: # valid -``` - -###trezor.crypto.hash - -``` python -def sha256(data: bytes) -> bytes32: # hashed - -def sha512(data: bytes) -> bytes64: # hashed - -def ripemd160(data: bytes) -> bytes20: # hashed -``` - -###trezor.crypto.hd - -TODO - -###trezor.crypto.hmac - -``` python -def hmac_sha256(key: bytes, message: bytes) -> bytes32: # hmac - -def hmac_sha512(key: bytes, message: bytes) -> bytes64: # hmac -``` - -###trezor.crypto.kdf - -``` python -def pbkdf2_hmac_sha256(password: bytes, salt: bytes, iterations: int, keylen: int) -> bytes32: # key - -def pbkdf2_hmac_sha512(password: bytes, salt: bytes, iterations: int, keylen: int) -> bytes32: # key -``` - -###trezor.crypto.mnemonic - -``` python -def bip39_generate(strength: int) -> bytes: # sentence - -def bip39_fromdata(data: bytes) -> bytes: # sentence - -def bip39_check(mnemonic: bytes) -> bool: # valid - -def bip39_seed(mnemonic: bytes, passphrase: bytes) -> bytes64: # seed -``` - -###trezor.crypto.nistp256 - -``` python -def to_public(secret_key: bytes32) -> bytes33: # public_key - -def sign(message: bytes, secret_key: bytes32, public_key: bytes33 = None) -> bytes65: # signature - -def verify(message: bytes, public_key: bytes33, signature: bytes65) -> bool: # valid -``` - -###trezor.crypto.reedsolomon - -``` python -def encode(data: bytes) -> bytes: # encoded - -def decode(data: bytes) -> bytes: # decoded -``` - -###trezor.crypto.secp256k1 - -``` python -def to_public(secret_key: bytes32) -> bytes33: # public_key - -def sign(message: bytes, secret_key: bytes32, public_key: bytes33 = None) -> bytes65: # signature - -def verify(message: bytes, public_key: bytes33, signature: bytes65) -> bool: # valid -``` diff --git a/docs/api.md b/docs/api.md index 9e381817a..ca0dee319 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1,77 +1,176 @@ #TREZOR OS API -Auxiliary classes used to tighten the type checking. - -``` python -bytes16 = bytes # bytes variable of exactly 16 bytes -bytes20 = bytes # bytes variable of exactly 20 bytes -bytes21 = bytes # bytes variable of exactly 21 bytes -bytes24 = bytes # bytes variable of exactly 24 bytes -bytes32 = bytes # bytes variable of exactly 32 bytes -bytes33 = bytes # bytes variable of exactly 33 bytes -bytes64 = bytes # bytes variable of exactly 64 bytes -bytes65 = bytes # bytes variable of exactly 65 bytes -``` - Syntax used below is a valid Python function declaration with type hints defined in [PEP 0484](https://www.python.org/dev/peps/pep-0484/). - ##trezor.crypto ###trezor.crypto.base58 ``` python def trezor.crypto.base58.encode(data: bytes) -> str - +``` +``` python def trezor.crypto.base58.decode(string: str) -> bytes ``` +``` python +def trezor.crypto.base58.encode_check(data: bytes) -> str +``` +``` python +def trezor.crypto.base58.decode_check(string: str) -> bytes +``` + +###trezor.crypto.curve + +``` python +def trezor.crypto.curve.ed25519.publickey(self, secret_key: bytes) -> bytes +``` +``` python +def trezor.crypto.curve.ed25519.sign(self, secret_key: bytes, message: bytes) -> bytes +``` +``` python +def trezor.crypto.curve.ed25519.verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool +``` + +``` python +def trezor.crypto.curve.nist256p1.publickey(self, secret_key: bytes, compressed: bool=True) -> bytes +``` +``` python +def trezor.crypto.curve.nist256p1.sign(self, secret_key: bytes, message: bytes) -> bytes +``` +``` python +def trezor.crypto.curve.nist256p1.verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool +``` + +``` python +def trezor.crypto.curve.secp256k1.publickey(self, secret_key: bytes, compressed: bool=True) -> bytes +``` +``` python +def trezor.crypto.curve.secp256k1.sign(self, secret_key: bytes, message: bytes) -> bytes +``` +``` python +def trezor.crypto.curve.secp256k1.verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool +``` + +###trezor.crypto.hashlib + +``` python +def trezor.crypto.hashlib.ripemd160(self, data: bytes=None) -> Ripemd160 +``` +``` python +def Ripemd160.update(self, data: bytes) -> None +``` +``` python +def Ripemd160.digest(self) -> bytes +``` + +``` python +def trezor.crypto.hashlib.sha256(self, data: bytes=None) -> Sha256 +``` +``` python +def Sha256.update(self, data: bytes) -> None +``` +``` python +def Sha256.digest(self) -> bytes +``` + +``` python +def trezor.crypto.hashlib.sha512(self, data: bytes=None) -> Sha512 +``` +``` python +def Sha512.hash(self, data: bytes) -> None +``` +``` python +def Sha512.digest(self) -> bytes +``` + +``` python +def trezor.crypto.hashlib.sha3_256(self, data: bytes=None) -> Sha3_256 +``` +``` python +def Sha3_256.update(self, data: bytes) -> None +``` +``` python +def Sha3_256.digest(self) -> bytes +``` + +``` python +def trezor.crypto.hashlib.sha3_512(self, data: bytes=None) -> Sha3_512 +``` +``` python +def Sha3_512.update(self, data: bytes) -> None +``` +``` python +def Sha3_512.digest(self) -> bytes +``` + +###trezor.crypto.hmac + +``` python +def trezor.crypto.hmac.new(key, msg, digestmod) -> Hmac +``` ##trezor.msg ``` python -def trezor.msg.receive(callback) -> None - -def trezor.msg.send(message) -> None +def trezor.msg.send(self, message) -> int ``` - -##trezor.protobuf - ``` python -def trezor.protobuf.encode(message) -> bytes - -def trezor.protobuf.decode(data: bytes) -> object +def trezor.msg.select(self, timeout_us: int) -> tuple ``` ##trezor.ui +``` python +def trezor.ui.rgbcolor(r: int, g: int, b: int) -> int +``` +``` python +def trezor.ui.lerpi(a: int, b: int, t: float) -> int +``` +``` python +def trezor.ui.blend(ca: int, cb: int, t: float) -> int +``` +``` python +def trezor.ui.animate_pulse(func, ca, cb, speed=200000, delay=30000) +``` + ###trezor.ui.display ``` python -trezor.ui.display.bar(self, x: int, y: int, w: int, h: int, color: int) -> None - -trezor.ui.display.blit(self, x: int, y: int, w: int, h: int, data: bytes) -> None - -trezor.ui.display.image(self, x: int, y: int, image: bytes) -> None - -trezor.ui.display.icon(self, x: int, y: int, icon: bytes, fgcolor: int, bgcolor: int) -> None - -trezor.ui.display.text(self, x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None - -trezor.ui.display.qrcode(self, x: int, y: int, data: bytes, scale: int) -> None - -trezor.ui.display.orientation(self, degrees: int) -> None - -trezor.ui.display.raw(self, reg: int, data: bytes) -> None - -trezor.ui.display.backlight(self, val: int) -> None +def trezor.ui.display.bar(self, x: int, y: int, w: int, h: int, fgcolor: int, bgcolor: int=None) -> None ``` - -###trezor.ui.touch - ``` python -trezor.ui.touch.start(self, callback) -> None - -trezor.ui.touch.move(self, callback) -> None - -trezor.ui.touch.end(self, callback) -> None +def trezor.ui.display.blit(self, x: int, y: int, w: int, h: int, data: bytes) -> None +``` +``` python +def trezor.ui.display.image(self, x: int, y: int, image: bytes) -> None +``` +``` python +def trezor.ui.display.icon(self, x: int, y: int, icon: bytes, fgcolor: int, bgcolor: int) -> None +``` +``` python +def trezor.ui.display.text(self, x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None +``` +``` python +def trezor.ui.display.text_center(self, x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None +``` +``` python +def trezor.ui.display.text_right(self, x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None +``` +``` python +def trezor.ui.display.text_width(self, text: bytes, font: int) -> int +``` +``` python +def trezor.ui.display.qrcode(self, x: int, y: int, data: bytes, scale: int) -> None +``` +``` python +def trezor.ui.display.loader(self, progress: int, fgcolor: int, bgcolor: int, icon: bytes=None, iconfgcolor: int=None) -> None +``` +``` python +def trezor.ui.display.orientation(self, degrees: int) -> None +``` +``` python +def trezor.ui.display.raw(self, reg: int, data: bytes) -> None +``` +``` python +def trezor.ui.display.backlight(self, val: int) -> None ``` diff --git a/docs/api.py b/docs/api.py new file mode 100755 index 000000000..4a0c6c3a6 --- /dev/null +++ b/docs/api.py @@ -0,0 +1,37 @@ +#!/usr/bin/python3 +import os + +def process_file(fn): + mod, ext = os.path.splitext(fn) + src = open('../%s' % (fn)).readlines() + r = [] + if ext in ['.h', '.c']: + for l in src: + l = l.rstrip() + if l.startswith('/// '): + r.append('``` python') + r.append(l[4:]) + r.append('```') + elif ext == '.py': + mod = mod[4:].replace('/', '.') + for l in src: + l = l.rstrip() + if l.startswith('def '): + r.append('``` python') + r.append('def %s.' % mod + l[4:-1]) + r.append('```') + return r + +def main(): + tpl = open('api.template.md', 'rt').readlines() + f = open('api.md', 'wt') + for line in tpl: + if line.startswith('@'): + for l in process_file(line[1:].strip()): + f.write(l + '\n') + else: + f.write(line) + f.close() + +if __name__ == "__main__": + main() diff --git a/docs/api.template.md b/docs/api.template.md new file mode 100644 index 000000000..42b9bbb3b --- /dev/null +++ b/docs/api.template.md @@ -0,0 +1,45 @@ +#TREZOR OS API + +Syntax used below is a valid Python function declaration with type hints defined in [PEP 0484](https://www.python.org/dev/peps/pep-0484/). + +##trezor.crypto + +###trezor.crypto.base58 + +@src/trezor/crypto/base58.py + +###trezor.crypto.curve + +@extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h + +@extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h + +@extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h + +###trezor.crypto.hashlib + +@extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h + +@extmod/modtrezorcrypto/modtrezorcrypto-sha256.h + +@extmod/modtrezorcrypto/modtrezorcrypto-sha512.h + +@extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h + +@extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h + +###trezor.crypto.hmac + +@src/trezor/crypto/hmac.py + +##trezor.msg + +@extmod/modtrezormsg/modtrezormsg.c + +##trezor.ui + +@src/trezor/ui.py + +###trezor.ui.display + +@extmod/modtrezorui/modtrezorui-display.h diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h b/extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h index e08fb84c8..bf6a7ad14 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h @@ -9,12 +9,10 @@ #include "trezor-crypto/ed25519-donna/ed25519.h" -// class Ed25519(object): typedef struct _mp_obj_Ed25519_t { mp_obj_base_t base; } mp_obj_Ed25519_t; -// def Ed25519.__init__(self) STATIC mp_obj_t mod_TrezorCrypto_Ed25519_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 0, false); mp_obj_Ed25519_t *o = m_new_obj(mp_obj_Ed25519_t); @@ -22,7 +20,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Ed25519_make_new(const mp_obj_type_t *type, siz return MP_OBJ_FROM_PTR(o); } -// def Ed25519.publickey(self, secret_key: bytes) -> bytes +/// def trezor.crypto.curve.ed25519.publickey(self, secret_key: bytes) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Ed25519_publickey(mp_obj_t self, mp_obj_t secret_key) { mp_buffer_info_t sk; mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ); @@ -36,7 +34,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Ed25519_publickey(mp_obj_t self, mp_obj_t secre } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Ed25519_publickey_obj, mod_TrezorCrypto_Ed25519_publickey); -// def Ed25519.sign(self, secret_key: bytes, message: bytes) -> bytes +/// def trezor.crypto.curve.ed25519.sign(self, secret_key: bytes, message: bytes) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Ed25519_sign(mp_obj_t self, mp_obj_t secret_key, mp_obj_t message) { mp_buffer_info_t sk, msg; mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ); @@ -53,7 +51,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Ed25519_sign(mp_obj_t self, mp_obj_t secret_key } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorCrypto_Ed25519_sign_obj, mod_TrezorCrypto_Ed25519_sign); -// def Ed25519.verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool +/// def trezor.crypto.curve.ed25519.verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool STATIC mp_obj_t mod_TrezorCrypto_Ed25519_verify(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t pk, sig, msg; mp_get_buffer_raise(args[1], &pk, MP_BUFFER_READ); @@ -69,8 +67,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Ed25519_verify(size_t n_args, const mp_obj_t *a } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Ed25519_verify_obj, 4, 4, mod_TrezorCrypto_Ed25519_verify); -// Ed25519 stuff - STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Ed25519_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_publickey), MP_ROM_PTR(&mod_TrezorCrypto_Ed25519_publickey_obj) }, { MP_ROM_QSTR(MP_QSTR_sign), MP_ROM_PTR(&mod_TrezorCrypto_Ed25519_sign_obj) }, diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h b/extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h index 8c6cdbe71..181305534 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h @@ -8,14 +8,12 @@ #include "py/objstr.h" #include "trezor-crypto/ecdsa.h" -#include "trezor-crypto/nist256p1.h" +#include "trezor.crypto.curve.nist256p1.h" -// class Nist256p1(object): typedef struct _mp_obj_Nist256p1_t { mp_obj_base_t base; } mp_obj_Nist256p1_t; -// def Nist256p1.__init__(self) STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 0, false); mp_obj_Nist256p1_t *o = m_new_obj(mp_obj_Nist256p1_t); @@ -23,7 +21,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_make_new(const mp_obj_type_t *type, s return MP_OBJ_FROM_PTR(o); } -// def Nist256p1.publickey(self, secret_key: bytes, compressed: bool=True) -> bytes +/// def trezor.crypto.curve.nist256p1.publickey(self, secret_key: bytes, compressed: bool=True) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_publickey(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t sk; mp_get_buffer_raise(args[1], &sk, MP_BUFFER_READ); @@ -43,7 +41,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_publickey(size_t n_args, const mp_obj } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Nist256p1_publickey_obj, 2, 3, mod_TrezorCrypto_Nist256p1_publickey); -// def Nist256p1.sign(self, secret_key: bytes, message: bytes) -> bytes +/// def trezor.crypto.curve.nist256p1.sign(self, secret_key: bytes, message: bytes) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_sign(mp_obj_t self, mp_obj_t secret_key, mp_obj_t message) { mp_buffer_info_t sk, msg; mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ); @@ -62,7 +60,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_sign(mp_obj_t self, mp_obj_t secret_k } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorCrypto_Nist256p1_sign_obj, mod_TrezorCrypto_Nist256p1_sign); -// def Nist256p1.verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool +/// def trezor.crypto.curve.nist256p1.verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_verify(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t pk, sig, msg; mp_get_buffer_raise(args[1], &pk, MP_BUFFER_READ); @@ -78,8 +76,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_verify(size_t n_args, const mp_obj_t } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Nist256p1_verify_obj, 4, 4, mod_TrezorCrypto_Nist256p1_verify); -// Nist256p1 stuff - STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Nist256p1_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_publickey), MP_ROM_PTR(&mod_TrezorCrypto_Nist256p1_publickey_obj) }, { MP_ROM_QSTR(MP_QSTR_sign), MP_ROM_PTR(&mod_TrezorCrypto_Nist256p1_sign_obj) }, diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h b/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h index 74d9e599a..34befdd43 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h @@ -9,7 +9,6 @@ #include "trezor-crypto/pbkdf2.h" -// class Pbkdf2(object): typedef struct _mp_obj_Pbkdf2_t { mp_obj_base_t base; union { @@ -21,7 +20,7 @@ typedef struct _mp_obj_Pbkdf2_t { STATIC mp_obj_t mod_TrezorCrypto_Pbkdf2_update(mp_obj_t self, mp_obj_t data); -// def Pbkdf2.__init__(self, prf: str, password: bytes, salt: bytes, iterations: int=None) +/// def trezor.crypto.pbkdf2(self, prf: str, password: bytes, salt: bytes, iterations: int=None) -> Pbkdf2 STATIC mp_obj_t mod_TrezorCrypto_Pbkdf2_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 3, 4, false); mp_obj_Pbkdf2_t *o = m_new_obj(mp_obj_Pbkdf2_t); @@ -53,7 +52,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Pbkdf2_make_new(const mp_obj_type_t *type, size return MP_OBJ_FROM_PTR(o); } -// def Pbkdf2.update(self, iterations: int) -> None +/// def Pbkdf2.update(self, iterations: int) -> None STATIC mp_obj_t mod_TrezorCrypto_Pbkdf2_update(mp_obj_t self, mp_obj_t iterations) { mp_obj_Pbkdf2_t *o = MP_OBJ_TO_PTR(self); uint32_t iter = mp_obj_get_int(iterations); @@ -67,7 +66,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Pbkdf2_update(mp_obj_t self, mp_obj_t iteration } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Pbkdf2_update_obj, mod_TrezorCrypto_Pbkdf2_update); -// def Pbkdf2.key(self) -> bytes +/// def Pbkdf2.key(self) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Pbkdf2_key(mp_obj_t self) { mp_obj_Pbkdf2_t *o = MP_OBJ_TO_PTR(self); vstr_t vstr; @@ -83,7 +82,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Pbkdf2_key(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Pbkdf2_key_obj, mod_TrezorCrypto_Pbkdf2_key); -// def Pbkdf2.__del__(self) -> None STATIC mp_obj_t mod_TrezorCrypto_Pbkdf2___del__(mp_obj_t self) { mp_obj_Pbkdf2_t *o = MP_OBJ_TO_PTR(self); memset(&(o->ctx256), 0, sizeof(PBKDF2_HMAC_SHA256_CTX)); @@ -92,8 +90,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Pbkdf2___del__(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Pbkdf2___del___obj, mod_TrezorCrypto_Pbkdf2___del__); -// Pbkdf2 stuff - STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Pbkdf2_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Pbkdf2_update_obj) }, { MP_ROM_QSTR(MP_QSTR_key), MP_ROM_PTR(&mod_TrezorCrypto_Pbkdf2_key_obj) }, diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h b/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h index 2e4cb6927..14231cd28 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h @@ -12,7 +12,6 @@ #define HASH_RIPEMD160_BLOCK_SIZE 64 #define HASH_RIPEMD160_DIGEST_SIZE 20 -// class Ripemd160(object): typedef struct _mp_obj_Ripemd160_t { mp_obj_base_t base; RIPEMD160_CTX ctx; @@ -20,7 +19,7 @@ typedef struct _mp_obj_Ripemd160_t { STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_update(mp_obj_t self, mp_obj_t data); -// def Ripemd160.__init__(self, data: bytes = None) +/// def trezor.crypto.hashlib.ripemd160(self, data: bytes=None) -> Ripemd160 STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_Ripemd160_t *o = m_new_obj(mp_obj_Ripemd160_t); @@ -33,7 +32,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_make_new(const mp_obj_type_t *type, s return MP_OBJ_FROM_PTR(o); } -// def Ripemd160.update(self, data: bytes) -> None +/// def Ripemd160.update(self, data: bytes) -> None 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; @@ -43,7 +42,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_update(mp_obj_t self, mp_obj_t data) } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Ripemd160_update_obj, mod_TrezorCrypto_Ripemd160_update); -// def Ripemd160.digest(self) -> bytes +/// def Ripemd160.digest(self) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_digest(mp_obj_t self) { mp_obj_Ripemd160_t *o = MP_OBJ_TO_PTR(self); vstr_t vstr; @@ -56,7 +55,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Ripemd160_digest(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Ripemd160_digest_obj, mod_TrezorCrypto_Ripemd160_digest); -// def Ripemd160.__del__(self) -> None STATIC mp_obj_t mod_TrezorCrypto_Ripemd160___del__(mp_obj_t self) { mp_obj_Ripemd160_t *o = MP_OBJ_TO_PTR(self); memset(&(o->ctx), 0, sizeof(RIPEMD160_CTX)); @@ -64,8 +62,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Ripemd160___del__(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Ripemd160___del___obj, mod_TrezorCrypto_Ripemd160___del__); -// Ripemd160 stuff - STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Ripemd160_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Ripemd160_update_obj) }, { MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Ripemd160_digest_obj) }, diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h b/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h index 2f5d77358..853f937aa 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h @@ -8,14 +8,12 @@ #include "py/objstr.h" #include "trezor-crypto/ecdsa.h" -#include "trezor-crypto/secp256k1.h" +#include "trezor.crypto.curve.secp256k1.h" -// class Secp256k1(object): typedef struct _mp_obj_Secp256k1_t { mp_obj_base_t base; } mp_obj_Secp256k1_t; -// def Secp256k1.__init__(self) STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 0, false); mp_obj_Secp256k1_t *o = m_new_obj(mp_obj_Secp256k1_t); @@ -23,7 +21,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_make_new(const mp_obj_type_t *type, s return MP_OBJ_FROM_PTR(o); } -// def Secp256k1.publickey(self, secret_key: bytes, compressed: bool=True) -> bytes +/// def trezor.crypto.curve.secp256k1.publickey(self, secret_key: bytes, compressed: bool=True) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_publickey(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t sk; mp_get_buffer_raise(args[1], &sk, MP_BUFFER_READ); @@ -43,7 +41,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_publickey(size_t n_args, const mp_obj } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Secp256k1_publickey_obj, 2, 3, mod_TrezorCrypto_Secp256k1_publickey); -// def Secp256k1.sign(self, secret_key: bytes, message: bytes) -> bytes +/// def trezor.crypto.curve.secp256k1.sign(self, secret_key: bytes, message: bytes) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_sign(mp_obj_t self, mp_obj_t secret_key, mp_obj_t message) { mp_buffer_info_t sk, msg; mp_get_buffer_raise(secret_key, &sk, MP_BUFFER_READ); @@ -62,7 +60,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_sign(mp_obj_t self, mp_obj_t secret_k } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorCrypto_Secp256k1_sign_obj, mod_TrezorCrypto_Secp256k1_sign); -// def Secp256k1.verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool +/// def trezor.crypto.curve.secp256k1.verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_verify(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t pk, sig, msg; mp_get_buffer_raise(args[1], &pk, MP_BUFFER_READ); @@ -78,8 +76,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_verify(size_t n_args, const mp_obj_t } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorCrypto_Secp256k1_verify_obj, 4, 4, mod_TrezorCrypto_Secp256k1_verify); -// Secp256k1 stuff - STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Secp256k1_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_publickey), MP_ROM_PTR(&mod_TrezorCrypto_Secp256k1_publickey_obj) }, { MP_ROM_QSTR(MP_QSTR_sign), MP_ROM_PTR(&mod_TrezorCrypto_Secp256k1_sign_obj) }, diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h b/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h index c60fba62d..438dfcee3 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-sha256.h @@ -12,7 +12,6 @@ #define HASH_SHA256_BLOCK_SIZE 64 #define HASH_SHA256_DIGEST_SIZE 32 -// class Sha256(object): typedef struct _mp_obj_Sha256_t { mp_obj_base_t base; SHA256_CTX ctx; @@ -20,7 +19,7 @@ typedef struct _mp_obj_Sha256_t { STATIC mp_obj_t mod_TrezorCrypto_Sha256_update(mp_obj_t self, mp_obj_t data); -// def Sha256.__init__(self, data: bytes = None) +/// def trezor.crypto.hashlib.sha256(self, data: bytes=None) -> Sha256 STATIC mp_obj_t mod_TrezorCrypto_Sha256_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_Sha256_t *o = m_new_obj(mp_obj_Sha256_t); @@ -33,7 +32,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha256_make_new(const mp_obj_type_t *type, size return MP_OBJ_FROM_PTR(o); } -// def Sha256.update(self, data: bytes) -> None +/// def Sha256.update(self, data: bytes) -> None 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; @@ -43,7 +42,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha256_update(mp_obj_t self, mp_obj_t data) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha256_update_obj, mod_TrezorCrypto_Sha256_update); -// def Sha256.digest(self) -> bytes +/// def Sha256.digest(self) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Sha256_digest(mp_obj_t self) { mp_obj_Sha256_t *o = MP_OBJ_TO_PTR(self); vstr_t vstr; @@ -56,7 +55,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha256_digest(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha256_digest_obj, mod_TrezorCrypto_Sha256_digest); -// def Sha256.__del__(self) -> None STATIC mp_obj_t mod_TrezorCrypto_Sha256___del__(mp_obj_t self) { mp_obj_Sha256_t *o = MP_OBJ_TO_PTR(self); memset(&(o->ctx), 0, sizeof(SHA256_CTX)); @@ -64,8 +62,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha256___del__(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha256___del___obj, mod_TrezorCrypto_Sha256___del__); -// Sha256 stuff - STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Sha256_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Sha256_update_obj) }, { MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Sha256_digest_obj) }, diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h b/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h index df8a54f6a..56162c32b 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h @@ -12,7 +12,6 @@ #define HASH_SHA3_256_BLOCK_SIZE 64 #define HASH_SHA3_256_DIGEST_SIZE 32 -// class Sha3_256(object): typedef struct _mp_obj_Sha3_256_t { mp_obj_base_t base; SHA3_CTX ctx; @@ -20,7 +19,7 @@ typedef struct _mp_obj_Sha3_256_t { STATIC mp_obj_t mod_TrezorCrypto_Sha3_256_update(mp_obj_t self, mp_obj_t data); -// def Sha3_256.__init__(self, data: bytes = None) +/// def trezor.crypto.hashlib.sha3_256(self, data: bytes=None) -> Sha3_256 STATIC mp_obj_t mod_TrezorCrypto_Sha3_256_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_Sha3_256_t *o = m_new_obj(mp_obj_Sha3_256_t); @@ -33,7 +32,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha3_256_make_new(const mp_obj_type_t *type, si return MP_OBJ_FROM_PTR(o); } -// def Sha3_256.update(self, data: bytes) -> None +/// def Sha3_256.update(self, data: bytes) -> None 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; @@ -43,7 +42,7 @@ 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 Sha3_256.digest(self) -> bytes +/// def Sha3_256.digest(self) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Sha3_256_digest(mp_obj_t self) { mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(self); vstr_t vstr; @@ -56,7 +55,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha3_256_digest(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha3_256_digest_obj, mod_TrezorCrypto_Sha3_256_digest); -// def Sha3_256.__del__(self) -> None STATIC mp_obj_t mod_TrezorCrypto_Sha3_256___del__(mp_obj_t self) { mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(self); memset(&(o->ctx), 0, sizeof(SHA3_CTX)); @@ -64,8 +62,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha3_256___del__(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha3_256___del___obj, mod_TrezorCrypto_Sha3_256___del__); -// Sha3_256 stuff - STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Sha3_256_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Sha3_256_update_obj) }, { MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Sha3_256_digest_obj) }, diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h b/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h index 02f1fcebd..2419528ac 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h @@ -12,7 +12,6 @@ #define HASH_SHA3_512_BLOCK_SIZE 128 #define HASH_SHA3_512_DIGEST_SIZE 64 -// class Sha3_512(object): typedef struct _mp_obj_Sha3_512_t { mp_obj_base_t base; SHA3_CTX ctx; @@ -20,7 +19,7 @@ typedef struct _mp_obj_Sha3_512_t { STATIC mp_obj_t mod_TrezorCrypto_Sha3_512_update(mp_obj_t self, mp_obj_t data); -// def Sha3_512.__init__(self, data: bytes = None) +/// def trezor.crypto.hashlib.sha3_512(self, data: bytes=None) -> Sha3_512 STATIC mp_obj_t mod_TrezorCrypto_Sha3_512_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_Sha3_512_t *o = m_new_obj(mp_obj_Sha3_512_t); @@ -33,7 +32,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha3_512_make_new(const mp_obj_type_t *type, si return MP_OBJ_FROM_PTR(o); } -// def Sha3_512.update(self, data: bytes) -> None +/// def Sha3_512.update(self, data: bytes) -> None 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; @@ -43,7 +42,7 @@ 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 Sha3_512.digest(self) -> bytes +/// def Sha3_512.digest(self) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Sha3_512_digest(mp_obj_t self) { mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(self); vstr_t vstr; @@ -56,7 +55,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha3_512_digest(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha3_512_digest_obj, mod_TrezorCrypto_Sha3_512_digest); -// def Sha3_512.__del__(self) -> None STATIC mp_obj_t mod_TrezorCrypto_Sha3_512___del__(mp_obj_t self) { mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(self); memset(&(o->ctx), 0, sizeof(SHA3_CTX)); @@ -64,8 +62,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha3_512___del__(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha3_512___del___obj, mod_TrezorCrypto_Sha3_512___del__); -// Sha3_512 stuff - STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Sha3_512_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Sha3_512_update_obj) }, { MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Sha3_512_digest_obj) }, diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h b/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h index 62db1e3e5..6823cab8e 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-sha512.h @@ -12,7 +12,6 @@ #define HASH_SHA512_BLOCK_SIZE 128 #define HASH_SHA512_DIGEST_SIZE 64 -// class Sha512(object): typedef struct _mp_obj_Sha512_t { mp_obj_base_t base; SHA512_CTX ctx; @@ -20,7 +19,7 @@ typedef struct _mp_obj_Sha512_t { STATIC mp_obj_t mod_TrezorCrypto_Sha512_update(mp_obj_t self, mp_obj_t data); -// def Sha512.__init__(self, data: bytes = None) +/// def trezor.crypto.hashlib.sha512(self, data: bytes=None) -> Sha512 STATIC mp_obj_t mod_TrezorCrypto_Sha512_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_obj_Sha512_t *o = m_new_obj(mp_obj_Sha512_t); @@ -32,7 +31,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha512_make_new(const mp_obj_type_t *type, size return MP_OBJ_FROM_PTR(o); } -// def Sha512.hash(self, data: bytes) -> None +/// def Sha512.hash(self, data: bytes) -> None 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; @@ -42,7 +41,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha512_update(mp_obj_t self, mp_obj_t data) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Sha512_update_obj, mod_TrezorCrypto_Sha512_update); -// def Sha512.digest(self) -> bytes +/// def Sha512.digest(self) -> bytes STATIC mp_obj_t mod_TrezorCrypto_Sha512_digest(mp_obj_t self) { mp_obj_Sha512_t *o = MP_OBJ_TO_PTR(self); vstr_t vstr; @@ -55,7 +54,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha512_digest(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha512_digest_obj, mod_TrezorCrypto_Sha512_digest); -// def Sha512.__del__(self) -> None STATIC mp_obj_t mod_TrezorCrypto_Sha512___del__(mp_obj_t self) { mp_obj_Sha512_t *o = MP_OBJ_TO_PTR(self); memset(&(o->ctx), 0, sizeof(SHA512_CTX)); @@ -63,8 +61,6 @@ STATIC mp_obj_t mod_TrezorCrypto_Sha512___del__(mp_obj_t self) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_TrezorCrypto_Sha512___del___obj, mod_TrezorCrypto_Sha512___del__); -// Sha512 stuff - STATIC const mp_rom_map_elem_t mod_TrezorCrypto_Sha512_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mod_TrezorCrypto_Sha512_update_obj) }, { MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&mod_TrezorCrypto_Sha512_digest_obj) }, diff --git a/extmod/modtrezorcrypto/modtrezorcrypto.c b/extmod/modtrezorcrypto/modtrezorcrypto.c index c200d584a..53de46243 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto.c +++ b/extmod/modtrezorcrypto/modtrezorcrypto.c @@ -25,8 +25,6 @@ #include "modtrezorcrypto-sha3-256.h" #include "modtrezorcrypto-sha3-512.h" -// module stuff - STATIC const mp_rom_map_elem_t mp_module_TrezorCrypto_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorCrypto) }, { MP_ROM_QSTR(MP_QSTR_Ed25519), MP_ROM_PTR(&mod_TrezorCrypto_Ed25519_type) }, diff --git a/extmod/modtrezorcrypto/todo.txt b/extmod/modtrezorcrypto/todo.txt new file mode 100644 index 000000000..8f0d8e012 --- /dev/null +++ b/extmod/modtrezorcrypto/todo.txt @@ -0,0 +1,55 @@ +###trezor.crypto.aes + +AES_CTX = object # AES context + +def aes_encrypt_key128(key: bytes16, iv: bytes16 = None) -> AES_CTX: # context + +def aes_encrypt_key192(key: bytes24, iv: bytes16 = None) -> AES_CTX: # context + +def aes_encrypt_key256(key: bytes32, iv: bytes16 = None) -> AES_CTX: # context + +def aes_decrypt_key128(key: bytes16, iv: bytes16 = None) -> AES_CTX: # context + +def aes_decrypt_key192(key: bytes24, iv: bytes16 = None) -> AES_CTX: # context + +def aes_decrypt_key256(key: bytes32, iv: bytes16 = None) -> AES_CTX: # context + +def aes_ecb_encrypt(ctx: AES_CTX, data: bytes) -> bytes: # encrypted + +def aes_cbc_encrypt(ctx: AES_CTX, data: bytes) -> bytes: # encrypted + +def aes_cfb_encrypt(ctx: AES_CTX, data: bytes) -> bytes: # encrypted + +def aes_ofb_encrypt(ctx: AES_CTX, data: bytes) -> bytes: # encrypted + +def aes_ctr_encrypt(ctx: AES_CTX, data: bytes) -> bytes: # encrypted + +def aes_ecb_decrypt(ctx: AES_CTX, data: bytes) -> bytes: # decrypted + +def aes_cbc_decrypt(ctx: AES_CTX, data: bytes) -> bytes: # decrypted + +def aes_cfb_decrypt(ctx: AES_CTX, data: bytes) -> bytes: # decrypted + +def aes_ofb_decrypt(ctx: AES_CTX, data: bytes) -> bytes: # decrypted + +def aes_ctr_decrypt(ctx: AES_CTX, data: bytes) -> bytes: # decrypted + +###trezor.crypto.hd + +TODO + +###trezor.crypto.mnemonic + +def bip39_generate(strength: int) -> bytes: # sentence + +def bip39_fromdata(data: bytes) -> bytes: # sentence + +def bip39_check(mnemonic: bytes) -> bool: # valid + +def bip39_seed(mnemonic: bytes, passphrase: bytes) -> bytes64: # seed + +###trezor.crypto.reedsolomon + +def encode(data: bytes) -> bytes: # encoded + +def decode(data: bytes) -> bytes: # decoded diff --git a/extmod/modtrezormsg/modtrezormsg.c b/extmod/modtrezormsg/modtrezormsg.c index 21645156e..739e1850c 100644 --- a/extmod/modtrezormsg/modtrezormsg.c +++ b/extmod/modtrezormsg/modtrezormsg.c @@ -24,12 +24,10 @@ #error Unsupported port. Only STMHAL and UNIX ports are supported. #endif -// class Msg(object): typedef struct _mp_obj_Msg_t { mp_obj_base_t base; } mp_obj_Msg_t; -// def Msg.__init__(self) STATIC mp_obj_t mod_TrezorMsg_Msg_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 0, false); msg_init(); @@ -38,7 +36,7 @@ STATIC mp_obj_t mod_TrezorMsg_Msg_make_new(const mp_obj_type_t *type, size_t n_a return MP_OBJ_FROM_PTR(o); } -// def Msg.send(self, message) -> int +/// def trezor.msg.send(self, message) -> int STATIC mp_obj_t mod_TrezorMsg_Msg_send(mp_obj_t self, mp_obj_t message) { mp_buffer_info_t msg; mp_get_buffer_raise(message, &msg, MP_BUFFER_READ); @@ -49,7 +47,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorMsg_Msg_send_obj, mod_TrezorMsg_Msg_s #define TICK_RESOLUTION 1000 -// def Msg.select(self, timeout_us: int) -> None/tuple/bytes +/// def trezor.msg.select(self, timeout_us: int) -> tuple STATIC mp_obj_t mod_TrezorMsg_Msg_select(mp_obj_t self, mp_obj_t timeout_us) { int timeout = mp_obj_get_int(timeout_us); if (timeout < 0) { @@ -84,8 +82,6 @@ STATIC mp_obj_t mod_TrezorMsg_Msg_select(mp_obj_t self, mp_obj_t timeout_us) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorMsg_Msg_select_obj, mod_TrezorMsg_Msg_select); -// Msg stuff - STATIC const mp_rom_map_elem_t mod_TrezorMsg_Msg_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_select), MP_ROM_PTR(&mod_TrezorMsg_Msg_select_obj) }, { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&mod_TrezorMsg_Msg_send_obj) }, @@ -99,8 +95,6 @@ STATIC const mp_obj_type_t mod_TrezorMsg_Msg_type = { .locals_dict = (void*)&mod_TrezorMsg_Msg_locals_dict, }; -// module stuff - STATIC const mp_rom_map_elem_t mp_module_TrezorMsg_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorMsg) }, { MP_ROM_QSTR(MP_QSTR_Msg), MP_ROM_PTR(&mod_TrezorMsg_Msg_type) }, diff --git a/extmod/modtrezorui/modtrezorui-display.h b/extmod/modtrezorui/modtrezorui-display.h index 06ac533aa..2f5da4620 100644 --- a/extmod/modtrezorui/modtrezorui-display.h +++ b/extmod/modtrezorui/modtrezorui-display.h @@ -279,12 +279,10 @@ static void display_raw(uint8_t reg, const uint8_t *data, int datalen) DATAS(data, datalen); } -// class Display(object): typedef struct _mp_obj_Display_t { mp_obj_base_t base; } mp_obj_Display_t; -// def Display.__init__(self) STATIC mp_obj_t mod_TrezorUi_Display_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 0, false); display_init(); @@ -293,7 +291,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_make_new(const mp_obj_type_t *type, size_t return MP_OBJ_FROM_PTR(o); } -// def Display.bar(self, x: int, y: int, w: int, h: int, fgcolor: int, bgcolor: int=None) -> None +/// def trezor.ui.display.bar(self, x: int, y: int, w: int, h: int, fgcolor: int, bgcolor: int=None) -> None STATIC mp_obj_t mod_TrezorUi_Display_bar(size_t n_args, const mp_obj_t *args) { mp_int_t x = mp_obj_get_int(args[1]); mp_int_t y = mp_obj_get_int(args[2]); @@ -313,7 +311,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_bar(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_bar_obj, 6, 7, mod_TrezorUi_Display_bar); -// def Display.blit(self, x: int, y: int, w: int, h: int, data: bytes) -> None +/// def trezor.ui.display.blit(self, x: int, y: int, w: int, h: int, data: bytes) -> None STATIC mp_obj_t mod_TrezorUi_Display_blit(size_t n_args, const mp_obj_t *args) { mp_int_t x = mp_obj_get_int(args[1]); mp_int_t y = mp_obj_get_int(args[2]); @@ -329,7 +327,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_blit(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_blit_obj, 6, 6, mod_TrezorUi_Display_blit); -// def Display.image(self, x: int, y: int, image: bytes) -> None +/// def trezor.ui.display.image(self, x: int, y: int, image: bytes) -> None STATIC mp_obj_t mod_TrezorUi_Display_image(size_t n_args, const mp_obj_t *args) { mp_int_t x = mp_obj_get_int(args[1]); mp_int_t y = mp_obj_get_int(args[2]); @@ -353,7 +351,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_image(size_t n_args, const mp_obj_t *args) } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_image_obj, 4, 4, mod_TrezorUi_Display_image); -// def Display.icon(self, x: int, y: int, icon: bytes, fgcolor: int, bgcolor: int) -> None +/// def trezor.ui.display.icon(self, x: int, y: int, icon: bytes, fgcolor: int, bgcolor: int) -> None STATIC mp_obj_t mod_TrezorUi_Display_icon(size_t n_args, const mp_obj_t *args) { mp_int_t x = mp_obj_get_int(args[1]); mp_int_t y = mp_obj_get_int(args[2]); @@ -379,7 +377,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_icon(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_icon_obj, 6, 6, mod_TrezorUi_Display_icon); -// def Display.text(self, x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None +/// def trezor.ui.display.text(self, x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None STATIC mp_obj_t mod_TrezorUi_Display_text(size_t n_args, const mp_obj_t *args) { mp_int_t x = mp_obj_get_int(args[1]); mp_int_t y = mp_obj_get_int(args[2]); @@ -393,7 +391,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_text(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_text_obj, 7, 7, mod_TrezorUi_Display_text); -// def Display.text_center(self, x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None +/// def trezor.ui.display.text_center(self, x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None STATIC mp_obj_t mod_TrezorUi_Display_text_center(size_t n_args, const mp_obj_t *args) { mp_int_t x = mp_obj_get_int(args[1]); mp_int_t y = mp_obj_get_int(args[2]); @@ -408,7 +406,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_text_center(size_t n_args, const mp_obj_t * } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_text_center_obj, 7, 7, mod_TrezorUi_Display_text_center); -// def Display.text_right(self, x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None +/// def trezor.ui.display.text_right(self, x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None STATIC mp_obj_t mod_TrezorUi_Display_text_right(size_t n_args, const mp_obj_t *args) { mp_int_t x = mp_obj_get_int(args[1]); mp_int_t y = mp_obj_get_int(args[2]); @@ -423,7 +421,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_text_right(size_t n_args, const mp_obj_t *a } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_text_right_obj, 7, 7, mod_TrezorUi_Display_text_right); -// def Display.text_width(self, text: bytes, font: int) -> int +/// def trezor.ui.display.text_width(self, text: bytes, font: int) -> int STATIC mp_obj_t mod_TrezorUi_Display_text_width(mp_obj_t self, mp_obj_t text, mp_obj_t font) { mp_buffer_info_t txt; mp_get_buffer_raise(text, &txt, MP_BUFFER_READ); @@ -433,7 +431,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_text_width(mp_obj_t self, mp_obj_t text, mp } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorUi_Display_text_width_obj, mod_TrezorUi_Display_text_width); -// def Display.qrcode(self, x: int, y: int, data: bytes, scale: int) -> None +/// def trezor.ui.display.qrcode(self, x: int, y: int, data: bytes, scale: int) -> None STATIC mp_obj_t mod_TrezorUi_Display_qrcode(size_t n_args, const mp_obj_t *args) { mp_int_t x = mp_obj_get_int(args[1]); mp_int_t y = mp_obj_get_int(args[2]); @@ -455,7 +453,7 @@ static void inflate_callback_loader(uint8_t byte, uint32_t pos, void *userdata) out[pos] = byte; } -// def Display.loader(self, progress: int, fgcolor: int, bgcolor: int, icon: bytes=None, iconfgcolor: int=None) -> None +/// def trezor.ui.display.loader(self, progress: int, fgcolor: int, bgcolor: int, icon: bytes=None, iconfgcolor: int=None) -> None STATIC mp_obj_t mod_TrezorUi_Display_loader(size_t n_args, const mp_obj_t *args) { mp_int_t progress = mp_obj_get_int(args[1]); mp_int_t fgcolor = mp_obj_get_int(args[2]); @@ -492,7 +490,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_loader(size_t n_args, const mp_obj_t *args) } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_loader_obj, 4, 6, mod_TrezorUi_Display_loader); -// def Display.orientation(self, degrees: int) -> None +/// def trezor.ui.display.orientation(self, degrees: int) -> None STATIC mp_obj_t mod_TrezorUi_Display_orientation(mp_obj_t self, mp_obj_t degrees) { mp_int_t deg = mp_obj_get_int(degrees); if (deg != 0 && deg != 90 && deg != 180 && deg != 270) { @@ -503,7 +501,7 @@ STATIC mp_obj_t mod_TrezorUi_Display_orientation(mp_obj_t self, mp_obj_t degrees } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorUi_Display_orientation_obj, mod_TrezorUi_Display_orientation); -// def Display.raw(self, reg: int, data: bytes) -> None +/// def trezor.ui.display.raw(self, reg: int, data: bytes) -> None STATIC mp_obj_t mod_TrezorUi_Display_raw(mp_obj_t self, mp_obj_t reg, mp_obj_t data) { mp_int_t r = mp_obj_get_int(reg); mp_buffer_info_t raw; @@ -513,15 +511,13 @@ STATIC mp_obj_t mod_TrezorUi_Display_raw(mp_obj_t self, mp_obj_t reg, mp_obj_t d } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorUi_Display_raw_obj, mod_TrezorUi_Display_raw); -// def Display.backlight(self, val: int) -> None +/// def trezor.ui.display.backlight(self, val: int) -> None STATIC mp_obj_t mod_TrezorUi_Display_backlight(mp_obj_t self, mp_obj_t reg) { display_backlight(mp_obj_get_int(reg)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorUi_Display_backlight_obj, mod_TrezorUi_Display_backlight); -// Display stuff - STATIC const mp_rom_map_elem_t mod_TrezorUi_Display_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_bar), MP_ROM_PTR(&mod_TrezorUi_Display_bar_obj) }, { MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(&mod_TrezorUi_Display_blit_obj) }, diff --git a/extmod/modtrezorui/modtrezorui.c b/extmod/modtrezorui/modtrezorui.c index 76064da44..c1fafc284 100644 --- a/extmod/modtrezorui/modtrezorui.c +++ b/extmod/modtrezorui/modtrezorui.c @@ -28,8 +28,6 @@ #include "modtrezorui-display.h" -// module stuff - STATIC const mp_rom_map_elem_t mp_module_TrezorUi_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorUi) }, { MP_ROM_QSTR(MP_QSTR_Display), MP_ROM_PTR(&mod_TrezorUi_Display_type) }, diff --git a/extmod/modtrezorutils/modtrezorutils.c b/extmod/modtrezorutils/modtrezorutils.c index 938240a7d..c457348b3 100644 --- a/extmod/modtrezorutils/modtrezorutils.c +++ b/extmod/modtrezorutils/modtrezorutils.c @@ -16,12 +16,10 @@ #if MICROPY_PY_TREZORUTILS -// class Utils(object): typedef struct _mp_obj_Utils_t { mp_obj_base_t base; } mp_obj_Utils_t; -// def Utils.__init__(self) STATIC mp_obj_t mod_TrezorUtils_Utils_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 0, false); mp_obj_Utils_t *o = m_new_obj(mp_obj_Utils_t); @@ -29,7 +27,7 @@ STATIC mp_obj_t mod_TrezorUtils_Utils_make_new(const mp_obj_type_t *type, size_t return MP_OBJ_FROM_PTR(o); } -// def Utils.memaccess(self, address: int, length: int) -> bytes +/// def trezor.utils.memaccess(self, address: int, length: int) -> bytes STATIC mp_obj_t mod_TrezorUtils_Utils_memaccess(mp_obj_t self, mp_obj_t address, mp_obj_t length) { uint32_t addr = mp_obj_get_int(address); uint32_t len = mp_obj_get_int(length); @@ -42,8 +40,6 @@ STATIC mp_obj_t mod_TrezorUtils_Utils_memaccess(mp_obj_t self, mp_obj_t address, } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorUtils_Utils_memaccess_obj, mod_TrezorUtils_Utils_memaccess); -// Utils stuff - STATIC const mp_rom_map_elem_t mod_TrezorUtils_Utils_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_memaccess), MP_ROM_PTR(&mod_TrezorUtils_Utils_memaccess_obj) }, }; @@ -56,8 +52,6 @@ STATIC const mp_obj_type_t mod_TrezorUtils_Utils_type = { .locals_dict = (void*)&mod_TrezorUtils_Utils_locals_dict, }; -// module stuff - STATIC const mp_rom_map_elem_t mp_module_TrezorUtils_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorUtils) }, { MP_ROM_QSTR(MP_QSTR_Utils), MP_ROM_PTR(&mod_TrezorUtils_Utils_type) }, diff --git a/src/trezor/crypto/base58.py b/src/trezor/crypto/base58.py index 3c1cd0091..4956cc17d 100644 --- a/src/trezor/crypto/base58.py +++ b/src/trezor/crypto/base58.py @@ -16,34 +16,34 @@ from .hashlib import sha256 # 58 character alphabet used -alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' +_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' -def encode(v): - origlen = len(v) - v = v.lstrip(b'\0') - newlen = len(v) +def encode(data: bytes) -> str: + origlen = len(data) + data = data.lstrip(b'\0') + newlen = len(data) p, acc = 1, 0 - for c in reversed(v): + for c in reversed(data): acc += p * c p = p << 8 result = '' while acc > 0: acc, mod = divmod(acc, 58) - result += alphabet[mod] + result += _alphabet[mod] - return ''.join([c for c in reversed(result + alphabet[0] * (origlen - newlen))]) + return ''.join([c for c in reversed(result + _alphabet[0] * (origlen - newlen))]) -def decode(v): - origlen = len(v) - v = v.lstrip(alphabet[0]) - newlen = len(v) +def decode(string: str) -> bytes: + origlen = len(string) + string = string.lstrip(_alphabet[0]) + newlen = len(string) p, acc = 1, 0 - for c in reversed(v): - acc += p * alphabet.index(c) + for c in reversed(string): + acc += p * _alphabet.index(c) p *= 58 result = [] @@ -54,13 +54,13 @@ def decode(v): return bytes([b for b in reversed(result +[0] * (origlen - newlen))]) -def encode_check(v): - digest = sha256(sha256(v).digest()).digest() - return encode(v + digest[:4]) +def encode_check(data: bytes) -> str: + digest = sha256(sha256(data).digest()).digest() + return encode(data + digest[:4]) -def decode_check(v): - result = decode(v) +def decode_check(string: str) -> bytes: + result = decode(string) result, check = result[:-4], result[-4:] digest = sha256(sha256(result).digest()).digest() diff --git a/src/trezor/crypto/hmac.py b/src/trezor/crypto/hmac.py index 76f7b1ab0..bf79a9965 100644 --- a/src/trezor/crypto/hmac.py +++ b/src/trezor/crypto/hmac.py @@ -11,14 +11,14 @@ class Hmac: if msg is not None: self.update(msg) - def update(self, msg): + def update(self, msg: bytes) -> None: self.__inner.update(msg) - def digest(self): + def digest(self) -> bytes: outer = self.__digestmod() outer.update(bytes((x ^ 0x5C) for x in self.__key)) outer.update(self.__inner.digest()) return outer.digest() -def new(key, msg, digestmod): +def new(key, msg, digestmod) -> Hmac: return Hmac(key, msg, digestmod)