Print this page
6799218 RSA using Solaris Kernel Crypto framework lagging behind OpenSSL
5016936 bignumimpl:big_mul: potential memory leak
6810280 panic from bignum module: vmem_xalloc(): size == 0

Split Close
Expand all
Collapse all
          --- old/usr/src/common/bignum/bignumimpl.c
          +++ new/usr/src/common/bignum/bignumimpl.c
↓ open down ↓ 11 lines elided ↑ open up ↑
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22      - * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
       22 + * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
  25   25  
  26      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  27      -
  28      -#define big_div_pos_fast big_div_pos
  29      -
  30      -#include "bignum.h"
  31      -
  32   26  /*
  33   27   * Configuration guide
  34   28   * -------------------
  35   29   *
  36   30   * There are 4 preprocessor symbols used to configure the bignum
  37   31   * implementation.  This file contains no logic to configure based on
  38   32   * processor; we leave that to the Makefiles to specify.
  39   33   *
  40   34   * USE_FLOATING_POINT
  41   35   *   Meaning: There is support for a fast floating-point implementation of
↓ open down ↓ 1 lines elided ↑ open up ↑
  43   37   *
  44   38   * PSR_MUL
  45   39   *   Meaning: There are processor-specific versions of the low level
  46   40   *   functions to implement big_mul.  Those functions are: big_mul_set_vec,
  47   41   *   big_mul_add_vec, big_mul_vec, and big_sqr_vec.  PSR_MUL implies support
  48   42   *   for all 4 functions.  You cannot pick and choose which subset of these
  49   43   *   functions to support; that would lead to a rat's nest of #ifdefs.
  50   44   *
  51   45   * HWCAP
  52   46   *   Meaning: Call multiply support functions through a function pointer.
  53      - *   On x86, there are multiple implementations for differnt hardware
       47 + *   On x86, there are multiple implementations for different hardware
  54   48   *   capabilities, such as MMX, SSE2, etc.  Tests are made at run-time, when
  55   49   *   a function is first used.  So, the support functions are called through
  56   50   *   a function pointer.  There is no need for that on Sparc, because there
  57   51   *   is only one implementation; support functions are called directly.
  58   52   *   Later, if there were some new VIS instruction, or something, and a
  59   53   *   run-time test were needed, rather than variant kernel modules and
  60   54   *   libraries, then HWCAP would be defined for Sparc, as well.
  61   55   *
  62   56   * UMUL64
  63   57   *   Meaning: It is safe to use generic C code that assumes the existence
  64   58   *   of a 32 x 32 --> 64 bit unsigned multiply.  If this is not defined,
  65   59   *   then the generic code for big_mul_add_vec() must necessarily be very slow,
  66   60   *   because it must fall back to using 16 x 16 --> 32 bit multiplication.
  67   61   *
  68   62   */
  69   63  
  70   64  
       65 +#include <sys/types.h>
       66 +#include "bignum.h"
       67 +
  71   68  #ifdef  _KERNEL
  72   69  #include <sys/ddi.h>
  73   70  #include <sys/mdesc.h>
  74   71  #include <sys/crypto/common.h>
  75   72  
  76      -#include <sys/types.h>
  77   73  #include <sys/kmem.h>
  78   74  #include <sys/param.h>
  79   75  #include <sys/sunddi.h>
  80   76  
       77 +#else
       78 +#include <stdlib.h>
       79 +#include <stdio.h>
       80 +#include <assert.h>
       81 +#define ASSERT  assert
       82 +#endif  /* _KERNEL */
       83 +
       84 +#ifdef  _LP64 /* truncate 64-bit size_t to 32-bits */
       85 +#define UI32(ui)        ((uint32_t)ui)
       86 +#else /* size_t already 32-bits */
       87 +#define UI32(ui)        (ui)
       88 +#endif
       89 +
       90 +
       91 +#ifdef  _KERNEL
  81   92  #define big_malloc(size)        kmem_alloc(size, KM_NOSLEEP)
  82   93  #define big_free(ptr, size)     kmem_free(ptr, size)
  83   94  
  84   95  void *
  85   96  big_realloc(void *from, size_t oldsize, size_t newsize)
  86   97  {
  87   98          void *rv;
  88   99  
  89  100          rv = kmem_alloc(newsize, KM_NOSLEEP);
  90  101          if (rv != NULL)
  91  102                  bcopy(from, rv, oldsize);
  92  103          kmem_free(from, oldsize);
  93  104          return (rv);
  94  105  }
  95  106  
  96  107  #else   /* _KERNEL */
  97  108  
  98      -#include <stdlib.h>
  99      -#include <stdio.h>
 100      -#include <assert.h>
 101      -#define ASSERT  assert
 102      -
 103  109  #ifndef MALLOC_DEBUG
 104  110  
 105  111  #define big_malloc(size)        malloc(size)
 106  112  #define big_free(ptr, size)     free(ptr)
 107  113  
 108  114  #else
 109  115  
 110  116  void
 111  117  big_free(void *ptr, size_t size)
 112  118  {
↓ open down ↓ 6 lines elided ↑ open up ↑
 119  125  {
 120  126          void *rv;
 121  127          rv = malloc(size);
 122  128          printf("malloced %d bytes, addr:%p\n", size, rv);
 123  129          return (rv);
 124  130  }
 125  131  #endif /* MALLOC_DEBUG */
 126  132  
 127  133  #define big_realloc(x, y, z) realloc((x), (z))
 128  134  
      135 +
      136 +/*
      137 + * printbignum()
      138 + * Print a BIGNUM type to stdout.
      139 + */
 129  140  void
 130  141  printbignum(char *aname, BIGNUM *a)
 131  142  {
 132  143          int i;
 133  144  
 134  145          (void) printf("\n%s\n%d\n", aname, a->sign*a->len);
 135  146          for (i = a->len - 1; i >= 0; i--) {
 136  147  #ifdef BIGNUM_CHUNK_32
 137  148                  (void) printf("%08x ", a->value[i]);
 138      -                if ((i % 8 == 0) && (i != 0)) {
      149 +                if (((i & (BITSINBYTE - 1)) == 0) && (i != 0)) {
 139  150                          (void) printf("\n");
 140  151                  }
 141  152  #else
 142  153                  (void) printf("%08x %08x ", (uint32_t)((a->value[i]) >> 32),
 143  154                      (uint32_t)((a->value[i]) & 0xffffffff));
 144      -                if ((i % 4 == 0) && (i != 0)) {
      155 +                if (((i & 3) == 0) && (i != 0)) { /* end of this chunk */
 145  156                          (void) printf("\n");
 146  157                  }
 147  158  #endif
 148  159          }
 149  160          (void) printf("\n");
 150  161  }
 151  162  
 152  163  #endif  /* _KERNEL */
 153  164  
 154  165  
 155      -/* size in BIG_CHUNK_SIZE-bit words */
      166 +/*
      167 + * big_init()
      168 + * Initialize and allocate memory for a BIGNUM type.
      169 + *
      170 + * big_init(number, size) is equivalent to big_init1(number, size, NULL, 0)
      171 + *
      172 + * Note: call big_finish() to free memory allocated by big_init().
      173 + *
      174 + * Input:
      175 + * number       Uninitialized memory for BIGNUM
      176 + * size         Minimum size, in BIG_CHUNK_SIZE-bit words, required for BIGNUM
      177 + *
      178 + * Output:
      179 + * number       Initialized BIGNUM
      180 + *
      181 + * Return BIG_OK on success or BIG_NO_MEM for an allocation error.
      182 + */
 156  183  BIG_ERR_CODE
 157  184  big_init(BIGNUM *number, int size)
 158  185  {
 159      -        number->value = big_malloc(sizeof (BIG_CHUNK_TYPE) * size);
      186 +        number->value = big_malloc(BIGNUM_WORDSIZE * size);
 160  187          if (number->value == NULL) {
 161  188                  return (BIG_NO_MEM);
 162  189          }
 163  190          number->size = size;
 164  191          number->len = 0;
 165  192          number->sign = 1;
 166  193          number->malloced = 1;
 167  194          return (BIG_OK);
 168  195  }
 169  196  
 170      -/* size in BIG_CHUNK_SIZE-bit words */
      197 +
      198 +/*
      199 + * big_init1()
      200 + * Initialize and, if needed, allocate memory for a BIGNUM type.
      201 + * Use the buffer passed, buf, if any, instad of allocating memory
      202 + * if it's at least "size" bytes.
      203 + *
      204 + * Note: call big_finish() to free memory allocated by big_init().
      205 + *
      206 + * Input:
      207 + * number       Uninitialized memory for BIGNUM
      208 + * size         Minimum size, in BIG_CHUNK_SIZE-bit words, required for BIGNUM
      209 + * buf          Buffer for storing a BIGNUM.
      210 + *              If NULL, big_init1() will allocate a buffer
      211 + * bufsize      Size, in BIG_CHUNK_SIZE_bit words, of buf
      212 + *
      213 + * Output:
      214 + * number       Initialized BIGNUM
      215 + *
      216 + * Return BIG_OK on success or BIG_NO_MEM for an allocation error.
      217 + */
 171  218  BIG_ERR_CODE
 172  219  big_init1(BIGNUM *number, int size, BIG_CHUNK_TYPE *buf, int bufsize)
 173  220  {
 174  221          if ((buf == NULL) || (size > bufsize)) {
 175      -                number->value = big_malloc(sizeof (BIG_CHUNK_TYPE) * size);
      222 +                number->value = big_malloc(BIGNUM_WORDSIZE * size);
 176  223                  if (number->value == NULL) {
 177  224                          return (BIG_NO_MEM);
 178  225                  }
 179  226                  number->size = size;
 180  227                  number->malloced = 1;
 181  228          } else {
 182  229                  number->value = buf;
 183  230                  number->size = bufsize;
 184  231                  number->malloced = 0;
 185  232          }
 186      -                number->len = 0;
 187      -                number->sign = 1;
      233 +        number->len = 0;
      234 +        number->sign = 1;
 188  235  
 189  236          return (BIG_OK);
 190  237  }
 191  238  
      239 +
      240 +/*
      241 + * big_finish()
      242 + * Free memory, if any, allocated by big_init() or big_init1().
      243 + */
 192  244  void
 193  245  big_finish(BIGNUM *number)
 194  246  {
 195  247          if (number->malloced == 1) {
 196      -                big_free(number->value,
 197      -                    sizeof (BIG_CHUNK_TYPE) * number->size);
      248 +                big_free(number->value, BIGNUM_WORDSIZE * number->size);
 198  249                  number->malloced = 0;
 199  250          }
 200  251  }
 201  252  
 202  253  
 203  254  /*
 204      - *  bn->size should be at least
 205      - * (len + sizeof (BIG_CHUNK_TYPE) - 1) / sizeof (BIG_CHUNK_TYPE) bytes
      255 + * bn->size should be at least
      256 + * (len + BIGNUM_WORDSIZE - 1) / BIGNUM_WORDSIZE bytes
 206  257   * converts from byte-big-endian format to bignum format (words in
 207  258   * little endian order, but bytes within the words big endian)
 208  259   */
 209  260  void
 210  261  bytestring2bignum(BIGNUM *bn, uchar_t *kn, size_t len)
 211  262  {
 212      -        int             i, j, offs;
      263 +        int             i, j;
      264 +        uint32_t        offs;
      265 +        const uint32_t  slen = UI32(len);
 213  266          BIG_CHUNK_TYPE  word;
 214  267          uchar_t         *knwordp;
 215  268  
 216      -#ifdef  _LP64
 217      -        offs = (uint32_t)len % sizeof (BIG_CHUNK_TYPE);
 218      -        bn->len = (uint32_t)len / sizeof (BIG_CHUNK_TYPE);
      269 +        if (slen == 0) {
      270 +                bn->len = 1;
      271 +                bn->value[0] = 0;
      272 +                return;
      273 +        }
 219  274  
 220      -        for (i = 0; i < (uint32_t)len / sizeof (BIG_CHUNK_TYPE); i++) {
 221      -#else   /* !_LP64 */
 222      -        offs = len % sizeof (BIG_CHUNK_TYPE);
 223      -        bn->len = len / sizeof (BIG_CHUNK_TYPE);
 224      -        for (i = 0; i < len / sizeof (BIG_CHUNK_TYPE); i++) {
 225      -#endif  /* _LP64 */
 226      -                knwordp = &(kn[len - sizeof (BIG_CHUNK_TYPE) * (i + 1)]);
      275 +        offs = slen % BIGNUM_WORDSIZE;
      276 +        bn->len = slen / BIGNUM_WORDSIZE;
      277 +
      278 +        for (i = 0; i < slen / BIGNUM_WORDSIZE; i++) {
      279 +                knwordp = &(kn[slen - BIGNUM_WORDSIZE * (i + 1)]);
 227  280                  word = knwordp[0];
 228      -                for (j = 1; j < sizeof (BIG_CHUNK_TYPE); j++) {
 229      -                        word = (word << 8)+ knwordp[j];
      281 +                for (j = 1; j < BIGNUM_WORDSIZE; j++) {
      282 +                        word = (word << BITSINBYTE) + knwordp[j];
 230  283                  }
 231  284                  bn->value[i] = word;
 232  285          }
 233  286          if (offs > 0) {
 234  287                  word = kn[0];
 235      -                for (i = 1; i < offs; i++) word = (word << 8) + kn[i];
      288 +                for (i = 1; i < offs; i++) word = (word << BITSINBYTE) + kn[i];
 236  289                  bn->value[bn->len++] = word;
 237  290          }
 238      -        while ((bn->len > 1) && (bn->value[bn->len-1] == 0)) {
      291 +        while ((bn->len > 1) && (bn->value[bn->len - 1] == 0)) {
 239  292                  bn->len --;
 240  293          }
 241  294  }
 242  295  
      296 +
 243  297  /*
 244  298   * copies the least significant len bytes if
 245      - * len < bn->len * sizeof (BIG_CHUNK_TYPE)
      299 + * len < bn->len * BIGNUM_WORDSIZE
 246  300   * converts from bignum format to byte-big-endian format.
 247  301   * bignum format is words of type  BIG_CHUNK_TYPE in little endian order.
 248  302   */
 249  303  void
 250  304  bignum2bytestring(uchar_t *kn, BIGNUM *bn, size_t len)
 251  305  {
 252      -        int             i, j, offs;
      306 +        int             i, j;
      307 +        uint32_t        offs;
      308 +        const uint32_t  slen = UI32(len);
 253  309          BIG_CHUNK_TYPE  word;
 254  310  
 255      -        if (len < sizeof (BIG_CHUNK_TYPE) * bn->len) {
 256      -#ifdef  _LP64
 257      -                for (i = 0; i < (uint32_t)len / sizeof (BIG_CHUNK_TYPE); i++) {
 258      -#else   /* !_LP64 */
 259      -                for (i = 0; i < len / sizeof (BIG_CHUNK_TYPE); i++) {
 260      -#endif  /* _LP64 */
      311 +        if (len < BIGNUM_WORDSIZE * bn->len) {
      312 +                for (i = 0; i < slen / BIGNUM_WORDSIZE; i++) {
 261  313                          word = bn->value[i];
 262      -                        for (j = 0; j < sizeof (BIG_CHUNK_TYPE); j++) {
 263      -                                kn[len - sizeof (BIG_CHUNK_TYPE) * i - j - 1] =
      314 +                        for (j = 0; j < BIGNUM_WORDSIZE; j++) {
      315 +                                kn[slen - BIGNUM_WORDSIZE * i - j - 1] =
 264  316                                      word & 0xff;
 265      -                                word = word >> 8;
      317 +                                word = word >> BITSINBYTE;
 266  318                          }
 267  319                  }
 268      -#ifdef  _LP64
 269      -                offs = (uint32_t)len % sizeof (BIG_CHUNK_TYPE);
 270      -#else   /* !_LP64 */
 271      -                offs = len % sizeof (BIG_CHUNK_TYPE);
 272      -#endif  /* _LP64 */
      320 +                offs = slen % BIGNUM_WORDSIZE;
 273  321                  if (offs > 0) {
 274      -                        word = bn->value[len / sizeof (BIG_CHUNK_TYPE)];
 275      -#ifdef  _LP64
 276      -                        for (i =  (uint32_t)len % sizeof (BIG_CHUNK_TYPE);
 277      -                            i > 0; i --) {
 278      -#else   /* !_LP64 */
 279      -                        for (i = len % sizeof (BIG_CHUNK_TYPE);
 280      -                            i > 0; i --) {
 281      -#endif  /* _LP64 */
      322 +                        word = bn->value[slen / BIGNUM_WORDSIZE];
      323 +                        for (i =  slen % BIGNUM_WORDSIZE; i > 0; i --) {
 282  324                                  kn[i - 1] = word & 0xff;
 283      -                                word = word >> 8;
      325 +                                word = word >> BITSINBYTE;
 284  326                          }
 285  327                  }
 286  328          } else {
 287  329                  for (i = 0; i < bn->len; i++) {
 288  330                          word = bn->value[i];
 289      -                        for (j = 0; j < sizeof (BIG_CHUNK_TYPE); j++) {
 290      -                                kn[len - sizeof (BIG_CHUNK_TYPE) * i - j - 1] =
      331 +                        for (j = 0; j < BIGNUM_WORDSIZE; j++) {
      332 +                                kn[slen - BIGNUM_WORDSIZE * i - j - 1] =
 291  333                                      word & 0xff;
 292      -                                word = word >> 8;
      334 +                                word = word >> BITSINBYTE;
 293  335                          }
 294  336                  }
 295      -#ifdef  _LP64
 296      -                for (i = 0;
 297      -                    i < (uint32_t)len - sizeof (BIG_CHUNK_TYPE) * bn->len;
 298      -                    i++) {
 299      -#else   /* !_LP64 */
 300      -                for (i = 0; i < len - sizeof (BIG_CHUNK_TYPE) * bn->len; i++) {
 301      -#endif  /* _LP64 */
      337 +                for (i = 0; i < slen - BIGNUM_WORDSIZE * bn->len; i++) {
 302  338                          kn[i] = 0;
 303  339                  }
 304  340          }
 305  341  }
 306  342  
 307  343  
 308  344  int
 309  345  big_bitlength(BIGNUM *a)
 310  346  {
 311  347          int             l = 0, b = 0;
 312  348          BIG_CHUNK_TYPE  c;
 313  349  
 314  350          l = a->len - 1;
 315  351          while ((l > 0) && (a->value[l] == 0)) {
 316  352                  l--;
 317  353          }
 318      -        b = sizeof (BIG_CHUNK_TYPE) * BITSINBYTE;
      354 +        b = BIG_CHUNK_SIZE;
 319  355          c = a->value[l];
 320  356          while ((b > 1) && ((c & BIG_CHUNK_HIGHBIT) == 0)) {
 321  357                  c = c << 1;
 322  358                  b--;
 323  359          }
 324  360  
 325      -        return (l * sizeof (BIG_CHUNK_TYPE) * BITSINBYTE + b);
      361 +        return (l * BIG_CHUNK_SIZE + b);
 326  362  }
 327  363  
 328  364  
 329  365  BIG_ERR_CODE
 330  366  big_copy(BIGNUM *dest, BIGNUM *src)
 331  367  {
 332  368          BIG_CHUNK_TYPE  *newptr;
 333  369          int             i, len;
 334  370  
 335  371          len = src->len;
 336  372          while ((len > 1) && (src->value[len - 1] == 0)) {
 337  373                  len--;
 338  374          }
 339  375          src->len = len;
 340  376          if (dest->size < len) {
 341  377                  if (dest->malloced == 1) {
 342  378                          newptr = (BIG_CHUNK_TYPE *)big_realloc(dest->value,
 343      -                            sizeof (BIG_CHUNK_TYPE) * dest->size,
 344      -                            sizeof (BIG_CHUNK_TYPE) * len);
      379 +                            BIGNUM_WORDSIZE * dest->size,
      380 +                            BIGNUM_WORDSIZE * len);
 345  381                  } else {
 346  382                          newptr = (BIG_CHUNK_TYPE *)
 347      -                            big_malloc(sizeof (BIG_CHUNK_TYPE) * len);
      383 +                            big_malloc(BIGNUM_WORDSIZE * len);
 348  384                          if (newptr != NULL) {
 349  385                                  dest->malloced = 1;
 350  386                          }
 351  387                  }
 352  388                  if (newptr == NULL) {
 353  389                          return (BIG_NO_MEM);
 354  390                  }
 355  391                  dest->value = newptr;
 356  392                  dest->size = len;
 357  393          }
↓ open down ↓ 10 lines elided ↑ open up ↑
 368  404  BIG_ERR_CODE
 369  405  big_extend(BIGNUM *number, int size)
 370  406  {
 371  407          BIG_CHUNK_TYPE  *newptr;
 372  408          int             i;
 373  409  
 374  410          if (number->size >= size)
 375  411                  return (BIG_OK);
 376  412          if (number->malloced) {
 377  413                  number->value = big_realloc(number->value,
 378      -                    sizeof (BIG_CHUNK_TYPE) * number->size,
 379      -                    sizeof (BIG_CHUNK_TYPE) * size);
      414 +                    BIGNUM_WORDSIZE * number->size,
      415 +                    BIGNUM_WORDSIZE * size);
 380  416          } else {
 381      -                newptr = big_malloc(sizeof (BIG_CHUNK_TYPE) * size);
      417 +                newptr = big_malloc(BIGNUM_WORDSIZE * size);
 382  418                  if (newptr != NULL) {
 383  419                          for (i = 0; i < number->size; i++) {
 384  420                                  newptr[i] = number->value[i];
 385  421                          }
 386  422                  }
 387  423                  number->value = newptr;
 388  424          }
 389  425  
 390  426          if (number->value == NULL) {
 391  427                  return (BIG_NO_MEM);
↓ open down ↓ 162 lines elided ↑ open up ↑
 554  590                                  return (1);
 555  591                          }
 556  592                  }
 557  593          } else if (aa->len < bb->len) {
 558  594                  for (i = bb->len - 1; i > aa->len - 1; i--) {
 559  595                          if (bb->value[i] > 0) {
 560  596                                  return (-1);
 561  597                          }
 562  598                  }
 563  599          } else {
 564      -                i = aa->len-1;
      600 +                i = aa->len - 1;
 565  601          }
 566  602          for (; i >= 0; i--) {
 567  603                  if (aa->value[i] > bb->value[i]) {
 568  604                          return (1);
 569  605                  } else if (aa->value[i] < bb->value[i]) {
 570  606                          return (-1);
 571  607                  }
 572  608          }
 573  609  
 574  610          return (0);
↓ open down ↓ 213 lines elided ↑ open up ↑
 788  824  
 789  825  
 790  826  /*
 791  827   * returns 1, 0, or -1 depending on whether |aa| > , ==, or <
 792  828   *                                      (2^BIG_CHUNK_SIZE)^lendiff * |bb|
 793  829   * aa->len should be >= bb->len
 794  830   */
 795  831  int
 796  832  big_cmp_abs_high(BIGNUM *aa, BIGNUM *bb)
 797  833  {
 798      -        int lendiff;
 799      -        BIGNUM aa1;
      834 +        int             lendiff;
      835 +        BIGNUM          aa1;
 800  836  
 801  837          lendiff = aa->len - bb->len;
 802  838          aa1.len = bb->len;
 803  839          aa1.size = aa->size - lendiff;
 804  840          aa1.malloced = 0;
 805  841          aa1.value = aa->value + lendiff;
 806  842          return (big_cmp_abs(&aa1, bb));
 807  843  }
 808  844  
 809  845  
↓ open down ↓ 95 lines elided ↑ open up ↑
 905  941  
 906  942          if (offs == 0) {
 907  943                  if (result != aa) {
 908  944                          (void) big_copy(result, aa);
 909  945                  }
 910  946                  return;
 911  947          }
 912  948          cy = aa->value[0] >> offs;
 913  949          for (i = 1; i < aa->len; i++) {
 914  950                  ai = aa->value[i];
 915      -                result->value[i-1] = (ai << (BIG_CHUNK_SIZE - offs)) | cy;
      951 +                result->value[i - 1] = (ai << (BIG_CHUNK_SIZE - offs)) | cy;
 916  952                  cy = ai >> offs;
 917  953          }
 918  954          result->len = aa->len;
 919  955          result->value[result->len - 1] = cy;
 920  956          result->sign = aa->sign;
 921  957  }
 922  958  
 923  959  
 924  960  /*
 925  961   * result = aa/bb   remainder = aa mod bb
 926  962   * it is assumed that aa and bb are positive
 927  963   */
 928  964  BIG_ERR_CODE
 929      -big_div_pos_fast(BIGNUM *result, BIGNUM *remainder, BIGNUM *aa, BIGNUM *bb)
      965 +big_div_pos(BIGNUM *result, BIGNUM *remainder, BIGNUM *aa, BIGNUM *bb)
 930  966  {
 931  967          BIG_ERR_CODE    err = BIG_OK;
 932  968          int             i, alen, blen, tlen, rlen, offs;
 933  969          BIG_CHUNK_TYPE  higha, highb, coeff;
 934  970          BIG_CHUNK_TYPE  *a, *b;
 935  971          BIGNUM          bbhigh, bblow, tresult, tmp1, tmp2;
 936  972          BIG_CHUNK_TYPE  tmp1value[BIGTMPSIZE];
 937  973          BIG_CHUNK_TYPE  tmp2value[BIGTMPSIZE];
 938  974          BIG_CHUNK_TYPE  tresultvalue[BIGTMPSIZE];
 939  975          BIG_CHUNK_TYPE  bblowvalue[BIGTMPSIZE];
↓ open down ↓ 130 lines elided ↑ open up ↑
1070 1106          big_finish(&tmp1);
1071 1107  ret3:
1072 1108          big_finish(&tmp2);
1073 1109  ret2:
1074 1110          big_finish(&bbhigh);
1075 1111  ret1:
1076 1112          big_finish(&bblow);
1077 1113          return (err);
1078 1114  }
1079 1115  
     1116 +
1080 1117  /*
1081 1118   * If there is no processor-specific integer implementation of
1082 1119   * the lower level multiply functions, then this code is provided
1083 1120   * for big_mul_set_vec(), big_mul_add_vec(), big_mul_vec() and
1084 1121   * big_sqr_vec().
1085 1122   *
1086 1123   * There are two generic implementations.  One that assumes that
1087 1124   * there is hardware and C compiler support for a 32 x 32 --> 64
1088 1125   * bit unsigned multiply, but otherwise is not specific to any
1089 1126   * processor, platform, or ISA.
↓ open down ↓ 9 lines elided ↑ open up ↑
1099 1136  #if !defined(PSR_MUL)
1100 1137  
1101 1138  #ifdef UMUL64
1102 1139  
1103 1140  #if (BIG_CHUNK_SIZE == 32)
1104 1141  
1105 1142  #define UNROLL8
1106 1143  
1107 1144  #define MUL_SET_VEC_ROUND_PREFETCH(R) \
1108 1145          p = pf * d; \
1109      -        pf = (uint64_t)a[R+1]; \
     1146 +        pf = (uint64_t)a[R + 1]; \
1110 1147          t = p + cy; \
1111 1148          r[R] = (uint32_t)t; \
1112 1149          cy = t >> 32
1113 1150  
1114 1151  #define MUL_SET_VEC_ROUND_NOPREFETCH(R) \
1115 1152          p = pf * d; \
1116 1153          t = p + cy; \
1117 1154          r[R] = (uint32_t)t; \
1118 1155          cy = t >> 32
1119 1156  
1120 1157  #define MUL_ADD_VEC_ROUND_PREFETCH(R) \
1121 1158          t = (uint64_t)r[R]; \
1122 1159          p = pf * d; \
1123      -        pf = (uint64_t)a[R+1]; \
     1160 +        pf = (uint64_t)a[R + 1]; \
1124 1161          t = p + t + cy; \
1125 1162          r[R] = (uint32_t)t; \
1126 1163          cy = t >> 32
1127 1164  
1128 1165  #define MUL_ADD_VEC_ROUND_NOPREFETCH(R) \
1129 1166          t = (uint64_t)r[R]; \
1130 1167          p = pf * d; \
1131 1168          t = p + t + cy; \
1132 1169          r[R] = (uint32_t)t; \
1133 1170          cy = t >> 32
↓ open down ↓ 101 lines elided ↑ open up ↑
1235 1272          if (len > 0) {
1236 1273                  MUL_ADD_VEC_ROUND_NOPREFETCH(0);
1237 1274          }
1238 1275          return ((uint32_t)cy);
1239 1276  }
1240 1277  #endif /* UNROLL8 */
1241 1278  
1242 1279  void
1243 1280  big_sqr_vec(uint32_t *r, uint32_t *a, int len)
1244 1281  {
1245      -        uint32_t *tr, *ta;
1246      -        int tlen, row, col;
1247      -        uint64_t p, s, t, t2, cy;
1248      -        uint32_t d;
     1282 +        uint32_t        *tr, *ta;
     1283 +        int             tlen, row, col;
     1284 +        uint64_t        p, s, t, t2, cy;
     1285 +        uint32_t        d;
1249 1286  
1250 1287          tr = r + 1;
1251 1288          ta = a;
1252 1289          tlen = len - 1;
1253 1290          tr[tlen] = big_mul_set_vec(tr, ta + 1, tlen, ta[0]);
1254 1291          while (--tlen > 0) {
1255 1292                  tr += 2;
1256 1293                  ++ta;
1257 1294                  tr[tlen] = big_mul_add_vec(tr, ta + 1, tlen, ta[0]);
1258 1295          }
↓ open down ↓ 10 lines elided ↑ open up ↑
1269 1306                  s = (uint64_t)a[row];
1270 1307                  s = s * s;
1271 1308                  p = (uint64_t)r[col] << 1;
1272 1309                  t = p + s;
1273 1310                  d = (uint32_t)t;
1274 1311                  t2 = (uint64_t)d + cy;
1275 1312                  r[col] = (uint32_t)t2;
1276 1313                  cy = (t >> 32) + (t2 >> 32);
1277 1314                  if (row == len - 1)
1278 1315                          break;
1279      -                p = ((uint64_t)r[col+1] << 1) + cy;
1280      -                r[col+1] = (uint32_t)p;
     1316 +                p = ((uint64_t)r[col + 1] << 1) + cy;
     1317 +                r[col + 1] = (uint32_t)p;
1281 1318                  cy = p >> 32;
1282 1319                  ++row;
1283 1320                  col += 2;
1284 1321          }
1285      -        r[col+1] = (uint32_t)cy;
     1322 +        r[col + 1] = (uint32_t)cy;
1286 1323  }
1287 1324  
1288 1325  #else /* BIG_CHUNK_SIZE == 64 */
1289 1326  
1290 1327  /*
1291 1328   * r = r + a * digit, r and a are vectors of length len
1292 1329   * returns the carry digit
1293 1330   */
1294 1331  BIG_CHUNK_TYPE
1295 1332  big_mul_add_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len,
↓ open down ↓ 58 lines elided ↑ open up ↑
1354 1391  }
1355 1392  
1356 1393  void
1357 1394  big_sqr_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int len)
1358 1395  {
1359 1396          int i;
1360 1397  
1361 1398          ASSERT(r != a);
1362 1399          r[len] = big_mul_set_vec(r, a, len, a[0]);
1363 1400          for (i = 1; i < len; ++i)
1364      -                r[len + i] = big_mul_add_vec(r+i, a, len, a[i]);
     1401 +                r[len + i] = big_mul_add_vec(r + i, a, len, a[i]);
1365 1402  }
1366 1403  
1367 1404  #endif /* BIG_CHUNK_SIZE == 32/64 */
1368 1405  
     1406 +
1369 1407  #else /* ! UMUL64 */
1370 1408  
1371 1409  #if (BIG_CHUNK_SIZE != 32)
1372 1410  #error Don't use 64-bit chunks without defining UMUL64
1373 1411  #endif
1374 1412  
1375 1413  
1376 1414  /*
1377 1415   * r = r + a * digit, r and a are vectors of length len
1378 1416   * returns the carry digit
↓ open down ↓ 46 lines elided ↑ open up ↑
1425 1463  }
1426 1464  
1427 1465  void
1428 1466  big_sqr_vec(uint32_t *r, uint32_t *a, int len)
1429 1467  {
1430 1468          int i;
1431 1469  
1432 1470          ASSERT(r != a);
1433 1471          r[len] = big_mul_set_vec(r, a, len, a[0]);
1434 1472          for (i = 1; i < len; ++i)
1435      -                r[len + i] = big_mul_add_vec(r+i, a, len, a[i]);
     1473 +                r[len + i] = big_mul_add_vec(r + i, a, len, a[i]);
1436 1474  }
1437 1475  
1438 1476  #endif /* UMUL64 */
1439 1477  
1440 1478  void
1441 1479  big_mul_vec(BIG_CHUNK_TYPE *r, BIG_CHUNK_TYPE *a, int alen,
1442 1480      BIG_CHUNK_TYPE *b, int blen)
1443 1481  {
1444 1482          int i;
1445 1483  
1446 1484          r[alen] = big_mul_set_vec(r, a, alen, b[0]);
1447 1485          for (i = 1; i < blen; ++i)
1448      -                r[alen + i] = big_mul_add_vec(r+i, a, alen, b[i]);
     1486 +                r[alen + i] = big_mul_add_vec(r + i, a, alen, b[i]);
1449 1487  }
1450 1488  
1451 1489  
1452 1490  #endif /* ! PSR_MUL */
1453 1491  
1454 1492  
1455 1493  /*
1456 1494   * result = aa * bb  result->value should be big enough to hold the result
1457 1495   *
1458 1496   * Implementation: Standard grammar school algorithm
↓ open down ↓ 26 lines elided ↑ open up ↑
1485 1523          while ((alen > 1) && (a[alen - 1] == 0)) {
1486 1524                  alen--;
1487 1525          }
1488 1526          aa->len = alen;
1489 1527          while ((blen > 1) && (b[blen - 1] == 0)) {
1490 1528                  blen--;
1491 1529          }
1492 1530          bb->len = blen;
1493 1531  
1494 1532          rsize = alen + blen;
     1533 +        ASSERT(rsize > 0);
1495 1534          if (result->size < rsize) {
1496 1535                  err = big_extend(result, rsize);
1497 1536                  if (err != BIG_OK) {
1498 1537                          return (err);
1499 1538                  }
1500 1539                  /* aa or bb might be an alias to result */
1501 1540                  a = aa->value;
1502 1541                  b = bb->value;
1503 1542          }
1504 1543          r = result->value;
↓ open down ↓ 37 lines elided ↑ open up ↑
1542 1581                  BIG_SQR_VEC(t, a, alen);
1543 1582          } else if (blen > 0) {
1544 1583                  BIG_MUL_VEC(t, a, alen, b, blen);
1545 1584          }
1546 1585  
1547 1586          if (t[rsize - 1] == 0) {
1548 1587                  tmp1.len = rsize - 1;
1549 1588          } else {
1550 1589                  tmp1.len = rsize;
1551 1590          }
1552      -        if ((err = big_copy(result, &tmp1)) != BIG_OK) {
1553      -                return (err);
1554      -        }
     1591 +
     1592 +        err = big_copy(result, &tmp1);
     1593 +
1555 1594          result->sign = sign;
1556 1595  
1557 1596          big_finish(&tmp1);
1558 1597  
1559      -        return (BIG_OK);
     1598 +        return (err);
1560 1599  }
1561 1600  
1562 1601  
1563 1602  /*
1564 1603   * caller must ensure that  a < n,  b < n  and  ret->size >=  2 * n->len + 1
1565 1604   * and that ret is not n
1566 1605   */
1567 1606  BIG_ERR_CODE
1568 1607  big_mont_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, BIGNUM *n, BIG_CHUNK_TYPE n0)
1569 1608  {
↓ open down ↓ 42 lines elided ↑ open up ↑
1612 1651                          }
1613 1652                  }
1614 1653          }
1615 1654          if (needsubtract)
1616 1655                  big_sub_vec(rr, rr + nlen, nn, nlen);
1617 1656          else {
1618 1657                  for (i = 0; i < nlen; i++) {
1619 1658                          rr[i] = rr[i + nlen];
1620 1659                  }
1621 1660          }
1622      -        for (i = nlen - 1; (i >= 0) && (rr[i] == 0); i--)
     1661 +
     1662 +        /* Remove leading zeros, but keep at least 1 digit: */
     1663 +        for (i = nlen - 1; (i > 0) && (rr[i] == 0); i--)
1623 1664                  ;
1624      -        ret->len = i+1;
     1665 +        ret->len = i + 1;
1625 1666  
1626 1667          return (BIG_OK);
1627 1668  }
1628 1669  
1629 1670  
1630 1671  BIG_CHUNK_TYPE
1631 1672  big_n0(BIG_CHUNK_TYPE n)
1632 1673  {
1633 1674          int             i;
1634 1675          BIG_CHUNK_TYPE  result, tmp;
↓ open down ↓ 122 lines elided ↑ open up ↑
1757 1798  /* ARGSUSED */
1758 1799  static BIG_ERR_CODE
1759 1800  big_modexp_ncp_int(BIGNUM *result, BIGNUM *ma, BIGNUM *e, BIGNUM *n,
1760 1801      BIGNUM *tmp, BIG_CHUNK_TYPE n0)
1761 1802  
1762 1803  {
1763 1804          BIGNUM          apowers[APOWERS_MAX_SIZE];
1764 1805          BIGNUM          tmp1;
1765 1806          BIG_CHUNK_TYPE  tmp1value[BIGTMPSIZE];
1766 1807          int             i, j, k, l, m, p;
1767      -        int             bit, bitind, bitcount, groupbits, apowerssize;
1768      -        int             nbits;
     1808 +        uint32_t        bit, bitind, bitcount, groupbits, apowerssize;
     1809 +        uint32_t        nbits;
1769 1810          BIG_ERR_CODE    err;
1770 1811  
1771 1812          nbits = big_numbits(e);
1772 1813          if (nbits < 50) {
1773 1814                  groupbits = 1;
1774 1815                  apowerssize = 1;
1775 1816          } else {
1776 1817                  groupbits = MAX_EXP_BIT_GROUP_SIZE;
1777 1818                  apowerssize = 1 << (groupbits - 1);
1778 1819          }
1779 1820  
1780 1821  
1781 1822          if ((err = big_init1(&tmp1, 2 * n->len + 1,
1782 1823              tmp1value, arraysize(tmp1value))) != BIG_OK) {
1783 1824                  return (err);
1784 1825          }
1785 1826  
1786      -        /* set the malloced bit to help cleanup */
     1827 +        /* clear the malloced bit to help cleanup */
1787 1828          for (i = 0; i < apowerssize; i++) {
1788 1829                  apowers[i].malloced = 0;
1789 1830          }
     1831 +
1790 1832          for (i = 0; i < apowerssize; i++) {
1791 1833                  if ((err = big_init1(&(apowers[i]), n->len, NULL, 0)) !=
1792 1834                      BIG_OK) {
1793 1835                          goto ret;
1794 1836                  }
1795 1837          }
1796 1838  
1797 1839          (void) big_copy(&(apowers[0]), ma);
1798 1840  
1799 1841          if ((err = big_mont_mul(&tmp1, ma, ma, n, n0)) != BIG_OK) {
1800 1842                  goto ret;
1801 1843          }
1802 1844          (void) big_copy(ma, &tmp1);
1803 1845  
1804 1846          for (i = 1; i < apowerssize; i++) {
1805 1847                  if ((err = big_mont_mul(&tmp1, ma,
1806      -                    &(apowers[i-1]), n, n0)) != BIG_OK) {
     1848 +                    &(apowers[i - 1]), n, n0)) != BIG_OK) {
1807 1849                          goto ret;
1808 1850                  }
1809 1851                  (void) big_copy(&apowers[i], &tmp1);
1810 1852          }
1811 1853  
1812 1854          bitind = nbits % BIG_CHUNK_SIZE;
1813 1855          k = 0;
1814 1856          l = 0;
1815 1857          p = 0;
1816 1858          bitcount = 0;
↓ open down ↓ 88 lines elided ↑ open up ↑
1905 1947  #endif /* _KERNEL */
1906 1948  
1907 1949  /*
1908 1950   * This version makes use of floating point for performance
1909 1951   */
1910 1952  static BIG_ERR_CODE
1911 1953  big_modexp_ncp_float(BIGNUM *result, BIGNUM *ma, BIGNUM *e, BIGNUM *n,
1912 1954      BIGNUM *tmp, BIG_CHUNK_TYPE n0)
1913 1955  {
1914 1956  
1915      -        int             i, j, k, l, m, p, bit, bitind, bitcount, nlen;
     1957 +        int             i, j, k, l, m, p;
     1958 +        uint32_t        bit, bitind, bitcount, nlen;
1916 1959          double          dn0;
1917 1960          double          *dn, *dt, *d16r, *d32r;
1918 1961          uint32_t        *nint, *prod;
1919 1962          double          *apowers[APOWERS_MAX_SIZE];
1920      -        int             nbits, groupbits, apowerssize;
     1963 +        uint32_t        nbits, groupbits, apowerssize;
1921 1964          BIG_ERR_CODE    err = BIG_OK;
1922 1965  
1923 1966  #ifdef _KERNEL
1924 1967          uint8_t fpua[sizeof (kfpu_t) + FPR_ALIGN];
1925 1968          kfpu_t *fpu;
1926 1969  
1927 1970  #ifdef DEBUG
1928 1971          if (!fpu_exists)
1929 1972                  return (BIG_GENERAL_ERR);
1930 1973  #endif
↓ open down ↓ 139 lines elided ↑ open up ↑
2070 2113                                  if (bitcount == groupbits) {
2071 2114                                          for (m = 0; m < k; m++) {
2072 2115                                                  conv_i32_to_d32_and_d16(d32r,
2073 2116                                                      d16r, prod, nlen);
2074 2117                                                  mont_mulf_noconv(prod, d32r,
2075 2118                                                      d16r, dt, dn, nint,
2076 2119                                                      nlen, dn0);
2077 2120                                          }
2078 2121                                          conv_i32_to_d32(d32r, prod, nlen);
2079 2122                                          mont_mulf_noconv(prod, d32r,
2080      -                                            apowers[p >> (l+1)],
     2123 +                                            apowers[p >> (l + 1)],
2081 2124                                              dt, dn, nint, nlen, dn0);
2082 2125                                          for (m = 0; m < l; m++) {
2083 2126                                                  conv_i32_to_d32_and_d16(d32r,
2084 2127                                                      d16r, prod, nlen);
2085 2128                                                  mont_mulf_noconv(prod, d32r,
2086 2129                                                      d16r, dt, dn, nint,
2087 2130                                                      nlen, dn0);
2088 2131                                          }
2089 2132                                          k = 0;
2090 2133                                          l = 0;
↓ open down ↓ 85 lines elided ↑ open up ↑
2176 2219                  return (err);
2177 2220          }
2178 2221          ma.len = 1;
2179 2222          ma.value[0] = 0;
2180 2223  
2181 2224          if ((err = big_init1(&tmp, 2 * n->len + 1,
2182 2225              tmpvalue, arraysize(tmpvalue))) != BIG_OK) {
2183 2226                  goto ret1;
2184 2227          }
2185 2228  
2186      -        /* set the malloced bit to help cleanup */
     2229 +        /* clear the malloced bit to help cleanup */
2187 2230          rr.malloced = 0;
     2231 +
2188 2232          if (n_rr == NULL) {
2189 2233                  if ((err = big_init1(&rr, 2 * n->len + 1,
2190 2234                      rrvalue, arraysize(rrvalue))) != BIG_OK) {
2191 2235                          goto ret2;
2192 2236                  }
2193 2237                  if (big_mont_rr(&rr, n) != BIG_OK) {
2194 2238                          goto ret;
2195 2239                  }
2196 2240                  n_rr = &rr;
2197 2241          }
↓ open down ↓ 176 lines elided ↑ open up ↑
2374 2418  
2375 2419  BIG_ERR_CODE
2376 2420  big_sqrt_pos(BIGNUM *result, BIGNUM *n)
2377 2421  {
2378 2422          BIGNUM          *high, *low, *mid, *t;
2379 2423          BIGNUM          t1, t2, t3, prod;
2380 2424          BIG_CHUNK_TYPE  t1value[BIGTMPSIZE];
2381 2425          BIG_CHUNK_TYPE  t2value[BIGTMPSIZE];
2382 2426          BIG_CHUNK_TYPE  t3value[BIGTMPSIZE];
2383 2427          BIG_CHUNK_TYPE  prodvalue[BIGTMPSIZE];
2384      -        int             i, nbits, diff, nrootbits, highbits;
     2428 +        int             i, diff;
     2429 +        uint32_t        nbits, nrootbits, highbits;
2385 2430          BIG_ERR_CODE    err;
2386 2431  
2387 2432          nbits = big_numbits(n);
2388 2433  
2389 2434          if ((err = big_init1(&t1, n->len + 1,
2390 2435              t1value, arraysize(t1value))) != BIG_OK)
2391 2436                  return (err);
2392 2437          if ((err = big_init1(&t2, n->len + 1,
2393 2438              t2value, arraysize(t2value))) != BIG_OK)
2394 2439                  goto ret1;
↓ open down ↓ 149 lines elided ↑ open up ↑
2544 2589  ret1:
2545 2590          if (t1.malloced) big_finish(&t1);
2546 2591  
2547 2592          return (err);
2548 2593  }
2549 2594  
2550 2595  
2551 2596  BIG_ERR_CODE
2552 2597  big_Lucas(BIGNUM *Lkminus1, BIGNUM *Lk, BIGNUM *p, BIGNUM *k, BIGNUM *n)
2553 2598  {
2554      -        int             m, w, i;
     2599 +        int             i;
     2600 +        uint32_t        m, w;
2555 2601          BIG_CHUNK_TYPE  bit;
2556 2602          BIGNUM          ki, tmp, tmp2;
2557 2603          BIG_CHUNK_TYPE  kivalue[BIGTMPSIZE];
2558 2604          BIG_CHUNK_TYPE  tmpvalue[BIGTMPSIZE];
2559 2605          BIG_CHUNK_TYPE  tmp2value[BIGTMPSIZE];
2560 2606          BIG_ERR_CODE    err;
2561 2607  
2562 2608          if (big_cmp_abs(k, &big_One) == 0) {
2563 2609                  (void) big_copy(Lk, p);
2564 2610                  (void) big_copy(Lkminus1, &big_Two);
2565 2611                  return (BIG_OK);
2566 2612          }
2567 2613  
2568 2614          if ((err = big_init1(&ki, k->len + 1,
2569 2615              kivalue, arraysize(kivalue))) != BIG_OK)
2570 2616                  return (err);
2571 2617  
2572      -        if ((err = big_init1(&tmp, 2 * n->len +1,
     2618 +        if ((err = big_init1(&tmp, 2 * n->len + 1,
2573 2619              tmpvalue, arraysize(tmpvalue))) != BIG_OK)
2574 2620                  goto ret1;
2575 2621  
2576 2622          if ((err = big_init1(&tmp2, n->len,
2577 2623              tmp2value, arraysize(tmp2value))) != BIG_OK)
2578 2624                  goto ret2;
2579 2625  
2580 2626          m = big_numbits(k);
2581 2627          ki.len = (m - 1) / BIG_CHUNK_SIZE + 1;
2582 2628          w = (m - 1) / BIG_CHUNK_SIZE;
↓ open down ↓ 102 lines elided ↑ open up ↑
2685 2731          if ((err = big_init1(&Lkminus1, n->len,
2686 2732              Lkminus1value, arraysize(Lkminus1value))) != BIG_OK) {
2687 2733                  goto ret3;
2688 2734          }
2689 2735  
2690 2736          if ((err = big_init1(&Lk, n->len,
2691 2737              Lkvalue, arraysize(Lkvalue))) != BIG_OK) {
2692 2738                  goto ret4;
2693 2739          }
2694 2740  
2695      -        (void) big_sub_pos(&o, n, &big_One);    /* cannot fail */
     2741 +        (void) big_sub_pos(&o, n, &big_One);    /* cannot fail */
2696 2742          (void) big_copy(&nminus1, &o);          /* cannot fail */
2697 2743          e = 0;
2698 2744          while ((o.value[0] & 1) == 0) {
2699 2745                  e++;
2700 2746                  (void) big_half_pos(&o, &o);  /* cannot fail */
2701 2747          }
2702 2748          if ((err = big_modexp_ext(&tmp, &big_Two, &o, n, NULL, info)) !=
2703 2749              BIG_OK) {
2704 2750                  goto ret;
2705 2751          }
↓ open down ↓ 68 lines elided ↑ open up ↑
2774 2820  
2775 2821  BIG_ERR_CODE
2776 2822  big_isprime_pos(BIGNUM *n)
2777 2823  {
2778 2824          return (big_isprime_pos_ext(n, NULL));
2779 2825  }
2780 2826  
2781 2827  
2782 2828  #define SIEVESIZE 1000
2783 2829  
2784      -uint32_t smallprimes[] =
2785      -{
2786      -3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
2787      -51, 53, 59, 61, 67, 71, 73, 79, 83, 89, 91, 97
2788      -};
2789 2830  
2790      -
2791 2831  BIG_ERR_CODE
2792 2832  big_nextprime_pos_ext(BIGNUM *result, BIGNUM *n, big_modexp_ncp_info_t *info)
2793 2833  {
     2834 +        static const uint32_t smallprimes[] = {
     2835 +            3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
     2836 +            51, 53, 59, 61, 67, 71, 73, 79, 83, 89, 91, 97 };
2794 2837          BIG_ERR_CODE    err;
2795 2838          int             sieve[SIEVESIZE];
2796 2839          int             i;
2797 2840          uint32_t        off, p;
2798 2841  
2799 2842          if ((err = big_copy(result, n)) != BIG_OK) {
2800 2843                  return (err);
2801 2844          }
2802 2845          result->value[0] |= 1;
2803 2846          /* CONSTCOND */
↓ open down ↓ 212 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX