2010-01-14 07:19:10 +00:00
|
|
|
// Copyright 2007,2008 Segher Boessenkool <segher@kernel.crashing.org>
|
|
|
|
// Licensed under the terms of the GNU GPL, version 2
|
|
|
|
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <cstdio>
|
2018-05-15 22:27:43 +00:00
|
|
|
#include <cstring>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-07-08 12:29:26 +00:00
|
|
|
#include "Common/Crypto/bn.h"
|
2010-01-14 07:19:10 +00:00
|
|
|
|
2018-05-15 22:27:43 +00:00
|
|
|
static void bn_zero(u8* d, int n)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
2018-05-15 22:27:43 +00:00
|
|
|
std::memset(d, 0, n);
|
2010-01-14 07:19:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 22:27:43 +00:00
|
|
|
static void bn_copy(u8* d, const u8* a, int n)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
2018-05-15 22:27:43 +00:00
|
|
|
std::memcpy(d, a, n);
|
2010-01-14 07:19:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 22:27:43 +00:00
|
|
|
int bn_compare(const u8* a, const u8* b, int n)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
2018-05-15 22:27:43 +00:00
|
|
|
return std::memcmp(a, b, n);
|
2010-01-14 07:19:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 22:27:43 +00:00
|
|
|
void bn_sub_modulus(u8* a, const u8* N, int n)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
2018-05-15 22:27:43 +00:00
|
|
|
u8 c = 0;
|
|
|
|
for (int i = n - 1; i >= 0; --i)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
2018-05-15 22:27:43 +00:00
|
|
|
u32 dig = N[i] + c;
|
2010-01-14 07:19:10 +00:00
|
|
|
c = (a[i] < dig);
|
|
|
|
a[i] -= dig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:27:43 +00:00
|
|
|
void bn_add(u8* d, const u8* a, const u8* b, const u8* N, int n)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
2018-05-15 22:27:43 +00:00
|
|
|
u8 c = 0;
|
|
|
|
for (int i = n - 1; i >= 0; --i)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
2018-05-15 22:27:43 +00:00
|
|
|
u32 dig = a[i] + b[i] + c;
|
2010-01-14 07:19:10 +00:00
|
|
|
c = (dig >= 0x100);
|
|
|
|
d[i] = dig;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-01-14 07:19:10 +00:00
|
|
|
if (c)
|
|
|
|
bn_sub_modulus(d, N, n);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-01-14 07:19:10 +00:00
|
|
|
if (bn_compare(d, N, n) >= 0)
|
|
|
|
bn_sub_modulus(d, N, n);
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:27:43 +00:00
|
|
|
void bn_mul(u8* d, const u8* a, const u8* b, const u8* N, int n)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
|
|
|
bn_zero(d, n);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-05-15 22:27:43 +00:00
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
for (u8 mask = 0x80; mask != 0; mask >>= 1)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
|
|
|
bn_add(d, d, d, N, n);
|
|
|
|
if ((a[i] & mask) != 0)
|
|
|
|
bn_add(d, d, b, N, n);
|
|
|
|
}
|
2018-05-15 22:27:43 +00:00
|
|
|
}
|
2010-01-14 07:19:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 22:27:43 +00:00
|
|
|
void bn_exp(u8* d, const u8* a, const u8* N, int n, const u8* e, int en)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
|
|
|
u8 t[512];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-01-14 07:19:10 +00:00
|
|
|
bn_zero(d, n);
|
|
|
|
d[n - 1] = 1;
|
2018-05-15 22:27:43 +00:00
|
|
|
for (int i = 0; i < en; i++)
|
|
|
|
{
|
|
|
|
for (u8 mask = 0x80; mask != 0; mask >>= 1)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
|
|
|
bn_mul(t, d, d, N, n);
|
|
|
|
if ((e[i] & mask) != 0)
|
|
|
|
bn_mul(d, t, a, N, n);
|
|
|
|
else
|
|
|
|
bn_copy(d, t, n);
|
|
|
|
}
|
2018-05-15 22:27:43 +00:00
|
|
|
}
|
2010-01-14 07:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// only for prime N -- stupid but lazy, see if I care
|
2018-05-15 22:27:43 +00:00
|
|
|
void bn_inv(u8* d, const u8* a, const u8* N, int n)
|
2010-01-14 07:19:10 +00:00
|
|
|
{
|
|
|
|
u8 t[512], s[512];
|
|
|
|
|
|
|
|
bn_copy(t, N, n);
|
|
|
|
bn_zero(s, n);
|
|
|
|
s[n - 1] = 2;
|
|
|
|
bn_sub_modulus(t, s, n);
|
|
|
|
bn_exp(d, a, N, n, t, n);
|
|
|
|
}
|