diff libtomcrypt/src/math/tfm_desc.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children
line wrap: on
line diff
--- a/libtomcrypt/src/math/tfm_desc.c	Thu Feb 08 23:11:40 2018 +0800
+++ b/libtomcrypt/src/math/tfm_desc.c	Fri Feb 09 21:44:05 2018 +0800
@@ -5,8 +5,6 @@
  *
  * The library is free for all purposes without any express
  * guarantee it works.
- *
- * Tom St Denis, [email protected], http://libtom.org
  */
 
 #define DESC_DEF_ONLY
@@ -25,7 +23,7 @@
 };
 
 /**
-   Convert a tfm error to a LTC error (Possibly the most powerful function ever!  Oh wait... no) 
+   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
 */
@@ -34,7 +32,7 @@
    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) { 
+       if (err == tfm_to_ltc_codes[x].tfm_code) {
           return tfm_to_ltc_codes[x].ltc_code;
        }
    }
@@ -84,7 +82,7 @@
 }
 
 /* ---- trivial ---- */
-static int set_int(void *a, unsigned long b)
+static int set_int(void *a, ltc_mp_digit b)
 {
    LTC_ARGCHK(a != NULL);
    fp_set(a, b);
@@ -99,7 +97,7 @@
    return A->used > 0 ? A->dp[0] : 0;
 }
 
-static unsigned long get_digit(void *a, int n)
+static ltc_mp_digit get_digit(void *a, int n)
 {
    fp_int *A;
    LTC_ARGCHK(a != NULL);
@@ -114,7 +112,7 @@
    A = a;
    return A->used;
 }
-   
+
 static int compare(void *a, void *b)
 {
    int ret;
@@ -129,7 +127,7 @@
    return 0;
 }
 
-static int compare_d(void *a, unsigned long b)
+static int compare_d(void *a, ltc_mp_digit b)
 {
    int ret;
    LTC_ARGCHK(a != NULL);
@@ -213,8 +211,8 @@
    fp_add(a, b, c);
    return CRYPT_OK;
 }
-  
-static int addi(void *a, unsigned long b, void *c)
+
+static int addi(void *a, ltc_mp_digit b, void *c)
 {
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(c != NULL);
@@ -232,7 +230,7 @@
    return CRYPT_OK;
 }
 
-static int subi(void *a, unsigned long b, void *c)
+static int subi(void *a, ltc_mp_digit b, void *c)
 {
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(c != NULL);
@@ -246,11 +244,11 @@
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(b != NULL);
    LTC_ARGCHK(c != NULL);
-   fp_mul(a, b, c); 
+   fp_mul(a, b, c);
    return CRYPT_OK;
 }
 
-static int muli(void *a, unsigned long b, void *c)
+static int muli(void *a, ltc_mp_digit b, void *c)
 {
    LTC_ARGCHK(a != NULL);
    LTC_ARGCHK(c != NULL);
@@ -284,7 +282,7 @@
 }
 
 /* modi */
-static int modi(void *a, unsigned long b, unsigned long *c)
+static int modi(void *a, ltc_mp_digit b, ltc_mp_digit *c)
 {
    fp_digit tmp;
    int      err;
@@ -297,7 +295,7 @@
    }
    *c = tmp;
    return CRYPT_OK;
-}  
+}
 
 /* gcd */
 static int gcd(void *a, void *b, void *c)
@@ -319,6 +317,24 @@
    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);
@@ -393,13 +409,16 @@
    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)
+static int isprime(void *a, int b, int *c)
 {
    LTC_ARGCHK(a != NULL);
-   LTC_ARGCHK(b != NULL);
-   *b = (fp_isprime(a) == FP_YES) ? LTC_MP_YES : LTC_MP_NO;
+   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;
 }
 
@@ -437,7 +456,7 @@
    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) {
@@ -496,7 +515,7 @@
       fp_add(R->x, modulus, R->x);
    }
 
-   /* Y = Y - 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);
@@ -509,7 +528,7 @@
    if (fp_cmp_d(R->y, 0) == FP_LT) {
       fp_add(R->y, modulus, R->y);
    }
- 
+
    return CRYPT_OK;
 }
 
@@ -519,14 +538,14 @@
    @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()
+   @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;  
-   
+   fp_digit mp;
+
    LTC_ARGCHK(P       != NULL);
    LTC_ARGCHK(Q       != NULL);
    LTC_ARGCHK(R       != NULL);
@@ -543,7 +562,7 @@
 
    /* should we dbl instead? */
    fp_sub(modulus, Q->y, &t1);
-   if ( (fp_cmp(P->x, Q->x) == FP_EQ) && 
+   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);
@@ -636,7 +655,7 @@
    /* 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);
@@ -650,7 +669,7 @@
    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) {
@@ -673,13 +692,20 @@
    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",
@@ -764,14 +790,18 @@
    &rsa_make_key,
    &rsa_exptmod,
 #else
-   NULL, NULL
+   NULL, NULL,
 #endif
-   
+   &addmod,
+   &submod,
+
+   set_rand,
+
 };
 
 
 #endif
 
-/* $Source$ */
-/* $Revision$ */
-/* $Date$ */
+/* ref:         $Format:%D$ */
+/* git commit:  $Format:%H$ */
+/* commit time: $Format:%ai$ */