1
0

open.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <string.h>
  2. #include "crypto_sign.h"
  3. #include "crypto_verify_32.h"
  4. #include "crypto_hash_sha512.h"
  5. #include "ge25519.h"
  6. int crypto_sign_open(
  7. unsigned char *m,unsigned long long *mlen,
  8. const unsigned char *sm,unsigned long long smlen,
  9. const unsigned char *pk
  10. )
  11. {
  12. unsigned char pkcopy[32];
  13. unsigned char rcopy[32];
  14. unsigned char hram[64];
  15. unsigned char rcheck[32];
  16. ge25519 get1, get2;
  17. sc25519 schram, scs;
  18. if (smlen < 64) goto badsig;
  19. if (sm[63] & 224) goto badsig;
  20. if (ge25519_unpackneg_vartime(&get1,pk)) goto badsig;
  21. memmove(pkcopy,pk,32);
  22. memmove(rcopy,sm,32);
  23. sc25519_from32bytes(&scs, sm+32);
  24. memmove(m,sm,smlen);
  25. memmove(m + 32,pkcopy,32);
  26. crypto_hash_sha512(hram,m,smlen);
  27. sc25519_from64bytes(&schram, hram);
  28. ge25519_double_scalarmult_vartime(&get2, &get1, &schram, &scs);
  29. ge25519_pack(rcheck, &get2);
  30. if (crypto_verify_32(rcopy,rcheck) == 0) {
  31. memmove(m,m + 64,smlen - 64);
  32. memset(m + smlen - 64,0,64);
  33. *mlen = smlen - 64;
  34. return 0;
  35. }
  36. badsig:
  37. *mlen = (unsigned long long) -1;
  38. memset(m,0,smlen);
  39. return -1;
  40. }