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

« back to all changes in this revision

Viewing changes to libtommath/bn_mp_div.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:
49
49
 
50
50
  mp_set(&tq, 1);
51
51
  n = mp_count_bits(a) - mp_count_bits(b);
52
 
  if (((res = mp_copy(a, &ta)) != MP_OKAY) ||
53
 
      ((res = mp_copy(b, &tb)) != MP_OKAY) || 
 
52
  if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
 
53
      ((res = mp_abs(b, &tb)) != MP_OKAY) || 
54
54
      ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
55
55
      ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
56
 
      goto __ERR;
 
56
      goto LBL_ERR;
57
57
  }
58
58
 
59
59
  while (n-- >= 0) {
60
60
     if (mp_cmp(&tb, &ta) != MP_GT) {
61
61
        if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) ||
62
62
            ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) {
63
 
           goto __ERR;
 
63
           goto LBL_ERR;
64
64
        }
65
65
     }
66
66
     if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) ||
67
67
         ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) {
68
 
           goto __ERR;
 
68
           goto LBL_ERR;
69
69
     }
70
70
  }
71
71
 
74
74
  n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
75
75
  if (c != NULL) {
76
76
     mp_exch(c, &q);
77
 
     c->sign  = n2;
 
77
     c->sign  = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2;
78
78
  }
79
79
  if (d != NULL) {
80
80
     mp_exch(d, &ta);
81
 
     d->sign = n;
 
81
     d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n;
82
82
  }
83
 
__ERR:
 
83
LBL_ERR:
84
84
   mp_clear_multi(&ta, &tb, &tq, &q, NULL);
85
85
   return res;
86
86
}
129
129
  q.used = a->used + 2;
130
130
 
131
131
  if ((res = mp_init (&t1)) != MP_OKAY) {
132
 
    goto __Q;
 
132
    goto LBL_Q;
133
133
  }
134
134
 
135
135
  if ((res = mp_init (&t2)) != MP_OKAY) {
136
 
    goto __T1;
 
136
    goto LBL_T1;
137
137
  }
138
138
 
139
139
  if ((res = mp_init_copy (&x, a)) != MP_OKAY) {
140
 
    goto __T2;
 
140
    goto LBL_T2;
141
141
  }
142
142
 
143
143
  if ((res = mp_init_copy (&y, b)) != MP_OKAY) {
144
 
    goto __X;
 
144
    goto LBL_X;
145
145
  }
146
146
 
147
147
  /* fix the sign */
153
153
  if (norm < (int)(DIGIT_BIT-1)) {
154
154
     norm = (DIGIT_BIT-1) - norm;
155
155
     if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) {
156
 
       goto __Y;
 
156
       goto LBL_Y;
157
157
     }
158
158
     if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) {
159
 
       goto __Y;
 
159
       goto LBL_Y;
160
160
     }
161
161
  } else {
162
162
     norm = 0;
168
168
 
169
169
  /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */
170
170
  if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */
171
 
    goto __Y;
 
171
    goto LBL_Y;
172
172
  }
173
173
 
174
174
  while (mp_cmp (&x, &y) != MP_LT) {
175
175
    ++(q.dp[n - t]);
176
176
    if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) {
177
 
      goto __Y;
 
177
      goto LBL_Y;
178
178
    }
179
179
  }
180
180
 
216
216
      t1.dp[1] = y.dp[t];
217
217
      t1.used = 2;
218
218
      if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) {
219
 
        goto __Y;
 
219
        goto LBL_Y;
220
220
      }
221
221
 
222
222
      /* find right hand */
228
228
 
229
229
    /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
230
230
    if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) {
231
 
      goto __Y;
 
231
      goto LBL_Y;
232
232
    }
233
233
 
234
234
    if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
235
 
      goto __Y;
 
235
      goto LBL_Y;
236
236
    }
237
237
 
238
238
    if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) {
239
 
      goto __Y;
 
239
      goto LBL_Y;
240
240
    }
241
241
 
242
242
    /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
243
243
    if (x.sign == MP_NEG) {
244
244
      if ((res = mp_copy (&y, &t1)) != MP_OKAY) {
245
 
        goto __Y;
 
245
        goto LBL_Y;
246
246
      }
247
247
      if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
248
 
        goto __Y;
 
248
        goto LBL_Y;
249
249
      }
250
250
      if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) {
251
 
        goto __Y;
 
251
        goto LBL_Y;
252
252
      }
253
253
 
254
254
      q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK;
275
275
 
276
276
  res = MP_OKAY;
277
277
 
278
 
__Y:mp_clear (&y);
279
 
__X:mp_clear (&x);
280
 
__T2:mp_clear (&t2);
281
 
__T1:mp_clear (&t1);
282
 
__Q:mp_clear (&q);
 
278
LBL_Y:mp_clear (&y);
 
279
LBL_X:mp_clear (&x);
 
280
LBL_T2:mp_clear (&t2);
 
281
LBL_T1:mp_clear (&t1);
 
282
LBL_Q:mp_clear (&q);
283
283
  return res;
284
284
}
285
285