Browse Source

fix changed ltm API

Steffen Jaeckel 6 years ago
parent
commit
a3310959d0
1 changed files with 22 additions and 12 deletions
  1. 22 12
      src/math/ltm_desc.c

+ 22 - 12
src/math/ltm_desc.c

@@ -15,13 +15,15 @@
 #include <tommath.h>
 #include <tommath.h>
 
 
 static const struct {
 static const struct {
-    int mpi_code, ltc_code;
+    mp_err mpi_code;
+    int ltc_code;
 } mpi_to_ltc_codes[] = {
 } mpi_to_ltc_codes[] = {
    { MP_OKAY ,  CRYPT_OK},
    { MP_OKAY ,  CRYPT_OK},
    { MP_MEM  ,  CRYPT_MEM},
    { MP_MEM  ,  CRYPT_MEM},
    { MP_VAL  ,  CRYPT_INVALID_ARG},
    { MP_VAL  ,  CRYPT_INVALID_ARG},
-#if defined(MP_ITER) || defined(MP_USE_ENUMS)
+#if defined(MP_BUF) || defined(MP_USE_ENUMS)
    { MP_ITER ,  CRYPT_INVALID_PACKET},
    { MP_ITER ,  CRYPT_INVALID_PACKET},
+   { MP_BUF  ,  CRYPT_BUFFER_OVERFLOW},
 #endif
 #endif
 };
 };
 
 
@@ -30,11 +32,11 @@ static const struct {
    @param err    The error to convert
    @param err    The error to convert
    @return The equivalent LTC error code or CRYPT_ERROR if none found
    @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) {
        if (err == mpi_to_ltc_codes[x].mpi_code) {
           return mpi_to_ltc_codes[x].ltc_code;
           return mpi_to_ltc_codes[x].ltc_code;
        }
        }
@@ -139,11 +141,9 @@ static int get_digit_count(void *a)
 
 
 static int compare(void *a, void *b)
 static int compare(void *a, void *b)
 {
 {
-   int ret;
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(b != 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_LT: return LTC_MP_LT;
       case MP_EQ: return LTC_MP_EQ;
       case MP_EQ: return LTC_MP_EQ;
       case MP_GT: return LTC_MP_GT;
       case MP_GT: return LTC_MP_GT;
@@ -153,10 +153,8 @@ static int compare(void *a, void *b)
 
 
 static int compare_d(void *a, ltc_mp_digit b)
 static int compare_d(void *a, ltc_mp_digit b)
 {
 {
-   int ret;
    LTC_ARGCHK(a != NULL);
    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_LT: return LTC_MP_LT;
       case MP_EQ: return LTC_MP_EQ;
       case MP_EQ: return LTC_MP_EQ;
       case MP_GT: return LTC_MP_GT;
       case MP_GT: return LTC_MP_GT;
@@ -201,7 +199,7 @@ static int write_radix(void *a, char *b, int radix)
 #ifdef BN_MP_TORADIX_C
 #ifdef BN_MP_TORADIX_C
    return mpi_to_ltc_error(mp_toradix(a, b, radix));
    return mpi_to_ltc_error(mp_toradix(a, b, radix));
 #else
 #else
-   return mpi_to_ltc_error(mp_to_radix(a, b, SIZE_MAX, radix));
+   return mpi_to_ltc_error(mp_to_radix(a, b, SIZE_MAX, NULL, radix));
 #endif
 #endif
 }
 }
 
 
@@ -209,7 +207,11 @@ static int write_radix(void *a, char *b, int radix)
 static unsigned long unsigned_size(void *a)
 static unsigned long unsigned_size(void *a)
 {
 {
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(a != NULL);
+#ifdef BN_MP_UNSIGNED_BIN_SIZE_C
    return mp_unsigned_bin_size(a);
    return mp_unsigned_bin_size(a);
+#else
+   return (unsigned long)mp_ubin_size(a);
+#endif
 }
 }
 
 
 /* store */
 /* store */
@@ -217,7 +219,11 @@ static int unsigned_write(void *a, unsigned char *b)
 {
 {
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(b != NULL);
    LTC_ARGCHK(b != NULL);
+#ifdef BN_MP_TO_UNSIGNED_BIN_C
    return mpi_to_ltc_error(mp_to_unsigned_bin(a, b));
    return mpi_to_ltc_error(mp_to_unsigned_bin(a, b));
+#else
+   return mpi_to_ltc_error(mp_to_ubin(a, b, SIZE_MAX, NULL));
+#endif
 }
 }
 
 
 /* read */
 /* read */
@@ -225,7 +231,11 @@ static int unsigned_read(void *a, unsigned char *b, unsigned long len)
 {
 {
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(b != NULL);
    LTC_ARGCHK(b != NULL);
+#ifdef BN_MP_READ_UNSIGNED_BIN_C
    return mpi_to_ltc_error(mp_read_unsigned_bin(a, b, len));
    return mpi_to_ltc_error(mp_read_unsigned_bin(a, b, len));
+#else
+   return mpi_to_ltc_error(mp_from_ubin(a, b, (size_t)len));
+#endif
 }
 }
 
 
 /* add */
 /* add */