Curve25519.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2022, stelar7 <dudedbz@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Random.h>
  8. namespace Crypto::Curves {
  9. class Curve25519 {
  10. public:
  11. static constexpr u8 BASE_POINT_L_ORDER[33] {
  12. 0xED, 0xD3, 0xF5, 0x5C, 0x1A, 0x63, 0x12, 0x58,
  13. 0xD6, 0x9C, 0xF7, 0xA2, 0xDE, 0xF9, 0xDE, 0x14,
  14. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  16. 0x00
  17. };
  18. static constexpr u32 CURVE_D[8] {
  19. 0x135978A3, 0x75EB4DCA, 0x4141D8AB, 0x00700A4D,
  20. 0x7779E898, 0x8CC74079, 0x2B6FFE73, 0x52036CEE
  21. };
  22. static constexpr u32 CURVE_D_2[8] {
  23. 0x26B2F159, 0xEBD69B94, 0x8283B156, 0x00E0149A,
  24. 0xEEF3D130, 0x198E80F2, 0x56DFFCE7, 0x2406D9DC
  25. };
  26. static constexpr u32 ZERO[8] {
  27. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000
  28. };
  29. static constexpr u32 SQRT_MINUS_1[8] {
  30. 0x4A0EA0B0, 0xC4EE1B27, 0xAD2FE478, 0x2F431806,
  31. 0x3DFBD7A7, 0x2B4D0099, 0x4FC1DF0B, 0x2B832480
  32. };
  33. static constexpr u8 BARRETT_REDUCTION_QUOTIENT[33] {
  34. 0x1B, 0x13, 0x2C, 0x0A, 0xA3, 0xE5, 0x9C, 0xED,
  35. 0xA7, 0x29, 0x63, 0x08, 0x5D, 0x21, 0x06, 0x21,
  36. 0xEB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  37. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  38. 0x0F
  39. };
  40. static constexpr u8 BITS = 255;
  41. static constexpr u8 BYTES = 32;
  42. static constexpr u8 WORDS = 8;
  43. static constexpr u32 A24 = 121666;
  44. static void set(u32* a, u32 b);
  45. static void select(u32* r, u32 const* a, u32 const* b, u32 c);
  46. static void copy(u32* a, u32 const* b);
  47. static void modular_square(u32* r, u32 const* a);
  48. static void modular_subtract(u32* r, u32 const* a, u32 const* b);
  49. static void modular_reduce(u32* r, u32 const* a);
  50. static void modular_add(u32* r, u32 const* a, u32 const* b);
  51. static void modular_multiply(u32* r, u32 const* a, u32 const* b);
  52. static void modular_multiply_inverse(u32* r, u32 const* a);
  53. static void to_power_of_2n(u32* r, u32 const* a, u8 n);
  54. static void export_state(u32* a, u8* data);
  55. static void import_state(u32* a, u8 const* data);
  56. static void modular_subtract_single(u32* r, u32 const* a, u32 b);
  57. static void modular_multiply_single(u32* r, u32 const* a, u32 b);
  58. static void modular_add_single(u32* r, u32 const* a, u32 b);
  59. static u32 modular_square_root(u32* r, u32 const* a, u32 const* b);
  60. static u32 compare(u32 const* a, u32 const* b);
  61. };
  62. }