~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtommath/bn_mp_prime_next_prime.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
   a->sign = MP_ZPOS;
36
36
 
37
37
   /* simple algo if a is less than the largest prime in the table */
38
 
   if (mp_cmp_d(a, __prime_tab[PRIME_SIZE-1]) == MP_LT) {
 
38
   if (mp_cmp_d(a, ltm_prime_tab[PRIME_SIZE-1]) == MP_LT) {
39
39
      /* find which prime it is bigger than */
40
40
      for (x = PRIME_SIZE - 2; x >= 0; x--) {
41
 
          if (mp_cmp_d(a, __prime_tab[x]) != MP_LT) {
 
41
          if (mp_cmp_d(a, ltm_prime_tab[x]) != MP_LT) {
42
42
             if (bbs_style == 1) {
43
43
                /* ok we found a prime smaller or
44
44
                 * equal [so the next is larger]
46
46
                 * however, the prime must be
47
47
                 * congruent to 3 mod 4
48
48
                 */
49
 
                if ((__prime_tab[x + 1] & 3) != 3) {
 
49
                if ((ltm_prime_tab[x + 1] & 3) != 3) {
50
50
                   /* scan upwards for a prime congruent to 3 mod 4 */
51
51
                   for (y = x + 1; y < PRIME_SIZE; y++) {
52
 
                       if ((__prime_tab[y] & 3) == 3) {
53
 
                          mp_set(a, __prime_tab[y]);
 
52
                       if ((ltm_prime_tab[y] & 3) == 3) {
 
53
                          mp_set(a, ltm_prime_tab[y]);
54
54
                          return MP_OKAY;
55
55
                       }
56
56
                   }
57
57
                }
58
58
             } else {
59
 
                mp_set(a, __prime_tab[x + 1]);
 
59
                mp_set(a, ltm_prime_tab[x + 1]);
60
60
                return MP_OKAY;
61
61
             }
62
62
          }
94
94
 
95
95
   /* generate the restable */
96
96
   for (x = 1; x < PRIME_SIZE; x++) {
97
 
      if ((err = mp_mod_d(a, __prime_tab[x], res_tab + x)) != MP_OKAY) {
 
97
      if ((err = mp_mod_d(a, ltm_prime_tab[x], res_tab + x)) != MP_OKAY) {
98
98
         return err;
99
99
      }
100
100
   }
120
120
             res_tab[x] += kstep;
121
121
 
122
122
             /* subtract the modulus [instead of using division] */
123
 
             if (res_tab[x] >= __prime_tab[x]) {
124
 
                res_tab[x]  -= __prime_tab[x];
 
123
             if (res_tab[x] >= ltm_prime_tab[x]) {
 
124
                res_tab[x]  -= ltm_prime_tab[x];
125
125
             }
126
126
 
127
127
             /* set flag if zero */
133
133
 
134
134
      /* add the step */
135
135
      if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
136
 
         goto __ERR;
 
136
         goto LBL_ERR;
137
137
      }
138
138
 
139
139
      /* if didn't pass sieve and step == MAX then skip test */
143
143
 
144
144
      /* is this prime? */
145
145
      for (x = 0; x < t; x++) {
146
 
          mp_set(&b, __prime_tab[t]);
 
146
          mp_set(&b, ltm_prime_tab[t]);
147
147
          if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
148
 
             goto __ERR;
 
148
             goto LBL_ERR;
149
149
          }
150
150
          if (res == MP_NO) {
151
151
             break;
158
158
   }
159
159
 
160
160
   err = MP_OKAY;
161
 
__ERR:
 
161
LBL_ERR:
162
162
   mp_clear(&b);
163
163
   return err;
164
164
}