scalarmult_ed25519_ref10.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <string.h>
  2. #include "crypto_scalarmult_ed25519.h"
  3. #include "private/ed25519_ref10.h"
  4. #include "utils.h"
  5. static int
  6. _crypto_scalarmult_ed25519_is_inf(const unsigned char s[32])
  7. {
  8. unsigned char c;
  9. unsigned int i;
  10. c = s[0] ^ 0x01;
  11. for (i = 1; i < 31; i++) {
  12. c |= s[i];
  13. }
  14. c |= s[31] & 0x7f;
  15. return ((((unsigned int) c) - 1U) >> 8) & 1;
  16. }
  17. static inline void
  18. _crypto_scalarmult_ed25519_clamp(unsigned char k[32])
  19. {
  20. k[0] &= 248;
  21. k[31] |= 64;
  22. }
  23. static int
  24. _crypto_scalarmult_ed25519(unsigned char *q, const unsigned char *n,
  25. const unsigned char *p, const int clamp)
  26. {
  27. unsigned char *t = q;
  28. ge25519_p3 Q;
  29. ge25519_p3 P;
  30. unsigned int i;
  31. if (ge25519_is_canonical(p) == 0 || ge25519_has_small_order(p) != 0 ||
  32. ge25519_frombytes(&P, p) != 0 || ge25519_is_on_main_subgroup(&P) == 0) {
  33. return -1;
  34. }
  35. for (i = 0; i < 32; ++i) {
  36. t[i] = n[i];
  37. }
  38. if (clamp != 0) {
  39. _crypto_scalarmult_ed25519_clamp(t);
  40. }
  41. t[31] &= 127;
  42. ge25519_scalarmult(&Q, t, &P);
  43. ge25519_p3_tobytes(q, &Q);
  44. if (_crypto_scalarmult_ed25519_is_inf(q) != 0 || sodium_is_zero(n, 32)) {
  45. return -1;
  46. }
  47. return 0;
  48. }
  49. int
  50. crypto_scalarmult_ed25519(unsigned char *q, const unsigned char *n,
  51. const unsigned char *p)
  52. {
  53. return _crypto_scalarmult_ed25519(q, n, p, 1);
  54. }
  55. int
  56. crypto_scalarmult_ed25519_noclamp(unsigned char *q, const unsigned char *n,
  57. const unsigned char *p)
  58. {
  59. return _crypto_scalarmult_ed25519(q, n, p, 0);
  60. }
  61. static int
  62. _crypto_scalarmult_ed25519_base(unsigned char *q,
  63. const unsigned char *n, const int clamp)
  64. {
  65. unsigned char *t = q;
  66. ge25519_p3 Q;
  67. unsigned int i;
  68. for (i = 0; i < 32; ++i) {
  69. t[i] = n[i];
  70. }
  71. if (clamp != 0) {
  72. _crypto_scalarmult_ed25519_clamp(t);
  73. }
  74. t[31] &= 127;
  75. ge25519_scalarmult_base(&Q, t);
  76. ge25519_p3_tobytes(q, &Q);
  77. if (_crypto_scalarmult_ed25519_is_inf(q) != 0 || sodium_is_zero(n, 32)) {
  78. return -1;
  79. }
  80. return 0;
  81. }
  82. int
  83. crypto_scalarmult_ed25519_base(unsigned char *q,
  84. const unsigned char *n)
  85. {
  86. return _crypto_scalarmult_ed25519_base(q, n, 1);
  87. }
  88. int
  89. crypto_scalarmult_ed25519_base_noclamp(unsigned char *q,
  90. const unsigned char *n)
  91. {
  92. return _crypto_scalarmult_ed25519_base(q, n, 0);
  93. }
  94. size_t
  95. crypto_scalarmult_ed25519_bytes(void)
  96. {
  97. return crypto_scalarmult_ed25519_BYTES;
  98. }
  99. size_t
  100. crypto_scalarmult_ed25519_scalarbytes(void)
  101. {
  102. return crypto_scalarmult_ed25519_SCALARBYTES;
  103. }