Sign.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/Sign.h"
  16. #include "node_build/dependencies/cnacl/crypto_sign/ed25519/ref10/ge.h"
  17. #include "node_build/dependencies/cnacl/crypto_sign/ed25519/ref10/sc.h"
  18. #include "crypto_hash_sha512.h"
  19. #include "crypto_sign_ed25519.h"
  20. #if crypto_sign_ed25519_open != crypto_sign_ed25519_ref10_open
  21. Assert_compileTime(crypto_sign_ed25519_open == crypto_sign_ed25519_ref10_open);
  22. #endif
  23. void Sign_signingKeyPairFromCurve25519(uint8_t keypairOut[64], uint8_t secretCryptoKey[32])
  24. {
  25. Bits_memcpy(keypairOut, secretCryptoKey, 32);
  26. keypairOut[0] &= 248;
  27. keypairOut[31] &= 63;
  28. keypairOut[31] |= 64;
  29. ge_p3 A;
  30. ge_scalarmult_base(&A, keypairOut);
  31. ge_p3_tobytes(&keypairOut[32], &A);
  32. }
  33. void Sign_publicKeyFromKeyPair(uint8_t publicSigningKey[32], uint8_t keyPair[64])
  34. {
  35. Bits_memcpy(publicSigningKey, &keyPair[32], 32);
  36. }
  37. void Sign_signMsg(uint8_t keyPair[64], struct Message* msg, struct Random* rand)
  38. {
  39. // az is set to the secret key followed by another secret value
  40. // which since we don't have a secret seed in this algorithm is just the
  41. // hash of the secret key and 32 bytes of random
  42. uint8_t az[64];
  43. uint8_t r[64];
  44. ge_p3 R;
  45. uint8_t hram[64];
  46. Bits_memcpy(az, keyPair, 32);
  47. Random_bytes(rand, &az[32], 32);
  48. crypto_hash_sha512(az,az,64);
  49. Bits_memcpy(az, keyPair, 32);
  50. az[0] &= 248;
  51. az[31] &= 63;
  52. az[31] |= 64;
  53. // hash message + secret number
  54. Message_push(msg, &az[32], 32, NULL);
  55. crypto_hash_sha512(r, msg->bytes, msg->length);
  56. // Replace secret number with public key
  57. Bits_memcpy(msg->bytes, &keyPair[32], 32);
  58. // push pointMul(r) to message
  59. sc_reduce(r);
  60. ge_scalarmult_base(&R,r);
  61. Message_shift(msg, 32, NULL);
  62. ge_p3_tobytes(msg->bytes,&R);
  63. crypto_hash_sha512(hram, msg->bytes, msg->length);
  64. sc_reduce(hram);
  65. sc_muladd(&msg->bytes[32], hram, az, r);
  66. }
  67. int Sign_verifyMsg(uint8_t publicSigningKey[32], struct Message* msg)
  68. {
  69. if (msg->length < 64) { return -1; }
  70. struct Allocator* alloc = Allocator_child(msg->alloc);
  71. uint8_t* buff = Allocator_malloc(alloc, msg->length);
  72. unsigned long long ml = msg->length;
  73. int ret = crypto_sign_ed25519_open(buff, &ml, msg->bytes, msg->length, publicSigningKey);
  74. Allocator_free(alloc);
  75. if (ret) {
  76. return -1;
  77. }
  78. Message_pop(msg, NULL, 64, NULL);
  79. return 0;
  80. }
  81. int Sign_publicSigningKeyToCurve25519(uint8_t curve25519keyOut[32], uint8_t publicSigningKey[32])
  82. {
  83. ge_p3 A;
  84. fe x;
  85. fe one_minus_y;
  86. if (ge_frombytes_negate_vartime(&A, publicSigningKey) != 0) {
  87. return -1;
  88. }
  89. fe_1(one_minus_y);
  90. fe_sub(one_minus_y, one_minus_y, A.Y);
  91. fe_invert(one_minus_y, one_minus_y);
  92. fe_1(x);
  93. fe_add(x, x, A.Y);
  94. fe_mul(x, x, one_minus_y);
  95. fe_tobytes(curve25519keyOut, x);
  96. return 0;
  97. }