Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 79ff36b

Browse files
committed
tests ecdsa. removing pyecdsa
1 parent 7c775ff commit 79ff36b

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

pytest/ecdsa_keys.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ def calc_fpr_ecdsa(n):
129129

130130

131131
def generate_key_ecdsa(ecdsa_curve):
132-
curve = find_curve_oid_hex(ecdsa_curve)
132+
curve = ec.get_curve_for_oid(get_curve_by_hex_oid(ecdsa_curve))
133133
assert not(curve is None)
134-
PrivateKey = ecdsa.SigningKey.generate(curve, hashfunc=sha256)
135-
PublicKey = PrivateKey.get_verifying_key()
134+
135+
PrivateKey = ec.generate_private_key(curve(), default_backend())
136+
PublicKey = PrivateKey.public_key()
136137
return PublicKey, PrivateKey
137138

138139

@@ -156,7 +157,8 @@ def build_privkey_template_ecdsa(openpgp_keyno, ecdsa_curve):
156157
keyspec = 0xa4
157158

158159
PublicKey, PrivateKey = generate_key_ecdsa(ecdsa_curve)
159-
return create_ecdsa_4D_key(keyspec, PrivateKey.to_string(), b"\x04" + PublicKey.to_string())
160+
return create_ecdsa_4D_key(keyspec, ecc_to_string(PrivateKey),
161+
ecc_to_string(PublicKey))
160162

161163

162164
def int_to_binstr(vint, size=None):
@@ -199,10 +201,10 @@ def ecc_to_string(key):
199201
serialization.PublicFormat.Raw)
200202

201203
if isinstance(key, ec.EllipticCurvePrivateKey):
202-
return key.private_bytes(
203-
serialization.Encoding.Raw,
204-
serialization.PrivateFormat.Raw,
205-
serialization.NoEncryption())
204+
numbers = key.private_numbers()
205+
keysize = curve_keysize_bytes(numbers.public_numbers.curve)
206+
print(numbers.private_value, keysize, int_to_binstr(numbers.private_value, keysize).hex())
207+
return int_to_binstr(numbers.private_value, keysize)
206208

207209
if isinstance(key, ec.EllipticCurvePublicKey):
208210
numbers = key.public_numbers()

pytest/test_035_ecdsa.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ def ECDSACheckPublicKey(curve_oid, public_key):
6060

6161
def check_ecdh(card, ECDSAcurve, key_num=2):
6262
myPublicKey, myPrivateKey = ecdsa_keys.generate_key_ecdsa(ECDSAcurve)
63-
myPublicKeyTLV = ecdh_public_key_encode(b"\x04" + myPublicKey.to_string())
63+
myPublicKeyTLV = ecdh_public_key_encode(
64+
ecdsa_keys.ecc_to_string(myPublicKey))
6465

65-
pk = card.cmd_get_public_key(2)
66+
pk = card.cmd_get_public_key(key_num)
6667
pk_info = get_pk_info(pk)
6768

68-
mySharedSecret = ecdsa_keys.ecdh(ECDSAcurve, myPrivateKey.to_string(),
69+
mySharedSecret = ecdsa_keys.ecdh(ECDSAcurve,
70+
ecdsa_keys.ecc_to_string(myPrivateKey),
6971
pk_info[0])
7072

7173
sharedSecret = card.cmd_pso(0x80, 0x86, myPublicKeyTLV)

0 commit comments

Comments
 (0)