You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.7 KiB

4 years ago
  1. from __future__ import division
  2. from . import der, ecdsa
  3. class UnknownCurveError(Exception):
  4. pass
  5. def orderlen(order):
  6. return (1+len("%x"%order))//2 # bytes
  7. # the NIST curves
  8. class Curve:
  9. def __init__(self, name, curve, generator, oid, openssl_name=None):
  10. self.name = name
  11. self.openssl_name = openssl_name # maybe None
  12. self.curve = curve
  13. self.generator = generator
  14. self.order = generator.order()
  15. self.baselen = orderlen(self.order)
  16. self.verifying_key_length = 2*self.baselen
  17. self.signature_length = 2*self.baselen
  18. self.oid = oid
  19. self.encoded_oid = der.encode_oid(*oid)
  20. NIST192p = Curve("NIST192p", ecdsa.curve_192, ecdsa.generator_192,
  21. (1, 2, 840, 10045, 3, 1, 1), "prime192v1")
  22. NIST224p = Curve("NIST224p", ecdsa.curve_224, ecdsa.generator_224,
  23. (1, 3, 132, 0, 33), "secp224r1")
  24. NIST256p = Curve("NIST256p", ecdsa.curve_256, ecdsa.generator_256,
  25. (1, 2, 840, 10045, 3, 1, 7), "prime256v1")
  26. NIST384p = Curve("NIST384p", ecdsa.curve_384, ecdsa.generator_384,
  27. (1, 3, 132, 0, 34), "secp384r1")
  28. NIST521p = Curve("NIST521p", ecdsa.curve_521, ecdsa.generator_521,
  29. (1, 3, 132, 0, 35), "secp521r1")
  30. SECP256k1 = Curve("SECP256k1", ecdsa.curve_secp256k1, ecdsa.generator_secp256k1,
  31. (1, 3, 132, 0, 10), "secp256k1")
  32. curves = [NIST192p, NIST224p, NIST256p, NIST384p, NIST521p, SECP256k1]
  33. def find_curve(oid_curve):
  34. for c in curves:
  35. if c.oid == oid_curve:
  36. return c
  37. raise UnknownCurveError("I don't know about the curve with oid %s."
  38. "I only know about these: %s" %
  39. (oid_curve, [c.name for c in curves]))