comparison common-kex.c @ 188:c9483550701b

- refactored random mp_int generation and byte->mp_int code - added RSA blinding
author Matt Johnston <matt@ucc.asn.au>
date Thu, 05 May 2005 03:58:21 +0000
parents 161557a9dde8
children ad1b24e39bf3
comparison
equal deleted inserted replaced
187:c44df7123b0a 188:c9483550701b
467 void gen_kexdh_vals(mp_int *dh_pub, mp_int *dh_priv) { 467 void gen_kexdh_vals(mp_int *dh_pub, mp_int *dh_priv) {
468 468
469 DEF_MP_INT(dh_p); 469 DEF_MP_INT(dh_p);
470 DEF_MP_INT(dh_q); 470 DEF_MP_INT(dh_q);
471 DEF_MP_INT(dh_g); 471 DEF_MP_INT(dh_g);
472 unsigned char randbuf[DH_P_LEN];
473 int dh_q_len;
474 472
475 TRACE(("enter send_msg_kexdh_reply")) 473 TRACE(("enter send_msg_kexdh_reply"))
476 474
477 m_mp_init_multi(&dh_g, &dh_p, &dh_q, NULL); 475 m_mp_init_multi(&dh_g, &dh_p, &dh_q, NULL);
478 476
479 /* read the prime and generator*/ 477 /* read the prime and generator*/
480 if (mp_read_unsigned_bin(&dh_p, (unsigned char*)dh_p_val, DH_P_LEN) 478 bytes_to_mp(&dh_p, (unsigned char*)dh_p_val, DH_P_LEN);
481 != MP_OKAY) {
482 dropbear_exit("Diffie-Hellman error");
483 }
484 479
485 if (mp_set_int(&dh_g, DH_G_VAL) != MP_OKAY) { 480 if (mp_set_int(&dh_g, DH_G_VAL) != MP_OKAY) {
486 dropbear_exit("Diffie-Hellman error"); 481 dropbear_exit("Diffie-Hellman error");
487 } 482 }
488 483
493 } 488 }
494 if (mp_div_2(dh_priv, &dh_q) != MP_OKAY) { 489 if (mp_div_2(dh_priv, &dh_q) != MP_OKAY) {
495 dropbear_exit("Diffie-Hellman error"); 490 dropbear_exit("Diffie-Hellman error");
496 } 491 }
497 492
498 dh_q_len = mp_unsigned_bin_size(&dh_q); 493 /* Generate a private portion 0 < dh_priv < dh_q */
499 494 gen_random_mpint(&dh_q, dh_priv);
500 /* calculate our random value dh_y */
501 do {
502 assert((unsigned int)dh_q_len <= sizeof(randbuf));
503 genrandom(randbuf, dh_q_len);
504 if (mp_read_unsigned_bin(dh_priv, randbuf, dh_q_len) != MP_OKAY) {
505 dropbear_exit("Diffie-Hellman error");
506 }
507 } while (mp_cmp(dh_priv, &dh_q) == MP_GT || mp_cmp_d(dh_priv, 0) != MP_GT);
508 495
509 /* f = g^y mod p */ 496 /* f = g^y mod p */
510 if (mp_exptmod(&dh_g, dh_priv, &dh_p, dh_pub) != MP_OKAY) { 497 if (mp_exptmod(&dh_g, dh_priv, &dh_p, dh_pub) != MP_OKAY) {
511 dropbear_exit("Diffie-Hellman error"); 498 dropbear_exit("Diffie-Hellman error");
512 } 499 }
524 mp_int *dh_e = NULL, *dh_f = NULL; 511 mp_int *dh_e = NULL, *dh_f = NULL;
525 hash_state hs; 512 hash_state hs;
526 513
527 /* read the prime and generator*/ 514 /* read the prime and generator*/
528 mp_init(&dh_p); 515 mp_init(&dh_p);
529 if (mp_read_unsigned_bin(&dh_p, (unsigned char*)dh_p_val, DH_P_LEN) 516 bytes_to_mp(&dh_p, dh_p_val, DH_P_LEN);
530 != MP_OKAY) {
531 dropbear_exit("Diffie-Hellman error");
532 }
533 517
534 /* Check that dh_pub_them (dh_e or dh_f) is in the range [1, p-1] */ 518 /* Check that dh_pub_them (dh_e or dh_f) is in the range [1, p-1] */
535 if (mp_cmp(dh_pub_them, &dh_p) != MP_LT 519 if (mp_cmp(dh_pub_them, &dh_p) != MP_LT
536 || mp_cmp_d(dh_pub_them, 0) != MP_GT) { 520 || mp_cmp_d(dh_pub_them, 0) != MP_GT) {
537 dropbear_exit("Diffie-Hellman error"); 521 dropbear_exit("Diffie-Hellman error");