| 1 |
#include <stdlib.h> |
| 2 |
#include <string.h> |
| 3 |
#include "ge.h" |
| 4 |
#include "curve_sigs.h" |
| 5 |
#include "crypto_sign.h" |
| 6 |
#include "crypto_additions.h" |
| 7 |
|
| 8 |
int curve25519_sign(unsigned char* signature_out, |
| 9 |
const unsigned char* curve25519_privkey, |
| 10 |
const unsigned char* msg, const unsigned long msg_len, |
| 11 |
const unsigned char* random) |
| 12 |
{ |
| 13 |
ge_p3 ed_pubkey_point; /* Ed25519 pubkey point */ |
| 14 |
unsigned char ed_pubkey[32]; /* Ed25519 encoded pubkey */ |
| 15 |
unsigned char *sigbuf; /* working buffer */ |
| 16 |
unsigned char sign_bit = 0; |
| 17 |
|
| 18 |
if ((sigbuf = malloc(msg_len + 128)) == 0) { |
| 19 |
memset(signature_out, 0, 64); |
| 20 |
return -1; |
| 21 |
} |
| 22 |
|
| 23 |
/* Convert the Curve25519 privkey to an Ed25519 public key */ |
| 24 |
ge_scalarmult_base(&ed_pubkey_point, curve25519_privkey); |
| 25 |
ge_p3_tobytes(ed_pubkey, &ed_pubkey_point); |
| 26 |
sign_bit = ed_pubkey[31] & 0x80; |
| 27 |
|
| 28 |
/* Perform an Ed25519 signature with explicit private key */ |
| 29 |
crypto_sign_modified(sigbuf, msg, msg_len, curve25519_privkey, |
| 30 |
ed_pubkey, random); |
| 31 |
memmove(signature_out, sigbuf, 64); |
| 32 |
|
| 33 |
/* Encode the sign bit into signature (in unused high bit of S) */ |
| 34 |
signature_out[63] &= 0x7F; /* bit should be zero already, but just in case */ |
| 35 |
signature_out[63] |= sign_bit; |
| 36 |
|
| 37 |
free(sigbuf); |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
int curve25519_verify(const unsigned char* signature, |
| 42 |
const unsigned char* curve25519_pubkey, |
| 43 |
const unsigned char* msg, const unsigned long msg_len) |
| 44 |
{ |
| 45 |
fe u; |
| 46 |
fe y; |
| 47 |
unsigned char ed_pubkey[32]; |
| 48 |
unsigned char *verifybuf = NULL; /* working buffer */ |
| 49 |
unsigned char *verifybuf2 = NULL; /* working buffer #2 */ |
| 50 |
int result; |
| 51 |
|
| 52 |
if ((verifybuf = malloc(msg_len + 64)) == 0) { |
| 53 |
result = -1; |
| 54 |
goto err; |
| 55 |
} |
| 56 |
|
| 57 |
if ((verifybuf2 = malloc(msg_len + 64)) == 0) { |
| 58 |
result = -1; |
| 59 |
goto err; |
| 60 |
} |
| 61 |
|
| 62 |
/* Convert the Curve25519 public key into an Ed25519 public key. In |
| 63 |
particular, convert Curve25519's "montgomery" x-coordinate (u) into an |
| 64 |
Ed25519 "edwards" y-coordinate: |
| 65 |
|
| 66 |
y = (u - 1) / (u + 1) |
| 67 |
|
| 68 |
NOTE: u=-1 is converted to y=0 since fe_invert is mod-exp |
| 69 |
|
| 70 |
Then move the sign bit into the pubkey from the signature. |
| 71 |
*/ |
| 72 |
fe_frombytes(u, curve25519_pubkey); |
| 73 |
fe_montx_to_edy(y, u); |
| 74 |
fe_tobytes(ed_pubkey, y); |
| 75 |
|
| 76 |
/* Copy the sign bit, and remove it from signature */ |
| 77 |
ed_pubkey[31] &= 0x7F; /* bit should be zero already, but just in case */ |
| 78 |
ed_pubkey[31] |= (signature[63] & 0x80); |
| 79 |
memmove(verifybuf, signature, 64); |
| 80 |
verifybuf[63] &= 0x7F; |
| 81 |
|
| 82 |
memmove(verifybuf+64, msg, msg_len); |
| 83 |
|
| 84 |
/* Then perform a normal Ed25519 verification, return 0 on success */ |
| 85 |
/* The below call has a strange API: */ |
| 86 |
/* verifybuf = R || S || message */ |
| 87 |
/* verifybuf2 = internal to next call gets a copy of verifybuf, S gets |
| 88 |
replaced with pubkey for hashing */ |
| 89 |
result = crypto_sign_open_modified(verifybuf2, verifybuf, 64 + msg_len, ed_pubkey); |
| 90 |
|
| 91 |
err: |
| 92 |
|
| 93 |
if (verifybuf != NULL) { |
| 94 |
free(verifybuf); |
| 95 |
} |
| 96 |
|
| 97 |
if (verifybuf2 != NULL) { |
| 98 |
free(verifybuf2); |
| 99 |
} |
| 100 |
|
| 101 |
return result; |
| 102 |
} |