mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-08-04 12:56:25 +00:00
python: reformat python scripts using black
This commit is contained in:
parent
02a988cd26
commit
2de6d876a3
42
setup.py
42
setup.py
@ -1,37 +1,39 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
from distutils.core import setup
|
from distutils.core import setup
|
||||||
from distutils.extension import Extension
|
from distutils.extension import Extension
|
||||||
|
|
||||||
from Cython.Build import cythonize
|
from Cython.Build import cythonize
|
||||||
from Cython.Distutils import build_ext
|
from Cython.Distutils import build_ext
|
||||||
|
|
||||||
srcs = [
|
srcs = [
|
||||||
'nist256p1',
|
"nist256p1",
|
||||||
'base58',
|
"base58",
|
||||||
'bignum',
|
"bignum",
|
||||||
'bip32',
|
"bip32",
|
||||||
'ecdsa',
|
"ecdsa",
|
||||||
'curve25519',
|
"curve25519",
|
||||||
'hmac',
|
"hmac",
|
||||||
'rand',
|
"rand",
|
||||||
'ripemd160',
|
"ripemd160",
|
||||||
'secp256k1',
|
"secp256k1",
|
||||||
'sha2',
|
"sha2",
|
||||||
]
|
]
|
||||||
|
|
||||||
extensions = [
|
extensions = [
|
||||||
Extension('TrezorCrypto',
|
Extension(
|
||||||
sources = ['TrezorCrypto.pyx', 'c.pxd'] + [ x + '.c' for x in srcs ],
|
"TrezorCrypto",
|
||||||
|
sources=["TrezorCrypto.pyx", "c.pxd"] + [x + ".c" for x in srcs],
|
||||||
extra_compile_args=[],
|
extra_compile_args=[],
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name = 'TrezorCrypto',
|
name="TrezorCrypto",
|
||||||
version = '0.0.0',
|
version="0.0.0",
|
||||||
description = 'Cython wrapper around trezor-crypto library',
|
description="Cython wrapper around trezor-crypto library",
|
||||||
author = 'Pavol Rusnak',
|
author="Pavol Rusnak",
|
||||||
author_email = 'stick@satoshilabs.com',
|
author_email="stick@satoshilabs.com",
|
||||||
url = 'https://github.com/trezor/trezor-crypto',
|
url="https://github.com/trezor/trezor-crypto",
|
||||||
cmdclass = {'build_ext': build_ext},
|
cmdclass={"build_ext": build_ext},
|
||||||
ext_modules=cythonize(extensions),
|
ext_modules=cythonize(extensions),
|
||||||
)
|
)
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
#!/usr/bin/py.test
|
#!/usr/bin/py.test
|
||||||
import ctypes as c
|
|
||||||
import curve25519
|
|
||||||
import random
|
|
||||||
import ecdsa
|
|
||||||
import hashlib
|
|
||||||
import binascii
|
import binascii
|
||||||
|
import ctypes as c
|
||||||
|
import hashlib
|
||||||
import os
|
import os
|
||||||
|
import random
|
||||||
|
|
||||||
|
import curve25519
|
||||||
|
import ecdsa
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def bytes2num(s):
|
def bytes2num(s):
|
||||||
res = 0
|
res = 0
|
||||||
for i, b in enumerate(reversed(bytearray(s))):
|
for i, b in enumerate(reversed(bytearray(s))):
|
||||||
@ -15,10 +17,8 @@ def bytes2num(s):
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
curves = {
|
curves = {"nist256p1": ecdsa.curves.NIST256p, "secp256k1": ecdsa.curves.SECP256k1}
|
||||||
'nist256p1': ecdsa.curves.NIST256p,
|
|
||||||
'secp256k1': ecdsa.curves.SECP256k1
|
|
||||||
}
|
|
||||||
|
|
||||||
class Point:
|
class Point:
|
||||||
def __init__(self, name, x, y):
|
def __init__(self, name, x, y):
|
||||||
@ -26,30 +26,70 @@ class Point:
|
|||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
|
|
||||||
|
|
||||||
points = [
|
points = [
|
||||||
Point('secp256k1', 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798, 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8),
|
Point(
|
||||||
Point('secp256k1', 0x1, 0x4218f20ae6c646b363db68605822fb14264ca8d2587fdd6fbc750d587e76a7ee),
|
"secp256k1",
|
||||||
Point('secp256k1', 0x2, 0x66fbe727b2ba09e09f5a98d70a5efce8424c5fa425bbda1c511f860657b8535e),
|
0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,
|
||||||
Point('secp256k1', 0x1b,0x1adcea1cf831b0ad1653e769d1a229091d0cc68d4b0328691b9caacc76e37c90),
|
0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8,
|
||||||
Point('nist256p1', 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296, 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5),
|
),
|
||||||
Point('nist256p1', 0x0, 0x66485c780e2f83d72433bd5d84a06bb6541c2af31dae871728bf856a174f93f4),
|
Point(
|
||||||
Point('nist256p1', 0x0, 0x99b7a386f1d07c29dbcc42a27b5f9449abe3d50de25178e8d7407a95e8b06c0b),
|
"secp256k1",
|
||||||
Point('nist256p1', 0xaf8bbdfe8cdd5577acbf345b543d28cf402f4e94d3865b97ea0787f2d3aa5d22,0x35802b8b376b995265918b078bc109c21a535176585c40f519aca52d6afc147c),
|
0x1,
|
||||||
Point('nist256p1', 0x80000, 0x580610071f440f0dcc14a22e2d5d5afc1224c0cd11a3b4b51b8ecd2224ee1ce2)
|
0x4218f20ae6c646b363db68605822fb14264ca8d2587fdd6fbc750d587e76a7ee,
|
||||||
|
),
|
||||||
|
Point(
|
||||||
|
"secp256k1",
|
||||||
|
0x2,
|
||||||
|
0x66fbe727b2ba09e09f5a98d70a5efce8424c5fa425bbda1c511f860657b8535e,
|
||||||
|
),
|
||||||
|
Point(
|
||||||
|
"secp256k1",
|
||||||
|
0x1b,
|
||||||
|
0x1adcea1cf831b0ad1653e769d1a229091d0cc68d4b0328691b9caacc76e37c90,
|
||||||
|
),
|
||||||
|
Point(
|
||||||
|
"nist256p1",
|
||||||
|
0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296,
|
||||||
|
0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5,
|
||||||
|
),
|
||||||
|
Point(
|
||||||
|
"nist256p1",
|
||||||
|
0x0,
|
||||||
|
0x66485c780e2f83d72433bd5d84a06bb6541c2af31dae871728bf856a174f93f4,
|
||||||
|
),
|
||||||
|
Point(
|
||||||
|
"nist256p1",
|
||||||
|
0x0,
|
||||||
|
0x99b7a386f1d07c29dbcc42a27b5f9449abe3d50de25178e8d7407a95e8b06c0b,
|
||||||
|
),
|
||||||
|
Point(
|
||||||
|
"nist256p1",
|
||||||
|
0xaf8bbdfe8cdd5577acbf345b543d28cf402f4e94d3865b97ea0787f2d3aa5d22,
|
||||||
|
0x35802b8b376b995265918b078bc109c21a535176585c40f519aca52d6afc147c,
|
||||||
|
),
|
||||||
|
Point(
|
||||||
|
"nist256p1",
|
||||||
|
0x80000,
|
||||||
|
0x580610071f440f0dcc14a22e2d5d5afc1224c0cd11a3b4b51b8ecd2224ee1ce2,
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
random_iters = int(os.environ.get('ITERS', 1))
|
random_iters = int(os.environ.get("ITERS", 1))
|
||||||
|
|
||||||
DIR = os.path.abspath(os.path.dirname(__file__))
|
DIR = os.path.abspath(os.path.dirname(__file__))
|
||||||
lib = c.cdll.LoadLibrary(os.path.join(DIR, 'libtrezor-crypto.so'))
|
lib = c.cdll.LoadLibrary(os.path.join(DIR, "libtrezor-crypto.so"))
|
||||||
|
|
||||||
|
|
||||||
class curve_info(c.Structure):
|
class curve_info(c.Structure):
|
||||||
_fields_ = [("bip32_name", c.c_char_p),
|
_fields_ = [("bip32_name", c.c_char_p), ("params", c.c_void_p)]
|
||||||
("params", c.c_void_p)]
|
|
||||||
|
|
||||||
lib.get_curve_by_name.restype = c.POINTER(curve_info)
|
lib.get_curve_by_name.restype = c.POINTER(curve_info)
|
||||||
|
|
||||||
BIGNUM = c.c_uint32 * 9
|
BIGNUM = c.c_uint32 * 9
|
||||||
|
|
||||||
|
|
||||||
class Random(random.Random):
|
class Random(random.Random):
|
||||||
def randbytes(self, n):
|
def randbytes(self, n):
|
||||||
buf = (c.c_uint8 * n)()
|
buf = (c.c_uint8 * n)()
|
||||||
@ -74,36 +114,40 @@ def int2bn(x, bn_type=BIGNUM):
|
|||||||
def bn2int(b):
|
def bn2int(b):
|
||||||
x = 0
|
x = 0
|
||||||
for i in range(len(b)):
|
for i in range(len(b)):
|
||||||
x += (b[i] << (30 * i))
|
x += b[i] << (30 * i)
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(params=range(random_iters))
|
@pytest.fixture(params=range(random_iters))
|
||||||
def r(request):
|
def r(request):
|
||||||
seed = request.param
|
seed = request.param
|
||||||
return Random(seed + int(os.environ.get('SEED', 0)))
|
return Random(seed + int(os.environ.get("SEED", 0)))
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(params=list(sorted(curves)))
|
@pytest.fixture(params=list(sorted(curves)))
|
||||||
def curve(request):
|
def curve(request):
|
||||||
name = request.param
|
name = request.param
|
||||||
curve_ptr = lib.get_curve_by_name(bytes(name, "ascii")).contents.params
|
curve_ptr = lib.get_curve_by_name(bytes(name, "ascii")).contents.params
|
||||||
assert curve_ptr, 'curve {} not found'.format(name)
|
assert curve_ptr, "curve {} not found".format(name)
|
||||||
curve_obj = curves[name]
|
curve_obj = curves[name]
|
||||||
curve_obj.ptr = c.c_void_p(curve_ptr)
|
curve_obj.ptr = c.c_void_p(curve_ptr)
|
||||||
curve_obj.p = curve_obj.curve.p() # shorthand
|
curve_obj.p = curve_obj.curve.p() # shorthand
|
||||||
return curve_obj
|
return curve_obj
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(params=points)
|
@pytest.fixture(params=points)
|
||||||
def point(request):
|
def point(request):
|
||||||
name = request.param.curve
|
name = request.param.curve
|
||||||
curve_ptr = lib.get_curve_by_name(bytes(name, "ascii")).contents.params
|
curve_ptr = lib.get_curve_by_name(bytes(name, "ascii")).contents.params
|
||||||
assert curve_ptr, 'curve {} not found'.format(name)
|
assert curve_ptr, "curve {} not found".format(name)
|
||||||
curve_obj = curves[name]
|
curve_obj = curves[name]
|
||||||
curve_obj.ptr = c.c_void_p(curve_ptr)
|
curve_obj.ptr = c.c_void_p(curve_ptr)
|
||||||
curve_obj.p = ecdsa.ellipticcurve.Point(curve_obj.curve, request.param.x, request.param.y)
|
curve_obj.p = ecdsa.ellipticcurve.Point(
|
||||||
|
curve_obj.curve, request.param.x, request.param.y
|
||||||
|
)
|
||||||
return curve_obj
|
return curve_obj
|
||||||
|
|
||||||
|
|
||||||
def test_inverse(curve, r):
|
def test_inverse(curve, r):
|
||||||
x = r.randrange(1, curve.p)
|
x = r.randrange(1, curve.p)
|
||||||
y = int2bn(x)
|
y = int2bn(x)
|
||||||
@ -138,7 +182,7 @@ def test_is_equal(curve, r):
|
|||||||
|
|
||||||
|
|
||||||
def test_is_zero(curve, r):
|
def test_is_zero(curve, r):
|
||||||
x = r.randrange(0, curve.p);
|
x = r.randrange(0, curve.p)
|
||||||
assert lib.bn_is_zero(int2bn(x)) == (not x)
|
assert lib.bn_is_zero(int2bn(x)) == (not x)
|
||||||
|
|
||||||
|
|
||||||
@ -265,6 +309,7 @@ def test_mod(curve, r):
|
|||||||
lib.bn_mod(y, int2bn(curve.p))
|
lib.bn_mod(y, int2bn(curve.p))
|
||||||
assert bn2int(y) == x % curve.p
|
assert bn2int(y) == x % curve.p
|
||||||
|
|
||||||
|
|
||||||
def test_mod_specific(curve):
|
def test_mod_specific(curve):
|
||||||
p = curve.p
|
p = curve.p
|
||||||
for x in [0, 1, 2, p - 2, p - 1, p, p + 1, p + 2, 2 * p - 2, 2 * p - 1]:
|
for x in [0, 1, 2, p - 2, p - 1, p, p + 1, p + 2, 2 * p - 2, 2 * p - 1]:
|
||||||
@ -272,13 +317,27 @@ def test_mod_specific(curve):
|
|||||||
lib.bn_mod(y, int2bn(curve.p))
|
lib.bn_mod(y, int2bn(curve.p))
|
||||||
assert bn2int(y) == x % p
|
assert bn2int(y) == x % p
|
||||||
|
|
||||||
|
|
||||||
POINT = BIGNUM * 2
|
POINT = BIGNUM * 2
|
||||||
to_POINT = lambda p: POINT(int2bn(p.x()), int2bn(p.y()))
|
|
||||||
from_POINT = lambda p: (bn2int(p[0]), bn2int(p[1]))
|
|
||||||
|
def to_POINT(p):
|
||||||
|
return POINT(int2bn(p.x()), int2bn(p.y()))
|
||||||
|
|
||||||
|
|
||||||
|
def from_POINT(p):
|
||||||
|
return lambda p: (bn2int(p[0]), bn2int(p[1]))
|
||||||
|
|
||||||
|
|
||||||
JACOBIAN = BIGNUM * 3
|
JACOBIAN = BIGNUM * 3
|
||||||
to_JACOBIAN = lambda jp: JACOBIAN(int2bn(jp[0]), int2bn(jp[1]), int2bn(jp[2]))
|
|
||||||
from_JACOBIAN = lambda p: (bn2int(p[0]), bn2int(p[1]), bn2int(p[2]))
|
|
||||||
|
def to_JACOBIAN(jp):
|
||||||
|
return JACOBIAN(int2bn(jp[0]), int2bn(jp[1]), int2bn(jp[2]))
|
||||||
|
|
||||||
|
|
||||||
|
def from_JACOBIAN(p):
|
||||||
|
return (bn2int(p[0]), bn2int(p[1]), bn2int(p[2]))
|
||||||
|
|
||||||
|
|
||||||
def test_point_multiply(curve, r):
|
def test_point_multiply(curve, r):
|
||||||
@ -348,6 +407,7 @@ def test_jacobian_add(curve, r):
|
|||||||
p_ = p1 + p2
|
p_ = p1 + p2
|
||||||
assert (p_.x(), p_.y()) == q
|
assert (p_.x(), p_.y()) == q
|
||||||
|
|
||||||
|
|
||||||
def test_jacobian_add_double(curve, r):
|
def test_jacobian_add_double(curve, r):
|
||||||
p1 = r.randpoint(curve)
|
p1 = r.randpoint(curve)
|
||||||
p2 = p1
|
p2 = p1
|
||||||
@ -361,6 +421,7 @@ def test_jacobian_add_double(curve, r):
|
|||||||
p_ = p1 + p2
|
p_ = p1 + p2
|
||||||
assert (p_.x(), p_.y()) == q
|
assert (p_.x(), p_.y()) == q
|
||||||
|
|
||||||
|
|
||||||
def test_jacobian_double(curve, r):
|
def test_jacobian_double(curve, r):
|
||||||
p = r.randpoint(curve)
|
p = r.randpoint(curve)
|
||||||
p2 = p.double()
|
p2 = p.double()
|
||||||
@ -373,6 +434,7 @@ def test_jacobian_double(curve, r):
|
|||||||
q = from_POINT(q)
|
q = from_POINT(q)
|
||||||
assert (p2.x(), p2.y()) == q
|
assert (p2.x(), p2.y()) == q
|
||||||
|
|
||||||
|
|
||||||
def sigdecode(sig, _):
|
def sigdecode(sig, _):
|
||||||
return map(bytes2num, [sig[:32], sig[32:]])
|
return map(bytes2num, [sig[:32], sig[32:]])
|
||||||
|
|
||||||
@ -385,15 +447,17 @@ def test_sign(curve, r):
|
|||||||
lib.ecdsa_sign_digest(curve.ptr, priv, digest, sig, c.c_void_p(0), c.c_void_p(0))
|
lib.ecdsa_sign_digest(curve.ptr, priv, digest, sig, c.c_void_p(0), c.c_void_p(0))
|
||||||
|
|
||||||
exp = bytes2num(priv)
|
exp = bytes2num(priv)
|
||||||
sk = ecdsa.SigningKey.from_secret_exponent(exp, curve,
|
sk = ecdsa.SigningKey.from_secret_exponent(exp, curve, hashfunc=hashlib.sha256)
|
||||||
hashfunc=hashlib.sha256)
|
|
||||||
vk = sk.get_verifying_key()
|
vk = sk.get_verifying_key()
|
||||||
|
|
||||||
sig_ref = sk.sign_digest_deterministic(digest, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_string_canonize)
|
sig_ref = sk.sign_digest_deterministic(
|
||||||
|
digest, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_string_canonize
|
||||||
|
)
|
||||||
assert binascii.hexlify(sig) == binascii.hexlify(sig_ref)
|
assert binascii.hexlify(sig) == binascii.hexlify(sig_ref)
|
||||||
|
|
||||||
assert vk.verify_digest(sig, digest, sigdecode)
|
assert vk.verify_digest(sig, digest, sigdecode)
|
||||||
|
|
||||||
|
|
||||||
def test_validate_pubkey(curve, r):
|
def test_validate_pubkey(curve, r):
|
||||||
p = r.randpoint(curve)
|
p = r.randpoint(curve)
|
||||||
assert lib.ecdsa_validate_pubkey(curve.ptr, to_POINT(p))
|
assert lib.ecdsa_validate_pubkey(curve.ptr, to_POINT(p))
|
||||||
@ -431,9 +495,13 @@ def test_curve25519_pubkey(r):
|
|||||||
|
|
||||||
|
|
||||||
def test_curve25519_scalarmult_from_gpg(r):
|
def test_curve25519_scalarmult_from_gpg(r):
|
||||||
sec = binascii.unhexlify('4a1e76f133afb29dbc7860bcbc16d0e829009cc15c2f81ed26de1179b1d9c938')
|
sec = binascii.unhexlify(
|
||||||
pub = binascii.unhexlify('5d6fc75c016e85b17f54e0128a216d5f9229f25bac1ec85cecab8daf48621b31')
|
"4a1e76f133afb29dbc7860bcbc16d0e829009cc15c2f81ed26de1179b1d9c938"
|
||||||
|
)
|
||||||
|
pub = binascii.unhexlify(
|
||||||
|
"5d6fc75c016e85b17f54e0128a216d5f9229f25bac1ec85cecab8daf48621b31"
|
||||||
|
)
|
||||||
res = r.randbytes(32)
|
res = r.randbytes(32)
|
||||||
lib.curve25519_scalarmult(res, sec[::-1], pub[::-1])
|
lib.curve25519_scalarmult(res, sec[::-1], pub[::-1])
|
||||||
expected = 'a93dbdb23e5c99da743e203bd391af79f2b83fb8d0fd6ec813371c71f08f2d4d'
|
expected = "a93dbdb23e5c99da743e203bd391af79f2b83fb8d0fd6ec813371c71f08f2d4d"
|
||||||
assert binascii.hexlify(bytearray(res)) == bytes(expected, "ascii")
|
assert binascii.hexlify(bytearray(res)) == bytes(expected, "ascii")
|
||||||
|
@ -1,46 +1,47 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
import ctypes
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
|
from binascii import hexlify, unhexlify
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from pyasn1.codec.ber.decoder import decode as ber_decode
|
||||||
from pyasn1.codec.der.decoder import decode as der_decode
|
from pyasn1.codec.der.decoder import decode as der_decode
|
||||||
from pyasn1.codec.der.encoder import encode as der_encode
|
from pyasn1.codec.der.encoder import encode as der_encode
|
||||||
from pyasn1.codec.ber.decoder import decode as ber_decode
|
from pyasn1.type import namedtype, univ
|
||||||
from pyasn1.type import univ, namedtype
|
|
||||||
from binascii import unhexlify, hexlify
|
|
||||||
import json
|
|
||||||
import ctypes
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
|
|
||||||
class EcSignature(univ.Sequence):
|
class EcSignature(univ.Sequence):
|
||||||
componentType = namedtype.NamedTypes(
|
componentType = namedtype.NamedTypes(
|
||||||
namedtype.NamedType('r', univ.Integer()),
|
namedtype.NamedType("r", univ.Integer()),
|
||||||
namedtype.NamedType('s', univ.Integer())
|
namedtype.NamedType("s", univ.Integer()),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class EcKeyInfo(univ.Sequence):
|
class EcKeyInfo(univ.Sequence):
|
||||||
componentType = namedtype.NamedTypes(
|
componentType = namedtype.NamedTypes(
|
||||||
namedtype.NamedType('key_type', univ.ObjectIdentifier()),
|
namedtype.NamedType("key_type", univ.ObjectIdentifier()),
|
||||||
namedtype.NamedType('curve_name', univ.ObjectIdentifier())
|
namedtype.NamedType("curve_name", univ.ObjectIdentifier()),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class EcPublicKey(univ.Sequence):
|
class EcPublicKey(univ.Sequence):
|
||||||
componentType = namedtype.NamedTypes(
|
componentType = namedtype.NamedTypes(
|
||||||
namedtype.NamedType('key_info', EcKeyInfo()),
|
namedtype.NamedType("key_info", EcKeyInfo()),
|
||||||
namedtype.NamedType('public_key', univ.BitString())
|
namedtype.NamedType("public_key", univ.BitString()),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class EdKeyInfo(univ.Sequence):
|
class EdKeyInfo(univ.Sequence):
|
||||||
componentType = namedtype.NamedTypes(
|
componentType = namedtype.NamedTypes(
|
||||||
namedtype.NamedType('key_type', univ.ObjectIdentifier()),
|
namedtype.NamedType("key_type", univ.ObjectIdentifier())
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class EdPublicKey(univ.Sequence):
|
class EdPublicKey(univ.Sequence):
|
||||||
componentType = namedtype.NamedTypes(
|
componentType = namedtype.NamedTypes(
|
||||||
namedtype.NamedType('key_info', EdKeyInfo()),
|
namedtype.NamedType("key_info", EdKeyInfo()),
|
||||||
namedtype.NamedType('public_key', univ.BitString())
|
namedtype.NamedType("public_key", univ.BitString()),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -73,12 +74,12 @@ def parse_eddsa_signature(signature):
|
|||||||
def parse_ecdh256_privkey(private_key):
|
def parse_ecdh256_privkey(private_key):
|
||||||
if private_key < 0 or private_key.bit_length() > 256:
|
if private_key < 0 or private_key.bit_length() > 256:
|
||||||
raise ParseError("Not a valid 256 bit ECDH private key")
|
raise ParseError("Not a valid 256 bit ECDH private key")
|
||||||
return private_key.to_bytes(32, byteorder='big')
|
return private_key.to_bytes(32, byteorder="big")
|
||||||
|
|
||||||
|
|
||||||
def parse_signed_hex(string):
|
def parse_signed_hex(string):
|
||||||
if len(string) % 2 == 1:
|
if len(string) % 2 == 1:
|
||||||
string = '0' + string
|
string = "0" + string
|
||||||
number = int(string, 16)
|
number = int(string, 16)
|
||||||
if int(string[0], 16) & 8:
|
if int(string[0], 16) & 8:
|
||||||
return -number
|
return -number
|
||||||
@ -111,10 +112,10 @@ def parse_ed_pubkey(public_key):
|
|||||||
except Exception:
|
except Exception:
|
||||||
raise ParseError("Not a BER encoded Edwards curve public key")
|
raise ParseError("Not a BER encoded Edwards curve public key")
|
||||||
|
|
||||||
if not public_key['key_info']['key_type'] == univ.ObjectIdentifier('1.3.101.112'):
|
if not public_key["key_info"]["key_type"] == univ.ObjectIdentifier("1.3.101.112"):
|
||||||
raise ParseError("Not a BER encoded Edwards curve public key")
|
raise ParseError("Not a BER encoded Edwards curve public key")
|
||||||
|
|
||||||
public_key = bytes(public_key['public_key'].asOctets())
|
public_key = bytes(public_key["public_key"].asOctets())
|
||||||
|
|
||||||
return public_key
|
return public_key
|
||||||
|
|
||||||
@ -125,16 +126,20 @@ def parse_ec_pubkey(public_key):
|
|||||||
except Exception:
|
except Exception:
|
||||||
raise ParseError("Not a BER encoded named elliptic curve public key")
|
raise ParseError("Not a BER encoded named elliptic curve public key")
|
||||||
|
|
||||||
if not public_key['key_info']['key_type'] == univ.ObjectIdentifier('1.2.840.10045.2.1'):
|
if not public_key["key_info"]["key_type"] == univ.ObjectIdentifier(
|
||||||
|
"1.2.840.10045.2.1"
|
||||||
|
):
|
||||||
raise ParseError("Not a BER encoded named elliptic curve public key")
|
raise ParseError("Not a BER encoded named elliptic curve public key")
|
||||||
curve_identifier = public_key['key_info']['curve_name']
|
curve_identifier = public_key["key_info"]["curve_name"]
|
||||||
curve_name = get_curve_name_by_identifier(curve_identifier)
|
curve_name = get_curve_name_by_identifier(curve_identifier)
|
||||||
|
|
||||||
if curve_name is None:
|
if curve_name is None:
|
||||||
raise NotSupported('Unsupported named elliptic curve: {}'.format(curve_identifier))
|
raise NotSupported(
|
||||||
|
"Unsupported named elliptic curve: {}".format(curve_identifier)
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
public_key = bytes(public_key['public_key'].asOctets())
|
public_key = bytes(public_key["public_key"].asOctets())
|
||||||
except:
|
except:
|
||||||
raise ParseError("Not a BER encoded named elliptic curve public key")
|
raise ParseError("Not a BER encoded named elliptic curve public key")
|
||||||
|
|
||||||
@ -150,8 +155,8 @@ def parse_ecdsa256_signature(signature):
|
|||||||
except:
|
except:
|
||||||
raise ParseError("Not a valid DER encoded ECDSA signature")
|
raise ParseError("Not a valid DER encoded ECDSA signature")
|
||||||
try:
|
try:
|
||||||
r = int(signature['r']).to_bytes(32, byteorder='big')
|
r = int(signature["r"]).to_bytes(32, byteorder="big")
|
||||||
s = int(signature['s']).to_bytes(32, byteorder='big')
|
s = int(signature["s"]).to_bytes(32, byteorder="big")
|
||||||
signature = r + s
|
signature = r + s
|
||||||
except:
|
except:
|
||||||
raise ParseError("Not a valid DER encoded 256 bit ECDSA signature")
|
raise ParseError("Not a valid DER encoded 256 bit ECDSA signature")
|
||||||
@ -167,29 +172,29 @@ def parse_digest(name):
|
|||||||
|
|
||||||
def get_curve_by_name(name):
|
def get_curve_by_name(name):
|
||||||
lib.get_curve_by_name.restype = ctypes.c_void_p
|
lib.get_curve_by_name.restype = ctypes.c_void_p
|
||||||
curve = lib.get_curve_by_name(bytes(name, 'ascii'))
|
curve = lib.get_curve_by_name(bytes(name, "ascii"))
|
||||||
if curve == None:
|
if curve is None:
|
||||||
return None
|
return None
|
||||||
curve = ctypes.cast(curve, ctypes.POINTER(curve_info))
|
curve = ctypes.cast(curve, ctypes.POINTER(curve_info))
|
||||||
return ctypes.c_void_p(curve.contents.params)
|
return ctypes.c_void_p(curve.contents.params)
|
||||||
|
|
||||||
|
|
||||||
def parse_curve_name(name):
|
def parse_curve_name(name):
|
||||||
if name == 'secp256r1':
|
if name == "secp256r1":
|
||||||
return 'nist256p1'
|
return "nist256p1"
|
||||||
elif name == 'secp256k1':
|
elif name == "secp256k1":
|
||||||
return 'secp256k1'
|
return "secp256k1"
|
||||||
elif name == 'curve25519':
|
elif name == "curve25519":
|
||||||
return 'curve25519'
|
return "curve25519"
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_curve_name_by_identifier(identifier):
|
def get_curve_name_by_identifier(identifier):
|
||||||
if identifier == univ.ObjectIdentifier('1.3.132.0.10'):
|
if identifier == univ.ObjectIdentifier("1.3.132.0.10"):
|
||||||
return 'secp256k1'
|
return "secp256k1"
|
||||||
elif identifier == univ.ObjectIdentifier('1.2.840.10045.3.1.7'):
|
elif identifier == univ.ObjectIdentifier("1.2.840.10045.3.1.7"):
|
||||||
return 'nist256p1'
|
return "nist256p1"
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -223,7 +228,10 @@ def add_pkcs_padding(data):
|
|||||||
|
|
||||||
def remove_pkcs_padding(data):
|
def remove_pkcs_padding(data):
|
||||||
padding_length = data[-1]
|
padding_length = data[-1]
|
||||||
if not (0 < padding_length <= 16 and data[-padding_length:] == bytes([padding_length]*padding_length)):
|
if not (
|
||||||
|
0 < padding_length <= 16
|
||||||
|
and data[-padding_length:] == bytes([padding_length] * padding_length)
|
||||||
|
):
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return data[:-padding_length]
|
return data[:-padding_length]
|
||||||
@ -245,7 +253,9 @@ def aes_cbc_encrypt(key, iv, plaintext):
|
|||||||
context = bytes(context_structure_length)
|
context = bytes(context_structure_length)
|
||||||
ciphertext = bytes(len(plaintext))
|
ciphertext = bytes(len(plaintext))
|
||||||
aes_encrypt_initialise(key, context)
|
aes_encrypt_initialise(key, context)
|
||||||
lib.aes_cbc_encrypt(plaintext, ciphertext, len(plaintext), bytes(bytearray(iv)), context)
|
lib.aes_cbc_encrypt(
|
||||||
|
plaintext, ciphertext, len(plaintext), bytes(bytearray(iv)), context
|
||||||
|
)
|
||||||
return ciphertext
|
return ciphertext
|
||||||
|
|
||||||
|
|
||||||
@ -281,25 +291,25 @@ def generate_aes(filename):
|
|||||||
|
|
||||||
data = load_json_testvectors(filename)
|
data = load_json_testvectors(filename)
|
||||||
|
|
||||||
if not keys_in_dict(data, {'algorithm', 'testGroups'}):
|
if not keys_in_dict(data, {"algorithm", "testGroups"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
if data['algorithm'] != 'AES-CBC-PKCS5':
|
if data["algorithm"] != "AES-CBC-PKCS5":
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
for test_group in data['testGroups']:
|
for test_group in data["testGroups"]:
|
||||||
if not keys_in_dict(test_group, {'tests'}):
|
if not keys_in_dict(test_group, {"tests"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
for test in test_group['tests']:
|
for test in test_group["tests"]:
|
||||||
if not keys_in_dict(test, {'key', 'iv', 'msg', 'ct', 'result'}):
|
if not keys_in_dict(test, {"key", "iv", "msg", "ct", "result"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
try:
|
try:
|
||||||
key = unhexlify(test['key'])
|
key = unhexlify(test["key"])
|
||||||
iv = unhexlify(test['iv'])
|
iv = unhexlify(test["iv"])
|
||||||
plaintext = unhexlify(test['msg'])
|
plaintext = unhexlify(test["msg"])
|
||||||
ciphertext = unhexlify(test['ct'])
|
ciphertext = unhexlify(test["ct"])
|
||||||
result = parse_result(test['result'])
|
result = parse_result(test["result"])
|
||||||
except:
|
except:
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
@ -309,7 +319,15 @@ def generate_aes(filename):
|
|||||||
if result is None:
|
if result is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
vectors.append((hexlify(key), hexlify(iv), hexlify(plaintext), hexlify(ciphertext), result))
|
vectors.append(
|
||||||
|
(
|
||||||
|
hexlify(key),
|
||||||
|
hexlify(iv),
|
||||||
|
hexlify(plaintext),
|
||||||
|
hexlify(ciphertext),
|
||||||
|
result,
|
||||||
|
)
|
||||||
|
)
|
||||||
return vectors
|
return vectors
|
||||||
|
|
||||||
|
|
||||||
@ -318,34 +336,46 @@ def generate_chacha_poly(filename):
|
|||||||
|
|
||||||
data = load_json_testvectors(filename)
|
data = load_json_testvectors(filename)
|
||||||
|
|
||||||
if not keys_in_dict(data, {'algorithm', 'testGroups'}):
|
if not keys_in_dict(data, {"algorithm", "testGroups"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
if data['algorithm'] != 'CHACHA20-POLY1305':
|
if data["algorithm"] != "CHACHA20-POLY1305":
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
for test_group in data['testGroups']:
|
for test_group in data["testGroups"]:
|
||||||
if not keys_in_dict(test_group, {'tests'}):
|
if not keys_in_dict(test_group, {"tests"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
for test in test_group['tests']:
|
for test in test_group["tests"]:
|
||||||
if not keys_in_dict(test, {'key', 'iv', 'aad', 'msg', 'ct', 'tag', 'result'}):
|
if not keys_in_dict(
|
||||||
|
test, {"key", "iv", "aad", "msg", "ct", "tag", "result"}
|
||||||
|
):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
try:
|
try:
|
||||||
key = unhexlify(test['key'])
|
key = unhexlify(test["key"])
|
||||||
iv = unhexlify(test['iv'])
|
iv = unhexlify(test["iv"])
|
||||||
associated_data = unhexlify(test['aad'])
|
associated_data = unhexlify(test["aad"])
|
||||||
plaintext = unhexlify(test['msg'])
|
plaintext = unhexlify(test["msg"])
|
||||||
ciphertext = unhexlify(test['ct'])
|
ciphertext = unhexlify(test["ct"])
|
||||||
tag = unhexlify(test['tag'])
|
tag = unhexlify(test["tag"])
|
||||||
result = parse_result(test['result'])
|
result = parse_result(test["result"])
|
||||||
except:
|
except:
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
if result is None:
|
if result is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
vectors.append((hexlify(key), hexlify(iv), hexlify(associated_data), hexlify(plaintext), hexlify(ciphertext), hexlify(tag), result))
|
vectors.append(
|
||||||
|
(
|
||||||
|
hexlify(key),
|
||||||
|
hexlify(iv),
|
||||||
|
hexlify(associated_data),
|
||||||
|
hexlify(plaintext),
|
||||||
|
hexlify(ciphertext),
|
||||||
|
hexlify(tag),
|
||||||
|
result,
|
||||||
|
)
|
||||||
|
)
|
||||||
return vectors
|
return vectors
|
||||||
|
|
||||||
|
|
||||||
@ -354,63 +384,70 @@ def generate_curve25519_dh(filename):
|
|||||||
|
|
||||||
data = load_json_testvectors(filename)
|
data = load_json_testvectors(filename)
|
||||||
|
|
||||||
if not keys_in_dict(data, {'algorithm', 'testGroups'}):
|
if not keys_in_dict(data, {"algorithm", "testGroups"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
if data['algorithm'] != 'X25519':
|
if data["algorithm"] != "X25519":
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
for test_group in data['testGroups']:
|
for test_group in data["testGroups"]:
|
||||||
if not keys_in_dict(test_group, {'tests'}):
|
if not keys_in_dict(test_group, {"tests"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
for test in test_group['tests']:
|
for test in test_group["tests"]:
|
||||||
if not keys_in_dict(test, {'public', 'private', 'shared', 'result', 'curve'}):
|
if not keys_in_dict(
|
||||||
|
test, {"public", "private", "shared", "result", "curve"}
|
||||||
|
):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
public_key = unhexlify(test['public'])
|
public_key = unhexlify(test["public"])
|
||||||
curve_name = parse_curve_name(test['curve'])
|
curve_name = parse_curve_name(test["curve"])
|
||||||
private_key = unhexlify(test['private'])
|
private_key = unhexlify(test["private"])
|
||||||
shared = unhexlify(test['shared'])
|
shared = unhexlify(test["shared"])
|
||||||
result = parse_result(test['result'])
|
result = parse_result(test["result"])
|
||||||
except:
|
except:
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
if curve_name != 'curve25519':
|
if curve_name != "curve25519":
|
||||||
continue
|
continue
|
||||||
if result is None:
|
if result is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
vectors.append((hexlify(public_key), hexlify(private_key), hexlify(shared), result))
|
vectors.append(
|
||||||
|
(hexlify(public_key), hexlify(private_key), hexlify(shared), result)
|
||||||
|
)
|
||||||
|
|
||||||
return vectors
|
return vectors
|
||||||
|
|
||||||
|
|
||||||
def generate_ecdh(filename):
|
def generate_ecdh(filename):
|
||||||
vectors = []
|
vectors = []
|
||||||
|
|
||||||
data = load_json_testvectors(filename)
|
data = load_json_testvectors(filename)
|
||||||
|
|
||||||
if not keys_in_dict(data, {'algorithm', 'testGroups'}):
|
if not keys_in_dict(data, {"algorithm", "testGroups"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
if data['algorithm'] != 'ECDH':
|
if data["algorithm"] != "ECDH":
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
for test_group in data['testGroups']:
|
for test_group in data["testGroups"]:
|
||||||
if not keys_in_dict(test_group, {'tests'}):
|
if not keys_in_dict(test_group, {"tests"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
for test in test_group['tests']:
|
for test in test_group["tests"]:
|
||||||
if not keys_in_dict(test, {'public', 'private', 'shared', 'result', 'curve'}):
|
if not keys_in_dict(
|
||||||
|
test, {"public", "private", "shared", "result", "curve"}
|
||||||
|
):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
public_key = unhexlify(test['public'])
|
public_key = unhexlify(test["public"])
|
||||||
curve_name = parse_curve_name(test['curve'])
|
curve_name = parse_curve_name(test["curve"])
|
||||||
private_key = parse_signed_hex(test['private'])
|
private_key = parse_signed_hex(test["private"])
|
||||||
shared = unhexlify(test['shared'])
|
shared = unhexlify(test["shared"])
|
||||||
result = parse_result(test['result'])
|
result = parse_result(test["result"])
|
||||||
except:
|
except:
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
@ -431,7 +468,15 @@ def generate_ecdh(filename):
|
|||||||
if result is None:
|
if result is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
vectors.append((curve_name, hexlify(public_key), hexlify(private_key), hexlify(shared), result))
|
vectors.append(
|
||||||
|
(
|
||||||
|
curve_name,
|
||||||
|
hexlify(public_key),
|
||||||
|
hexlify(private_key),
|
||||||
|
hexlify(shared),
|
||||||
|
result,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return vectors
|
return vectors
|
||||||
|
|
||||||
@ -441,18 +486,18 @@ def generate_ecdsa(filename):
|
|||||||
|
|
||||||
data = load_json_testvectors(filename)
|
data = load_json_testvectors(filename)
|
||||||
|
|
||||||
if not keys_in_dict(data, {'algorithm', 'testGroups'}):
|
if not keys_in_dict(data, {"algorithm", "testGroups"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
if data['algorithm'] != 'ECDSA':
|
if data["algorithm"] != "ECDSA":
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
for test_group in data['testGroups']:
|
for test_group in data["testGroups"]:
|
||||||
if not keys_in_dict(test_group, {'tests', 'keyDer', 'sha'}):
|
if not keys_in_dict(test_group, {"tests", "keyDer", "sha"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
public_key = unhexlify(test_group['keyDer'])
|
public_key = unhexlify(test_group["keyDer"])
|
||||||
except:
|
except:
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
@ -464,18 +509,18 @@ def generate_ecdsa(filename):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
hasher = parse_digest(test_group['sha'])
|
hasher = parse_digest(test_group["sha"])
|
||||||
except NotSupported:
|
except NotSupported:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for test in test_group['tests']:
|
for test in test_group["tests"]:
|
||||||
if not keys_in_dict(test, {'sig', 'msg', 'result'}):
|
if not keys_in_dict(test, {"sig", "msg", "result"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
signature = unhexlify(test['sig'])
|
signature = unhexlify(test["sig"])
|
||||||
message = unhexlify(test['msg'])
|
message = unhexlify(test["msg"])
|
||||||
result = parse_result(test['result'])
|
result = parse_result(test["result"])
|
||||||
except:
|
except:
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
@ -487,7 +532,16 @@ def generate_ecdsa(filename):
|
|||||||
except ParseError:
|
except ParseError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
vectors.append((curve_name, hexlify(public_key), hasher, hexlify(message), hexlify(signature), result))
|
vectors.append(
|
||||||
|
(
|
||||||
|
curve_name,
|
||||||
|
hexlify(public_key),
|
||||||
|
hasher,
|
||||||
|
hexlify(message),
|
||||||
|
hexlify(signature),
|
||||||
|
result,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return vectors
|
return vectors
|
||||||
|
|
||||||
@ -497,19 +551,18 @@ def generate_eddsa(filename):
|
|||||||
|
|
||||||
data = load_json_testvectors(filename)
|
data = load_json_testvectors(filename)
|
||||||
|
|
||||||
|
if not keys_in_dict(data, {"algorithm", "testGroups"}):
|
||||||
if not keys_in_dict(data, {'algorithm', 'testGroups'}):
|
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
if data['algorithm'] != 'EDDSA':
|
if data["algorithm"] != "EDDSA":
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
for test_group in data['testGroups']:
|
for test_group in data["testGroups"]:
|
||||||
if not keys_in_dict(test_group, {'tests', 'keyDer'}):
|
if not keys_in_dict(test_group, {"tests", "keyDer"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
public_key = unhexlify(test_group['keyDer'])
|
public_key = unhexlify(test_group["keyDer"])
|
||||||
except:
|
except:
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
@ -518,14 +571,14 @@ def generate_eddsa(filename):
|
|||||||
except ParseError:
|
except ParseError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for test in test_group['tests']:
|
for test in test_group["tests"]:
|
||||||
if not keys_in_dict(test, {'sig', 'msg', 'result'}):
|
if not keys_in_dict(test, {"sig", "msg", "result"}):
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
signature = unhexlify(test['sig'])
|
signature = unhexlify(test["sig"])
|
||||||
message = unhexlify(test['msg'])
|
message = unhexlify(test["msg"])
|
||||||
result = parse_result(test['result'])
|
result = parse_result(test["result"])
|
||||||
except:
|
except:
|
||||||
raise DataError()
|
raise DataError()
|
||||||
|
|
||||||
@ -537,21 +590,31 @@ def generate_eddsa(filename):
|
|||||||
except ParseError:
|
except ParseError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
vectors.append((hexlify(public_key), hexlify(message), hexlify(signature), result))
|
vectors.append(
|
||||||
|
(hexlify(public_key), hexlify(message), hexlify(signature), result)
|
||||||
|
)
|
||||||
|
|
||||||
return vectors
|
return vectors
|
||||||
|
|
||||||
|
|
||||||
dir = os.path.abspath(os.path.dirname(__file__))
|
dir = os.path.abspath(os.path.dirname(__file__))
|
||||||
lib = ctypes.cdll.LoadLibrary(os.path.join(dir, 'libtrezor-crypto.so'))
|
lib = ctypes.cdll.LoadLibrary(os.path.join(dir, "libtrezor-crypto.so"))
|
||||||
testvectors_directory = os.path.join(dir, 'wycheproof/testvectors')
|
testvectors_directory = os.path.join(dir, "wycheproof/testvectors")
|
||||||
context_structure_length = 1024
|
context_structure_length = 1024
|
||||||
|
|
||||||
ecdh_vectors = generate_ecdh("ecdh_test.json")
|
ecdh_vectors = generate_ecdh("ecdh_test.json")
|
||||||
curve25519_dh_vectors = generate_curve25519_dh("x25519_test.json")
|
curve25519_dh_vectors = generate_curve25519_dh("x25519_test.json")
|
||||||
eddsa_vectors = generate_eddsa("eddsa_test.json")
|
eddsa_vectors = generate_eddsa("eddsa_test.json")
|
||||||
ecdsa_vectors = generate_ecdsa("ecdsa_test.json") + generate_ecdsa("ecdsa_secp256k1_sha256_test.json") + generate_ecdsa("ecdsa_secp256r1_sha256_test.json")
|
ecdsa_vectors = (
|
||||||
ecdh_vectors = generate_ecdh("ecdh_test.json") + generate_ecdh("ecdh_secp256k1_test.json") + generate_ecdh("ecdh_secp256r1_test.json")
|
generate_ecdsa("ecdsa_test.json")
|
||||||
|
+ generate_ecdsa("ecdsa_secp256k1_sha256_test.json")
|
||||||
|
+ generate_ecdsa("ecdsa_secp256r1_sha256_test.json")
|
||||||
|
)
|
||||||
|
ecdh_vectors = (
|
||||||
|
generate_ecdh("ecdh_test.json")
|
||||||
|
+ generate_ecdh("ecdh_secp256k1_test.json")
|
||||||
|
+ generate_ecdh("ecdh_secp256r1_test.json")
|
||||||
|
)
|
||||||
chacha_poly_vectors = generate_chacha_poly("chacha20_poly1305_test.json")
|
chacha_poly_vectors = generate_chacha_poly("chacha20_poly1305_test.json")
|
||||||
aes_vectors = generate_aes("aes_cbc_pkcs5_test.json")
|
aes_vectors = generate_aes("aes_cbc_pkcs5_test.json")
|
||||||
|
|
||||||
@ -562,25 +625,34 @@ def test_eddsa(public_key, message, signature, result):
|
|||||||
signature = unhexlify(signature)
|
signature = unhexlify(signature)
|
||||||
message = unhexlify(message)
|
message = unhexlify(message)
|
||||||
|
|
||||||
computed_result = lib.ed25519_sign_open(message, len(message), public_key, signature) == 0
|
computed_result = (
|
||||||
|
lib.ed25519_sign_open(message, len(message), public_key, signature) == 0
|
||||||
|
)
|
||||||
assert result == computed_result
|
assert result == computed_result
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("curve_name, public_key, hasher, message, signature, result", ecdsa_vectors)
|
@pytest.mark.parametrize(
|
||||||
|
"curve_name, public_key, hasher, message, signature, result", ecdsa_vectors
|
||||||
|
)
|
||||||
def test_ecdsa(curve_name, public_key, hasher, message, signature, result):
|
def test_ecdsa(curve_name, public_key, hasher, message, signature, result):
|
||||||
curve = get_curve_by_name(curve_name)
|
curve = get_curve_by_name(curve_name)
|
||||||
if curve is None:
|
if curve is None:
|
||||||
raise NotSupported("Curve not supported: {}".format(curve_Name))
|
raise NotSupported("Curve not supported: {}".format(curve_name))
|
||||||
|
|
||||||
public_key = unhexlify(public_key)
|
public_key = unhexlify(public_key)
|
||||||
signature = unhexlify(signature)
|
signature = unhexlify(signature)
|
||||||
message = unhexlify(message)
|
message = unhexlify(message)
|
||||||
|
|
||||||
computed_result = lib.ecdsa_verify(curve, hasher, public_key, signature, message, len(message)) == 0
|
computed_result = (
|
||||||
|
lib.ecdsa_verify(curve, hasher, public_key, signature, message, len(message))
|
||||||
|
== 0
|
||||||
|
)
|
||||||
assert result == computed_result
|
assert result == computed_result
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("public_key, private_key, shared, result", curve25519_dh_vectors)
|
@pytest.mark.parametrize(
|
||||||
|
"public_key, private_key, shared, result", curve25519_dh_vectors
|
||||||
|
)
|
||||||
def test_curve25519_dh(public_key, private_key, shared, result):
|
def test_curve25519_dh(public_key, private_key, shared, result):
|
||||||
public_key = unhexlify(public_key)
|
public_key = unhexlify(public_key)
|
||||||
private_key = unhexlify(private_key)
|
private_key = unhexlify(private_key)
|
||||||
@ -592,7 +664,9 @@ def test_curve25519_dh(public_key, private_key, shared, result):
|
|||||||
assert result == computed_result
|
assert result == computed_result
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("curve_name, public_key, private_key, shared, result", ecdh_vectors)
|
@pytest.mark.parametrize(
|
||||||
|
"curve_name, public_key, private_key, shared, result", ecdh_vectors
|
||||||
|
)
|
||||||
def test_ecdh(curve_name, public_key, private_key, shared, result):
|
def test_ecdh(curve_name, public_key, private_key, shared, result):
|
||||||
curve = get_curve_by_name(curve_name)
|
curve = get_curve_by_name(curve_name)
|
||||||
if curve is None:
|
if curve is None:
|
||||||
@ -609,7 +683,9 @@ def test_ecdh(curve_name, public_key, private_key, shared, result):
|
|||||||
assert result == computed_result
|
assert result == computed_result
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("key, iv, associated_data, plaintext, ciphertext, tag, result", chacha_poly_vectors)
|
@pytest.mark.parametrize(
|
||||||
|
"key, iv, associated_data, plaintext, ciphertext, tag, result", chacha_poly_vectors
|
||||||
|
)
|
||||||
def test_chacha_poly(key, iv, associated_data, plaintext, ciphertext, tag, result):
|
def test_chacha_poly(key, iv, associated_data, plaintext, ciphertext, tag, result):
|
||||||
key = unhexlify(key)
|
key = unhexlify(key)
|
||||||
iv = unhexlify(iv)
|
iv = unhexlify(iv)
|
||||||
@ -618,7 +694,9 @@ def test_chacha_poly(key, iv, associated_data, plaintext, ciphertext, tag, resul
|
|||||||
ciphertext = unhexlify(ciphertext)
|
ciphertext = unhexlify(ciphertext)
|
||||||
tag = unhexlify(tag)
|
tag = unhexlify(tag)
|
||||||
|
|
||||||
computed_ciphertext, computed_tag = chacha_poly_encrypt(key, iv, associated_data, plaintext)
|
computed_ciphertext, computed_tag = chacha_poly_encrypt(
|
||||||
|
key, iv, associated_data, plaintext
|
||||||
|
)
|
||||||
computed_result = ciphertext == computed_ciphertext and tag == computed_tag
|
computed_result = ciphertext == computed_ciphertext and tag == computed_tag
|
||||||
assert result == computed_result
|
assert result == computed_result
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user