From eea88f9bae2d0ed9260f1022e81e537bf5cf6030 Mon Sep 17 00:00:00 2001 From: slush0 Date: Tue, 27 Sep 2016 16:48:21 +0200 Subject: [PATCH] Mocks generated from Core's C modules --- mocks/trezor/.mock-generated | 0 mocks/trezor/__init__.py | 0 mocks/trezor/config.py | 13 +++ mocks/trezor/crypto.py | 4 + mocks/trezor/crypto/.mock-generated | 0 mocks/trezor/crypto/__init__.py | 0 mocks/trezor/crypto/aes/.mock-generated | 0 mocks/trezor/crypto/aes/AES.py | 6 ++ mocks/trezor/crypto/aes/__init__.py | 0 mocks/trezor/crypto/bip39.py | 24 ++++++ mocks/trezor/crypto/curve/.mock-generated | 0 mocks/trezor/crypto/curve/__init__.py | 0 mocks/trezor/crypto/curve/ed25519.py | 19 +++++ mocks/trezor/crypto/curve/nist256p1.py | 19 +++++ mocks/trezor/crypto/curve/secp256k1.py | 19 +++++ mocks/trezor/crypto/hashlib.py | 30 +++++++ mocks/trezor/crypto/hashlib/.mock-generated | 0 mocks/trezor/crypto/hashlib/Ripemd160.py | 12 +++ mocks/trezor/crypto/hashlib/Sha256.py | 12 +++ mocks/trezor/crypto/hashlib/Sha3_256.py | 12 +++ mocks/trezor/crypto/hashlib/Sha3_512.py | 12 +++ mocks/trezor/crypto/hashlib/Sha512.py | 12 +++ mocks/trezor/crypto/hashlib/__init__.py | 0 mocks/trezor/crypto/pbkdf2/.mock-generated | 0 mocks/trezor/crypto/pbkdf2/Pbkdf2.py | 8 ++ mocks/trezor/crypto/pbkdf2/__init__.py | 0 mocks/trezor/crypto/random.py | 18 ++++ mocks/trezor/crypto/ssss.py | 12 +++ mocks/trezor/debug.py | 6 ++ mocks/trezor/msg.py | 19 +++++ mocks/trezor/ui/.mock-generated | 0 mocks/trezor/ui/__init__.py | 0 mocks/trezor/ui/display.py | 92 +++++++++++++++++++++ 33 files changed, 349 insertions(+) create mode 100644 mocks/trezor/.mock-generated create mode 100644 mocks/trezor/__init__.py create mode 100644 mocks/trezor/config.py create mode 100644 mocks/trezor/crypto.py create mode 100644 mocks/trezor/crypto/.mock-generated create mode 100644 mocks/trezor/crypto/__init__.py create mode 100644 mocks/trezor/crypto/aes/.mock-generated create mode 100644 mocks/trezor/crypto/aes/AES.py create mode 100644 mocks/trezor/crypto/aes/__init__.py create mode 100644 mocks/trezor/crypto/bip39.py create mode 100644 mocks/trezor/crypto/curve/.mock-generated create mode 100644 mocks/trezor/crypto/curve/__init__.py create mode 100644 mocks/trezor/crypto/curve/ed25519.py create mode 100644 mocks/trezor/crypto/curve/nist256p1.py create mode 100644 mocks/trezor/crypto/curve/secp256k1.py create mode 100644 mocks/trezor/crypto/hashlib.py create mode 100644 mocks/trezor/crypto/hashlib/.mock-generated create mode 100644 mocks/trezor/crypto/hashlib/Ripemd160.py create mode 100644 mocks/trezor/crypto/hashlib/Sha256.py create mode 100644 mocks/trezor/crypto/hashlib/Sha3_256.py create mode 100644 mocks/trezor/crypto/hashlib/Sha3_512.py create mode 100644 mocks/trezor/crypto/hashlib/Sha512.py create mode 100644 mocks/trezor/crypto/hashlib/__init__.py create mode 100644 mocks/trezor/crypto/pbkdf2/.mock-generated create mode 100644 mocks/trezor/crypto/pbkdf2/Pbkdf2.py create mode 100644 mocks/trezor/crypto/pbkdf2/__init__.py create mode 100644 mocks/trezor/crypto/random.py create mode 100644 mocks/trezor/crypto/ssss.py create mode 100644 mocks/trezor/debug.py create mode 100644 mocks/trezor/msg.py create mode 100644 mocks/trezor/ui/.mock-generated create mode 100644 mocks/trezor/ui/__init__.py create mode 100644 mocks/trezor/ui/display.py diff --git a/mocks/trezor/.mock-generated b/mocks/trezor/.mock-generated new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/__init__.py b/mocks/trezor/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/config.py b/mocks/trezor/config.py new file mode 100644 index 00000000..d9dc6f5d --- /dev/null +++ b/mocks/trezor/config.py @@ -0,0 +1,13 @@ + +# ../extmod/modtrezorconfig/modtrezorconfig.c +def get(app: int, key: int) -> bytes: + ''' + Gets a value of given key for given app (or None if not set). + ''' + +# ../extmod/modtrezorconfig/modtrezorconfig.c +def set(app: int, key: int) -> bool: + ''' + Sets a value of given key for given app. + Returns True on success. + ''' diff --git a/mocks/trezor/crypto.py b/mocks/trezor/crypto.py new file mode 100644 index 00000000..1e64f9ca --- /dev/null +++ b/mocks/trezor/crypto.py @@ -0,0 +1,4 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h +def pbkdf2(prf: str, password: bytes, salt: bytes, iterations: int=None) -> Pbkdf2: + pass diff --git a/mocks/trezor/crypto/.mock-generated b/mocks/trezor/crypto/.mock-generated new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/crypto/__init__.py b/mocks/trezor/crypto/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/crypto/aes/.mock-generated b/mocks/trezor/crypto/aes/.mock-generated new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/crypto/aes/AES.py b/mocks/trezor/crypto/aes/AES.py new file mode 100644 index 00000000..163c28d2 --- /dev/null +++ b/mocks/trezor/crypto/aes/AES.py @@ -0,0 +1,6 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-aes.h +def update(self, data: bytes) -> bytes: + ''' + Update AES context + ''' diff --git a/mocks/trezor/crypto/aes/__init__.py b/mocks/trezor/crypto/aes/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/crypto/bip39.py b/mocks/trezor/crypto/bip39.py new file mode 100644 index 00000000..9a63bdb8 --- /dev/null +++ b/mocks/trezor/crypto/bip39.py @@ -0,0 +1,24 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-bip39.h +def generate(strength: int) -> str: + ''' + Generate a mnemonic of given strength (128, 160, 192, 224 and 256 bits) + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-bip39.h +def from_data(data: bytes) -> str: + ''' + Generate a mnemonic from given data (of 16, 20, 24, 28 and 32 bytes) + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-bip39.h +def check(mnemonic: str) -> bool: + ''' + Check whether given mnemonic is valid + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-bip39.h +def seed(mnemonic: str, passphrase: str) -> bytes: + ''' + Generate seed from mnemonic and passphrase + ''' diff --git a/mocks/trezor/crypto/curve/.mock-generated b/mocks/trezor/crypto/curve/.mock-generated new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/crypto/curve/__init__.py b/mocks/trezor/crypto/curve/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/crypto/curve/ed25519.py b/mocks/trezor/crypto/curve/ed25519.py new file mode 100644 index 00000000..8d1a596e --- /dev/null +++ b/mocks/trezor/crypto/curve/ed25519.py @@ -0,0 +1,19 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h +def publickey(secret_key: bytes) -> bytes: + ''' + Computes public key from secret key. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h +def sign(secret_key: bytes, message: bytes) -> bytes: + ''' + Uses secret key to produce the signature of message. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h +def verify(public_key: bytes, signature: bytes, message: bytes) -> bool: + ''' + Uses public key to verify the signature of the message. + Returns True on success. + ''' diff --git a/mocks/trezor/crypto/curve/nist256p1.py b/mocks/trezor/crypto/curve/nist256p1.py new file mode 100644 index 00000000..fe71a895 --- /dev/null +++ b/mocks/trezor/crypto/curve/nist256p1.py @@ -0,0 +1,19 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h +def publickey(secret_key: bytes, compressed: bool=True) -> bytes: + ''' + Computes public key from secret key. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h +def sign(secret_key: bytes, message: bytes) -> bytes: + ''' + Uses secret key to produce the signature of message. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h +def verify(public_key: bytes, signature: bytes, message: bytes) -> bool: + ''' + Uses public key to verify the signature of the message + Returns True on success. + ''' diff --git a/mocks/trezor/crypto/curve/secp256k1.py b/mocks/trezor/crypto/curve/secp256k1.py new file mode 100644 index 00000000..d520e1d6 --- /dev/null +++ b/mocks/trezor/crypto/curve/secp256k1.py @@ -0,0 +1,19 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h +def publickey(secret_key: bytes, compressed: bool=True) -> bytes: + ''' + Computes public key from secret key. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h +def sign(secret_key: bytes, message: bytes) -> bytes: + ''' + Uses secret key to produce the signature of message. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h +def verify(public_key: bytes, signature: bytes, message: bytes) -> bool: + ''' + Uses public key to verify the signature of the message + Returns True on success. + ''' diff --git a/mocks/trezor/crypto/hashlib.py b/mocks/trezor/crypto/hashlib.py new file mode 100644 index 00000000..12b76db0 --- /dev/null +++ b/mocks/trezor/crypto/hashlib.py @@ -0,0 +1,30 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h +def ripemd160(data: bytes=None) -> Ripemd160: + ''' + Creates a hash context object. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h +def sha3_512(data: bytes=None) -> Sha3_512: + ''' + Creates a hash context object. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha512.h +def sha512(data: bytes=None) -> Sha512: + ''' + Creates a hash context object. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha256.h +def sha256(data: bytes=None) -> Sha256: + ''' + Creates a hash context object. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h +def sha3_256(data: bytes=None) -> Sha3_256: + ''' + Creates a hash context object. + ''' diff --git a/mocks/trezor/crypto/hashlib/.mock-generated b/mocks/trezor/crypto/hashlib/.mock-generated new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/crypto/hashlib/Ripemd160.py b/mocks/trezor/crypto/hashlib/Ripemd160.py new file mode 100644 index 00000000..6c3b7a48 --- /dev/null +++ b/mocks/trezor/crypto/hashlib/Ripemd160.py @@ -0,0 +1,12 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h +def update(self, data: bytes) -> None: + ''' + Update the hash context with hashed data. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-ripemd160.h +def digest(self) -> bytes: + ''' + Returns the digest of hashed data. + ''' diff --git a/mocks/trezor/crypto/hashlib/Sha256.py b/mocks/trezor/crypto/hashlib/Sha256.py new file mode 100644 index 00000000..af1cd862 --- /dev/null +++ b/mocks/trezor/crypto/hashlib/Sha256.py @@ -0,0 +1,12 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha256.h +def update(self, data: bytes) -> None: + ''' + Update the hash context with hashed data. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha256.h +def digest(self) -> bytes: + ''' + Returns the digest of hashed data. + ''' diff --git a/mocks/trezor/crypto/hashlib/Sha3_256.py b/mocks/trezor/crypto/hashlib/Sha3_256.py new file mode 100644 index 00000000..fef5ce66 --- /dev/null +++ b/mocks/trezor/crypto/hashlib/Sha3_256.py @@ -0,0 +1,12 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h +def update(self, data: bytes) -> None: + ''' + Update the hash context with hashed data. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha3-256.h +def digest(self) -> bytes: + ''' + Returns the digest of hashed data. + ''' diff --git a/mocks/trezor/crypto/hashlib/Sha3_512.py b/mocks/trezor/crypto/hashlib/Sha3_512.py new file mode 100644 index 00000000..ba592a9b --- /dev/null +++ b/mocks/trezor/crypto/hashlib/Sha3_512.py @@ -0,0 +1,12 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h +def update(self, data: bytes) -> None: + ''' + Update the hash context with hashed data. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha3-512.h +def digest(self) -> bytes: + ''' + Returns the digest of hashed data. + ''' diff --git a/mocks/trezor/crypto/hashlib/Sha512.py b/mocks/trezor/crypto/hashlib/Sha512.py new file mode 100644 index 00000000..2343620b --- /dev/null +++ b/mocks/trezor/crypto/hashlib/Sha512.py @@ -0,0 +1,12 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha512.h +def hash(self, data: bytes) -> None: + ''' + Update the hash context with hashed data. + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-sha512.h +def digest(self) -> bytes: + ''' + Returns the digest of hashed data. + ''' diff --git a/mocks/trezor/crypto/hashlib/__init__.py b/mocks/trezor/crypto/hashlib/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/crypto/pbkdf2/.mock-generated b/mocks/trezor/crypto/pbkdf2/.mock-generated new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/crypto/pbkdf2/Pbkdf2.py b/mocks/trezor/crypto/pbkdf2/Pbkdf2.py new file mode 100644 index 00000000..bfa9dec9 --- /dev/null +++ b/mocks/trezor/crypto/pbkdf2/Pbkdf2.py @@ -0,0 +1,8 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h +def update(self, iterations: int) -> None: + pass + +# ../extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h +def key(self) -> bytes: + pass diff --git a/mocks/trezor/crypto/pbkdf2/__init__.py b/mocks/trezor/crypto/pbkdf2/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/crypto/random.py b/mocks/trezor/crypto/random.py new file mode 100644 index 00000000..f6a05e08 --- /dev/null +++ b/mocks/trezor/crypto/random.py @@ -0,0 +1,18 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-random.h +def uniform(n: int) -> int: + ''' + Compute uniform random number from interval 0 ... n - 1 + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-random.h +def bytes(len: int) -> bytes: + ''' + Generate random bytes sequence of length len + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-random.h +def shuffle(data: list) -> None: + ''' + Shuffles items of given list (in-place) + ''' diff --git a/mocks/trezor/crypto/ssss.py b/mocks/trezor/crypto/ssss.py new file mode 100644 index 00000000..8870e8a6 --- /dev/null +++ b/mocks/trezor/crypto/ssss.py @@ -0,0 +1,12 @@ + +# ../extmod/modtrezorcrypto/modtrezorcrypto-ssss.h +def split(m: int, n: int, secret: bytes) -> tuple: + ''' + Split secret to (M of N) shares using Shamir's Secret Sharing Scheme + ''' + +# ../extmod/modtrezorcrypto/modtrezorcrypto-ssss.h +def combine(shares: tuple) -> bytes: + ''' + Combine M shares of Shamir's Secret Sharing Scheme into secret + ''' diff --git a/mocks/trezor/debug.py b/mocks/trezor/debug.py new file mode 100644 index 00000000..ef3ef38b --- /dev/null +++ b/mocks/trezor/debug.py @@ -0,0 +1,6 @@ + +# ../extmod/modtrezordebug/modtrezordebug.c +def memaccess(address: int, length: int) -> bytes: + ''' + Creates a bytes object that can be used to access certain memory location. + ''' diff --git a/mocks/trezor/msg.py b/mocks/trezor/msg.py new file mode 100644 index 00000000..7b175195 --- /dev/null +++ b/mocks/trezor/msg.py @@ -0,0 +1,19 @@ + +# ../extmod/modtrezormsg/modtrezormsg.c +def setup(ifaces: list) -> None: + ''' + Configures USB interfaces with a list of tuples (interface_number, usage_page) + ''' + +# ../extmod/modtrezormsg/modtrezormsg.c +def send(iface: int, message: bytes) -> int: + ''' + Sends message using USB HID (device) or UDP (emulator). + ''' + +# ../extmod/modtrezormsg/modtrezormsg.c +def select(timeout_us: int) -> tuple: + ''' + Polls the event queue and returns the event object. + Function returns None if timeout specified in microseconds is reached. + ''' diff --git a/mocks/trezor/ui/.mock-generated b/mocks/trezor/ui/.mock-generated new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/ui/__init__.py b/mocks/trezor/ui/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/mocks/trezor/ui/display.py b/mocks/trezor/ui/display.py new file mode 100644 index 00000000..9225549e --- /dev/null +++ b/mocks/trezor/ui/display.py @@ -0,0 +1,92 @@ + +# ../extmod/modtrezorui/modtrezorui-display.h +def bar(x: int, y: int, w: int, h: int, fgcolor: int, bgcolor: int=None, radius: int=None) -> None: + ''' + Renders a bar at position (x,y = upper left corner) with width w and height h of color fgcolor. + When a bgcolor is set, the bar is drawn with rounded corners and bgcolor is used for background. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def blit(x: int, y: int, w: int, h: int, data: bytes) -> None: + ''' + Renders rectangle at position (x,y = upper left corner) with width w and height h with data. + The data needs to have the correct format. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def image(x: int, y: int, image: bytes) -> None: + ''' + Renders an image at position (x,y). + The image needs to be in TREZOR Optimized Image Format (TOIF) - full-color mode. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def icon(x: int, y: int, icon: bytes, fgcolor: int, bgcolor: int) -> None: + ''' + Renders an icon at position (x,y), fgcolor is used as foreground color, bgcolor as background. + The image needs to be in TREZOR Optimized Image Format (TOIF) - gray-scale mode. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def text(x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None: + ''' + Renders left-aligned text at position (x,y) where x is left position and y is baseline. + Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def text_center(x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None: + ''' + Renders text centered at position (x,y) where x is text center and y is baseline. + Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def text_right(x: int, y: int, text: bytes, font: int, fgcolor: int, bgcolor: int) -> None: + ''' + Renders right-aligned text at position (x,y) where x is right position and y is baseline. + Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def text_width(text: bytes, font: int) -> int: + ''' + Returns a width of text in pixels. Font font is used for rendering. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def qrcode(x: int, y: int, data: bytes, scale: int) -> None: + ''' + Renders data encoded as a QR code at position (x,y). + Scale determines a zoom factor. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def loader(progress: int, fgcolor: int, bgcolor: int, icon: bytes=None, iconfgcolor: int=None) -> None: + ''' + Renders a rotating loader graphic. + Progress determines its position (0-1000), fgcolor is used as foreground color, bgcolor as background. + When icon and iconfgcolor are provided, an icon is drawn in the middle using the color specified in iconfgcolor. + Icon needs to be of exaclty 96x96 pixels size. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def orientation(degrees: int=None) -> int: + ''' + Sets display orientation to 0, 90, 180 or 270 degrees. + Everything needs to be redrawn again when this function is used. + Call without the degrees parameter to just perform the read of the value. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def backlight(val: int=None) -> int: + ''' + Sets backlight intensity to the value specified in val. + Call without the val parameter to just perform the read of the value. + ''' + +# ../extmod/modtrezorui/modtrezorui-display.h +def raw(reg: int, data: bytes) -> None: + ''' + Performs a raw command on the display. Read the datasheet to learn more. + '''