{"id":13801398,"url":"https://github.com/dmazzella/ucrypto","last_synced_at":"2026-01-18T10:48:37.991Z","repository":{"id":68705940,"uuid":"365288224","full_name":"dmazzella/ucrypto","owner":"dmazzella","description":"Micropython package for doing fast rsa and elliptic curve cryptography, specifically digital signatures","archived":false,"fork":false,"pushed_at":"2025-01-28T12:07:41.000Z","size":251,"stargazers_count":36,"open_issues_count":0,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-28T13:28:04.390Z","etag":null,"topics":["cryptography","elliptic-curves","micropython","rsa"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dmazzella.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-07T16:10:35.000Z","updated_at":"2025-01-28T12:07:44.000Z","dependencies_parsed_at":"2024-06-17T22:21:14.291Z","dependency_job_id":"596cc190-f72e-4ea2-aeaa-95e8c6f37ecb","html_url":"https://github.com/dmazzella/ucrypto","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmazzella%2Fucrypto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmazzella%2Fucrypto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmazzella%2Fucrypto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmazzella%2Fucrypto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmazzella","download_url":"https://codeload.github.com/dmazzella/ucrypto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253932819,"owners_count":21986454,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","elliptic-curves","micropython","rsa"],"created_at":"2024-08-04T00:01:22.421Z","updated_at":"2026-01-18T10:48:37.629Z","avatar_url":"https://github.com/dmazzella.png","language":"C","readme":"# Description\n\n**Micropython package for doing fast rsa and elliptic curve cryptography, specifically digital signatures.\nECDSA API design inspired from [fastecdsa](https://github.com/AntonKueltz/fastecdsa) and implementation based on [tomsfastmath](https://github.com/libtom/tomsfastmath).**\n\n\u003e [!TIP]\n\u003e If you find **ucrypto** useful, consider :star: this project\n\u003e and why not ... [Buy me a coffee](https://www.buymeacoffee.com/damianomazp) :smile:\n\n## Examples\n\n- Signing and Verifying **ufastrsa**\n    ```python\n    from ufastrsa.rsa import RSA, genrsa\n\n\n    def main():\n\n        bits = 1024\n        print(\"RSA bits\", bits)\n        r = RSA(*genrsa(bits, e=65537))\n        if r:\n            print(\"RSA OK\")\n            data = b\"a message to sign and encrypt via RSA\"\n            print(\"random data len:\", len(data), data)\n            assert r.pkcs_verify(r.pkcs_sign(data)) == data\n            print(\"pkcs_verify OK\")\n            assert r.pkcs_decrypt(r.pkcs_encrypt(data)) == data\n            print(\"pkcs_decrypt OK\")\n\n\n    if __name__ == \"__main__\":\n        main()\n    ```\n\n- Signing and Verifying **ufastecdsa**\n    ```python\n    try:\n        from ufastecdsa import curve, ecdsa, keys, util\n\n        get_bit_length = util.get_bit_length\n    except ImportError:\n        from fastecdsa import curve, ecdsa, keys, util\n\n        get_bit_length = int.bit_length\n\n\n    def main():\n\n        # private_key = 82378264402520040413352233063555671940555718680152892238371187003380781159101\n        # public_key = keys.get_public_key(private_key, curve.P256)\n\n        private_key, public_key = keys.gen_keypair(curve.P256)\n        print(\"private_key:\", private_key)\n        print(\"public_key:\", public_key.x, public_key.y, public_key.curve.name)\n\n        m = \"a message to sign via ECDSA\"\n\n        r, s = ecdsa.sign(m, private_key)\n\n        print(\"R:\", r)\n        print(\"S:\", s)\n\n        verified = ecdsa.verify((r, s), m, public_key)\n        print(verified)\n\n\n    if __name__ == \"__main__\":\n        main()\n    ```\n\n - Arbitrary Elliptic Curve Arithmetic\n    ```python\n    from _crypto import ECC\n\n    P256 = ECC.Curve(\n        0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff,\n        -0x3,\n        0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b,\n        0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551,\n        0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296,\n        0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5\n    )\n\n    S = ECC.Point(\n        0xde2444bebc8d36e682edd27e0f271508617519b3221a8fa0b77cab3989da97c9,\n        0xc093ae7ff36e5380fc01a5aad1e66659702de80f53cec576b6350b243042a256,\n        P256\n    )\n\n    T = ECC.Point(\n        0x55a8b00f8da1d44e62f6b3b25316212e39540dc861c89575bb8cf92e35e0986b,\n        0x5421c3209c2d6c704835d82ac4c3dd90f61a8a52598b9e7ab656e9d8c8b24316,\n        P256\n    )\n\n    print(\"S==S  = \", S == S)\n\n    print(\"S==T  = \", S == T)\n\n    R = S + T\n    print(\"S+T   = ({:X}, {:X})\".format(R.x, R.y))\n\n    R = S - T\n    print(\"S-T   = ({:X}, {:X})\".format(R.x, R.y))\n\n    R = 2 * S\n    print(\"2S    = ({:X}, {:X})\".format(R.x, R.y))\n\n    d = 0xc51e4753afdec1e6b6c6a5b992f43f8dd0c7a8933072708b6522468b2ffb06fd\n    e = 0xd37f628ece72a462f0145cbefe3f0b355ee8332d37acdd83a358016aea029db7\n    R = (d * S) + (e * T)\n    print(\"dS+eT = ({:X}, {:X})\".format(R.x, R.y))\n\n    R = S + S\n    print(\"S+S   = ({:X}, {:X})\".format(R.x, R.y))\n\n    R = S - S\n    print(\"S-S   = ({:X}, {:X})\".format(R.x, R.y))\n\n    ```\n\n- **for other examples:** [tests](https://github.com/dmazzella/ucrypto/tree/master/tests)\n\n# Optimizations are disabled by **default** for easy build on different platforms\n```c\n#define TFM_NO_ASM\n\n// #define TFM_ECC192\n// #define TFM_ECC224\n// #define TFM_ECC256\n// #define TFM_ECC384\n// #define TFM_ECC512\n// #define TFM_RSA512\n// #define TFM_RSA1024\n// #define TFM_RSA2048\n```\n\n# Compiling the cmodule into MicroPython\n\nTo build such a module, compile MicroPython with an extra make flag named ```USER_C_MODULES``` set to the directory containing all modules you want included (not to the module itself).\n\n\n- Example:\n    ### PYBD_SF6\n    ```bash\n    ➜  ~ git clone https://github.com/micropython/micropython.git micropython\n    ➜  ~ cd micropython\n    ➜  micropython (master) ✗ git submodule update --init\n    ➜  micropython (master) ✗ git clone https://github.com/dmazzella/ucrypto.git ports/stm32/boards/PYBD_SF6/cmodules/ucrypto\n    ➜  micropython (master) ✗ make -j8 -C mpy-cross \u0026\u0026 make -j8 -C ports/stm32/ BOARD=\"PYBD_SF6\" USER_C_MODULES=\"$(pwd)/ports/stm32/boards/PYBD_SF6/cmodules\"\n    ```\n    ### ESP32_GENERIC\n    ```bash\n    ➜  ~ git clone https://github.com/micropython/micropython.git micropython\n    ➜  ~ cd micropython\n    ➜  micropython (master) ✗ git submodule update --init\n    ➜  micropython (master) ✗ git clone https://github.com/dmazzella/ucrypto.git ports/esp32/boards/ESP32_GENERIC/cmodules/ucrypto\n    ➜  micropython (master) ✗ make -j8 -C mpy-cross \u0026\u0026 make -j8 -C ports/esp32/ BOARD=\"ESP32_GENERIC\" USER_C_MODULES=\"$(pwd)/ports/esp32/boards/ESP32_GENERIC/cmodules/ucrypto/micropython.cmake\"\n    ```\n    ### ARDUINO_NANO_RP2040_CONNECT\n    ```bash\n    ➜  ~ git clone https://github.com/micropython/micropython.git micropython\n    ➜  ~ cd micropython\n    ➜  micropython (master) ✗ git submodule update --init\n    ➜  micropython (master) ✗ git clone https://github.com/dmazzella/ucrypto.git ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/cmodules/ucrypto\n    ➜  micropython (master) ✗ make -j8 -C mpy-cross \u0026\u0026 make -j8 -C ports/rp2/ BOARD=\"ARDUINO_NANO_RP2040_CONNECT\" USER_C_MODULES=\"$(pwd)/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/cmodules/ucrypto/micropython.cmake\"\n    ```\n\n## Build size:\n\nThe build size depends on the asm optimizations of the tomsfastmath library that are enabled into ```ucrypto/tomsfastmath/tfm_mpi.h```\n```c\n#define TFM_ECC192\n#define TFM_ECC224\n#define TFM_ECC256\n#define TFM_ECC384\n#define TFM_ECC512\n#define TFM_RSA512\n#define TFM_RSA1024\n#define TFM_RSA2048\n```\n\n- PYBD_SF6 without ucrypto:\n    ```\n    LINK build-PYBD_SF6/firmware.elf\n    text\t   data\t    bss\t    dec\t    hex\tfilename\n    1012856\t    328\t 100576\t1113760\t 10fea0\tbuild-PYBD_SF6/firmware.elf\n    ```\n- PYBD_SF6 with ucrypto and with tomsfastmath only ECC 256 asm optimizations:\n    ```c\n    // #define TFM_ECC192\n    // #define TFM_ECC224\n    #define TFM_ECC256\n    // #define TFM_ECC384\n    // #define TFM_ECC512\n    // #define TFM_RSA512\n    // #define TFM_RSA1024\n    // #define TFM_RSA2048\n    ```\n    ```\n    LINK build-PYBD_SF6/firmware.elf\n    text\t   data\t    bss\t    dec\t    hex\tfilename\n    1034872\t    452\t 101600\t1136924\t 11591c\tbuild-PYBD_SF6/firmware.elf\n    ```\n- PYBD_SF6 with ucrypto and without tomsfastmath RSA asm optimizations:\n    ```c\n    #define TFM_ECC192\n    #define TFM_ECC224\n    #define TFM_ECC256\n    #define TFM_ECC384\n    #define TFM_ECC512\n    // #define TFM_RSA512\n    // #define TFM_RSA1024\n    // #define TFM_RSA2048\n    ```\n    ```\n    LINK build-PYBD_SF6/firmware.elf\n    text\t   data\t    bss\t    dec\t    hex\tfilename\n    1042552\t    452\t 101600\t1144604\t 11771c\tbuild-PYBD_SF6/firmware.elf\n    ```\n- PYBD_SF6 with ucrypto and full tomsfastmath asm optimizations:\n    ```\n    LINK build-PYBD_SF6/firmware.elf\n    text\t   data\t    bss\t    dec\t    hex\tfilename\n    1209976\t    452\t 101600\t1312028\t 14051c\tbuild-PYBD_SF6/firmware.elf\n    ```\n\nTo see which optimizations are enabled in the build:\n```python\nMicroPython v1.19.1-705-gac5934c96-dirty on 2022-11-22; PORTENTA with STM32H747\nType \"help()\" for more information.\n\u003e\u003e\u003e import _crypto\n\u003e\u003e\u003e print(_crypto.NUMBER.ident())\nTomsFastMath v0.13.1-next\n\nSizeofs\n        fp_digit = 4\n        fp_word  = 8\n\nFP_MAX_SIZE = 4352\n\nDefines: \n TFM_ARM  TFM_ECC192  TFM_ECC224  TFM_ECC256  TFM_ECC384  TFM_ECC512  TFM_RSA512  TFM_RSA1024  TFM_RSA2048  TFM_ASM  TFM_MUL6  TFM_SQR6  TFM_MUL7  TFM_SQR7  TFM_MUL8  TFM_SQR8  TFM_MUL12  TFM_SQR12  TFM_SMALL_SET  TFM_MUL17  TFM_SQR17  TFM_MUL32  TFM_SQR32  TFM_MUL64  TFM_SQR64 \n\n\u003e\u003e\u003e\n```\n","funding_links":["https://www.buymeacoffee.com/damianomazp"],"categories":["Libraries"],"sub_categories":["Communications"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmazzella%2Fucrypto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmazzella%2Fucrypto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmazzella%2Fucrypto/lists"}