1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-23 03:02:53 +01:00

Crypto: modernize ec.h

Add const, use u8
This commit is contained in:
Nekotekina 2021-01-14 13:27:24 +03:00
parent 376e564cff
commit 8b01d05146
2 changed files with 105 additions and 104 deletions

View File

@ -5,7 +5,7 @@
#include "utils.h" #include "utils.h"
#include <string> #include <string>
void bn_print(char *name, u8 *a, u32 n) void bn_print(char* name, u8* a, u32 n)
{ {
u32 i; u32 i;
@ -17,20 +17,22 @@ void bn_print(char *name, u8 *a, u32 n)
printf("\n"); printf("\n");
} }
static void bn_zero(u8 *d, u32 n) static void bn_zero(u8* d, u32 n)
{ {
memset(d, 0, n); memset(d, 0, n);
} }
void bn_copy(u8 *d, u8 *a, u32 n)
void bn_copy(u8* d, u8* a, u32 n)
{ {
memcpy(d, a, n); memcpy(d, a, n);
} }
int bn_compare(u8 *a, u8 *b, u32 n) int bn_compare(u8* a, u8* b, u32 n)
{ {
u32 i; u32 i;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++)
{
if (a[i] < b[i]) if (a[i] < b[i])
return -1; return -1;
if (a[i] > b[i]) if (a[i] > b[i])
@ -40,14 +42,15 @@ int bn_compare(u8 *a, u8 *b, u32 n)
return 0; return 0;
} }
static u8 bn_add_1(u8 *d, u8 *a, u8 *b, u32 n) static u8 bn_add_1(u8* d, u8* a, u8* b, u32 n)
{ {
u32 i; u32 i;
u32 dig; u32 dig;
u8 c; u8 c;
c = 0; c = 0;
for (i = n - 1; i < n; i--) { for (i = n - 1; i < n; i--)
{
dig = a[i] + b[i] + c; dig = a[i] + b[i] + c;
c = dig >> 8; c = dig >> 8;
d[i] = dig; d[i] = dig;
@ -56,14 +59,15 @@ static u8 bn_add_1(u8 *d, u8 *a, u8 *b, u32 n)
return c; return c;
} }
static u8 bn_sub_1(u8 *d, u8 *a, u8 *b, u32 n) static u8 bn_sub_1(u8* d, u8* a, u8* b, u32 n)
{ {
u32 i; u32 i;
u32 dig; u32 dig;
u8 c; u8 c;
c = 1; c = 1;
for (i = n - 1; i < n; i--) { for (i = n - 1; i < n; i--)
{
dig = a[i] + 255 - b[i] + c; dig = a[i] + 255 - b[i] + c;
c = dig >> 8; c = dig >> 8;
d[i] = dig; d[i] = dig;
@ -72,13 +76,13 @@ static u8 bn_sub_1(u8 *d, u8 *a, u8 *b, u32 n)
return 1 - c; return 1 - c;
} }
void bn_reduce(u8 *d, u8 *N, u32 n) void bn_reduce(u8* d, u8* N, u32 n)
{ {
if (bn_compare(d, N, n) >= 0) if (bn_compare(d, N, n) >= 0)
bn_sub_1(d, d, N, n); bn_sub_1(d, d, N, n);
} }
void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n) void bn_add(u8* d, u8* a, u8* b, u8* N, u32 n)
{ {
if (bn_add_1(d, a, b, n)) if (bn_add_1(d, a, b, n))
bn_sub_1(d, d, N, n); bn_sub_1(d, d, N, n);
@ -86,7 +90,7 @@ void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n)
bn_reduce(d, N, n); bn_reduce(d, N, n);
} }
void bn_sub(u8 *d, u8 *a, u8 *b, u8 *N, u32 n) void bn_sub(u8* d, u8* a, u8* b, u8* N, u32 n)
{ {
if (bn_sub_1(d, a, b, n)) if (bn_sub_1(d, a, b, n))
bn_add_1(d, d, N, n); bn_add_1(d, d, N, n);
@ -111,19 +115,20 @@ static constexpr u8 inv256[0x80] = {
0x11, 0x3b, 0x5d, 0xc7, 0x49, 0x33, 0x55, 0xff, 0x11, 0x3b, 0x5d, 0xc7, 0x49, 0x33, 0x55, 0xff,
}; };
static void bn_mon_muladd_dig(u8 *d, u8 *a, u8 b, u8 *N, u32 n) static void bn_mon_muladd_dig(u8* d, u8* a, u8 b, u8* N, u32 n)
{ {
u32 dig; u32 dig;
u32 i; u32 i;
u8 z = -(d[n-1] + a[n-1]*b) * inv256[N[n-1]/2]; u8 z = -(d[n - 1] + a[n - 1] * b) * inv256[N[n - 1] / 2];
dig = d[n-1] + a[n-1]*b + N[n-1]*z; dig = d[n - 1] + a[n - 1] * b + N[n - 1] * z;
dig >>= 8; dig >>= 8;
for (i = n - 2; i < n; i--) { for (i = n - 2; i < n; i--)
dig += d[i] + a[i]*b + N[i]*z; {
d[i+1] = dig; dig += d[i] + a[i] * b + N[i] * z;
d[i + 1] = dig;
dig >>= 8; dig >>= 8;
} }
@ -136,7 +141,7 @@ static void bn_mon_muladd_dig(u8 *d, u8 *a, u8 b, u8 *N, u32 n)
bn_reduce(d, N, n); bn_reduce(d, N, n);
} }
void bn_mon_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n) void bn_mon_mul(u8* d, u8* a, u8* b, u8* N, u32 n)
{ {
u8 t[512]; u8 t[512];
u32 i; u32 i;
@ -149,35 +154,36 @@ void bn_mon_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n)
bn_copy(d, t, n); bn_copy(d, t, n);
} }
void bn_to_mon(u8 *d, u8 *N, u32 n) void bn_to_mon(u8* d, u8* N, u32 n)
{ {
u32 i; u32 i;
for (i = 0; i < 8*n; i++) for (i = 0; i < 8 * n; i++)
bn_add(d, d, d, N, n); bn_add(d, d, d, N, n);
} }
void bn_from_mon(u8 *d, u8 *N, u32 n) void bn_from_mon(u8* d, u8* N, u32 n)
{ {
u8 t[512]; u8 t[512];
bn_zero(t, n); bn_zero(t, n);
t[n-1] = 1; t[n - 1] = 1;
bn_mon_mul(d, d, t, N, n); bn_mon_mul(d, d, t, N, n);
} }
static void bn_mon_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en) static void bn_mon_exp(u8* d, u8* a, u8* N, u32 n, u8* e, u32 en)
{ {
u8 t[512]; u8 t[512];
u32 i; u32 i;
u8 mask; u8 mask;
bn_zero(d, n); bn_zero(d, n);
d[n-1] = 1; d[n - 1] = 1;
bn_to_mon(d, N, n); bn_to_mon(d, N, n);
for (i = 0; i < en; i++) for (i = 0; i < en; i++)
for (mask = 0x80; mask != 0; mask >>= 1) { for (mask = 0x80; mask != 0; mask >>= 1)
{
bn_mon_mul(t, d, d, N, n); bn_mon_mul(t, d, d, N, n);
if ((e[i] & mask) != 0) if ((e[i] & mask) != 0)
bn_mon_mul(d, t, a, N, n); bn_mon_mul(d, t, a, N, n);
@ -186,27 +192,18 @@ static void bn_mon_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en)
} }
} }
void bn_mon_inv(u8 *d, u8 *a, u8 *N, u32 n) void bn_mon_inv(u8* d, u8* a, u8* N, u32 n)
{ {
u8 t[512], s[512]; u8 t[512], s[512];
bn_zero(s, n); bn_zero(s, n);
s[n-1] = 2; s[n - 1] = 2;
bn_sub_1(t, N, s, n); bn_sub_1(t, N, s, n);
bn_mon_exp(d, a, N, n, t, n); bn_mon_exp(d, a, N, n, t, n);
} }
void bn_copy(u8 *d, u8 *a, u32 n); struct point
int bn_compare(u8 *a, u8 *b, u32 n); {
void bn_reduce(u8 *d, u8 *N, u32 n);
void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);
void bn_sub(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);
void bn_to_mon(u8 *d, u8 *N, u32 n);
void bn_from_mon(u8 *d, u8 *N, u32 n);
void bn_mon_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);
void bn_mon_inv(u8 *d, u8 *a, u8 *N, u32 n);
struct point {
u8 x[20]; u8 x[20];
u8 y[20]; u8 y[20];
}; };
@ -219,17 +216,17 @@ static thread_local point ec_G{}; // mon
static thread_local point ec_Q{}; // mon static thread_local point ec_Q{}; // mon
static thread_local u8 ec_k[21]{}; static thread_local u8 ec_k[21]{};
static void elt_copy(u8 *d, u8 *a) static void elt_copy(u8* d, u8* a)
{ {
memcpy(d, a, 20); memcpy(d, a, 20);
} }
static void elt_zero(u8 *d) static void elt_zero(u8* d)
{ {
memset(d, 0, 20); memset(d, 0, 20);
} }
static int elt_is_zero(u8 *d) static int elt_is_zero(u8* d)
{ {
u32 i; u32 i;
@ -240,47 +237,47 @@ static int elt_is_zero(u8 *d)
return 1; return 1;
} }
static void elt_add(u8 *d, u8 *a, u8 *b) static void elt_add(u8* d, u8* a, u8* b)
{ {
bn_add(d, a, b, ec_p, 20); bn_add(d, a, b, ec_p, 20);
} }
static void elt_sub(u8 *d, u8 *a, u8 *b) static void elt_sub(u8* d, u8* a, u8* b)
{ {
bn_sub(d, a, b, ec_p, 20); bn_sub(d, a, b, ec_p, 20);
} }
static void elt_mul(u8 *d, u8 *a, u8 *b) static void elt_mul(u8* d, u8* a, u8* b)
{ {
bn_mon_mul(d, a, b, ec_p, 20); bn_mon_mul(d, a, b, ec_p, 20);
} }
static void elt_square(u8 *d, u8 *a) static void elt_square(u8* d, u8* a)
{ {
elt_mul(d, a, a); elt_mul(d, a, a);
} }
static void elt_inv(u8 *d, u8 *a) static void elt_inv(u8* d, u8* a)
{ {
u8 s[20]; u8 s[20];
elt_copy(s, a); elt_copy(s, a);
bn_mon_inv(d, s, ec_p, 20); bn_mon_inv(d, s, ec_p, 20);
} }
static void point_to_mon(struct point *p) static void point_to_mon(point* p)
{ {
bn_to_mon(p->x, ec_p, 20); bn_to_mon(p->x, ec_p, 20);
bn_to_mon(p->y, ec_p, 20); bn_to_mon(p->y, ec_p, 20);
} }
static void point_from_mon(struct point *p) static void point_from_mon(point* p)
{ {
bn_from_mon(p->x, ec_p, 20); bn_from_mon(p->x, ec_p, 20);
bn_from_mon(p->y, ec_p, 20); bn_from_mon(p->y, ec_p, 20);
} }
#if 0 #if 0
static int point_is_on_curve(u8 *p) static int point_is_on_curve(u8* p)
{ {
u8 s[20], t[20]; u8 s[20], t[20];
u8 *x, *y; u8 *x, *y;
@ -303,21 +300,21 @@ static int point_is_on_curve(u8 *p)
} }
#endif #endif
static void point_zero(struct point *p) static void point_zero(point* p)
{ {
elt_zero(p->x); elt_zero(p->x);
elt_zero(p->y); elt_zero(p->y);
} }
static int point_is_zero(struct point *p) static int point_is_zero(point* p)
{ {
return elt_is_zero(p->x) && elt_is_zero(p->y); return elt_is_zero(p->x) && elt_is_zero(p->y);
} }
static void point_double(struct point *r, struct point *p) static void point_double(point* r, point* p)
{ {
u8 s[20], t[20]; u8 s[20], t[20];
struct point pp; point pp;
u8 *px, *py, *rx, *ry; u8 *px, *py, *rx, *ry;
pp = *p; pp = *p;
@ -327,7 +324,8 @@ static void point_double(struct point *r, struct point *p)
rx = r->x; rx = r->x;
ry = r->y; ry = r->y;
if (elt_is_zero(py)) { if (elt_is_zero(py))
{
point_zero(r); point_zero(r);
return; return;
} }
@ -349,11 +347,11 @@ static void point_double(struct point *r, struct point *p)
elt_sub(ry, ry, py); // ry = -s*(rx-px) - py elt_sub(ry, ry, py); // ry = -s*(rx-px) - py
} }
static void point_add(struct point *r, struct point *p, struct point *q) static void point_add(point* r, point* p, point* q)
{ {
u8 s[20], t[20], u[20]; u8 s[20], t[20], u[20];
u8 *px, *py, *qx, *qy, *rx, *ry; u8 *px, *py, *qx, *qy, *rx, *ry;
struct point pp, qq; point pp, qq;
pp = *p; pp = *p;
qq = *q; qq = *q;
@ -365,13 +363,15 @@ static void point_add(struct point *r, struct point *p, struct point *q)
rx = r->x; rx = r->x;
ry = r->y; ry = r->y;
if (point_is_zero(&pp)) { if (point_is_zero(&pp))
{
elt_copy(rx, qx); elt_copy(rx, qx);
elt_copy(ry, qy); elt_copy(ry, qy);
return; return;
} }
if (point_is_zero(&qq)) { if (point_is_zero(&qq))
{
elt_copy(rx, px); elt_copy(rx, px);
elt_copy(ry, py); elt_copy(ry, py);
return; return;
@ -379,7 +379,8 @@ static void point_add(struct point *r, struct point *p, struct point *q)
elt_sub(u, qx, px); elt_sub(u, qx, px);
if (elt_is_zero(u)) { if (elt_is_zero(u))
{
elt_sub(u, qy, py); elt_sub(u, qy, py);
if (elt_is_zero(u)) if (elt_is_zero(u))
point_double(r, &pp); point_double(r, &pp);
@ -402,7 +403,7 @@ static void point_add(struct point *r, struct point *p, struct point *q)
elt_sub(ry, ry, py); // ry = -s*(rx-px) - py elt_sub(ry, ry, py); // ry = -s*(rx-px) - py
} }
static void point_mul(struct point *d, u8 *a, struct point *b) // a is bignum static void point_mul(point* d, u8* a, point* b) // a is bignum
{ {
u32 i; u32 i;
u8 mask; u8 mask;
@ -410,14 +411,15 @@ static void point_mul(struct point *d, u8 *a, struct point *b) // a is bignum
point_zero(d); point_zero(d);
for (i = 0; i < 21; i++) for (i = 0; i < 21; i++)
for (mask = 0x80; mask != 0; mask >>= 1) { for (mask = 0x80; mask != 0; mask >>= 1)
{
point_double(d, d); point_double(d, d);
if ((a[i] & mask) != 0) if ((a[i] & mask) != 0)
point_add(d, d, b); point_add(d, d, b);
} }
} }
static int check_ecdsa(struct point *Q, u8 *R, u8 *S, u8 *hash) static int check_ecdsa(struct point* Q, u8* R, u8* S, u8* hash)
{ {
u8 Sinv[21]; u8 Sinv[21];
u8 e[21]; u8 e[21];
@ -459,13 +461,13 @@ static int check_ecdsa(struct point *Q, u8 *R, u8 *S, u8 *hash)
} }
#if 0 #if 0
static void ec_priv_to_pub(u8 *k, u8 *Q) static void ec_priv_to_pub(u8* k, u8* Q)
{ {
point_mul(Q, k, ec_G); point_mul(Q, k, ec_G);
} }
#endif #endif
int ecdsa_set_curve(u8* p, u8* a, u8* b, u8* N, u8* Gx, u8* Gy) int ecdsa_set_curve(const u8* p, const u8* a, const u8* b, const u8* N, const u8* Gx, const u8* Gy)
{ {
memcpy(ec_p, p, 20); memcpy(ec_p, p, 20);
memcpy(ec_a, a, 20); memcpy(ec_a, a, 20);
@ -482,19 +484,19 @@ int ecdsa_set_curve(u8* p, u8* a, u8* b, u8* N, u8* Gx, u8* Gy)
return 0; return 0;
} }
void ecdsa_set_pub(u8 *Q) void ecdsa_set_pub(const u8* Q)
{ {
memcpy(ec_Q.x, Q, 20); memcpy(ec_Q.x, Q, 20);
memcpy(ec_Q.y, Q+20, 20); memcpy(ec_Q.y, Q+20, 20);
point_to_mon(&ec_Q); point_to_mon(&ec_Q);
} }
void ecdsa_set_priv(u8 *k) void ecdsa_set_priv(const u8* k)
{ {
memcpy(ec_k, k, sizeof ec_k); memcpy(ec_k, k, sizeof ec_k);
} }
int ecdsa_verify(u8 *hash, u8 *R, u8 *S) int ecdsa_verify(u8* hash, u8* R, u8* S)
{ {
return check_ecdsa(&ec_Q, R, S, hash); return check_ecdsa(&ec_Q, R, S, hash);
} }

View File

@ -4,10 +4,9 @@
// Licensed under the terms of the GNU GPL, version 3 // Licensed under the terms of the GNU GPL, version 3
// http://www.gnu.org/licenses/gpl-3.0.txt // http://www.gnu.org/licenses/gpl-3.0.txt
#include <string.h> #include "util/types.hpp"
#include <stdio.h>
int ecdsa_set_curve(unsigned char *p, unsigned char *a, unsigned char *b, unsigned char *N, unsigned char *Gx, unsigned char *Gy); int ecdsa_set_curve(const u8* p, const u8* a, const u8* b, const u8* N, const u8* Gx, const u8* Gy);
void ecdsa_set_pub(unsigned char *Q); void ecdsa_set_pub(const u8* Q);
void ecdsa_set_priv(unsigned char *k); void ecdsa_set_priv(const u8* k);
int ecdsa_verify(unsigned char *hash, unsigned char *R, unsigned char *S); int ecdsa_verify(u8* hash, u8* R, u8* S);