2016-04-22 15:47:48 +00:00
|
|
|
/*
|
|
|
|
Public domain by Andrew M. <liquidsun@gmail.com>
|
|
|
|
|
|
|
|
Ed25519 reference implementation using Ed25519-donna
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ed25519-donna.h"
|
|
|
|
#include "ed25519.h"
|
2017-03-28 16:32:58 +00:00
|
|
|
#include "ed25519-hash-custom.h"
|
2016-04-22 15:47:48 +00:00
|
|
|
|
2017-03-28 17:48:36 +00:00
|
|
|
#include "curve25519-donna-scalarmult-base.h"
|
|
|
|
|
2016-04-22 15:47:48 +00:00
|
|
|
/*
|
|
|
|
Generates a (extsk[0..31]) and aExt (extsk[32..63])
|
|
|
|
*/
|
|
|
|
DONNA_INLINE static void
|
|
|
|
ed25519_extsk(hash_512bits extsk, const ed25519_secret_key sk) {
|
|
|
|
ed25519_hash(extsk, sk, 32);
|
|
|
|
extsk[0] &= 248;
|
|
|
|
extsk[31] &= 127;
|
|
|
|
extsk[31] |= 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ed25519_hram(hash_512bits hram, const ed25519_signature RS, const ed25519_public_key pk, const unsigned char *m, size_t mlen) {
|
|
|
|
ed25519_hash_context ctx;
|
|
|
|
ed25519_hash_init(&ctx);
|
|
|
|
ed25519_hash_update(&ctx, RS, 32);
|
|
|
|
ed25519_hash_update(&ctx, pk, 32);
|
|
|
|
ed25519_hash_update(&ctx, m, mlen);
|
|
|
|
ed25519_hash_final(&ctx, hram);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-28 17:48:36 +00:00
|
|
|
ed25519_publickey(const ed25519_secret_key sk, ed25519_public_key pk) {
|
2016-04-22 15:47:48 +00:00
|
|
|
bignum256modm a;
|
|
|
|
ge25519 ALIGN(16) A;
|
|
|
|
hash_512bits extsk;
|
|
|
|
|
|
|
|
/* A = aB */
|
|
|
|
ed25519_extsk(extsk, sk);
|
|
|
|
expand256_modm(a, extsk, 32);
|
|
|
|
ge25519_scalarmult_base_niels(&A, ge25519_niels_base_multiples, a);
|
|
|
|
ge25519_pack(pk, &A);
|
|
|
|
}
|
|
|
|
|
2017-04-01 22:08:04 +00:00
|
|
|
int
|
|
|
|
ed25519_cosi_combine_publickeys(ed25519_public_key res, const ed25519_public_key *pks, size_t n) {
|
|
|
|
size_t i = 0;
|
|
|
|
ge25519 P;
|
|
|
|
ge25519_pniels sump;
|
|
|
|
ge25519_p1p1 sump1;
|
|
|
|
|
|
|
|
if (n == 1) {
|
|
|
|
memcpy(res, pks, sizeof(ed25519_public_key));
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-01 23:28:18 +00:00
|
|
|
if (!ge25519_unpack_negative_vartime(&P, pks[i++])) {
|
2017-04-01 22:08:04 +00:00
|
|
|
return -1;
|
2017-04-01 23:28:18 +00:00
|
|
|
}
|
2017-04-01 22:08:04 +00:00
|
|
|
ge25519_full_to_pniels(&sump, &P);
|
2017-04-01 23:28:18 +00:00
|
|
|
while (i < n - 1) {
|
|
|
|
if (!ge25519_unpack_negative_vartime(&P, pks[i++])) {
|
2017-04-01 22:08:04 +00:00
|
|
|
return -1;
|
2017-04-01 23:28:18 +00:00
|
|
|
}
|
2017-04-01 22:08:04 +00:00
|
|
|
ge25519_pnielsadd(&sump, &P, &sump);
|
|
|
|
}
|
2017-04-01 23:28:18 +00:00
|
|
|
if (!ge25519_unpack_negative_vartime(&P, pks[i++])) {
|
2017-04-01 22:08:04 +00:00
|
|
|
return -1;
|
2017-04-01 23:28:18 +00:00
|
|
|
}
|
2017-04-01 22:08:04 +00:00
|
|
|
ge25519_pnielsadd_p1p1(&sump1, &P, &sump, 0);
|
|
|
|
ge25519_p1p1_to_partial(&P, &sump1);
|
|
|
|
curve25519_neg(P.x, P.x);
|
|
|
|
ge25519_pack(res, &P);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ed25519_cosi_combine_signatures(ed25519_signature res, const ed25519_public_key R, const ed25519_cosi_signature *sigs, size_t n) {
|
|
|
|
bignum256modm s, t;
|
2017-04-01 23:28:18 +00:00
|
|
|
size_t i = 0;
|
2017-04-01 22:08:04 +00:00
|
|
|
|
|
|
|
expand256_modm(s, sigs[i++], 32);
|
|
|
|
while (i < n) {
|
|
|
|
expand256_modm(t, sigs[i++], 32);
|
|
|
|
add256_modm(s, s, t);
|
|
|
|
}
|
|
|
|
memcpy(res, R, 32);
|
|
|
|
contract256_modm(res + 32, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ed25519_cosi_sign(const unsigned char *m, size_t mlen, const ed25519_secret_key sk, const ed25519_secret_key nonce, const ed25519_public_key R, const ed25519_public_key pk, ed25519_cosi_signature sig) {
|
|
|
|
bignum256modm r, S, a;
|
|
|
|
hash_512bits extsk, extnonce, hram;
|
|
|
|
|
|
|
|
ed25519_extsk(extsk, sk);
|
|
|
|
ed25519_extsk(extnonce, nonce);
|
|
|
|
|
|
|
|
/* r = nonce */
|
|
|
|
expand256_modm(r, extnonce, 32);
|
|
|
|
|
|
|
|
/* S = H(R,A,m).. */
|
|
|
|
ed25519_hram(hram, R, pk, m, mlen);
|
|
|
|
expand256_modm(S, hram, 64);
|
|
|
|
|
|
|
|
/* S = H(R,A,m)a */
|
|
|
|
expand256_modm(a, extsk, 32);
|
|
|
|
mul256_modm(S, S, a);
|
|
|
|
|
|
|
|
/* S = (r + H(R,A,m)a) */
|
|
|
|
add256_modm(S, S, r);
|
|
|
|
|
|
|
|
/* S = (r + H(R,A,m)a) mod L */
|
|
|
|
contract256_modm(sig, S);
|
|
|
|
}
|
|
|
|
|
2016-04-22 15:47:48 +00:00
|
|
|
void
|
2017-03-28 17:48:36 +00:00
|
|
|
ed25519_sign(const unsigned char *m, size_t mlen, const ed25519_secret_key sk, const ed25519_public_key pk, ed25519_signature RS) {
|
2016-04-22 15:47:48 +00:00
|
|
|
ed25519_hash_context ctx;
|
|
|
|
bignum256modm r, S, a;
|
|
|
|
ge25519 ALIGN(16) R;
|
|
|
|
hash_512bits extsk, hashr, hram;
|
|
|
|
|
|
|
|
ed25519_extsk(extsk, sk);
|
|
|
|
|
|
|
|
/* r = H(aExt[32..64], m) */
|
|
|
|
ed25519_hash_init(&ctx);
|
|
|
|
ed25519_hash_update(&ctx, extsk + 32, 32);
|
|
|
|
ed25519_hash_update(&ctx, m, mlen);
|
|
|
|
ed25519_hash_final(&ctx, hashr);
|
|
|
|
expand256_modm(r, hashr, 64);
|
|
|
|
|
|
|
|
/* R = rB */
|
|
|
|
ge25519_scalarmult_base_niels(&R, ge25519_niels_base_multiples, r);
|
|
|
|
ge25519_pack(RS, &R);
|
|
|
|
|
|
|
|
/* S = H(R,A,m).. */
|
|
|
|
ed25519_hram(hram, RS, pk, m, mlen);
|
|
|
|
expand256_modm(S, hram, 64);
|
|
|
|
|
|
|
|
/* S = H(R,A,m)a */
|
|
|
|
expand256_modm(a, extsk, 32);
|
|
|
|
mul256_modm(S, S, a);
|
|
|
|
|
|
|
|
/* S = (r + H(R,A,m)a) */
|
|
|
|
add256_modm(S, S, r);
|
|
|
|
|
2017-04-01 22:08:04 +00:00
|
|
|
/* S = (r + H(R,A,m)a) mod L */
|
2016-04-22 15:47:48 +00:00
|
|
|
contract256_modm(RS + 32, S);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-03-28 17:48:36 +00:00
|
|
|
ed25519_sign_open(const unsigned char *m, size_t mlen, const ed25519_public_key pk, const ed25519_signature RS) {
|
2016-04-22 15:47:48 +00:00
|
|
|
ge25519 ALIGN(16) R, A;
|
|
|
|
hash_512bits hash;
|
|
|
|
bignum256modm hram, S;
|
|
|
|
unsigned char checkR[32];
|
|
|
|
|
|
|
|
if ((RS[63] & 224) || !ge25519_unpack_negative_vartime(&A, pk))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* hram = H(R,A,m) */
|
|
|
|
ed25519_hram(hash, RS, pk, m, mlen);
|
|
|
|
expand256_modm(hram, hash, 64);
|
|
|
|
|
|
|
|
/* S */
|
|
|
|
expand256_modm(S, RS + 32, 32);
|
|
|
|
|
|
|
|
/* SB - H(R,A,m)A */
|
|
|
|
ge25519_double_scalarmult_vartime(&R, &A, hram, S);
|
|
|
|
ge25519_pack(checkR, &R);
|
|
|
|
|
|
|
|
/* check that R = SB - H(R,A,m)A */
|
|
|
|
return ed25519_verify(RS, checkR, 32) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Fast Curve25519 basepoint scalar multiplication
|
|
|
|
*/
|
|
|
|
void
|
2017-03-28 21:05:59 +00:00
|
|
|
curve25519_scalarmult_basepoint(curve25519_key pk, const curve25519_key e) {
|
|
|
|
curve25519_key ec;
|
2016-04-22 15:47:48 +00:00
|
|
|
bignum256modm s;
|
|
|
|
bignum25519 ALIGN(16) yplusz, zminusy;
|
|
|
|
ge25519 ALIGN(16) p;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* clamp */
|
|
|
|
for (i = 0; i < 32; i++) ec[i] = e[i];
|
|
|
|
ec[0] &= 248;
|
|
|
|
ec[31] &= 127;
|
|
|
|
ec[31] |= 64;
|
|
|
|
|
|
|
|
expand_raw256_modm(s, ec);
|
|
|
|
|
|
|
|
/* scalar * basepoint */
|
|
|
|
ge25519_scalarmult_base_niels(&p, ge25519_niels_base_multiples, s);
|
|
|
|
|
|
|
|
/* u = (y + z) / (z - y) */
|
|
|
|
curve25519_add(yplusz, p.y, p.z);
|
|
|
|
curve25519_sub(zminusy, p.z, p.y);
|
|
|
|
curve25519_recip(zminusy, zminusy);
|
|
|
|
curve25519_mul(yplusz, yplusz, zminusy);
|
|
|
|
curve25519_contract(pk, yplusz);
|
|
|
|
}
|
|
|
|
|
2017-03-28 17:48:36 +00:00
|
|
|
void
|
2017-03-28 21:05:59 +00:00
|
|
|
curve25519_scalarmult(curve25519_key mypublic, const curve25519_key secret, const curve25519_key basepoint) {
|
2017-03-28 17:48:36 +00:00
|
|
|
curve25519_key e;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0;i < 32;++i) e[i] = secret[i];
|
|
|
|
e[0] &= 0xf8;
|
|
|
|
e[31] &= 0x7f;
|
|
|
|
e[31] |= 0x40;
|
|
|
|
curve25519_scalarmult_donna(mypublic, e, basepoint);
|
|
|
|
}
|