Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/crypto/external/bsd/netpgp/dist/src/netpgpverify Update the ...



details:   https://anonhg.NetBSD.org/src/rev/e36adf1c061a
branches:  trunk
changeset: 797267:e36adf1c061a
user:      agc <agc%NetBSD.org@localhost>
date:      Sat Jul 12 15:51:56 2014 +0000

description:
Update the bignum implementation in netpgpverify, and sync all uses of it

        + radix conversion routines added
        + bitwise operations added
        + whitespace cleanups

diffstat:

 crypto/external/bsd/netpgp/dist/src/netpgpverify/bignum.c |  8811 ++++++------
 crypto/external/bsd/netpgp/dist/src/netpgpverify/bn.h     |     6 +
 2 files changed, 4430 insertions(+), 4387 deletions(-)

diffs (truncated from 9596 to 300 lines):

diff -r 6d2d5277682a -r e36adf1c061a crypto/external/bsd/netpgp/dist/src/netpgpverify/bignum.c
--- a/crypto/external/bsd/netpgp/dist/src/netpgpverify/bignum.c Sat Jul 12 15:42:56 2014 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/netpgpverify/bignum.c Sat Jul 12 15:51:56 2014 +0000
@@ -39,26 +39,20 @@
 #include "config.h"
 
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/param.h>
 
 #ifdef _KERNEL
 # include <sys/kmem.h>
 #else
 # include <arpa/inet.h>
-# include <ctype.h>
-# include <inttypes.h>
 # include <stdarg.h>
 # include <stdio.h>
 # include <stdlib.h>
 # include <string.h>
-# include <time.h>
 # include <unistd.h>
 #endif
 
-#include "misc.h"
 #include "bn.h"
-#include "digest.h"
 
 /**************************************************************************/
 
@@ -94,116 +88,108 @@
 #define        __arraycount(__x)       (sizeof(__x) / sizeof(__x[0]))
 #endif
 
-#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
-
-#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
+#define MP_ISZERO(a) (((a)->used == 0) ? MP_YES : MP_NO)
 
 typedef int           mp_err;
 
-static int mp_mul(mp_int * a, mp_int * b, mp_int * c);
-static int mp_sqr(mp_int * a, mp_int * b);
-
-static int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
+static int signed_multiply(mp_int * a, mp_int * b, mp_int * c);
+static int square(mp_int * a, mp_int * b);
+
+static int signed_subtract_word(mp_int *a, mp_digit b, mp_int *c);
+
+static inline void *
+allocate(size_t n, size_t m)
+{
+       return calloc(n, m);
+}
+
+static inline void
+deallocate(void *v, size_t sz)
+{
+       USE_ARG(sz);
+       free(v);
+}
 
 /* set to zero */
-static void
+static inline void
 mp_zero(mp_int *a)
 {
-  int       n;
-  mp_digit *tmp;
-
-  a->sign = MP_ZPOS;
-  a->used = 0;
-
-  tmp = a->dp;
-  /* XXX - memset */
-  for (n = 0; n < a->alloc; n++) {
-     *tmp++ = 0;
-  }
+       a->sign = MP_ZPOS;
+       a->used = 0;
+       memset(a->dp, 0x0, a->alloc * sizeof(*a->dp));
 }
 
 /* grow as required */
 static int
 mp_grow(mp_int *a, int size)
 {
-  int     i;
-  mp_digit *tmp;
-
-  /* if the alloc size is smaller alloc more ram */
-  if (a->alloc < size) {
-    /* ensure there are always at least MP_PREC digits extra on top */
-    size += (MP_PREC * 2) - (size % MP_PREC);
-
-    /* reallocate the array a->dp
-     *
-     * We store the return in a temporary variable
-     * in case the operation failed we don't want
-     * to overwrite the dp member of a.
-     */
-    tmp = realloc(a->dp, sizeof(*tmp) * size);
-    if (tmp == NULL) {
-      /* reallocation failed but "a" is still valid [can be freed] */
-      return MP_MEM;
-    }
-
-    /* reallocation succeeded so set a->dp */
-    a->dp = tmp;
-
-    /* zero excess digits */
-    i        = a->alloc;
-    a->alloc = size;
-    for (; i < a->alloc; i++) {
-      a->dp[i] = 0;
-    }
-  }
-  return MP_OKAY;
+       mp_digit *tmp;
+
+       /* if the alloc size is smaller alloc more ram */
+       if (a->alloc < size) {
+               /* ensure there are always at least MP_PREC digits extra on top */
+               size += (MP_PREC * 2) - (size % MP_PREC);
+
+               /* reallocate the array a->dp
+               *
+               * We store the return in a temporary variable
+               * in case the operation failed we don't want
+               * to overwrite the dp member of a.
+               */
+               tmp = realloc(a->dp, sizeof(*tmp) * size);
+               if (tmp == NULL) {
+                       /* reallocation failed but "a" is still valid [can be freed] */
+                       return MP_MEM;
+               }
+
+               /* reallocation succeeded so set a->dp */
+               a->dp = tmp;
+               /* zero excess digits */
+               memset(&a->dp[a->alloc], 0x0, (size - a->alloc) * sizeof(*a->dp));
+               a->alloc = size;
+       }
+       return MP_OKAY;
 }
 
 /* shift left a certain amount of digits */
 static int
-mp_lshd (mp_int * a, int b)
+lshift_digits(mp_int * a, int b)
 {
-  int     x, res;
-
-  /* if its less than zero return */
-  if (b <= 0) {
-    return MP_OKAY;
-  }
-
-  /* grow to fit the new digits */
-  if (a->alloc < a->used + b) {
-     if ((res = mp_grow (a, a->used + b)) != MP_OKAY) {
-       return res;
-     }
-  }
-
-  {
-    mp_digit *top, *bottom;
-
-    /* increment the used by the shift amount then copy upwards */
-    a->used += b;
-
-    /* top */
-    top = a->dp + a->used - 1;
-
-    /* base */
-    bottom = a->dp + a->used - 1 - b;
-
-    /* much like mp_rshd this is implemented using a sliding window
-     * except the window goes the otherway around.  Copying from
-     * the bottom to the top.  see bn_mp_rshd.c for more info.
-     */
-    for (x = a->used - 1; x >= b; x--) {
-      *top-- = *bottom--;
-    }
-
-    /* zero the lower digits */
-    top = a->dp;
-    for (x = 0; x < b; x++) {
-      *top++ = 0;
-    }
-  }
-  return MP_OKAY;
+       mp_digit *top, *bottom;
+       int     x, res;
+
+       /* if its less than zero return */
+       if (b <= 0) {
+               return MP_OKAY;
+       }
+
+       /* grow to fit the new digits */
+       if (a->alloc < a->used + b) {
+               if ((res = mp_grow(a, a->used + b)) != MP_OKAY) {
+                       return res;
+               }
+       }
+
+       /* increment the used by the shift amount then copy upwards */
+       a->used += b;
+
+       /* top */
+       top = a->dp + a->used - 1;
+
+       /* base */
+       bottom = a->dp + a->used - 1 - b;
+
+       /* much like rshift_digits this is implemented using a sliding window
+       * except the window goes the otherway around.  Copying from
+       * the bottom to the top.
+       */
+       for (x = a->used - 1; x >= b; x--) {
+               *top-- = *bottom--;
+       }
+
+       /* zero the lower digits */
+       memset(a->dp, 0x0, b * sizeof(*a->dp));
+       return MP_OKAY;
 }
 
 /* trim unused digits 
@@ -214,710 +200,646 @@
  * are no more leading digits
  */
 static void
-mp_clamp (mp_int * a)
+trim_unused_digits(mp_int * a)
 {
-  /* decrease used while the most significant digit is
-   * zero.
-   */
-  while (a->used > 0 && a->dp[a->used - 1] == 0) {
-    --(a->used);
-  }
-
-  /* reset the sign flag if used == 0 */
-  if (a->used == 0) {
-    a->sign = MP_ZPOS;
-  }
+       /* decrease used while the most significant digit is
+       * zero.
+       */
+       while (a->used > 0 && a->dp[a->used - 1] == 0) {
+               a->used -= 1;
+       }
+       /* reset the sign flag if used == 0 */
+       if (a->used == 0) {
+               a->sign = MP_ZPOS;
+       }
 }
 
 /* copy, b = a */
 static int
 mp_copy(BIGNUM *a, BIGNUM *b)
 {
-  int     res, n;
-
-  /* if dst == src do nothing */
-  if (a == b) {
-    return MP_OKAY;
-  }
-  if (a == NULL || b == NULL) {
-       return MP_VAL;
-  }
-
-  /* grow dest */
-  if (b->alloc < a->used) {
-     if ((res = mp_grow (b, a->used)) != MP_OKAY) {
-        return res;
-     }
-  }
-
-  /* zero b and copy the parameters over */
-  {
-    mp_digit *tmpa, *tmpb;
-
-    /* pointer aliases */
-
-    /* source */
-    tmpa = a->dp;
-
-    /* destination */
-    tmpb = b->dp;
-
-    /* copy all the digits */
-    for (n = 0; n < a->used; n++) {
-      *tmpb++ = *tmpa++;
-    }
-
-    /* clear high digits */



Home | Main Index | Thread Index | Old Index