2020-05-22 13:18:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2020, The PurpleI2P Project
|
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2020-01-21 17:18:31 +00:00
|
|
|
#include <openssl/rand.h>
|
2019-12-05 21:03:11 +00:00
|
|
|
#include "Crypto.h"
|
2019-12-04 20:37:24 +00:00
|
|
|
#include "Elligator.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace crypto
|
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2019-12-04 20:37:24 +00:00
|
|
|
Elligator2::Elligator2 ()
|
|
|
|
{
|
2019-12-06 19:54:15 +00:00
|
|
|
// TODO: share with Ed22519
|
|
|
|
p = BN_new ();
|
|
|
|
// 2^255-19
|
|
|
|
BN_set_bit (p, 255); // 2^255
|
|
|
|
BN_sub_word (p, 19);
|
|
|
|
p38 = BN_dup (p); BN_add_word (p38, 3); BN_div_word (p38, 8); // (p+3)/8
|
|
|
|
p12 = BN_dup (p); BN_sub_word (p12, 1); BN_div_word (p12, 2); // (p-1)/2
|
2019-12-07 01:29:03 +00:00
|
|
|
p14 = BN_dup (p); BN_sub_word (p14, 1); BN_div_word (p14, 4); // (p-1)/4
|
2019-12-06 19:54:15 +00:00
|
|
|
|
2019-12-10 17:51:39 +00:00
|
|
|
A = BN_new (); BN_set_word (A, 486662);
|
|
|
|
nA = BN_new (); BN_sub (nA, p, A);
|
2019-12-05 21:13:59 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
BN_CTX * ctx = BN_CTX_new ();
|
|
|
|
// calculate sqrt(-1)
|
2019-12-06 19:54:15 +00:00
|
|
|
sqrtn1 = BN_new ();
|
2020-03-01 10:25:50 +00:00
|
|
|
BN_set_word (sqrtn1, 2);
|
2019-12-07 01:29:03 +00:00
|
|
|
BN_mod_exp (sqrtn1, sqrtn1, p14, p, ctx); // 2^((p-1)/4
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2019-12-07 01:29:03 +00:00
|
|
|
u = BN_new (); BN_set_word (u, 2);
|
2020-03-01 10:25:50 +00:00
|
|
|
iu = BN_new (); BN_mod_inverse (iu, u, p, ctx);
|
|
|
|
|
2019-12-05 21:03:11 +00:00
|
|
|
BN_CTX_free (ctx);
|
2019-12-04 20:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Elligator2::~Elligator2 ()
|
|
|
|
{
|
2019-12-07 01:29:03 +00:00
|
|
|
BN_free (p); BN_free (p38); BN_free (p12); BN_free (p14);
|
2020-03-01 10:25:50 +00:00
|
|
|
BN_free (sqrtn1); BN_free (A); BN_free (nA);
|
|
|
|
BN_free (u); BN_free (iu);
|
2019-12-05 21:03:11 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 22:53:48 +00:00
|
|
|
bool Elligator2::Encode (const uint8_t * key, uint8_t * encoded, bool highY, bool random) const
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
2019-12-10 15:45:08 +00:00
|
|
|
bool ret = true;
|
2019-12-05 21:03:11 +00:00
|
|
|
BN_CTX * ctx = BN_CTX_new ();
|
|
|
|
BN_CTX_start (ctx);
|
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
uint8_t key1[32];
|
2019-12-09 21:11:46 +00:00
|
|
|
for (size_t i = 0; i < 16; i++) // from Little Endian
|
|
|
|
{
|
2019-12-10 17:51:39 +00:00
|
|
|
key1[i] = key[31 - i];
|
|
|
|
key1[31 - i] = key[i];
|
2019-12-09 21:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BIGNUM * x = BN_CTX_get (ctx); BN_bin2bn (key1, 32, x);
|
2019-12-07 01:29:03 +00:00
|
|
|
BIGNUM * xA = BN_CTX_get (ctx); BN_add (xA, x, A); // x + A
|
|
|
|
BN_sub (xA, p, xA); // p - (x + A)
|
2019-12-05 21:03:11 +00:00
|
|
|
|
2019-12-10 15:45:08 +00:00
|
|
|
BIGNUM * uxxA = BN_CTX_get (ctx); // u*x*xA
|
|
|
|
BN_mod_mul (uxxA, u, x, p, ctx);
|
2020-03-01 10:25:50 +00:00
|
|
|
BN_mod_mul (uxxA, uxxA, xA, p, ctx);
|
|
|
|
|
2019-12-10 15:45:08 +00:00
|
|
|
if (Legendre (uxxA, ctx) != -1)
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
|
|
|
uint8_t randByte = 0; // random highest bits and high y
|
2020-01-21 22:53:48 +00:00
|
|
|
if (random)
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
|
|
|
RAND_bytes (&randByte, 1);
|
|
|
|
highY = randByte & 0x01;
|
2020-01-21 22:53:48 +00:00
|
|
|
}
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2019-12-10 15:45:08 +00:00
|
|
|
BIGNUM * r = BN_CTX_get (ctx);
|
2019-12-10 19:10:12 +00:00
|
|
|
if (highY)
|
|
|
|
{
|
|
|
|
BN_mod_inverse (r, x, p, ctx);
|
2020-03-01 10:25:50 +00:00
|
|
|
BN_mod_mul (r, r, xA, p, ctx);
|
2019-12-10 19:10:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
BN_mod_inverse (r, xA, p, ctx);
|
2019-12-10 19:10:12 +00:00
|
|
|
BN_mod_mul (r, r, x, p, ctx);
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
|
|
|
BN_mod_mul (r, r, iu, p, ctx);
|
|
|
|
|
2019-12-10 15:45:08 +00:00
|
|
|
SquareRoot (r, r, ctx);
|
|
|
|
bn2buf (r, encoded, 32);
|
2020-01-21 22:53:48 +00:00
|
|
|
|
|
|
|
if (random)
|
2020-03-01 10:25:50 +00:00
|
|
|
encoded[0] |= (randByte & 0xC0); // copy two highest bits from randByte
|
2019-12-10 15:45:08 +00:00
|
|
|
for (size_t i = 0; i < 16; i++) // To Little Endian
|
|
|
|
{
|
|
|
|
uint8_t tmp = encoded[i];
|
2019-12-10 17:51:39 +00:00
|
|
|
encoded[i] = encoded[31 - i];
|
|
|
|
encoded[31 - i] = tmp;
|
2019-12-10 15:45:08 +00:00
|
|
|
}
|
2019-12-09 21:11:46 +00:00
|
|
|
}
|
2019-12-10 15:45:08 +00:00
|
|
|
else
|
|
|
|
ret = false;
|
2019-12-09 21:11:46 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
BN_CTX_end (ctx);
|
2019-12-09 21:11:46 +00:00
|
|
|
BN_CTX_free (ctx);
|
2019-12-10 15:45:08 +00:00
|
|
|
return ret;
|
2019-12-09 21:11:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:45:08 +00:00
|
|
|
bool Elligator2::Decode (const uint8_t * encoded, uint8_t * key) const
|
2019-12-09 21:11:46 +00:00
|
|
|
{
|
2019-12-10 15:45:08 +00:00
|
|
|
bool ret = true;
|
2019-12-09 21:11:46 +00:00
|
|
|
BN_CTX * ctx = BN_CTX_new ();
|
|
|
|
BN_CTX_start (ctx);
|
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
uint8_t encoded1[32];
|
2019-12-09 21:11:46 +00:00
|
|
|
for (size_t i = 0; i < 16; i++) // from Little Endian
|
|
|
|
{
|
2019-12-10 17:51:39 +00:00
|
|
|
encoded1[i] = encoded[31 - i];
|
|
|
|
encoded1[31 - i] = encoded[i];
|
2019-12-09 21:11:46 +00:00
|
|
|
}
|
2020-03-01 10:25:50 +00:00
|
|
|
encoded1[0] &= 0x3F; // drop two highest bits
|
2019-12-09 21:11:46 +00:00
|
|
|
|
|
|
|
BIGNUM * r = BN_CTX_get (ctx); BN_bin2bn (encoded1, 32, r);
|
|
|
|
|
2019-12-10 18:40:04 +00:00
|
|
|
if (BN_cmp (r, p12) <= 0) // r < (p-1)/2
|
2019-12-09 21:11:46 +00:00
|
|
|
{
|
2019-12-10 15:45:08 +00:00
|
|
|
// v = -A/(1+u*r^2)
|
2020-03-01 10:25:50 +00:00
|
|
|
BIGNUM * v = BN_CTX_get (ctx); BN_mod_sqr (v, r, p, ctx);
|
2019-12-10 15:45:08 +00:00
|
|
|
BN_mod_mul (v, v, u, p, ctx);
|
|
|
|
BN_add_word (v, 1);
|
2020-03-01 10:25:50 +00:00
|
|
|
BN_mod_inverse (v, v, p, ctx);
|
2019-12-10 15:45:08 +00:00
|
|
|
BN_mod_mul (v, v, nA, p, ctx);
|
|
|
|
|
|
|
|
BIGNUM * vpA = BN_CTX_get (ctx);
|
|
|
|
BN_add (vpA, v, A); // v + A
|
|
|
|
// t = v^3+A*v^2+v = v^2*(v+A)+v
|
2020-03-01 10:25:50 +00:00
|
|
|
BIGNUM * t = BN_CTX_get (ctx); BN_mod_sqr (t, v, p, ctx);
|
|
|
|
BN_mod_mul (t, t, vpA, p, ctx);
|
|
|
|
BN_mod_add (t, t, v, p, ctx);
|
2019-12-10 15:45:08 +00:00
|
|
|
|
|
|
|
int legendre = Legendre (t, ctx);
|
|
|
|
BIGNUM * x = BN_CTX_get (ctx);
|
|
|
|
if (legendre == 1)
|
|
|
|
BN_copy (x, v);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BN_sub (x, p, v);
|
|
|
|
BN_mod_sub (x, x, A, p, ctx);
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bn2buf (x, key, 32);
|
2019-12-10 15:45:08 +00:00
|
|
|
for (size_t i = 0; i < 16; i++) // To Little Endian
|
|
|
|
{
|
|
|
|
uint8_t tmp = key[i];
|
2019-12-10 17:51:39 +00:00
|
|
|
key[i] = key[31 - i];
|
|
|
|
key[31 - i] = tmp;
|
2019-12-10 15:45:08 +00:00
|
|
|
}
|
2019-12-09 21:11:46 +00:00
|
|
|
}
|
2019-12-10 15:45:08 +00:00
|
|
|
else
|
|
|
|
ret = false;
|
2019-12-05 21:03:11 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
BN_CTX_end (ctx);
|
2019-12-05 21:03:11 +00:00
|
|
|
BN_CTX_free (ctx);
|
2019-12-10 15:45:08 +00:00
|
|
|
|
|
|
|
return ret;
|
2019-12-04 20:37:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 01:29:03 +00:00
|
|
|
void Elligator2::SquareRoot (const BIGNUM * x, BIGNUM * r, BN_CTX * ctx) const
|
|
|
|
{
|
|
|
|
BIGNUM * t = BN_CTX_get (ctx);
|
|
|
|
BN_mod_exp (t, x, p14, p, ctx); // t = x^((p-1)/4)
|
|
|
|
BN_mod_exp (r, x, p38, p, ctx); // r = x^((p+3)/8)
|
|
|
|
BN_add_word (t, 1);
|
|
|
|
|
|
|
|
if (!BN_cmp (t, p))
|
2020-03-01 10:25:50 +00:00
|
|
|
BN_mod_mul (r, r, sqrtn1, p, ctx);
|
2019-12-07 01:29:03 +00:00
|
|
|
|
|
|
|
if (BN_cmp (r, p12) > 0) // r > (p-1)/2
|
|
|
|
BN_sub (r, p, r);
|
2020-03-01 10:25:50 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:45:08 +00:00
|
|
|
int Elligator2::Legendre (const BIGNUM * a, BN_CTX * ctx) const
|
|
|
|
{
|
2019-12-10 15:53:39 +00:00
|
|
|
// assume a < p, so don't check for a % p = 0, but a = 0 only
|
2020-03-01 10:25:50 +00:00
|
|
|
if (BN_is_zero(a)) return 0;
|
2019-12-10 15:45:08 +00:00
|
|
|
BIGNUM * r = BN_CTX_get (ctx);
|
2022-05-20 16:56:05 +00:00
|
|
|
BN_mod_exp (r, a, p12, p, ctx); // r = a^((p-1)/2) mod p
|
2020-03-01 10:25:50 +00:00
|
|
|
if (BN_is_word(r, 1))
|
2019-12-10 15:45:08 +00:00
|
|
|
return 1;
|
2020-03-01 10:25:50 +00:00
|
|
|
else if (BN_is_zero(r))
|
2019-12-10 15:45:08 +00:00
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-12-04 20:37:24 +00:00
|
|
|
static std::unique_ptr<Elligator2> g_Elligator;
|
|
|
|
std::unique_ptr<Elligator2>& GetElligator ()
|
|
|
|
{
|
|
|
|
if (!g_Elligator)
|
|
|
|
{
|
|
|
|
auto el = new Elligator2();
|
|
|
|
if (!g_Elligator) // make sure it was not created already
|
|
|
|
g_Elligator.reset (el);
|
|
|
|
else
|
|
|
|
delete el;
|
|
|
|
}
|
|
|
|
return g_Elligator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|