comparison buffer.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents d5cdc60db08e
children ff51d5967e2d
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
305 305
306 306
307 /* for our purposes we only need positive (or 0) numbers, so will 307 /* for our purposes we only need positive (or 0) numbers, so will
308 * fail if we get negative numbers */ 308 * fail if we get negative numbers */
309 void buf_putmpint(buffer* buf, mp_int * mp) { 309 void buf_putmpint(buffer* buf, mp_int * mp) {
310 310 size_t written;
311 unsigned int len, pad = 0; 311 unsigned int len, pad = 0;
312 TRACE2(("enter buf_putmpint")) 312 TRACE2(("enter buf_putmpint"))
313 313
314 dropbear_assert(mp != NULL); 314 dropbear_assert(mp != NULL);
315 315
316 if (SIGN(mp) == MP_NEG) { 316 if (mp_isneg(mp)) {
317 dropbear_exit("negative bignum"); 317 dropbear_exit("negative bignum");
318 } 318 }
319 319
320 /* zero check */ 320 /* zero check */
321 if (USED(mp) == 1 && DIGIT(mp, 0) == 0) { 321 if (mp_iszero(mp)) {
322 len = 0; 322 len = 0;
323 } else { 323 } else {
324 /* SSH spec requires padding for mpints with the MSB set, this code 324 /* SSH spec requires padding for mpints with the MSB set, this code
325 * implements it */ 325 * implements it */
326 len = mp_count_bits(mp); 326 len = mp_count_bits(mp);
337 /* store the actual value */ 337 /* store the actual value */
338 if (len > 0) { 338 if (len > 0) {
339 if (pad) { 339 if (pad) {
340 buf_putbyte(buf, 0x00); 340 buf_putbyte(buf, 0x00);
341 } 341 }
342 if (mp_to_unsigned_bin(mp, buf_getwriteptr(buf, len-pad)) != MP_OKAY) { 342 if (mp_to_ubin(mp, buf_getwriteptr(buf, len-pad), len-pad, &written) != MP_OKAY) {
343 dropbear_exit("mpint error"); 343 dropbear_exit("mpint error");
344 } 344 }
345 buf_incrwritepos(buf, len-pad); 345 buf_incrwritepos(buf, written);
346 } 346 }
347 347
348 TRACE2(("leave buf_putmpint")) 348 TRACE2(("leave buf_putmpint"))
349 } 349 }
350 350
368 /* check for negative */ 368 /* check for negative */
369 if (*buf_getptr(buf, 1) & (1 << (CHAR_BIT-1))) { 369 if (*buf_getptr(buf, 1) & (1 << (CHAR_BIT-1))) {
370 return DROPBEAR_FAILURE; 370 return DROPBEAR_FAILURE;
371 } 371 }
372 372
373 if (mp_read_unsigned_bin(mp, buf_getptr(buf, len), len) != MP_OKAY) { 373 if (mp_from_ubin(mp, buf_getptr(buf, len), len) != MP_OKAY) {
374 return DROPBEAR_FAILURE; 374 return DROPBEAR_FAILURE;
375 } 375 }
376 376
377 buf_incrpos(buf, len); 377 buf_incrpos(buf, len);
378 return DROPBEAR_SUCCESS; 378 return DROPBEAR_SUCCESS;