From 46e7c0657f99cca765a692184093e5fbef750cae Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Fri, 28 Feb 2014 12:41:10 +0100 Subject: [PATCH] Crypto: small cleanup --- Source/Core/Common/Crypto/bn.cpp | 27 ++------ Source/Core/Common/Crypto/bn.h | 16 +++++ Source/Core/Common/Crypto/ec.cpp | 66 ++++++++----------- Source/Core/Common/Crypto/ec.h | 13 ++++ Source/Core/Common/Crypto/tools.h | 22 ------- Source/Core/Core/ec_wii.cpp | 2 +- .../DolphinWX/MemoryCards/WiiSaveCrypted.cpp | 2 +- 7 files changed, 66 insertions(+), 82 deletions(-) create mode 100644 Source/Core/Common/Crypto/bn.h create mode 100644 Source/Core/Common/Crypto/ec.h delete mode 100644 Source/Core/Common/Crypto/tools.h diff --git a/Source/Core/Common/Crypto/bn.cpp b/Source/Core/Common/Crypto/bn.cpp index e2a9dd6a4a..4f7c93e841 100644 --- a/Source/Core/Common/Crypto/bn.cpp +++ b/Source/Core/Common/Crypto/bn.cpp @@ -6,31 +6,18 @@ #include #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]; diff --git a/Source/Core/Common/Crypto/bn.h b/Source/Core/Common/Crypto/bn.h new file mode 100644 index 0000000000..0a6ce0111d --- /dev/null +++ b/Source/Core/Common/Crypto/bn.h @@ -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); diff --git a/Source/Core/Common/Crypto/ec.cpp b/Source/Core/Common/Crypto/ec.cpp index 727f5d828c..acbbac1a8c 100644 --- a/Source/Core/Common/Crypto/ec.cpp +++ b/Source/Core/Common/Crypto/ec.cpp @@ -12,38 +12,26 @@ #include #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]; diff --git a/Source/Core/Common/Crypto/ec.h b/Source/Core/Common/Crypto/ec.h new file mode 100644 index 0000000000..14329d41cc --- /dev/null +++ b/Source/Core/Common/Crypto/ec.h @@ -0,0 +1,13 @@ +// Copyright 2007,2008 Segher Boessenkool +// 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); diff --git a/Source/Core/Common/Crypto/tools.h b/Source/Core/Common/Crypto/tools.h deleted file mode 100644 index 0e4d49be1a..0000000000 --- a/Source/Core/Common/Crypto/tools.h +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2007,2008 Segher Boessenkool -// 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 - -// 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 diff --git a/Source/Core/Core/ec_wii.cpp b/Source/Core/Core/ec_wii.cpp index d89e17f71d..0d4d72b0a2 100644 --- a/Source/Core/Core/ec_wii.cpp +++ b/Source/Core/Core/ec_wii.cpp @@ -13,7 +13,7 @@ #include #include "Common/FileUtil.h" -#include "Common/Crypto/tools.h" +#include "Common/Crypto/ec.h" #include "Core/ec_wii.h" diff --git a/Source/Core/DolphinWX/MemoryCards/WiiSaveCrypted.cpp b/Source/Core/DolphinWX/MemoryCards/WiiSaveCrypted.cpp index c69013887e..cf5949d5c6 100644 --- a/Source/Core/DolphinWX/MemoryCards/WiiSaveCrypted.cpp +++ b/Source/Core/DolphinWX/MemoryCards/WiiSaveCrypted.cpp @@ -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;