Any perl-monks crypto savvy ?
I'd like to have an opinion on how to choose good primes for DH agreement :
here is an except of my perl code, I have mixed feeling about it, please comment !
(link for downloading the file directly : http://gateway.ipfs.io/ipfs/QmRi8Ag9ApvgXYhHybvdY9RXRwxG1gkUTuihN6qq2MBjeq )

#!/usr/bin/perl our $dbug = 0; use YAML::Syck qw(DumpFile); # 0) initialization get (g,p) # 1) Bob to create keypair # 2) Alice to get Bob's public key # 3) Alice to compute a shared-secret with Bob's public key... # 4) Alice to send her public-key to Bob # 5) Bob to generate his side of the shared-secret # 6) compare ! my $user = 'Bob'; # --------------------------------------------------- $PKI = { anon => ['anonymous', 'pubkey' => '123', secret => undef ] }; # --------------------------------------------------- my $seed = srand(); printf "seed: %s\n",$seed; my $iv_key = rand(4125415); my ($g,$p) = &get_IV($iv_key); # keys agreement : use Crypt::DH; my $bob; # Bob's DH # --------------------------------------------------------- if ($user eq 'Bob') { my $I = uc substr($user,0,1); $bob = Crypt::DH->new( p => "$p", g => $g ); if ($dbug) { printf "g: %s\n",$bob->g(); printf "p: %s\n",$bob->p(); } my $pub = $bob->generate_keys; my $keypair = { user => $user, pubkey => $pub, seckey => $bob->priv_key() }; printf "%s:\n",$user; printf "pub(%s): %s\n",$I,substr($pub,0,32); printf "priv(%s): %s...\n",$I,substr($bob->priv_key(),0,32); DumpFile("DH_$user.key",$keypair); $PKI->{bob}{pubkey} = $bob->pub_key(); print ".\n"; } # --------------------------------------------------------- { # Alice's my $dh = Crypt::DH->new( p => "$p", g => $g ); my $pub = $dh->generate_keys; my $keypair = { user => 'Alice', pubkey => $pub, seckey => $dh->priv_key() }; printf "%s:\n",'Alice'; printf "pub(A): %s...\n",substr($pub,0,32); printf "priv(A): %s...\n",substr($dh->priv_key(),0,32); DumpFile("DH_Alice.key",$keypair); print ".\n"; my $secret = $dh->compute_secret( $PKI->{bob}{pubkey} ); printf "Alice's secret : %s\n",$secret; $PKI->{alice}{pubkey} = $dh->pub_key(); $PKI->{alice}{secret} = $secret; } # --------------------------------------------------------- { # BOB again ... my $secret = $bob->compute_secret( $PKI->{alice}{pubkey} ); printf "Bob's secret : %s\n",$secret; die if ($secret ne $PKI->{alice}{secret}); } exit $?; # -------------------------------- sub get_IV { # set initialization vector for keypair generation my $key = shift; my ($g,$p) = (2,undef); if ($dbug == 1) { ($g,$p) = (3,107); # very bad choice but simple case to verify t +he maths ! } else { $p = &get_prime($key); } return ($g,$p); } # -------------------------------- # ------------------------------------------ sub get_prime { my $key = shift; local $/ = "\n"; my $buf = ''; while (<DATA>) { # get prime from the bottom of this file ! next if /^#/o; # skip comments next if /^__\w+__/; # & markers chomp; y/ //d; $buf .= $_; } printf "buf: %s.\n",$buf if $dbug > 1; use Math::BigInt; my $bint = Math::BigInt->from_hex($buf); if (defined $key) { use Math::Primality qw(is_prime next_prime); my $digest = &digest('RIPEMD-160',$key); $digest =~ s/ //go; my $offset = Math::BigInt->from_hex($digest); my $candidate = $bint + $offset; if ($dbug > 1) { print "hash: $digest\n"; print "cand: $candidate\n"; } if (! &is_prime($candidate)) { print " p is not a prime ... will find an other one ! \n" if $ +dbug; $bint = &next_prime($candidate); if (&is_prime($bint)) { printf "%s is now a prime\n",$bint if $dbug; } } else { $bint = $candidate; printf "%s is prime!\n",$bint; } } return $bint; } # ------------------------------------------ sub digest { use Digest qw(); my $alg = shift; my $msg = Digest->new($alg) or die $!; $msg->add(join'',@_); my $digest = lc( $msg->hexdigest() ); return $digest; # hex form ! } # ------------------------------------------ 1; __DATA__ # SSH2 prime: FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE65381 FFFFFFFF FFFFFFFF __END__

In reply to How to get good primes for DH agreement ? by iglake

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.