| 1 | #include <string.h> | ||
| 2 | #include "ed25519.h" | ||
| 3 | #include "sha512.h" | ||
| 4 | #include "ge.h" | ||
| 5 | #include "sc.h" | ||
| 6 | |||
| 7 | |||
| 8 | 0 | void ed25519_sign(unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *private_key) { | |
| 9 | sha512_context hash; | ||
| 10 | unsigned char hram[64]; | ||
| 11 | unsigned char nonce[64]; | ||
| 12 | unsigned char az[64]; | ||
| 13 | ge_p3 R; | ||
| 14 | |||
| 15 | 0 | sha512(private_key, 32, az); | |
| 16 | 0 | az[0] &= 248; | |
| 17 | 0 | az[31] &= 63; | |
| 18 | 0 | az[31] |= 64; | |
| 19 | |||
| 20 | 0 | sha512_init(&hash); | |
| 21 | 0 | sha512_update(&hash, az + 32, 32); | |
| 22 | 0 | sha512_update(&hash, message, message_len); | |
| 23 | 0 | sha512_final(&hash, nonce); | |
| 24 | |||
| 25 | 0 | memmove(signature + 32, private_key + 32, 32); | |
| 26 | |||
| 27 | 0 | sc_reduce(nonce); | |
| 28 | 0 | ge_scalarmult_base(&R, nonce); | |
| 29 | 0 | ge_p3_tobytes(signature, &R); | |
| 30 | |||
| 31 | 0 | sha512_init(&hash); | |
| 32 | 0 | sha512_update(&hash, signature, 64); | |
| 33 | 0 | sha512_update(&hash, message, message_len); | |
| 34 | 0 | sha512_final(&hash, hram); | |
| 35 | |||
| 36 | 0 | sc_reduce(hram); | |
| 37 | 0 | sc_muladd(signature + 32, hram, az, nonce); | |
| 38 | |||
| 39 | 0 | memset(az, 0, sizeof(az)); | |
| 40 | 0 | memset(nonce, 0, sizeof(nonce)); | |
| 41 | 0 | } |