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