mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 06:49:28 -05:00
Import code from previous AssetBuilder version
This commit is contained in:
1585
thirdparty/libtomcrypt/math/fp/ltc_ecc_fp_mulmod.c
vendored
Normal file
1585
thirdparty/libtomcrypt/math/fp/ltc_ecc_fp_mulmod.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
554
thirdparty/libtomcrypt/math/gmp_desc.c
vendored
Normal file
554
thirdparty/libtomcrypt/math/gmp_desc.c
vendored
Normal file
@ -0,0 +1,554 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#define DESC_DEF_ONLY
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef GMP_DESC
|
||||
|
||||
#include <stdio.h>
|
||||
#include <gmp.h>
|
||||
|
||||
static int init(void **a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
|
||||
*a = XCALLOC(1, sizeof(__mpz_struct));
|
||||
if (*a == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
mpz_init(((__mpz_struct *)*a));
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static void deinit(void *a)
|
||||
{
|
||||
LTC_ARGCHKVD(a != NULL);
|
||||
mpz_clear(a);
|
||||
XFREE(a);
|
||||
}
|
||||
|
||||
static int neg(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
mpz_neg(b, a);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int copy(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
mpz_set(b, a);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int init_copy(void **a, void *b)
|
||||
{
|
||||
if (init(a) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
return copy(b, *a);
|
||||
}
|
||||
|
||||
/* ---- trivial ---- */
|
||||
static int set_int(void *a, ltc_mp_digit b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
mpz_set_ui(((__mpz_struct *)a), b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static unsigned long get_int(void *a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mpz_get_ui(a);
|
||||
}
|
||||
|
||||
static ltc_mp_digit get_digit(void *a, int n)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mpz_getlimbn(a, n);
|
||||
}
|
||||
|
||||
static int get_digit_count(void *a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mpz_size(a);
|
||||
}
|
||||
|
||||
static int compare(void *a, void *b)
|
||||
{
|
||||
int ret;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
ret = mpz_cmp(a, b);
|
||||
if (ret < 0) {
|
||||
return LTC_MP_LT;
|
||||
} else if (ret > 0) {
|
||||
return LTC_MP_GT;
|
||||
} else {
|
||||
return LTC_MP_EQ;
|
||||
}
|
||||
}
|
||||
|
||||
static int compare_d(void *a, ltc_mp_digit b)
|
||||
{
|
||||
int ret;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
ret = mpz_cmp_ui(((__mpz_struct *)a), b);
|
||||
if (ret < 0) {
|
||||
return LTC_MP_LT;
|
||||
} else if (ret > 0) {
|
||||
return LTC_MP_GT;
|
||||
} else {
|
||||
return LTC_MP_EQ;
|
||||
}
|
||||
}
|
||||
|
||||
static int count_bits(void *a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mpz_sizeinbase(a, 2);
|
||||
}
|
||||
|
||||
static int count_lsb_bits(void *a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mpz_scan1(a, 0);
|
||||
}
|
||||
|
||||
|
||||
static int twoexpt(void *a, int n)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
mpz_set_ui(a, 0);
|
||||
mpz_setbit(a, n);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* ---- conversions ---- */
|
||||
|
||||
static const char rmap[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
|
||||
|
||||
/* read ascii string */
|
||||
static int read_radix(void *a, const char *b, int radix)
|
||||
{
|
||||
int ret;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
if (radix == 64) {
|
||||
/* Sadly, GMP only supports radixes up to 62, but we need 64.
|
||||
* So, although this is not the most elegant or efficient way,
|
||||
* let's just convert the base 64 string (6 bits per digit) to
|
||||
* an octal string (3 bits per digit) that's twice as long. */
|
||||
char c, *tmp, *q;
|
||||
const char *p;
|
||||
int i;
|
||||
tmp = XMALLOC (1 + 2 * strlen (b));
|
||||
if (tmp == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
p = b;
|
||||
q = tmp;
|
||||
while ((c = *p++) != 0) {
|
||||
for (i = 0; i < 64; i++) {
|
||||
if (c == rmap[i])
|
||||
break;
|
||||
}
|
||||
if (i == 64) {
|
||||
XFREE (tmp);
|
||||
/* printf ("c = '%c'\n", c); */
|
||||
return CRYPT_ERROR;
|
||||
}
|
||||
*q++ = '0' + (i / 8);
|
||||
*q++ = '0' + (i % 8);
|
||||
}
|
||||
*q = 0;
|
||||
ret = mpz_set_str(a, tmp, 8);
|
||||
/* printf ("ret = %d for '%s'\n", ret, tmp); */
|
||||
XFREE (tmp);
|
||||
} else {
|
||||
ret = mpz_set_str(a, b, radix);
|
||||
}
|
||||
return (ret == 0 ? CRYPT_OK : CRYPT_ERROR);
|
||||
}
|
||||
|
||||
/* write one */
|
||||
static int write_radix(void *a, char *b, int radix)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
if (radix >= 11 && radix <= 36)
|
||||
/* If radix is positive, GMP uses lowercase, and if negative, uppercase.
|
||||
* We want it to use uppercase, to match the test vectors (presumably
|
||||
* generated with LibTomMath). */
|
||||
radix = -radix;
|
||||
mpz_get_str(b, radix, a);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* get size as unsigned char string */
|
||||
static unsigned long unsigned_size(void *a)
|
||||
{
|
||||
unsigned long t;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
t = mpz_sizeinbase(a, 2);
|
||||
if (mpz_cmp_ui(((__mpz_struct *)a), 0) == 0) return 0;
|
||||
return (t>>3) + ((t&7)?1:0);
|
||||
}
|
||||
|
||||
/* store */
|
||||
static int unsigned_write(void *a, unsigned char *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
mpz_export(b, NULL, 1, 1, 1, 0, ((__mpz_struct*)a));
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* read */
|
||||
static int unsigned_read(void *a, unsigned char *b, unsigned long len)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
mpz_import(a, len, 1, 1, 1, 0, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* add */
|
||||
static int add(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
mpz_add(c, a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int addi(void *a, ltc_mp_digit b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
mpz_add_ui(c, a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* sub */
|
||||
static int sub(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
mpz_sub(c, a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int subi(void *a, ltc_mp_digit b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
mpz_sub_ui(c, a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* mul */
|
||||
static int mul(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
mpz_mul(c, a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int muli(void *a, ltc_mp_digit b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
mpz_mul_ui(c, a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* sqr */
|
||||
static int sqr(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
mpz_mul(b, a, a);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* div */
|
||||
static int divide(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
mpz_t tmp;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
if (c != NULL) {
|
||||
mpz_init(tmp);
|
||||
mpz_divexact(tmp, a, b);
|
||||
}
|
||||
if (d != NULL) {
|
||||
mpz_mod(d, a, b);
|
||||
}
|
||||
if (c != NULL) {
|
||||
mpz_set(c, tmp);
|
||||
mpz_clear(tmp);
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int div_2(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
mpz_divexact_ui(b, a, 2);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* modi */
|
||||
static int modi(void *a, ltc_mp_digit b, ltc_mp_digit *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
|
||||
*c = mpz_fdiv_ui(a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* gcd */
|
||||
static int gcd(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
mpz_gcd(c, a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* lcm */
|
||||
static int lcm(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
mpz_lcm(c, a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int addmod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
mpz_add(d, a, b);
|
||||
mpz_mod(d, d, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int submod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
mpz_sub(d, a, b);
|
||||
mpz_mod(d, d, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int mulmod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
mpz_mul(d, a, b);
|
||||
mpz_mod(d, d, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int sqrmod(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
mpz_mul(c, a, a);
|
||||
mpz_mod(c, c, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* invmod */
|
||||
static int invmod(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
mpz_invert(c, a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* setup */
|
||||
static int montgomery_setup(void *a, void **b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
*b = (void *)1;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* get normalization value */
|
||||
static int montgomery_normalization(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
mpz_set_ui(a, 1);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* reduce */
|
||||
static int montgomery_reduce(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
mpz_mod(a, a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* clean up */
|
||||
static void montgomery_deinit(void *a)
|
||||
{
|
||||
LTC_UNUSED_PARAM(a);
|
||||
}
|
||||
|
||||
static int exptmod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
mpz_powm(d, a, b, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int isprime(void *a, int b, int *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
if (b == 0) {
|
||||
b = LTC_MILLER_RABIN_REPS;
|
||||
} /* if */
|
||||
*c = mpz_probab_prime_p(a, b) > 0 ? LTC_MP_YES : LTC_MP_NO;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int set_rand(void *a, int size)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
mpz_random(a, size);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
const ltc_math_descriptor gmp_desc = {
|
||||
"GNU MP",
|
||||
sizeof(mp_limb_t) * CHAR_BIT - GMP_NAIL_BITS,
|
||||
|
||||
&init,
|
||||
&init_copy,
|
||||
&deinit,
|
||||
|
||||
&neg,
|
||||
©,
|
||||
|
||||
&set_int,
|
||||
&get_int,
|
||||
&get_digit,
|
||||
&get_digit_count,
|
||||
&compare,
|
||||
&compare_d,
|
||||
&count_bits,
|
||||
&count_lsb_bits,
|
||||
&twoexpt,
|
||||
|
||||
&read_radix,
|
||||
&write_radix,
|
||||
&unsigned_size,
|
||||
&unsigned_write,
|
||||
&unsigned_read,
|
||||
|
||||
&add,
|
||||
&addi,
|
||||
&sub,
|
||||
&subi,
|
||||
&mul,
|
||||
&muli,
|
||||
&sqr,
|
||||
÷,
|
||||
&div_2,
|
||||
&modi,
|
||||
&gcd,
|
||||
&lcm,
|
||||
|
||||
&mulmod,
|
||||
&sqrmod,
|
||||
&invmod,
|
||||
|
||||
&montgomery_setup,
|
||||
&montgomery_normalization,
|
||||
&montgomery_reduce,
|
||||
&montgomery_deinit,
|
||||
|
||||
&exptmod,
|
||||
&isprime,
|
||||
|
||||
#ifdef LTC_MECC
|
||||
#ifdef LTC_MECC_FP
|
||||
<c_ecc_fp_mulmod,
|
||||
#else
|
||||
<c_ecc_mulmod,
|
||||
#endif /* LTC_MECC_FP */
|
||||
<c_ecc_projective_add_point,
|
||||
<c_ecc_projective_dbl_point,
|
||||
<c_ecc_map,
|
||||
#ifdef LTC_ECC_SHAMIR
|
||||
#ifdef LTC_MECC_FP
|
||||
<c_ecc_fp_mul2add,
|
||||
#else
|
||||
<c_ecc_mul2add,
|
||||
#endif /* LTC_MECC_FP */
|
||||
#else
|
||||
NULL,
|
||||
#endif /* LTC_ECC_SHAMIR */
|
||||
#else
|
||||
NULL, NULL, NULL, NULL, NULL,
|
||||
#endif /* LTC_MECC */
|
||||
|
||||
#ifdef LTC_MRSA
|
||||
&rsa_make_key,
|
||||
&rsa_exptmod,
|
||||
#else
|
||||
NULL, NULL,
|
||||
#endif
|
||||
&addmod,
|
||||
&submod,
|
||||
|
||||
&set_rand,
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
513
thirdparty/libtomcrypt/math/ltm_desc.c
vendored
Normal file
513
thirdparty/libtomcrypt/math/ltm_desc.c
vendored
Normal file
@ -0,0 +1,513 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#define DESC_DEF_ONLY
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTM_DESC
|
||||
|
||||
#include <tommath.h>
|
||||
|
||||
static const struct {
|
||||
int mpi_code, ltc_code;
|
||||
} mpi_to_ltc_codes[] = {
|
||||
{ MP_OKAY , CRYPT_OK},
|
||||
{ MP_MEM , CRYPT_MEM},
|
||||
{ MP_VAL , CRYPT_INVALID_ARG},
|
||||
};
|
||||
|
||||
/**
|
||||
Convert a MPI error to a LTC error (Possibly the most powerful function ever! Oh wait... no)
|
||||
@param err The error to convert
|
||||
@return The equivalent LTC error code or CRYPT_ERROR if none found
|
||||
*/
|
||||
static int mpi_to_ltc_error(int err)
|
||||
{
|
||||
int x;
|
||||
|
||||
for (x = 0; x < (int)(sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0])); x++) {
|
||||
if (err == mpi_to_ltc_codes[x].mpi_code) {
|
||||
return mpi_to_ltc_codes[x].ltc_code;
|
||||
}
|
||||
}
|
||||
return CRYPT_ERROR;
|
||||
}
|
||||
|
||||
static int init(void **a)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(a != NULL);
|
||||
|
||||
*a = XCALLOC(1, sizeof(mp_int));
|
||||
if (*a == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
if ((err = mpi_to_ltc_error(mp_init(*a))) != CRYPT_OK) {
|
||||
XFREE(*a);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static void deinit(void *a)
|
||||
{
|
||||
LTC_ARGCHKVD(a != NULL);
|
||||
mp_clear(a);
|
||||
XFREE(a);
|
||||
}
|
||||
|
||||
static int neg(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return mpi_to_ltc_error(mp_neg(a, b));
|
||||
}
|
||||
|
||||
static int copy(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return mpi_to_ltc_error(mp_copy(a, b));
|
||||
}
|
||||
|
||||
static int init_copy(void **a, void *b)
|
||||
{
|
||||
if (init(a) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
return copy(b, *a);
|
||||
}
|
||||
|
||||
/* ---- trivial ---- */
|
||||
static int set_int(void *a, ltc_mp_digit b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mpi_to_ltc_error(mp_set_int(a, b));
|
||||
}
|
||||
|
||||
static unsigned long get_int(void *a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mp_get_int(a);
|
||||
}
|
||||
|
||||
static ltc_mp_digit get_digit(void *a, int n)
|
||||
{
|
||||
mp_int *A;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
A = a;
|
||||
return (n >= A->used || n < 0) ? 0 : A->dp[n];
|
||||
}
|
||||
|
||||
static int get_digit_count(void *a)
|
||||
{
|
||||
mp_int *A;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
A = a;
|
||||
return A->used;
|
||||
}
|
||||
|
||||
static int compare(void *a, void *b)
|
||||
{
|
||||
int ret;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
ret = mp_cmp(a, b);
|
||||
switch (ret) {
|
||||
case MP_LT: return LTC_MP_LT;
|
||||
case MP_EQ: return LTC_MP_EQ;
|
||||
case MP_GT: return LTC_MP_GT;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int compare_d(void *a, ltc_mp_digit b)
|
||||
{
|
||||
int ret;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
ret = mp_cmp_d(a, b);
|
||||
switch (ret) {
|
||||
case MP_LT: return LTC_MP_LT;
|
||||
case MP_EQ: return LTC_MP_EQ;
|
||||
case MP_GT: return LTC_MP_GT;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int count_bits(void *a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mp_count_bits(a);
|
||||
}
|
||||
|
||||
static int count_lsb_bits(void *a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mp_cnt_lsb(a);
|
||||
}
|
||||
|
||||
|
||||
static int twoexpt(void *a, int n)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mpi_to_ltc_error(mp_2expt(a, n));
|
||||
}
|
||||
|
||||
/* ---- conversions ---- */
|
||||
|
||||
/* read ascii string */
|
||||
static int read_radix(void *a, const char *b, int radix)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return mpi_to_ltc_error(mp_read_radix(a, b, radix));
|
||||
}
|
||||
|
||||
/* write one */
|
||||
static int write_radix(void *a, char *b, int radix)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return mpi_to_ltc_error(mp_toradix(a, b, radix));
|
||||
}
|
||||
|
||||
/* get size as unsigned char string */
|
||||
static unsigned long unsigned_size(void *a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mp_unsigned_bin_size(a);
|
||||
}
|
||||
|
||||
/* store */
|
||||
static int unsigned_write(void *a, unsigned char *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return mpi_to_ltc_error(mp_to_unsigned_bin(a, b));
|
||||
}
|
||||
|
||||
/* read */
|
||||
static int unsigned_read(void *a, unsigned char *b, unsigned long len)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return mpi_to_ltc_error(mp_read_unsigned_bin(a, b, len));
|
||||
}
|
||||
|
||||
/* add */
|
||||
static int add(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return mpi_to_ltc_error(mp_add(a, b, c));
|
||||
}
|
||||
|
||||
static int addi(void *a, ltc_mp_digit b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return mpi_to_ltc_error(mp_add_d(a, b, c));
|
||||
}
|
||||
|
||||
/* sub */
|
||||
static int sub(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return mpi_to_ltc_error(mp_sub(a, b, c));
|
||||
}
|
||||
|
||||
static int subi(void *a, ltc_mp_digit b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return mpi_to_ltc_error(mp_sub_d(a, b, c));
|
||||
}
|
||||
|
||||
/* mul */
|
||||
static int mul(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return mpi_to_ltc_error(mp_mul(a, b, c));
|
||||
}
|
||||
|
||||
static int muli(void *a, ltc_mp_digit b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return mpi_to_ltc_error(mp_mul_d(a, b, c));
|
||||
}
|
||||
|
||||
/* sqr */
|
||||
static int sqr(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return mpi_to_ltc_error(mp_sqr(a, b));
|
||||
}
|
||||
|
||||
/* div */
|
||||
static int divide(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return mpi_to_ltc_error(mp_div(a, b, c, d));
|
||||
}
|
||||
|
||||
static int div_2(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return mpi_to_ltc_error(mp_div_2(a, b));
|
||||
}
|
||||
|
||||
/* modi */
|
||||
static int modi(void *a, ltc_mp_digit b, ltc_mp_digit *c)
|
||||
{
|
||||
mp_digit tmp;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
|
||||
if ((err = mpi_to_ltc_error(mp_mod_d(a, b, &tmp))) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
*c = tmp;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* gcd */
|
||||
static int gcd(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return mpi_to_ltc_error(mp_gcd(a, b, c));
|
||||
}
|
||||
|
||||
/* lcm */
|
||||
static int lcm(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return mpi_to_ltc_error(mp_lcm(a, b, c));
|
||||
}
|
||||
|
||||
static int addmod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
return mpi_to_ltc_error(mp_addmod(a,b,c,d));
|
||||
}
|
||||
|
||||
static int submod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
return mpi_to_ltc_error(mp_submod(a,b,c,d));
|
||||
}
|
||||
|
||||
static int mulmod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
return mpi_to_ltc_error(mp_mulmod(a,b,c,d));
|
||||
}
|
||||
|
||||
static int sqrmod(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return mpi_to_ltc_error(mp_sqrmod(a,b,c));
|
||||
}
|
||||
|
||||
/* invmod */
|
||||
static int invmod(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return mpi_to_ltc_error(mp_invmod(a, b, c));
|
||||
}
|
||||
|
||||
/* setup */
|
||||
static int montgomery_setup(void *a, void **b)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
*b = XCALLOC(1, sizeof(mp_digit));
|
||||
if (*b == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
if ((err = mpi_to_ltc_error(mp_montgomery_setup(a, (mp_digit *)*b))) != CRYPT_OK) {
|
||||
XFREE(*b);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/* get normalization value */
|
||||
static int montgomery_normalization(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return mpi_to_ltc_error(mp_montgomery_calc_normalization(a, b));
|
||||
}
|
||||
|
||||
/* reduce */
|
||||
static int montgomery_reduce(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return mpi_to_ltc_error(mp_montgomery_reduce(a, b, *((mp_digit *)c)));
|
||||
}
|
||||
|
||||
/* clean up */
|
||||
static void montgomery_deinit(void *a)
|
||||
{
|
||||
XFREE(a);
|
||||
}
|
||||
|
||||
static int exptmod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
return mpi_to_ltc_error(mp_exptmod(a,b,c,d));
|
||||
}
|
||||
|
||||
static int isprime(void *a, int b, int *c)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
if (b == 0) {
|
||||
b = LTC_MILLER_RABIN_REPS;
|
||||
} /* if */
|
||||
err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c));
|
||||
*c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO;
|
||||
return err;
|
||||
}
|
||||
|
||||
static int set_rand(void *a, int size)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return mpi_to_ltc_error(mp_rand(a, size));
|
||||
}
|
||||
|
||||
const ltc_math_descriptor ltm_desc = {
|
||||
|
||||
"LibTomMath",
|
||||
(int)DIGIT_BIT,
|
||||
|
||||
&init,
|
||||
&init_copy,
|
||||
&deinit,
|
||||
|
||||
&neg,
|
||||
©,
|
||||
|
||||
&set_int,
|
||||
&get_int,
|
||||
&get_digit,
|
||||
&get_digit_count,
|
||||
&compare,
|
||||
&compare_d,
|
||||
&count_bits,
|
||||
&count_lsb_bits,
|
||||
&twoexpt,
|
||||
|
||||
&read_radix,
|
||||
&write_radix,
|
||||
&unsigned_size,
|
||||
&unsigned_write,
|
||||
&unsigned_read,
|
||||
|
||||
&add,
|
||||
&addi,
|
||||
&sub,
|
||||
&subi,
|
||||
&mul,
|
||||
&muli,
|
||||
&sqr,
|
||||
÷,
|
||||
&div_2,
|
||||
&modi,
|
||||
&gcd,
|
||||
&lcm,
|
||||
|
||||
&mulmod,
|
||||
&sqrmod,
|
||||
&invmod,
|
||||
|
||||
&montgomery_setup,
|
||||
&montgomery_normalization,
|
||||
&montgomery_reduce,
|
||||
&montgomery_deinit,
|
||||
|
||||
&exptmod,
|
||||
&isprime,
|
||||
|
||||
#ifdef LTC_MECC
|
||||
#ifdef LTC_MECC_FP
|
||||
<c_ecc_fp_mulmod,
|
||||
#else
|
||||
<c_ecc_mulmod,
|
||||
#endif
|
||||
<c_ecc_projective_add_point,
|
||||
<c_ecc_projective_dbl_point,
|
||||
<c_ecc_map,
|
||||
#ifdef LTC_ECC_SHAMIR
|
||||
#ifdef LTC_MECC_FP
|
||||
<c_ecc_fp_mul2add,
|
||||
#else
|
||||
<c_ecc_mul2add,
|
||||
#endif /* LTC_MECC_FP */
|
||||
#else
|
||||
NULL,
|
||||
#endif /* LTC_ECC_SHAMIR */
|
||||
#else
|
||||
NULL, NULL, NULL, NULL, NULL,
|
||||
#endif /* LTC_MECC */
|
||||
|
||||
#ifdef LTC_MRSA
|
||||
&rsa_make_key,
|
||||
&rsa_exptmod,
|
||||
#else
|
||||
NULL, NULL,
|
||||
#endif
|
||||
&addmod,
|
||||
&submod,
|
||||
|
||||
&set_rand,
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
76
thirdparty/libtomcrypt/math/multi.c
vendored
Normal file
76
thirdparty/libtomcrypt/math/multi.c
vendored
Normal file
@ -0,0 +1,76 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_MPI
|
||||
#include <stdarg.h>
|
||||
|
||||
int ltc_init_multi(void **a, ...)
|
||||
{
|
||||
void **cur = a;
|
||||
int np = 0;
|
||||
va_list args;
|
||||
|
||||
va_start(args, a);
|
||||
while (cur != NULL) {
|
||||
if (mp_init(cur) != CRYPT_OK) {
|
||||
/* failed */
|
||||
va_list clean_list;
|
||||
|
||||
va_start(clean_list, a);
|
||||
cur = a;
|
||||
while (np--) {
|
||||
mp_clear(*cur);
|
||||
cur = va_arg(clean_list, void**);
|
||||
}
|
||||
va_end(clean_list);
|
||||
va_end(args);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
++np;
|
||||
cur = va_arg(args, void**);
|
||||
}
|
||||
va_end(args);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
void ltc_deinit_multi(void *a, ...)
|
||||
{
|
||||
void *cur = a;
|
||||
va_list args;
|
||||
|
||||
va_start(args, a);
|
||||
while (cur != NULL) {
|
||||
mp_clear(cur);
|
||||
cur = va_arg(args, void *);
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void ltc_cleanup_multi(void **a, ...)
|
||||
{
|
||||
void **cur = a;
|
||||
va_list args;
|
||||
|
||||
va_start(args, a);
|
||||
while (cur != NULL) {
|
||||
if (*cur != NULL) {
|
||||
mp_clear(*cur);
|
||||
*cur = NULL;
|
||||
}
|
||||
cur = va_arg(args, void**);
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
62
thirdparty/libtomcrypt/math/radix_to_bin.c
vendored
Normal file
62
thirdparty/libtomcrypt/math/radix_to_bin.c
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file radix_to_bin.c
|
||||
Convert data from a specific radix to binary.
|
||||
Steffen Jaeckel
|
||||
*/
|
||||
|
||||
/**
|
||||
Convert data from a specific radix to binary
|
||||
|
||||
The default MPI descriptors #ltm_desc, #tfm_desc and #gmp_desc
|
||||
have the following restrictions on parameters:
|
||||
|
||||
\p in - NUL-terminated char buffer
|
||||
|
||||
\p radix - 2..64
|
||||
|
||||
@param in The input
|
||||
@param radix The radix of the input
|
||||
@param out The output buffer
|
||||
@param len [in/out] The length of the output buffer
|
||||
|
||||
@return CRYPT_OK on success.
|
||||
*/
|
||||
int radix_to_bin(const void *in, int radix, void *out, unsigned long *len)
|
||||
{
|
||||
unsigned long l;
|
||||
void* mpi;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(len != NULL);
|
||||
|
||||
if ((err = mp_init(&mpi)) != CRYPT_OK) return err;
|
||||
if ((err = mp_read_radix(mpi, in, radix)) != CRYPT_OK) goto LBL_ERR;
|
||||
|
||||
if ((l = mp_unsigned_bin_size(mpi)) > *len) {
|
||||
*len = l;
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
*len = l;
|
||||
|
||||
if ((err = mp_to_unsigned_bin(mpi, out)) != CRYPT_OK) goto LBL_ERR;
|
||||
|
||||
LBL_ERR:
|
||||
mp_clear(mpi);
|
||||
return err;
|
||||
}
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
75
thirdparty/libtomcrypt/math/rand_bn.c
vendored
Normal file
75
thirdparty/libtomcrypt/math/rand_bn.c
vendored
Normal file
@ -0,0 +1,75 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#if defined(LTC_MDSA) || defined(LTC_MECC)
|
||||
/**
|
||||
Generate a random number N with given bitlength (note: MSB can be 0)
|
||||
*/
|
||||
|
||||
int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng)
|
||||
{
|
||||
int res, bytes;
|
||||
unsigned char *buf, mask;
|
||||
|
||||
LTC_ARGCHK(N != NULL);
|
||||
LTC_ARGCHK(bits > 1);
|
||||
|
||||
/* check PRNG */
|
||||
if ((res = prng_is_valid(wprng)) != CRYPT_OK) return res;
|
||||
|
||||
bytes = (bits+7) >> 3;
|
||||
mask = 0xff << (8 - bits % 8);
|
||||
|
||||
/* allocate buffer */
|
||||
if ((buf = XCALLOC(1, bytes)) == NULL) return CRYPT_MEM;
|
||||
|
||||
/* generate random bytes */
|
||||
if (prng_descriptor[wprng].read(buf, bytes, prng) != (unsigned long)bytes) {
|
||||
res = CRYPT_ERROR_READPRNG;
|
||||
goto cleanup;
|
||||
}
|
||||
/* mask bits */
|
||||
buf[0] &= ~mask;
|
||||
/* load value */
|
||||
if ((res = mp_read_unsigned_bin(N, buf, bytes)) != CRYPT_OK) goto cleanup;
|
||||
|
||||
res = CRYPT_OK;
|
||||
|
||||
cleanup:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(buf, bytes);
|
||||
#endif
|
||||
XFREE(buf);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
Generate a random number N in a range: 1 <= N < limit
|
||||
*/
|
||||
int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng)
|
||||
{
|
||||
int res, bits;
|
||||
|
||||
LTC_ARGCHK(N != NULL);
|
||||
LTC_ARGCHK(limit != NULL);
|
||||
|
||||
bits = mp_count_bits(limit);
|
||||
do {
|
||||
res = rand_bn_bits(N, bits, prng, wprng);
|
||||
if (res != CRYPT_OK) return res;
|
||||
} while (mp_cmp_d(N, 0) != LTC_MP_GT || mp_cmp(N, limit) != LTC_MP_LT);
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
88
thirdparty/libtomcrypt/math/rand_prime.c
vendored
Normal file
88
thirdparty/libtomcrypt/math/rand_prime.c
vendored
Normal file
@ -0,0 +1,88 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#if defined(LTC_MRSA) || (!defined(LTC_NO_MATH) && !defined(LTC_NO_PRNGS))
|
||||
|
||||
/**
|
||||
@file rand_prime.c
|
||||
Generate a random prime, Tom St Denis
|
||||
*/
|
||||
|
||||
#define USE_BBS 1
|
||||
|
||||
int rand_prime(void *N, long len, prng_state *prng, int wprng)
|
||||
{
|
||||
int err, res, type;
|
||||
unsigned char *buf;
|
||||
|
||||
LTC_ARGCHK(N != NULL);
|
||||
|
||||
/* get type */
|
||||
if (len < 0) {
|
||||
type = USE_BBS;
|
||||
len = -len;
|
||||
} else {
|
||||
type = 0;
|
||||
}
|
||||
|
||||
/* allow sizes between 2 and 512 bytes for a prime size */
|
||||
if (len < 2 || len > 512) {
|
||||
return CRYPT_INVALID_PRIME_SIZE;
|
||||
}
|
||||
|
||||
/* valid PRNG? Better be! */
|
||||
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* allocate buffer to work with */
|
||||
buf = XCALLOC(1, len);
|
||||
if (buf == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
do {
|
||||
/* generate value */
|
||||
if (prng_descriptor[wprng].read(buf, len, prng) != (unsigned long)len) {
|
||||
XFREE(buf);
|
||||
return CRYPT_ERROR_READPRNG;
|
||||
}
|
||||
|
||||
/* munge bits */
|
||||
buf[0] |= 0x80 | 0x40;
|
||||
buf[len-1] |= 0x01 | ((type & USE_BBS) ? 0x02 : 0x00);
|
||||
|
||||
/* load value */
|
||||
if ((err = mp_read_unsigned_bin(N, buf, len)) != CRYPT_OK) {
|
||||
XFREE(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
/* test */
|
||||
if ((err = mp_prime_is_prime(N, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) {
|
||||
XFREE(buf);
|
||||
return err;
|
||||
}
|
||||
} while (res == LTC_MP_NO);
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(buf, len);
|
||||
#endif
|
||||
|
||||
XFREE(buf);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif /* LTC_NO_MATH */
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
807
thirdparty/libtomcrypt/math/tfm_desc.c
vendored
Normal file
807
thirdparty/libtomcrypt/math/tfm_desc.c
vendored
Normal file
@ -0,0 +1,807 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#define DESC_DEF_ONLY
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef TFM_DESC
|
||||
|
||||
#include <tfm.h>
|
||||
|
||||
static const struct {
|
||||
int tfm_code, ltc_code;
|
||||
} tfm_to_ltc_codes[] = {
|
||||
{ FP_OKAY , CRYPT_OK},
|
||||
{ FP_MEM , CRYPT_MEM},
|
||||
{ FP_VAL , CRYPT_INVALID_ARG},
|
||||
};
|
||||
|
||||
/**
|
||||
Convert a tfm error to a LTC error (Possibly the most powerful function ever! Oh wait... no)
|
||||
@param err The error to convert
|
||||
@return The equivalent LTC error code or CRYPT_ERROR if none found
|
||||
*/
|
||||
static int tfm_to_ltc_error(int err)
|
||||
{
|
||||
int x;
|
||||
|
||||
for (x = 0; x < (int)(sizeof(tfm_to_ltc_codes)/sizeof(tfm_to_ltc_codes[0])); x++) {
|
||||
if (err == tfm_to_ltc_codes[x].tfm_code) {
|
||||
return tfm_to_ltc_codes[x].ltc_code;
|
||||
}
|
||||
}
|
||||
return CRYPT_ERROR;
|
||||
}
|
||||
|
||||
static int init(void **a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
|
||||
*a = XCALLOC(1, sizeof(fp_int));
|
||||
if (*a == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
fp_init(*a);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static void deinit(void *a)
|
||||
{
|
||||
LTC_ARGCHKVD(a != NULL);
|
||||
XFREE(a);
|
||||
}
|
||||
|
||||
static int neg(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
fp_neg(((fp_int*)a), ((fp_int*)b));
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int copy(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
fp_copy(a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int init_copy(void **a, void *b)
|
||||
{
|
||||
if (init(a) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
return copy(b, *a);
|
||||
}
|
||||
|
||||
/* ---- trivial ---- */
|
||||
static int set_int(void *a, ltc_mp_digit b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
fp_set(a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static unsigned long get_int(void *a)
|
||||
{
|
||||
fp_int *A;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
A = a;
|
||||
return A->used > 0 ? A->dp[0] : 0;
|
||||
}
|
||||
|
||||
static ltc_mp_digit get_digit(void *a, int n)
|
||||
{
|
||||
fp_int *A;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
A = a;
|
||||
return (n >= A->used || n < 0) ? 0 : A->dp[n];
|
||||
}
|
||||
|
||||
static int get_digit_count(void *a)
|
||||
{
|
||||
fp_int *A;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
A = a;
|
||||
return A->used;
|
||||
}
|
||||
|
||||
static int compare(void *a, void *b)
|
||||
{
|
||||
int ret;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
ret = fp_cmp(a, b);
|
||||
switch (ret) {
|
||||
case FP_LT: return LTC_MP_LT;
|
||||
case FP_EQ: return LTC_MP_EQ;
|
||||
case FP_GT: return LTC_MP_GT;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int compare_d(void *a, ltc_mp_digit b)
|
||||
{
|
||||
int ret;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
ret = fp_cmp_d(a, b);
|
||||
switch (ret) {
|
||||
case FP_LT: return LTC_MP_LT;
|
||||
case FP_EQ: return LTC_MP_EQ;
|
||||
case FP_GT: return LTC_MP_GT;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int count_bits(void *a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return fp_count_bits(a);
|
||||
}
|
||||
|
||||
static int count_lsb_bits(void *a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return fp_cnt_lsb(a);
|
||||
}
|
||||
|
||||
static int twoexpt(void *a, int n)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
fp_2expt(a, n);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* ---- conversions ---- */
|
||||
|
||||
/* read ascii string */
|
||||
static int read_radix(void *a, const char *b, int radix)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return tfm_to_ltc_error(fp_read_radix(a, (char *)b, radix));
|
||||
}
|
||||
|
||||
/* write one */
|
||||
static int write_radix(void *a, char *b, int radix)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return tfm_to_ltc_error(fp_toradix(a, b, radix));
|
||||
}
|
||||
|
||||
/* get size as unsigned char string */
|
||||
static unsigned long unsigned_size(void *a)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
return fp_unsigned_bin_size(a);
|
||||
}
|
||||
|
||||
/* store */
|
||||
static int unsigned_write(void *a, unsigned char *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
fp_to_unsigned_bin(a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* read */
|
||||
static int unsigned_read(void *a, unsigned char *b, unsigned long len)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
fp_read_unsigned_bin(a, b, len);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* add */
|
||||
static int add(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
fp_add(a, b, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int addi(void *a, ltc_mp_digit b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
fp_add_d(a, b, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* sub */
|
||||
static int sub(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
fp_sub(a, b, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int subi(void *a, ltc_mp_digit b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
fp_sub_d(a, b, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* mul */
|
||||
static int mul(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
fp_mul(a, b, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int muli(void *a, ltc_mp_digit b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
fp_mul_d(a, b, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* sqr */
|
||||
static int sqr(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
fp_sqr(a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* div */
|
||||
static int divide(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
return tfm_to_ltc_error(fp_div(a, b, c, d));
|
||||
}
|
||||
|
||||
static int div_2(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
fp_div_2(a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* modi */
|
||||
static int modi(void *a, ltc_mp_digit b, ltc_mp_digit *c)
|
||||
{
|
||||
fp_digit tmp;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
|
||||
if ((err = tfm_to_ltc_error(fp_mod_d(a, b, &tmp))) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
*c = tmp;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* gcd */
|
||||
static int gcd(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
fp_gcd(a, b, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* lcm */
|
||||
static int lcm(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
fp_lcm(a, b, c);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
static int addmod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
return tfm_to_ltc_error(fp_addmod(a,b,c,d));
|
||||
}
|
||||
|
||||
static int submod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
return tfm_to_ltc_error(fp_submod(a,b,c,d));
|
||||
}
|
||||
|
||||
static int mulmod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
return tfm_to_ltc_error(fp_mulmod(a,b,c,d));
|
||||
}
|
||||
|
||||
static int sqrmod(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return tfm_to_ltc_error(fp_sqrmod(a,b,c));
|
||||
}
|
||||
|
||||
/* invmod */
|
||||
static int invmod(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
return tfm_to_ltc_error(fp_invmod(a, b, c));
|
||||
}
|
||||
|
||||
/* setup */
|
||||
static int montgomery_setup(void *a, void **b)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
*b = XCALLOC(1, sizeof(fp_digit));
|
||||
if (*b == NULL) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
if ((err = tfm_to_ltc_error(fp_montgomery_setup(a, (fp_digit *)*b))) != CRYPT_OK) {
|
||||
XFREE(*b);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/* get normalization value */
|
||||
static int montgomery_normalization(void *a, void *b)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
fp_montgomery_calc_normalization(a, b);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* reduce */
|
||||
static int montgomery_reduce(void *a, void *b, void *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
fp_montgomery_reduce(a, b, *((fp_digit *)c));
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/* clean up */
|
||||
static void montgomery_deinit(void *a)
|
||||
{
|
||||
XFREE(a);
|
||||
}
|
||||
|
||||
static int exptmod(void *a, void *b, void *c, void *d)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(b != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
LTC_ARGCHK(d != NULL);
|
||||
return tfm_to_ltc_error(fp_exptmod(a,b,c,d));
|
||||
}
|
||||
|
||||
static int isprime(void *a, int b, int *c)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
LTC_ARGCHK(c != NULL);
|
||||
if (b == 0) {
|
||||
b = LTC_MILLER_RABIN_REPS;
|
||||
} /* if */
|
||||
*c = (fp_isprime_ex(a, b) == FP_YES) ? LTC_MP_YES : LTC_MP_NO;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#if defined(LTC_MECC) && defined(LTC_MECC_ACCEL)
|
||||
|
||||
static int tfm_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *Mp)
|
||||
{
|
||||
fp_int t1, t2;
|
||||
fp_digit mp;
|
||||
|
||||
LTC_ARGCHK(P != NULL);
|
||||
LTC_ARGCHK(R != NULL);
|
||||
LTC_ARGCHK(modulus != NULL);
|
||||
LTC_ARGCHK(Mp != NULL);
|
||||
|
||||
mp = *((fp_digit*)Mp);
|
||||
|
||||
fp_init(&t1);
|
||||
fp_init(&t2);
|
||||
|
||||
if (P != R) {
|
||||
fp_copy(P->x, R->x);
|
||||
fp_copy(P->y, R->y);
|
||||
fp_copy(P->z, R->z);
|
||||
}
|
||||
|
||||
/* t1 = Z * Z */
|
||||
fp_sqr(R->z, &t1);
|
||||
fp_montgomery_reduce(&t1, modulus, mp);
|
||||
/* Z = Y * Z */
|
||||
fp_mul(R->z, R->y, R->z);
|
||||
fp_montgomery_reduce(R->z, modulus, mp);
|
||||
/* Z = 2Z */
|
||||
fp_add(R->z, R->z, R->z);
|
||||
if (fp_cmp(R->z, modulus) != FP_LT) {
|
||||
fp_sub(R->z, modulus, R->z);
|
||||
}
|
||||
|
||||
/* &t2 = X - T1 */
|
||||
fp_sub(R->x, &t1, &t2);
|
||||
if (fp_cmp_d(&t2, 0) == FP_LT) {
|
||||
fp_add(&t2, modulus, &t2);
|
||||
}
|
||||
/* T1 = X + T1 */
|
||||
fp_add(&t1, R->x, &t1);
|
||||
if (fp_cmp(&t1, modulus) != FP_LT) {
|
||||
fp_sub(&t1, modulus, &t1);
|
||||
}
|
||||
/* T2 = T1 * T2 */
|
||||
fp_mul(&t1, &t2, &t2);
|
||||
fp_montgomery_reduce(&t2, modulus, mp);
|
||||
/* T1 = 2T2 */
|
||||
fp_add(&t2, &t2, &t1);
|
||||
if (fp_cmp(&t1, modulus) != FP_LT) {
|
||||
fp_sub(&t1, modulus, &t1);
|
||||
}
|
||||
/* T1 = T1 + T2 */
|
||||
fp_add(&t1, &t2, &t1);
|
||||
if (fp_cmp(&t1, modulus) != FP_LT) {
|
||||
fp_sub(&t1, modulus, &t1);
|
||||
}
|
||||
|
||||
/* Y = 2Y */
|
||||
fp_add(R->y, R->y, R->y);
|
||||
if (fp_cmp(R->y, modulus) != FP_LT) {
|
||||
fp_sub(R->y, modulus, R->y);
|
||||
}
|
||||
/* Y = Y * Y */
|
||||
fp_sqr(R->y, R->y);
|
||||
fp_montgomery_reduce(R->y, modulus, mp);
|
||||
/* T2 = Y * Y */
|
||||
fp_sqr(R->y, &t2);
|
||||
fp_montgomery_reduce(&t2, modulus, mp);
|
||||
/* T2 = T2/2 */
|
||||
if (fp_isodd(&t2)) {
|
||||
fp_add(&t2, modulus, &t2);
|
||||
}
|
||||
fp_div_2(&t2, &t2);
|
||||
/* Y = Y * X */
|
||||
fp_mul(R->y, R->x, R->y);
|
||||
fp_montgomery_reduce(R->y, modulus, mp);
|
||||
|
||||
/* X = T1 * T1 */
|
||||
fp_sqr(&t1, R->x);
|
||||
fp_montgomery_reduce(R->x, modulus, mp);
|
||||
/* X = X - Y */
|
||||
fp_sub(R->x, R->y, R->x);
|
||||
if (fp_cmp_d(R->x, 0) == FP_LT) {
|
||||
fp_add(R->x, modulus, R->x);
|
||||
}
|
||||
/* X = X - Y */
|
||||
fp_sub(R->x, R->y, R->x);
|
||||
if (fp_cmp_d(R->x, 0) == FP_LT) {
|
||||
fp_add(R->x, modulus, R->x);
|
||||
}
|
||||
|
||||
/* Y = Y - X */
|
||||
fp_sub(R->y, R->x, R->y);
|
||||
if (fp_cmp_d(R->y, 0) == FP_LT) {
|
||||
fp_add(R->y, modulus, R->y);
|
||||
}
|
||||
/* Y = Y * T1 */
|
||||
fp_mul(R->y, &t1, R->y);
|
||||
fp_montgomery_reduce(R->y, modulus, mp);
|
||||
/* Y = Y - T2 */
|
||||
fp_sub(R->y, &t2, R->y);
|
||||
if (fp_cmp_d(R->y, 0) == FP_LT) {
|
||||
fp_add(R->y, modulus, R->y);
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Add two ECC points
|
||||
@param P The point to add
|
||||
@param Q The point to add
|
||||
@param R [out] The destination of the double
|
||||
@param modulus The modulus of the field the ECC curve is in
|
||||
@param Mp The "b" value from montgomery_setup()
|
||||
@return CRYPT_OK on success
|
||||
*/
|
||||
static int tfm_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *Mp)
|
||||
{
|
||||
fp_int t1, t2, x, y, z;
|
||||
fp_digit mp;
|
||||
|
||||
LTC_ARGCHK(P != NULL);
|
||||
LTC_ARGCHK(Q != NULL);
|
||||
LTC_ARGCHK(R != NULL);
|
||||
LTC_ARGCHK(modulus != NULL);
|
||||
LTC_ARGCHK(Mp != NULL);
|
||||
|
||||
mp = *((fp_digit*)Mp);
|
||||
|
||||
fp_init(&t1);
|
||||
fp_init(&t2);
|
||||
fp_init(&x);
|
||||
fp_init(&y);
|
||||
fp_init(&z);
|
||||
|
||||
/* should we dbl instead? */
|
||||
fp_sub(modulus, Q->y, &t1);
|
||||
if ( (fp_cmp(P->x, Q->x) == FP_EQ) &&
|
||||
(Q->z != NULL && fp_cmp(P->z, Q->z) == FP_EQ) &&
|
||||
(fp_cmp(P->y, Q->y) == FP_EQ || fp_cmp(P->y, &t1) == FP_EQ)) {
|
||||
return tfm_ecc_projective_dbl_point(P, R, modulus, Mp);
|
||||
}
|
||||
|
||||
fp_copy(P->x, &x);
|
||||
fp_copy(P->y, &y);
|
||||
fp_copy(P->z, &z);
|
||||
|
||||
/* if Z is one then these are no-operations */
|
||||
if (Q->z != NULL) {
|
||||
/* T1 = Z' * Z' */
|
||||
fp_sqr(Q->z, &t1);
|
||||
fp_montgomery_reduce(&t1, modulus, mp);
|
||||
/* X = X * T1 */
|
||||
fp_mul(&t1, &x, &x);
|
||||
fp_montgomery_reduce(&x, modulus, mp);
|
||||
/* T1 = Z' * T1 */
|
||||
fp_mul(Q->z, &t1, &t1);
|
||||
fp_montgomery_reduce(&t1, modulus, mp);
|
||||
/* Y = Y * T1 */
|
||||
fp_mul(&t1, &y, &y);
|
||||
fp_montgomery_reduce(&y, modulus, mp);
|
||||
}
|
||||
|
||||
/* T1 = Z*Z */
|
||||
fp_sqr(&z, &t1);
|
||||
fp_montgomery_reduce(&t1, modulus, mp);
|
||||
/* T2 = X' * T1 */
|
||||
fp_mul(Q->x, &t1, &t2);
|
||||
fp_montgomery_reduce(&t2, modulus, mp);
|
||||
/* T1 = Z * T1 */
|
||||
fp_mul(&z, &t1, &t1);
|
||||
fp_montgomery_reduce(&t1, modulus, mp);
|
||||
/* T1 = Y' * T1 */
|
||||
fp_mul(Q->y, &t1, &t1);
|
||||
fp_montgomery_reduce(&t1, modulus, mp);
|
||||
|
||||
/* Y = Y - T1 */
|
||||
fp_sub(&y, &t1, &y);
|
||||
if (fp_cmp_d(&y, 0) == FP_LT) {
|
||||
fp_add(&y, modulus, &y);
|
||||
}
|
||||
/* T1 = 2T1 */
|
||||
fp_add(&t1, &t1, &t1);
|
||||
if (fp_cmp(&t1, modulus) != FP_LT) {
|
||||
fp_sub(&t1, modulus, &t1);
|
||||
}
|
||||
/* T1 = Y + T1 */
|
||||
fp_add(&t1, &y, &t1);
|
||||
if (fp_cmp(&t1, modulus) != FP_LT) {
|
||||
fp_sub(&t1, modulus, &t1);
|
||||
}
|
||||
/* X = X - T2 */
|
||||
fp_sub(&x, &t2, &x);
|
||||
if (fp_cmp_d(&x, 0) == FP_LT) {
|
||||
fp_add(&x, modulus, &x);
|
||||
}
|
||||
/* T2 = 2T2 */
|
||||
fp_add(&t2, &t2, &t2);
|
||||
if (fp_cmp(&t2, modulus) != FP_LT) {
|
||||
fp_sub(&t2, modulus, &t2);
|
||||
}
|
||||
/* T2 = X + T2 */
|
||||
fp_add(&t2, &x, &t2);
|
||||
if (fp_cmp(&t2, modulus) != FP_LT) {
|
||||
fp_sub(&t2, modulus, &t2);
|
||||
}
|
||||
|
||||
/* if Z' != 1 */
|
||||
if (Q->z != NULL) {
|
||||
/* Z = Z * Z' */
|
||||
fp_mul(&z, Q->z, &z);
|
||||
fp_montgomery_reduce(&z, modulus, mp);
|
||||
}
|
||||
|
||||
/* Z = Z * X */
|
||||
fp_mul(&z, &x, &z);
|
||||
fp_montgomery_reduce(&z, modulus, mp);
|
||||
|
||||
/* T1 = T1 * X */
|
||||
fp_mul(&t1, &x, &t1);
|
||||
fp_montgomery_reduce(&t1, modulus, mp);
|
||||
/* X = X * X */
|
||||
fp_sqr(&x, &x);
|
||||
fp_montgomery_reduce(&x, modulus, mp);
|
||||
/* T2 = T2 * x */
|
||||
fp_mul(&t2, &x, &t2);
|
||||
fp_montgomery_reduce(&t2, modulus, mp);
|
||||
/* T1 = T1 * X */
|
||||
fp_mul(&t1, &x, &t1);
|
||||
fp_montgomery_reduce(&t1, modulus, mp);
|
||||
|
||||
/* X = Y*Y */
|
||||
fp_sqr(&y, &x);
|
||||
fp_montgomery_reduce(&x, modulus, mp);
|
||||
/* X = X - T2 */
|
||||
fp_sub(&x, &t2, &x);
|
||||
if (fp_cmp_d(&x, 0) == FP_LT) {
|
||||
fp_add(&x, modulus, &x);
|
||||
}
|
||||
|
||||
/* T2 = T2 - X */
|
||||
fp_sub(&t2, &x, &t2);
|
||||
if (fp_cmp_d(&t2, 0) == FP_LT) {
|
||||
fp_add(&t2, modulus, &t2);
|
||||
}
|
||||
/* T2 = T2 - X */
|
||||
fp_sub(&t2, &x, &t2);
|
||||
if (fp_cmp_d(&t2, 0) == FP_LT) {
|
||||
fp_add(&t2, modulus, &t2);
|
||||
}
|
||||
/* T2 = T2 * Y */
|
||||
fp_mul(&t2, &y, &t2);
|
||||
fp_montgomery_reduce(&t2, modulus, mp);
|
||||
/* Y = T2 - T1 */
|
||||
fp_sub(&t2, &t1, &y);
|
||||
if (fp_cmp_d(&y, 0) == FP_LT) {
|
||||
fp_add(&y, modulus, &y);
|
||||
}
|
||||
/* Y = Y/2 */
|
||||
if (fp_isodd(&y)) {
|
||||
fp_add(&y, modulus, &y);
|
||||
}
|
||||
fp_div_2(&y, &y);
|
||||
|
||||
fp_copy(&x, R->x);
|
||||
fp_copy(&y, R->y);
|
||||
fp_copy(&z, R->z);
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
static int set_rand(void *a, int size)
|
||||
{
|
||||
LTC_ARGCHK(a != NULL);
|
||||
fp_rand(a, size);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
const ltc_math_descriptor tfm_desc = {
|
||||
|
||||
"TomsFastMath",
|
||||
(int)DIGIT_BIT,
|
||||
|
||||
&init,
|
||||
&init_copy,
|
||||
&deinit,
|
||||
|
||||
&neg,
|
||||
©,
|
||||
|
||||
&set_int,
|
||||
&get_int,
|
||||
&get_digit,
|
||||
&get_digit_count,
|
||||
&compare,
|
||||
&compare_d,
|
||||
&count_bits,
|
||||
&count_lsb_bits,
|
||||
&twoexpt,
|
||||
|
||||
&read_radix,
|
||||
&write_radix,
|
||||
&unsigned_size,
|
||||
&unsigned_write,
|
||||
&unsigned_read,
|
||||
|
||||
&add,
|
||||
&addi,
|
||||
&sub,
|
||||
&subi,
|
||||
&mul,
|
||||
&muli,
|
||||
&sqr,
|
||||
÷,
|
||||
&div_2,
|
||||
&modi,
|
||||
&gcd,
|
||||
&lcm,
|
||||
|
||||
&mulmod,
|
||||
&sqrmod,
|
||||
&invmod,
|
||||
|
||||
&montgomery_setup,
|
||||
&montgomery_normalization,
|
||||
&montgomery_reduce,
|
||||
&montgomery_deinit,
|
||||
|
||||
&exptmod,
|
||||
&isprime,
|
||||
|
||||
#ifdef LTC_MECC
|
||||
#ifdef LTC_MECC_FP
|
||||
<c_ecc_fp_mulmod,
|
||||
#else
|
||||
<c_ecc_mulmod,
|
||||
#endif /* LTC_MECC_FP */
|
||||
#ifdef LTC_MECC_ACCEL
|
||||
&tfm_ecc_projective_add_point,
|
||||
&tfm_ecc_projective_dbl_point,
|
||||
#else
|
||||
<c_ecc_projective_add_point,
|
||||
<c_ecc_projective_dbl_point,
|
||||
#endif /* LTC_MECC_ACCEL */
|
||||
<c_ecc_map,
|
||||
#ifdef LTC_ECC_SHAMIR
|
||||
#ifdef LTC_MECC_FP
|
||||
<c_ecc_fp_mul2add,
|
||||
#else
|
||||
<c_ecc_mul2add,
|
||||
#endif /* LTC_MECC_FP */
|
||||
#else
|
||||
NULL,
|
||||
#endif /* LTC_ECC_SHAMIR */
|
||||
#else
|
||||
NULL, NULL, NULL, NULL, NULL,
|
||||
#endif /* LTC_MECC */
|
||||
|
||||
#ifdef LTC_MRSA
|
||||
&rsa_make_key,
|
||||
&rsa_exptmod,
|
||||
#else
|
||||
NULL, NULL,
|
||||
#endif
|
||||
&addmod,
|
||||
&submod,
|
||||
|
||||
set_rand,
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
Reference in New Issue
Block a user