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