dmazzella / ucrypto

Micropython package for doing fast rsa and elliptic curve cryptography, specifically digital signatures
31 stars 11 forks source link

[Question] Support for SECP128r1? #1

Closed ekawahyu closed 3 years ago

ekawahyu commented 3 years ago

Will isupport for SECP128r1 is in the plan? Thank you.

dmazzella commented 3 years ago

Hi @ekawahyu , thanks for your question, this has shown a bug (fixed in b666c97).

For your question a rapid solution can be edit tests/fastecdsa_1.py:

try:
    from ufastecdsa import curve, ecdsa, keys, util

    get_bit_length = util.get_bit_length

except ImportError:
    from fastecdsa import curve, ecdsa, keys, util

    get_bit_length = int.bit_length

def main():

    SECP128r1 = curve.Curve(
        "SECP128r1",
        0xFFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF,
        0xFFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC,
        0xE87579C11079F43DD824993C2CEE5ED3,
        0xFFFFFFFE0000000075A30D1B9038A115,
        0x161FF7528B899B2D0C28607CA52C5B86,
        0xCF5AC8395BAFEB13C02DA292DDED7A83,
    )
    setattr(curve, "SECP128r1", SECP128r1)

    c = curve.SECP128r1  # curve.P256
    private_key = 129440090160411729250090482679899609177  # 82378264402520040413352233063555671940555718680152892238371187003380781159101
    public_key = keys.get_public_key(private_key, c)
    # private_key, public_key = keys.gen_keypair(c)

    print("PRIVATE KEY:", private_key)
    print("PUBLIC KEY:", public_key.x, public_key.y, public_key.curve.name)

    m = "a message to sign via ECDSA"

    r, s = ecdsa.sign(m, private_key, curve=c)

    print("R:", r, "S:", s)

    verified = ecdsa.verify((r, s), m, public_key, curve=c)
    print(verified)

if __name__ == "__main__":
    main()

the output with ufastecdsa:

➜  micropython (master) ✗ python -Bu tools/pyboard.py -d /dev/cu.usbmodem3267334E30372 ports/stm32/boards/PYBD_SF6/cmodules/ucrypto/tests/fastecdsa_1.py
PRIVATE KEY: 129440090160411729250090482679899609177
PUBLIC KEY: 28682380564611372386993306297071487642 241623269395742587426803653662887823675 SECP128r1
R: 205073144472716698218745305852045595857 S: 273838288313259806971983106551617826270
True

the output with fastecdsa:


➜  micropython (master) ✗ python ports/stm32/boards/PYBD_SF6/cmodules/ucrypto/tests/fastecdsa_1.py                                                      
PRIVATE KEY: 129440090160411729250090482679899609177
PUBLIC KEY: 28682380564611372386993306297071487642 241623269395742587426803653662887823675 SECP128r1
R: 205073144472716698218745305852045595857 S: 273838288313259806971983106551617826270
True
➜  micropython (master) ✗
ekawahyu commented 3 years ago

I think I mistakenly looked at your ucrypto with ucryptolib. I have never tried to build/compile micropython module, I will give it a try.