open.c 960 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* (Modified by Tor to verify signature separately from message) */
  2. #include "crypto_sign.h"
  3. #include <string.h>
  4. #include "crypto_hash_sha512.h"
  5. #include "crypto_verify_32.h"
  6. #include "ge.h"
  7. #include "sc.h"
  8. /* 'signature' must be 64-bytes long. */
  9. int crypto_sign_open(
  10. const unsigned char *signature,
  11. const unsigned char *m, size_t mlen,
  12. const unsigned char *pk
  13. )
  14. {
  15. unsigned char pkcopy[32];
  16. unsigned char rcopy[32];
  17. unsigned char scopy[32];
  18. unsigned char h[64];
  19. unsigned char rcheck[32];
  20. ge_p3 A;
  21. ge_p2 R;
  22. if (signature[63] & 224) goto badsig;
  23. if (ge_frombytes_negate_vartime(&A,pk) != 0) goto badsig;
  24. memmove(pkcopy,pk,32);
  25. memmove(rcopy,signature,32);
  26. memmove(scopy,signature + 32,32);
  27. crypto_hash_sha512_3(h, rcopy, 32, pkcopy, 32, m, mlen);
  28. sc_reduce(h);
  29. ge_double_scalarmult_vartime(&R,h,&A,scopy);
  30. ge_tobytes(rcheck,&R);
  31. if (crypto_verify_32(rcheck,rcopy) == 0) {
  32. return 0;
  33. }
  34. badsig:
  35. return -1;
  36. }