sign.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <string.h>
  2. #include "crypto_sign.h"
  3. #include "crypto_hash_sha512.h"
  4. #include "ge25519.h"
  5. int crypto_sign(
  6. unsigned char *sm,unsigned long long *smlen,
  7. const unsigned char *m,unsigned long long mlen,
  8. const unsigned char *sk
  9. )
  10. {
  11. unsigned char pk[32];
  12. unsigned char nonce[64];
  13. unsigned char hram[64];
  14. sc25519 sck, scs, scsk;
  15. ge25519 ger;
  16. /* sk: 32-byte scalar a, 32-byte randomizer z */
  17. crypto_sign_pubkey(pk,sk);
  18. /* pk: 32-byte public key A */
  19. *smlen = mlen + 64;
  20. memmove(sm + 64,m,mlen);
  21. memmove(sm + 32,sk + 32,32);
  22. /* sm: 32-byte uninit, 32-byte z, mlen-byte m */
  23. crypto_hash_sha512(nonce, sm+32, mlen+32);
  24. /* nonce: 64-byte H(z,m) */
  25. sc25519_from64bytes(&sck, nonce);
  26. ge25519_scalarmult_base(&ger, &sck);
  27. ge25519_pack(sm, &ger);
  28. /* sm: 32-byte R, 32-byte z, mlen-byte m */
  29. memmove(sm + 32,pk,32);
  30. /* sm: 32-byte R, 32-byte A, mlen-byte m */
  31. crypto_hash_sha512(hram,sm,mlen + 64);
  32. /* hram: 64-byte H(R,A,m) */
  33. sc25519_from64bytes(&scs, hram);
  34. sc25519_from32bytes(&scsk, sk);
  35. sc25519_mul(&scs, &scs, &scsk);
  36. sc25519_add(&scs, &scs, &sck);
  37. /* scs: S = nonce + H(R,A,m)a */
  38. sc25519_to32bytes(sm + 32,&scs);
  39. /* sm: 32-byte R, 32-byte S, mlen-byte m */
  40. return 0;
  41. }