sign.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <string.h>
  2. #include "crypto_hash_sha512.h"
  3. #include "crypto_sign_ed25519.h"
  4. #include "sign_ed25519_ref10.h"
  5. #include "private/ed25519_ref10.h"
  6. #include "randombytes.h"
  7. #include "utils.h"
  8. void
  9. _crypto_sign_ed25519_ref10_hinit(crypto_hash_sha512_state *hs, int prehashed)
  10. {
  11. static const unsigned char DOM2PREFIX[32 + 2] = {
  12. 'S', 'i', 'g', 'E', 'd', '2', '5', '5', '1', '9', ' ',
  13. 'n', 'o', ' ',
  14. 'E', 'd', '2', '5', '5', '1', '9', ' ',
  15. 'c', 'o', 'l', 'l', 'i', 's', 'i', 'o', 'n', 's', 1, 0
  16. };
  17. crypto_hash_sha512_init(hs);
  18. if (prehashed) {
  19. crypto_hash_sha512_update(hs, DOM2PREFIX, sizeof DOM2PREFIX);
  20. }
  21. }
  22. static inline void
  23. _crypto_sign_ed25519_clamp(unsigned char k[32])
  24. {
  25. k[0] &= 248;
  26. k[31] &= 127;
  27. k[31] |= 64;
  28. }
  29. #ifdef ED25519_NONDETERMINISTIC
  30. /* r = hash(B || empty_labelset || Z || pad1 || k || pad2 || empty_labelset || K || extra || M) (mod q) */
  31. static void
  32. _crypto_sign_ed25519_synthetic_r_hv(crypto_hash_sha512_state *hs,
  33. unsigned char Z[32],
  34. const unsigned char sk[64])
  35. {
  36. static const unsigned char B[32] = {
  37. 0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  38. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  39. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  40. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  41. };
  42. static const unsigned char zeros[128] = { 0x00 };
  43. static const unsigned char empty_labelset[3] = { 0x02, 0x00, 0x00 };
  44. crypto_hash_sha512_update(hs, B, 32);
  45. crypto_hash_sha512_update(hs, empty_labelset, 3);
  46. randombytes_buf(Z, 32);
  47. crypto_hash_sha512_update(hs, Z, 32);
  48. crypto_hash_sha512_update(hs, zeros, 128 - (32 + 3 + 32) % 128);
  49. crypto_hash_sha512_update(hs, sk, 32);
  50. crypto_hash_sha512_update(hs, zeros, 128 - 32 % 128);
  51. crypto_hash_sha512_update(hs, empty_labelset, 3);
  52. crypto_hash_sha512_update(hs, sk + 32, 32);
  53. /* empty extra */
  54. }
  55. #endif
  56. int
  57. _crypto_sign_ed25519_detached(unsigned char *sig, unsigned long long *siglen_p,
  58. const unsigned char *m, unsigned long long mlen,
  59. const unsigned char *sk, int prehashed)
  60. {
  61. crypto_hash_sha512_state hs;
  62. unsigned char az[64];
  63. unsigned char nonce[64];
  64. unsigned char hram[64];
  65. ge25519_p3 R;
  66. _crypto_sign_ed25519_ref10_hinit(&hs, prehashed);
  67. crypto_hash_sha512(az, sk, 32);
  68. #ifdef ED25519_NONDETERMINISTIC
  69. _crypto_sign_ed25519_synthetic_r_hv(&hs, nonce /* Z */, az);
  70. #else
  71. crypto_hash_sha512_update(&hs, az + 32, 32);
  72. #endif
  73. crypto_hash_sha512_update(&hs, m, mlen);
  74. crypto_hash_sha512_final(&hs, nonce);
  75. memmove(sig + 32, sk + 32, 32);
  76. sc25519_reduce(nonce);
  77. ge25519_scalarmult_base(&R, nonce);
  78. ge25519_p3_tobytes(sig, &R);
  79. _crypto_sign_ed25519_ref10_hinit(&hs, prehashed);
  80. crypto_hash_sha512_update(&hs, sig, 64);
  81. crypto_hash_sha512_update(&hs, m, mlen);
  82. crypto_hash_sha512_final(&hs, hram);
  83. sc25519_reduce(hram);
  84. _crypto_sign_ed25519_clamp(az);
  85. sc25519_muladd(sig + 32, hram, az, nonce);
  86. sodium_memzero(az, sizeof az);
  87. sodium_memzero(nonce, sizeof nonce);
  88. if (siglen_p != NULL) {
  89. *siglen_p = 64U;
  90. }
  91. return 0;
  92. }
  93. int
  94. crypto_sign_ed25519_detached(unsigned char *sig, unsigned long long *siglen_p,
  95. const unsigned char *m, unsigned long long mlen,
  96. const unsigned char *sk)
  97. {
  98. return _crypto_sign_ed25519_detached(sig, siglen_p, m, mlen, sk, 0);
  99. }
  100. int
  101. crypto_sign_ed25519(unsigned char *sm, unsigned long long *smlen_p,
  102. const unsigned char *m, unsigned long long mlen,
  103. const unsigned char *sk)
  104. {
  105. unsigned long long siglen;
  106. memmove(sm + crypto_sign_ed25519_BYTES, m, mlen);
  107. /* LCOV_EXCL_START */
  108. if (crypto_sign_ed25519_detached(
  109. sm, &siglen, sm + crypto_sign_ed25519_BYTES, mlen, sk) != 0 ||
  110. siglen != crypto_sign_ed25519_BYTES) {
  111. if (smlen_p != NULL) {
  112. *smlen_p = 0;
  113. }
  114. memset(sm, 0, mlen + crypto_sign_ed25519_BYTES);
  115. return -1;
  116. }
  117. /* LCOV_EXCL_STOP */
  118. if (smlen_p != NULL) {
  119. *smlen_p = mlen + siglen;
  120. }
  121. return 0;
  122. }