Skip to content

Commit

Permalink
Correctly check that the scalar matches the point when importing an E…
Browse files Browse the repository at this point in the history
…CC private key
  • Loading branch information
Legrandin committed May 15, 2023
1 parent d64618b commit 2b11e5b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ New features
---------------
* GH#722: ``nonce`` attribute was not correctly set for XChaCha20_Poly1305 ciphers. Thanks to Liam Haber.
* GH#739: OID encoding for arc 2 didn't accept children larger than 39. Thanks to James.
* Correctly check that the scalar matches the point when importing an ECC private key.

3.17.0 (29 January 2023)
++++++++++++++++++++++++++
Expand Down
4 changes: 2 additions & 2 deletions lib/Crypto/PublicKey/ECC.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,8 +1396,8 @@ def _import_rfc5915_der(encoded, passphrase, curve_oid=None):
d = Integer.from_bytes(scalar_bytes)

# Decode public key (if any)
if len(private_key) == 4:
public_key_enc = DerBitString(explicit=1).decode(private_key[3]).value
if len(private_key) > 2:
public_key_enc = DerBitString(explicit=1).decode(private_key[-1]).value
public_key = _import_public_der(public_key_enc, curve_oid=curve_oid)
point_x = public_key.pointQ.x
point_y = public_key.pointQ.y
Expand Down
10 changes: 10 additions & 0 deletions lib/Crypto/SelfTest/PublicKey/test_import_ECC.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ class TestImport(unittest.TestCase):
def test_empty(self):
self.assertRaises(ValueError, ECC.import_key, b"")

def test_mismatch(self):
# The private key does not match the public key
mismatch = """-----BEGIN PRIVATE KEY-----
MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJChZANiAAQarFRaqflo
I+d61SRvU8Za2EurxtW20eZzca7dnNYMYf3boIkDuAUU7FfO7l0/4iGzzvfUinng
o4N+LZfQYcTxmdwlkWOrfzCjtHDix6EznPO/LlxTsV+zfTJ/ijTjeXk=
-----END PRIVATE KEY-----"""
self.assertRaises(ValueError, ECC.import_key, mismatch)


class TestImport_P192(unittest.TestCase):

Expand Down

0 comments on commit 2b11e5b

Please sign in to comment.