ed25519.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "crypto_sign.h"
  2. #include "crypto_verify_32.h"
  3. #include "crypto_hash_sha512.h"
  4. #include "randombytes.h"
  5. #include "ge25519.h"
  6. static void get_hram(unsigned char *hram, const unsigned char *sm, const unsigned char *pk, unsigned char *playground, unsigned long long smlen)
  7. {
  8. unsigned long long i;
  9. for (i = 0;i < 32;++i) playground[i] = sm[i];
  10. for (i = 32;i < 64;++i) playground[i] = pk[i-32];
  11. for (i = 64;i < smlen;++i) playground[i] = sm[i];
  12. crypto_hash_sha512(hram,playground,smlen);
  13. }
  14. int crypto_sign_keypair(
  15. unsigned char *pk,
  16. unsigned char *sk
  17. )
  18. {
  19. sc25519 scsk;
  20. ge25519 gepk;
  21. unsigned char extsk[64];
  22. int i;
  23. randombytes(sk, 32);
  24. crypto_hash_sha512(extsk, sk, 32);
  25. extsk[0] &= 248;
  26. extsk[31] &= 127;
  27. extsk[31] |= 64;
  28. sc25519_from32bytes(&scsk,extsk);
  29. ge25519_scalarmult_base(&gepk, &scsk);
  30. ge25519_pack(pk, &gepk);
  31. for(i=0;i<32;i++)
  32. sk[32 + i] = pk[i];
  33. return 0;
  34. }
  35. int crypto_sign(
  36. unsigned char *sm,unsigned long long *smlen,
  37. const unsigned char *m,unsigned long long mlen,
  38. const unsigned char *sk
  39. )
  40. {
  41. sc25519 sck, scs, scsk;
  42. ge25519 ger;
  43. unsigned char r[32];
  44. unsigned char s[32];
  45. unsigned char extsk[64];
  46. unsigned long long i;
  47. unsigned char hmg[crypto_hash_sha512_BYTES];
  48. unsigned char hram[crypto_hash_sha512_BYTES];
  49. crypto_hash_sha512(extsk, sk, 32);
  50. extsk[0] &= 248;
  51. extsk[31] &= 127;
  52. extsk[31] |= 64;
  53. *smlen = mlen+64;
  54. for(i=0;i<mlen;i++)
  55. sm[64 + i] = m[i];
  56. for(i=0;i<32;i++)
  57. sm[32 + i] = extsk[32+i];
  58. crypto_hash_sha512(hmg, sm+32, mlen+32); /* Generate k as h(extsk[32],...,extsk[63],m) */
  59. /* Computation of R */
  60. sc25519_from64bytes(&sck, hmg);
  61. ge25519_scalarmult_base(&ger, &sck);
  62. ge25519_pack(r, &ger);
  63. /* Computation of s */
  64. for(i=0;i<32;i++)
  65. sm[i] = r[i];
  66. get_hram(hram, sm, sk+32, sm, mlen+64);
  67. sc25519_from64bytes(&scs, hram);
  68. sc25519_from32bytes(&scsk, extsk);
  69. sc25519_mul(&scs, &scs, &scsk);
  70. sc25519_add(&scs, &scs, &sck);
  71. sc25519_to32bytes(s,&scs); /* cat s */
  72. for(i=0;i<32;i++)
  73. sm[32 + i] = s[i];
  74. return 0;
  75. }
  76. int crypto_sign_open(
  77. unsigned char *m,unsigned long long *mlen,
  78. const unsigned char *sm,unsigned long long smlen,
  79. const unsigned char *pk
  80. )
  81. {
  82. int i, ret;
  83. unsigned char t2[32];
  84. ge25519 get1, get2;
  85. sc25519 schram, scs;
  86. unsigned char hram[crypto_hash_sha512_BYTES];
  87. *mlen = (unsigned long long) -1;
  88. if (smlen < 64) return -1;
  89. if (ge25519_unpackneg_vartime(&get1, pk)) return -1;
  90. get_hram(hram,sm,pk,m,smlen);
  91. sc25519_from64bytes(&schram, hram);
  92. sc25519_from32bytes(&scs, sm+32);
  93. ge25519_double_scalarmult_vartime(&get2, &get1, &schram, &ge25519_base, &scs);
  94. ge25519_pack(t2, &get2);
  95. ret = crypto_verify_32(sm, t2);
  96. if (!ret)
  97. {
  98. for(i=0;i<smlen-64;i++)
  99. m[i] = sm[i + 64];
  100. *mlen = smlen-64;
  101. }
  102. else
  103. {
  104. for(i=0;i<smlen-64;i++)
  105. m[i] = 0;
  106. }
  107. return ret;
  108. }