_edwards.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # This file is licensed under the BSD 2-Clause License.
  2. # See https://opensource.org/licenses/BSD-2-Clause for details.
  3. from ._curve import _Curve
  4. from Crypto.Math.Numbers import Integer
  5. from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, VoidPointer,
  6. SmartPointer)
  7. def ed25519_curve():
  8. p = 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed # 2**255 - 19
  9. order = 0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed
  10. Gx = 0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a
  11. Gy = 0x6666666666666666666666666666666666666666666666666666666666666658
  12. _ed25519_lib = load_pycryptodome_raw_lib("Crypto.PublicKey._ed25519", """
  13. typedef void Point;
  14. int ed25519_new_point(Point **out,
  15. const uint8_t x[32],
  16. const uint8_t y[32],
  17. size_t modsize,
  18. const void *context);
  19. int ed25519_clone(Point **P, const Point *Q);
  20. void ed25519_free_point(Point *p);
  21. int ed25519_cmp(const Point *p1, const Point *p2);
  22. int ed25519_neg(Point *p);
  23. int ed25519_get_xy(uint8_t *xb, uint8_t *yb, size_t modsize, Point *p);
  24. int ed25519_double(Point *p);
  25. int ed25519_add(Point *P1, const Point *P2);
  26. int ed25519_scalar(Point *P, const uint8_t *scalar, size_t scalar_len, uint64_t seed);
  27. """)
  28. class EcLib(object):
  29. new_point = _ed25519_lib.ed25519_new_point
  30. clone = _ed25519_lib.ed25519_clone
  31. free_point = _ed25519_lib.ed25519_free_point
  32. cmp = _ed25519_lib.ed25519_cmp
  33. neg = _ed25519_lib.ed25519_neg
  34. get_xy = _ed25519_lib.ed25519_get_xy
  35. double = _ed25519_lib.ed25519_double
  36. add = _ed25519_lib.ed25519_add
  37. scalar = _ed25519_lib.ed25519_scalar
  38. ed25519 = _Curve(Integer(p),
  39. None,
  40. Integer(order),
  41. Integer(Gx),
  42. Integer(Gy),
  43. None,
  44. 255,
  45. "1.3.101.112", # RFC8410
  46. None,
  47. "Ed25519",
  48. "ssh-ed25519",
  49. EcLib)
  50. return ed25519
  51. def ed448_curve():
  52. p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffff # 2**448 - 2**224 - 1
  53. order = 0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3
  54. Gx = 0x4f1970c66bed0ded221d15a622bf36da9e146570470f1767ea6de324a3d3a46412ae1af72ab66511433b80e18b00938e2626a82bc70cc05e
  55. Gy = 0x693f46716eb6bc248876203756c9c7624bea73736ca3984087789c1e05a0c2d73ad3ff1ce67c39c4fdbd132c4ed7c8ad9808795bf230fa14
  56. _ed448_lib = load_pycryptodome_raw_lib("Crypto.PublicKey._ed448", """
  57. typedef void EcContext;
  58. typedef void PointEd448;
  59. int ed448_new_context(EcContext **pec_ctx);
  60. void ed448_context(EcContext *ec_ctx);
  61. void ed448_free_context(EcContext *ec_ctx);
  62. int ed448_new_point(PointEd448 **out,
  63. const uint8_t x[56],
  64. const uint8_t y[56],
  65. size_t len,
  66. const EcContext *context);
  67. int ed448_clone(PointEd448 **P, const PointEd448 *Q);
  68. void ed448_free_point(PointEd448 *p);
  69. int ed448_cmp(const PointEd448 *p1, const PointEd448 *p2);
  70. int ed448_neg(PointEd448 *p);
  71. int ed448_get_xy(uint8_t *xb, uint8_t *yb, size_t len, const PointEd448 *p);
  72. int ed448_double(PointEd448 *p);
  73. int ed448_add(PointEd448 *P1, const PointEd448 *P2);
  74. int ed448_scalar(PointEd448 *P, const uint8_t *scalar, size_t scalar_len, uint64_t seed);
  75. """)
  76. class EcLib(object):
  77. new_point = _ed448_lib.ed448_new_point
  78. clone = _ed448_lib.ed448_clone
  79. free_point = _ed448_lib.ed448_free_point
  80. cmp = _ed448_lib.ed448_cmp
  81. neg = _ed448_lib.ed448_neg
  82. get_xy = _ed448_lib.ed448_get_xy
  83. double = _ed448_lib.ed448_double
  84. add = _ed448_lib.ed448_add
  85. scalar = _ed448_lib.ed448_scalar
  86. ed448_context = VoidPointer()
  87. result = _ed448_lib.ed448_new_context(ed448_context.address_of())
  88. if result:
  89. raise ImportError("Error %d initializing Ed448 context" % result)
  90. context = SmartPointer(ed448_context.get(), _ed448_lib.ed448_free_context)
  91. ed448 = _Curve(Integer(p),
  92. None,
  93. Integer(order),
  94. Integer(Gx),
  95. Integer(Gy),
  96. None,
  97. 448,
  98. "1.3.101.113", # RFC8410
  99. context,
  100. "Ed448",
  101. None,
  102. EcLib)
  103. return ed448