Crypto: small cleanup

This commit is contained in:
Tillmann Karras 2014-02-28 12:41:10 +01:00
parent 315a8ba1c0
commit 46e7c0657f
7 changed files with 66 additions and 82 deletions

View File

@ -6,31 +6,18 @@
#include <string.h>
#include "Common/Common.h"
#include "Common/Crypto/tools.h"
/*static void bn_print(char *name, u8 *a, u32 n)
{
u32 i;
printf("%s = ", name);
for (i = 0; i < n; i++)
printf("%02x", a[i]);
printf("\n");
}*/
static void bn_zero(u8 *d, u32 n)
{
memset(d, 0, n);
}
static void bn_copy(u8 *d, u8 *a, u32 n)
static void bn_copy(u8 *d, const u8 *a, u32 n)
{
memcpy(d, a, n);
}
int bn_compare(u8 *a, u8 *b, u32 n)
int bn_compare(const u8 *a, const u8 *b, u32 n)
{
u32 i;
@ -44,7 +31,7 @@ int bn_compare(u8 *a, u8 *b, u32 n)
return 0;
}
void bn_sub_modulus(u8 *a, u8 *N, u32 n)
void bn_sub_modulus(u8 *a, const u8 *N, u32 n)
{
u32 i;
u32 dig;
@ -58,7 +45,7 @@ void bn_sub_modulus(u8 *a, u8 *N, u32 n)
}
}
void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n)
void bn_add(u8 *d, const u8 *a, const u8 *b, const u8 *N, u32 n)
{
u32 i;
u32 dig;
@ -78,7 +65,7 @@ void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n)
bn_sub_modulus(d, N, n);
}
void bn_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n)
void bn_mul(u8 *d, const u8 *a, const u8 *b, const u8 *N, u32 n)
{
u32 i;
u8 mask;
@ -93,7 +80,7 @@ void bn_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n)
}
}
void bn_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en)
void bn_exp(u8 *d, const u8 *a, const u8 *N, u32 n, const u8 *e, u32 en)
{
u8 t[512];
u32 i;
@ -112,7 +99,7 @@ void bn_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en)
}
// only for prime N -- stupid but lazy, see if I care
void bn_inv(u8 *d, u8 *a, u8 *N, u32 n)
void bn_inv(u8 *d, const u8 *a, const u8 *N, u32 n)
{
u8 t[512], s[512];

View File

@ -0,0 +1,16 @@
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#pragma once
#include "Common/CommonTypes.h"
// bignum arithmetic
int bn_compare(const u8 *a, const u8 *b, u32 n);
void bn_sub_modulus(u8 *a, const u8 *N, u32 n);
void bn_add(u8 *d, const u8 *a, const u8 *b, const u8 *N, u32 n);
void bn_mul(u8 *d, const u8 *a, const u8 *b, const u8 *N, u32 n);
void bn_inv(u8 *d, const u8 *a, const u8 *N, u32 n); // only for prime N
void bn_exp(u8 *d, const u8 *a, const u8 *N, u32 n, const u8 *e, u32 en);

View File

@ -12,38 +12,26 @@
#include <string.h>
#include "Common/Common.h"
#include "Common/Crypto/tools.h"
#include "Common/Crypto/bn.h"
#include "Common/Crypto/ec.h"
// y**2 + x*y = x**3 + x + b
/*
static u8 ec_b[30] =
UNUSED static const u8 ec_b[30] =
{0x00,0x66,0x64,0x7e,0xde,0x6c,0x33,0x2c,0x7f,0x8c,0x09,0x23,0xbb,0x58,0x21
,0x3b,0x33,0x3b,0x20,0xe9,0xce,0x42,0x81,0xfe,0x11,0x5f,0x7d,0x8f,0x90,0xad};
*/
// order of the addition group of points
static u8 ec_N[30] =
static const u8 ec_N[30] =
{0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
,0x13,0xe9,0x74,0xe7,0x2f,0x8a,0x69,0x22,0x03,0x1d,0x26,0x03,0xcf,0xe0,0xd7};
// base point
static u8 ec_G[60] =
static const u8 ec_G[60] =
{0x00,0xfa,0xc9,0xdf,0xcb,0xac,0x83,0x13,0xbb,0x21,0x39,0xf1,0xbb,0x75,0x5f
,0xef,0x65,0xbc,0x39,0x1f,0x8b,0x36,0xf8,0xf8,0xeb,0x73,0x71,0xfd,0x55,0x8b
,0x01,0x00,0x6a,0x08,0xa4,0x19,0x03,0x35,0x06,0x78,0xe5,0x85,0x28,0xbe,0xbf
,0x8a,0x0b,0xef,0xf8,0x67,0xa7,0xca,0x36,0x71,0x6f,0x7e,0x01,0xf8,0x10,0x52};
/*static void elt_print(char *name, u8 *a)
{
u32 i;
printf("%s = ", name);
for (i = 0; i < 30; i++)
printf("%02x", a[i]);
printf("\n");
}*/
static void elt_copy(u8 *d, const u8 *a)
{
memcpy(d, a, 30);
@ -54,7 +42,7 @@ static void elt_zero(u8 *d)
memset(d, 0, 30);
}
static int elt_is_zero(u8 *d)
static int elt_is_zero(const u8 *d)
{
u32 i;
@ -65,7 +53,7 @@ static int elt_is_zero(u8 *d)
return 1;
}
static void elt_add(u8 *d, u8 *a, u8 *b)
static void elt_add(u8 *d, const u8 *a, const u8 *b)
{
u32 i;
@ -73,7 +61,7 @@ static void elt_add(u8 *d, u8 *a, u8 *b)
d[i] = a[i] ^ b[i];
}
static void elt_mul_x(u8 *d, u8 *a)
static void elt_mul_x(u8 *d, const u8 *a)
{
u8 carry, x, y;
u32 i;
@ -91,7 +79,7 @@ static void elt_mul_x(u8 *d, u8 *a)
d[20] ^= carry << 2;
}
static void elt_mul(u8 *d, u8 *a, u8 *b)
static void elt_mul(u8 *d, const u8 *a, const u8 *b)
{
u32 i, n;
u8 mask;
@ -115,9 +103,9 @@ static void elt_mul(u8 *d, u8 *a, u8 *b)
}
static const u8 square[16] =
{0x00,0x01,0x04,0x05,0x10,0x11,0x14,0x15,0x40,0x41,0x44,0x45,0x50,0x51,0x54,0x55};
{0x00,0x01,0x04,0x05,0x10,0x11,0x14,0x15,0x40,0x41,0x44,0x45,0x50,0x51,0x54,0x55};
static void elt_square_to_wide(u8 *d, u8 *a)
static void elt_square_to_wide(u8 *d, const u8 *a)
{
u32 i;
@ -152,7 +140,7 @@ static void wide_reduce(u8 *d)
d[30] &= 1;
}
static void elt_square(u8 *d, u8 *a)
static void elt_square(u8 *d, const u8 *a)
{
u8 wide[60];
@ -162,7 +150,7 @@ static void elt_square(u8 *d, u8 *a)
elt_copy(d, wide + 30);
}
static void itoh_tsujii(u8 *d, u8 *a, u8 *b, u32 j)
static void itoh_tsujii(u8 *d, const u8 *a, const u8 *b, u32 j)
{
u8 t[30];
@ -175,7 +163,7 @@ static void itoh_tsujii(u8 *d, u8 *a, u8 *b, u32 j)
elt_mul(d, t, b);
}
static void elt_inv(u8 *d, u8 *a)
static void elt_inv(u8 *d, const u8 *a)
{
u8 t[30];
u8 s[30];
@ -193,7 +181,7 @@ static void elt_inv(u8 *d, u8 *a)
elt_square(d, s);
}
/*static int point_is_on_curve(u8 *p)
UNUSED static int point_is_on_curve(u8 *p)
{
u8 s[30], t[30];
u8 *x, *y;
@ -216,16 +204,17 @@ static void elt_inv(u8 *d, u8 *a)
return elt_is_zero(s);
}
*/
static int point_is_zero(u8 *p)
static int point_is_zero(const u8 *p)
{
return elt_is_zero(p) && elt_is_zero(p + 30);
}
static void point_double(u8 *r, u8 *p)
static void point_double(u8 *r, const u8 *p)
{
u8 s[30], t[30];
u8 *px, *py, *rx, *ry;
const u8 *px, *py;
u8 *rx, *ry;
px = p;
py = p + 30;
@ -254,10 +243,11 @@ static void point_double(u8 *r, u8 *p)
elt_add(ry, ry, t);
}
static void point_add(u8 *r, u8 *p, u8 *q)
static void point_add(u8 *r, const u8 *p, const u8 *q)
{
u8 s[30], t[30], u[30];
u8 *px, *py, *qx, *qy, *rx, *ry;
const u8 *px, *py, *qx, *qy;
u8 *rx, *ry;
px = p;
py = p + 30;
@ -307,7 +297,7 @@ static void point_add(u8 *r, u8 *p, u8 *q)
elt_add(ry, s, rx);
}
void point_mul(u8 *d, const u8 *a, u8 *b) // a is bignum
void point_mul(u8 *d, const u8 *a, const u8 *b) // a is bignum
{
u32 i;
u8 mask;
@ -323,7 +313,7 @@ void point_mul(u8 *d, const u8 *a, u8 *b) // a is bignum
}
}
void silly_random(u8 * rndArea, u8 count)
static void silly_random(u8 * rndArea, u8 count)
{
u16 i;
srand((unsigned) (time(NULL)));
@ -334,7 +324,7 @@ void silly_random(u8 * rndArea, u8 count)
}
}
void generate_ecdsa(u8 *R, u8 *S, const u8 *k, u8 *hash)
void generate_ecdsa(u8 *R, u8 *S, const u8 *k, const u8 *hash)
{
u8 e[30];
u8 kk[30];
@ -372,7 +362,7 @@ void generate_ecdsa(u8 *R, u8 *S, const u8 *k, u8 *hash)
bn_mul(S, minv, kk, ec_N, 30);
}
int check_ecdsa(u8 *Q, u8 *R, u8 *S, u8 *hash)
UNUSED static int check_ecdsa(u8 *Q, u8 *R, u8 *S, const u8 *hash)
{
u8 Sinv[30];
u8 e[30];

View File

@ -0,0 +1,13 @@
// 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
#pragma once
#include "Common/CommonTypes.h"
void point_mul(u8 *d, const u8 *a, const u8 *b);
void generate_ecdsa(u8 *R, u8 *S, const u8 *k, const u8 *hash);
void ec_priv_to_pub(const u8 *k, u8 *Q);

View File

@ -1,22 +0,0 @@
// 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
#ifndef _TOOLS_H
#define _TOOLS_H
#include <polarssl/sha1.h>
// bignum
int bn_compare(u8 *a, u8 *b, u32 n);
void bn_sub_modulus(u8 *a, u8 *N, u32 n);
void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);
void bn_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);
void bn_inv(u8 *d, u8 *a, u8 *N, u32 n); // only for prime N
void bn_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en);
void point_mul(u8 *d, const u8 *a, u8 *b);
void generate_ecdsa(u8 *R, u8 *S, const u8 *k, u8 *hash);
void ec_priv_to_pub(const u8 *k, u8 *Q);
#endif

View File

@ -13,7 +13,7 @@
#include <polarssl/sha1.h>
#include "Common/FileUtil.h"
#include "Common/Crypto/tools.h"
#include "Common/Crypto/ec.h"
#include "Core/ec_wii.h"

View File

@ -22,7 +22,7 @@
#include "Common/MathUtil.h"
#include "Common/NandPaths.h"
#include "Common/StringUtil.h"
#include "Common/Crypto/tools.h"
#include "Common/Crypto/ec.h"
#include "DolphinWX/MemoryCards/WiiSaveCrypted.h"
static Common::replace_v replacements;