Skip to content

Commit

Permalink
Add support for P-192
Browse files Browse the repository at this point in the history
  • Loading branch information
Legrandin committed Jan 29, 2022
1 parent 960f14d commit 8731dd2
Show file tree
Hide file tree
Showing 32 changed files with 2,166 additions and 133 deletions.
7 changes: 7 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

IN PROGRESS
++++++++++++++++++++++++++

New features
------------
* Add support for curve NIST P-192.

3.13.0 (23 January 2022)
++++++++++++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion Doc/src/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ A list of useful resources in that area can be found on `Matthew Green's blog`_.
* Asymmetric key generation:

- RSA
- ECC (NIST P-224, P-256, P-384 and P-521 curve only)
- ECC (NIST curves P-192, P-224, P-256, P-384 and P-521)
- DSA
- ElGamal (legacy)

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ with respect to the last official version of PyCrypto (2.6.1):
* Authenticated encryption modes (GCM, CCM, EAX, SIV, OCB)
* Accelerated AES on Intel platforms via AES-NI
* First class support for PyPy
* Elliptic curves cryptography (NIST P-224, P-256, P-384 and P-521 curves only)
* Elliptic curves cryptography (NIST curves P-192, P-224, P-256, P-384 and P-521)
* Better and more compact API (`nonce` and `iv` attributes for ciphers,
automatic generation of random nonces and IVs, simplified CTR cipher mode,
and more)
Expand Down
54 changes: 53 additions & 1 deletion lib/Crypto/PublicKey/ECC.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,53 @@
_Curve = namedtuple("_Curve", "p b order Gx Gy G modulus_bits oid context desc openssh")
_curves = {}


p192_names = ["p192", "NIST P-192", "P-192", "prime192v1", "secp192r1",
"nistp192"]


def init_p192():
p = 0xfffffffffffffffffffffffffffffffeffffffffffffffff
b = 0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1
order = 0xffffffffffffffffffffffff99def836146bc9b1b4d22831
Gx = 0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012
Gy = 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811

p192_modulus = long_to_bytes(p, 24)
p192_b = long_to_bytes(b, 24)
p192_order = long_to_bytes(order, 24)

ec_p192_context = VoidPointer()
result = _ec_lib.ec_ws_new_context(ec_p192_context.address_of(),
c_uint8_ptr(p192_modulus),
c_uint8_ptr(p192_b),
c_uint8_ptr(p192_order),
c_size_t(len(p192_modulus)),
c_ulonglong(getrandbits(64))
)
if result:
raise ImportError("Error %d initializing P-192 context" % result)

context = SmartPointer(ec_p192_context.get(), _ec_lib.ec_free_context)
p192 = _Curve(Integer(p),
Integer(b),
Integer(order),
Integer(Gx),
Integer(Gy),
None,
192,
"1.2.840.10045.3.1.1", # ANSI X9.62 / SEC2
context,
"NIST P-192",
"ecdsa-sha2-nistp192")
global p192_names
_curves.update(dict.fromkeys(p192_names, p192))


init_p192()
del init_p192


p224_names = ["p224", "NIST P-224", "P-224", "prime224v1", "secp224r1",
"nistp224"]

Expand Down Expand Up @@ -171,7 +218,7 @@ def init_p256():
Integer(Gy),
None,
256,
"1.2.840.10045.3.1.7", # ANSI X9.62
"1.2.840.10045.3.1.7", # ANSI X9.62 / SEC2
context,
"NIST P-256",
"ecdsa-sha2-nistp256")
Expand Down Expand Up @@ -452,6 +499,11 @@ def __rmul__(self, left_hand):


# Last piece of initialization
p192_G = EccPoint(_curves['p192'].Gx, _curves['p192'].Gy, "p192")
p192 = _curves['p192']._replace(G=p192_G)
_curves.update(dict.fromkeys(p192_names, p192))
del p192_G, p192, p192_names

p224_G = EccPoint(_curves['p224'].Gx, _curves['p224'].Gy, "p224")
p224 = _curves['p224']._replace(G=p224_G)
_curves.update(dict.fromkeys(p224_names, p224))
Expand Down
Loading

0 comments on commit 8731dd2

Please sign in to comment.