athena/src/bn.cpp

123 lines
2.2 KiB
C++
Raw Normal View History

2013-02-16 04:22:16 +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
#include <string.h>
#include <stdio.h>
#include "bn.h"
2015-05-19 03:24:56 +00:00
static void bn_zero(atUint8* d, atUint32 n)
2013-02-16 04:22:16 +00:00
{
2013-07-21 03:57:20 +00:00
memset(d, 0, n);
2013-02-16 04:22:16 +00:00
}
2015-05-19 03:24:56 +00:00
static void bn_copy(atUint8* d, atUint8* a, atUint32 n)
2013-02-16 04:22:16 +00:00
{
2013-07-21 03:57:20 +00:00
memcpy(d, a, n);
2013-02-16 04:22:16 +00:00
}
2015-05-19 03:24:56 +00:00
int bn_compare(atUint8* a, atUint8* b, atUint32 n)
2013-02-16 04:22:16 +00:00
{
atUint32 i;
2013-02-16 04:22:16 +00:00
2015-05-19 03:24:56 +00:00
for (i = 0; i < n; i++)
{
2013-07-21 03:57:20 +00:00
if (a[i] < b[i])
return -1;
2015-05-19 03:24:56 +00:00
2013-07-21 03:57:20 +00:00
if (a[i] > b[i])
return 1;
}
2013-02-16 04:22:16 +00:00
2013-07-21 03:57:20 +00:00
return 0;
2013-02-16 04:22:16 +00:00
}
2015-05-19 03:24:56 +00:00
void bn_sub_modulus(atUint8* a, atUint8* N, atUint32 n)
2013-02-16 04:22:16 +00:00
{
atUint32 i;
atUint32 dig;
atUint8 c;
2013-02-16 04:22:16 +00:00
2013-07-21 03:57:20 +00:00
c = 0;
2015-05-19 03:24:56 +00:00
for (i = n - 1; i < n; i--)
{
2013-07-21 03:57:20 +00:00
dig = N[i] + c;
c = (a[i] < dig);
a[i] -= dig;
}
2013-02-16 04:22:16 +00:00
}
2015-05-19 03:24:56 +00:00
void bn_add(atUint8* d, atUint8* a, atUint8* b, atUint8* N, atUint32 n)
2013-02-16 04:22:16 +00:00
{
atUint32 i;
atUint32 dig;
atUint8 c;
2013-02-16 04:22:16 +00:00
2013-07-21 03:57:20 +00:00
c = 0;
2015-05-19 03:24:56 +00:00
for (i = n - 1; i < n; i--)
{
2013-07-21 03:57:20 +00:00
dig = a[i] + b[i] + c;
c = (dig >= 0x100);
d[i] = dig;
}
2013-02-16 04:22:16 +00:00
2013-07-21 03:57:20 +00:00
if (c)
bn_sub_modulus(d, N, n);
2013-02-16 04:22:16 +00:00
2013-07-21 03:57:20 +00:00
if (bn_compare(d, N, n) >= 0)
bn_sub_modulus(d, N, n);
2013-02-16 04:22:16 +00:00
}
2015-05-19 03:24:56 +00:00
void bn_mul(atUint8* d, atUint8* a, atUint8* b, atUint8* N, atUint32 n)
2013-02-16 04:22:16 +00:00
{
atUint32 i;
atUint8 mask;
2013-02-16 04:22:16 +00:00
2013-07-21 03:57:20 +00:00
bn_zero(d, n);
2013-02-16 04:22:16 +00:00
2013-07-21 03:57:20 +00:00
for (i = 0; i < n; i++)
2015-05-19 03:24:56 +00:00
for (mask = 0x80; mask != 0; mask >>= 1)
{
2013-07-21 03:57:20 +00:00
bn_add(d, d, d, N, n);
2015-05-19 03:24:56 +00:00
2013-07-21 03:57:20 +00:00
if ((a[i] & mask) != 0)
bn_add(d, d, b, N, n);
}
2013-02-16 04:22:16 +00:00
}
2015-05-19 03:24:56 +00:00
void bn_exp(atUint8* d, atUint8* a, atUint8* N, atUint32 n, atUint8* e, atUint32 en)
2013-02-16 04:22:16 +00:00
{
atUint8 t[512];
atUint32 i;
atUint8 mask;
2013-02-16 04:22:16 +00:00
2013-07-21 03:57:20 +00:00
bn_zero(d, n);
2015-05-19 03:24:56 +00:00
d[n - 1] = 1;
2013-07-21 03:57:20 +00:00
for (i = 0; i < en; i++)
2015-05-19 03:24:56 +00:00
for (mask = 0x80; mask != 0; mask >>= 1)
{
2013-07-21 03:57:20 +00:00
bn_mul(t, d, d, N, n);
2015-05-19 03:24:56 +00:00
2013-07-21 03:57:20 +00:00
if ((e[i] & mask) != 0)
bn_mul(d, t, a, N, n);
else
bn_copy(d, t, n);
}
2013-02-16 04:22:16 +00:00
}
// only for prime N -- stupid but lazy, see if I care
2015-05-19 03:24:56 +00:00
void bn_inv(atUint8* d, atUint8* a, atUint8* N, atUint32 n)
2013-02-16 04:22:16 +00:00
{
atUint8 t[512], s[512];
2013-02-16 04:22:16 +00:00
2013-07-21 03:57:20 +00:00
bn_copy(t, N, n);
bn_zero(s, n);
2015-05-19 03:24:56 +00:00
s[n - 1] = 2;
2013-07-21 03:57:20 +00:00
bn_sub_modulus(t, s, n);
bn_exp(d, a, N, n, t, n);
2013-02-16 04:22:16 +00:00
}