Update of /cvsroot/opentnl/tnl/libtomcrypt/misc/mpi
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/misc/mpi
Added Files:
is_prime.c mpi.c mpi_to_ltc_error.c rand_prime.c
Log Message:
Updated to libtomcrypt 1.0.2
Not tested on linux or OS X
--- NEW FILE: mpi.c ---
/* Start: bn_error.c */
#include <ltc_tommath.h>
#ifdef BN_ERROR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tom...@gm..., http://math.libtomcrypt.org
*/
static const struct {
[...9005 lines suppressed...]
/* Known optimal configurations
CPU /Compiler /MUL CUTOFF/SQR CUTOFF
-------------------------------------------------------------
Intel P4 Northwood /GCC v3.4.1 / 88/ 128/LTM 0.32 ;-)
AMD Athlon64 /GCC v3.4.4 / 74/ 124/LTM 0.34
*/
int KARATSUBA_MUL_CUTOFF = 74, /* Min. number of digits before Karatsuba multiplication is used. */
KARATSUBA_SQR_CUTOFF = 124, /* Min. number of digits before Karatsuba squaring is used. */
TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */
TOOM_SQR_CUTOFF = 400;
#endif
/* End: bncore.c */
/* EOF */
--- NEW FILE: mpi_to_ltc_error.c ---
/* 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.
*
* Tom St Denis, tom...@gm..., http://libtomcrypt.org
*/
#include "tomcrypt.h"
/**
@file mpi_to_ltc_error.c
Convert MPI errors to LTC, Tom St Denis
*/
#ifdef MPI
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
*/
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;
}
#endif
--- NEW FILE: is_prime.c ---
/* 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.
*
* Tom St Denis, tom...@gm..., http://libtomcrypt.org
*/
#include "tomcrypt.h"
/**
@file is_prime.c
Determines if integer is prime for LTC, Tom St Denis
*/
#ifdef MPI
/* figures out if a number is prime (MR test) */
int is_prime(mp_int *N, int *result)
{
int err;
LTC_ARGCHK(N != NULL);
LTC_ARGCHK(result != NULL);
if ((err = mp_prime_is_prime(N, mp_prime_rabin_miller_trials(mp_count_bits(N)), result)) != MP_OKAY) {
return mpi_to_ltc_error(err);
}
return CRYPT_OK;
}
#endif
--- NEW FILE: rand_prime.c ---
/* 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.
*
* Tom St Denis, tom...@gm..., http://libtomcrypt.org
*/
#include "tomcrypt.h"
/**
@file rand_prime.c
Generate a random prime, Tom St Denis
*/
#ifdef MPI
struct rng_data {
prng_state *prng;
int wprng;
};
static int rand_prime_helper(unsigned char *dst, int len, void *dat)
{
return (int)prng_descriptor[((struct rng_data *)dat)->wprng].read(dst, len, ((struct rng_data *)dat)->prng);
}
int rand_prime(mp_int *N, long len, prng_state *prng, int wprng)
{
struct rng_data rng;
int type, err;
LTC_ARGCHK(N != NULL);
/* allow sizes between 2 and 256 bytes for a prime size */
if (len < 16 || len > 4096) {
return CRYPT_INVALID_PRIME_SIZE;
}
/* valid PRNG? Better be! */
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
return err;
}
/* setup our callback data, then world domination! */
rng.prng = prng;
rng.wprng = wprng;
/* get type */
if (len < 0) {
type = LTM_PRIME_BBS;
len = -len;
} else {
type = 0;
}
type |= LTM_PRIME_2MSB_ON;
/* New prime generation makes the code even more cryptoish-insane. Do you know what this means!!!
-- Gir: Yeah, oh wait, er, no.
*/
return mpi_to_ltc_error(mp_prime_random_ex(N, mp_prime_rabin_miller_trials(len), len, type, rand_prime_helper, &rng));
}
#endif
|