summaryrefslogtreecommitdiff
path: root/libtomcrypt/src/math
diff options
context:
space:
mode:
Diffstat (limited to 'libtomcrypt/src/math')
-rw-r--r--libtomcrypt/src/math/ltm_desc.c63
-rw-r--r--libtomcrypt/src/math/multi.c1
-rw-r--r--libtomcrypt/src/math/rand_bn.c2
3 files changed, 37 insertions, 29 deletions
diff --git a/libtomcrypt/src/math/ltm_desc.c b/libtomcrypt/src/math/ltm_desc.c
index 3e2a0c9..24e8611 100644
--- a/libtomcrypt/src/math/ltm_desc.c
+++ b/libtomcrypt/src/math/ltm_desc.c
@@ -15,11 +15,14 @@
#include <tommath.h>
static const struct {
- int mpi_code, ltc_code;
+ mp_err mpi_code;
+ int ltc_code;
} mpi_to_ltc_codes[] = {
{ MP_OKAY , CRYPT_OK},
{ MP_MEM , CRYPT_MEM},
{ MP_VAL , CRYPT_INVALID_ARG},
+ { MP_ITER , CRYPT_INVALID_PACKET},
+ { MP_BUF , CRYPT_BUFFER_OVERFLOW},
};
/**
@@ -27,11 +30,11 @@ static const struct {
@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)
+static int mpi_to_ltc_error(mp_err err)
{
- int x;
+ size_t x;
- for (x = 0; x < (int)(sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0])); x++) {
+ for (x = 0; x < 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;
}
@@ -39,17 +42,27 @@ static int mpi_to_ltc_error(int err)
return CRYPT_ERROR;
}
-static int init(void **a)
+static int init_mpi(void **a)
{
- int err;
-
LTC_ARGCHK(a != NULL);
*a = XCALLOC(1, sizeof(mp_int));
if (*a == NULL) {
return CRYPT_MEM;
+ } else {
+ return CRYPT_OK;
}
+}
+
+static int init(void **a)
+{
+ int err;
+ LTC_ARGCHK(a != NULL);
+
+ if ((err = init_mpi(a)) != CRYPT_OK) {
+ return err;
+ }
if ((err = mpi_to_ltc_error(mp_init(*a))) != CRYPT_OK) {
XFREE(*a);
}
@@ -79,23 +92,25 @@ static int copy(void *a, void *b)
static int init_copy(void **a, void *b)
{
- if (init(a) != CRYPT_OK) {
- return CRYPT_MEM;
- }
- return copy(b, *a);
+ int err;
+ LTC_ARGCHK(a != NULL);
+ LTC_ARGCHK(b != NULL);
+ if ((err = init_mpi(a)) != CRYPT_OK) return err;
+ return mpi_to_ltc_error(mp_init_copy(*a, b));
}
/* ---- 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));
+ mp_set_u32(a, b);
+ return CRYPT_OK;
}
static unsigned long get_int(void *a)
{
LTC_ARGCHK(a != NULL);
- return mp_get_int(a);
+ return mp_get_ul(a);
}
static ltc_mp_digit get_digit(void *a, int n)
@@ -116,11 +131,9 @@ static int get_digit_count(void *a)
static int compare(void *a, void *b)
{
- int ret;
LTC_ARGCHK(a != NULL);
LTC_ARGCHK(b != NULL);
- ret = mp_cmp(a, b);
- switch (ret) {
+ switch (mp_cmp(a, b)) {
case MP_LT: return LTC_MP_LT;
case MP_EQ: return LTC_MP_EQ;
case MP_GT: return LTC_MP_GT;
@@ -130,10 +143,8 @@ static int compare(void *a, void *b)
static int compare_d(void *a, ltc_mp_digit b)
{
- int ret;
LTC_ARGCHK(a != NULL);
- ret = mp_cmp_d(a, b);
- switch (ret) {
+ switch (mp_cmp_d(a, b)) {
case MP_LT: return LTC_MP_LT;
case MP_EQ: return LTC_MP_EQ;
case MP_GT: return LTC_MP_GT;
@@ -175,14 +186,14 @@ 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));
+ return mpi_to_ltc_error(mp_to_radix(a, b, SIZE_MAX, NULL, radix));
}
/* get size as unsigned char string */
static unsigned long unsigned_size(void *a)
{
LTC_ARGCHK(a != NULL);
- return mp_unsigned_bin_size(a);
+ return (unsigned long)mp_ubin_size(a);
}
/* store */
@@ -190,7 +201,7 @@ 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));
+ return mpi_to_ltc_error(mp_to_ubin(a, b, SIZE_MAX, NULL));
}
/* read */
@@ -198,7 +209,7 @@ 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));
+ return mpi_to_ltc_error(mp_from_ubin(a, b, (size_t)len));
}
/* add */
@@ -403,9 +414,7 @@ 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 */
+ b = mp_prime_rabin_miller_trials(mp_count_bits(a));
err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c));
*c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO;
return err;
@@ -420,7 +429,7 @@ static int set_rand(void *a, int size)
const ltc_math_descriptor ltm_desc = {
"LibTomMath",
- (int)DIGIT_BIT,
+ (int)MP_DIGIT_BIT,
&init,
&init_copy,
diff --git a/libtomcrypt/src/math/multi.c b/libtomcrypt/src/math/multi.c
index da5bb60..cfe1451 100644
--- a/libtomcrypt/src/math/multi.c
+++ b/libtomcrypt/src/math/multi.c
@@ -67,7 +67,6 @@ void ltc_cleanup_multi(void **a, ...)
cur = va_arg(args, void**);
}
va_end(args);
- return;
}
#endif
diff --git a/libtomcrypt/src/math/rand_bn.c b/libtomcrypt/src/math/rand_bn.c
index a42ba64..aa6539c 100644
--- a/libtomcrypt/src/math/rand_bn.c
+++ b/libtomcrypt/src/math/rand_bn.c
@@ -8,7 +8,7 @@
*/
#include "tomcrypt.h"
-#ifdef LTC_MDSA
+#if defined(LTC_MDSA) || defined(LTC_MECC)
/**
Generate a random number N with given bitlength (note: MSB can be 0)
*/