comparison buffer.c @ 640:76097ec1a29a dropbear-tfm

- Bring in original tomsfastmath patch against 0.52 from Peter Turczak in 2008
author Matt Johnston <matt@ucc.asn.au>
date Mon, 21 Nov 2011 19:19:57 +0800
parents c5d3ef11155f
children 2b1bb792cd4d
comparison
equal deleted inserted replaced
518:ce104c8b0be1 640:76097ec1a29a
35 #define BUF_MAX_SIZE 1000000000 35 #define BUF_MAX_SIZE 1000000000
36 36
37 /* avoid excessively large numbers, > ~8192 bits */ 37 /* avoid excessively large numbers, > ~8192 bits */
38 #define BUF_MAX_MPINT (8240 / 8) 38 #define BUF_MAX_MPINT (8240 / 8)
39 39
40 #define BUF_MAX_FPINT (FP_MAX_SIZE / 8)
41
40 /* Create (malloc) a new buffer of size */ 42 /* Create (malloc) a new buffer of size */
41 buffer* buf_new(unsigned int size) { 43 buffer* buf_new(unsigned int size) {
42 44
43 buffer* buf; 45 buffer* buf;
44 46
263 } 265 }
264 266
265 267
266 /* for our purposes we only need positive (or 0) numbers, so will 268 /* for our purposes we only need positive (or 0) numbers, so will
267 * fail if we get negative numbers */ 269 * fail if we get negative numbers */
268 void buf_putmpint(buffer* buf, mp_int * mp) { 270 void buf_putfpint(buffer* buf, fp_int * fp) {
269 271
270 unsigned int len, pad = 0; 272 unsigned int len, pad = 0;
271 TRACE(("enter buf_putmpint")) 273 TRACE(("enter buf_putfpint"))
272 274
273 dropbear_assert(mp != NULL); 275 dropbear_assert(fp != NULL);
274 276
275 if (SIGN(mp) == MP_NEG) { 277 if (SIGN(fp) == FP_NEG) {
276 dropbear_exit("negative bignum"); 278 dropbear_exit("negative bignum");
277 } 279 }
278 280
279 /* zero check */ 281 /* zero check */
280 if (USED(mp) == 1 && DIGIT(mp, 0) == 0) { 282 if (USED(fp) == 1 && DIGIT(fp, 0) == 0) {
281 len = 0; 283 len = 0;
282 } else { 284 } else {
283 /* SSH spec requires padding for mpints with the MSB set, this code 285 /* SSH spec requires padding for fpints with the MSB set, this code
284 * implements it */ 286 * ifplements it */
285 len = mp_count_bits(mp); 287 len = fp_count_bits(fp);
286 /* if the top bit of MSB is set, we need to pad */ 288 /* if the top bit of MSB is set, we need to pad */
287 pad = (len%8 == 0) ? 1 : 0; 289 pad = (len%8 == 0) ? 1 : 0;
288 len = len / 8 + 1; /* don't worry about rounding, we need it for 290 len = len / 8 + 1; /* don't worry about rounding, we need it for
289 padding anyway when len%8 == 0 */ 291 padding anyway when len%8 == 0 */
290 292
296 /* store the actual value */ 298 /* store the actual value */
297 if (len > 0) { 299 if (len > 0) {
298 if (pad) { 300 if (pad) {
299 buf_putbyte(buf, 0x00); 301 buf_putbyte(buf, 0x00);
300 } 302 }
301 if (mp_to_unsigned_bin(mp, buf_getwriteptr(buf, len-pad)) != MP_OKAY) { 303 /* Should always succseed */
302 dropbear_exit("mpint error"); 304 fp_to_unsigned_bin(fp, buf_getwriteptr(buf, len-pad)) ;
303 } 305
304 buf_incrwritepos(buf, len-pad); 306 buf_incrwritepos(buf, len-pad);
305 } 307 }
306 308
307 TRACE(("leave buf_putmpint")) 309 TRACE(("leave buf_putfpint"))
308 } 310 }
309 311
310 /* Retrieve an mp_int from the buffer. 312 /* Retrieve an fp_int from the buffer.
311 * Will fail for -ve since they shouldn't be required here. 313 * Will fail for -ve since they shouldn't be required here.
312 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ 314 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
313 int buf_getmpint(buffer* buf, mp_int* mp) { 315 int buf_getfpint(buffer* buf, fp_int* fp) {
314 316
315 unsigned int len; 317 unsigned int len;
316 len = buf_getint(buf); 318 len = buf_getint(buf);
317 319
318 if (len == 0) { 320 if (len == 0) {
319 mp_zero(mp); 321 fp_zero(fp);
320 return DROPBEAR_SUCCESS; 322 return DROPBEAR_SUCCESS;
321 } 323 }
322 324
323 if (len > BUF_MAX_MPINT) { 325 if (len > BUF_MAX_FPINT) {
324 return DROPBEAR_FAILURE; 326 return DROPBEAR_FAILURE;
325 } 327 }
326 328
327 /* check for negative */ 329 /* check for negative */
328 if (*buf_getptr(buf, 1) & (1 << (CHAR_BIT-1))) { 330 if (*buf_getptr(buf, 1) & (1 << (CHAR_BIT-1))) {
329 return DROPBEAR_FAILURE; 331 return DROPBEAR_FAILURE;
330 } 332 }
331 333
332 if (mp_read_unsigned_bin(mp, buf_getptr(buf, len), len) != MP_OKAY) { 334 /** Should always succseed */
333 return DROPBEAR_FAILURE; 335 fp_read_unsigned_bin(fp, buf_getptr(buf, len), len);
334 }
335 336
336 buf_incrpos(buf, len); 337 buf_incrpos(buf, len);
337 return DROPBEAR_SUCCESS; 338 return DROPBEAR_SUCCESS;
338 } 339 }