home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / DH.pm < prev    next >
Encoding:
Perl POD Document  |  2002-01-20  |  5.2 KB  |  206 lines

  1. # $Id: DH.pm,v 1.10 2002/01/20 22:46:26 btrott Exp $
  2.  
  3. package Crypt::DH;
  4. use strict;
  5.  
  6. use Crypt::Random qw( makerandom );
  7. use Math::Pari qw( PARI floor pari2num Mod lift pari2pv );
  8.  
  9. use vars qw( $VERSION );
  10. $VERSION = '0.03';
  11.  
  12. sub new {
  13.     my $class = shift;
  14.     my $dh = bless {}, $class;
  15.     $dh->init(@_);
  16.     $dh;
  17. }
  18.  
  19. BEGIN {
  20.     no strict 'refs';
  21.     for my $meth (qw( p g pub_key priv_key )) {
  22.         *$meth = sub {
  23.             my($key, $value) = @_;
  24.             if (ref $value eq 'Math::Pari') {
  25.                 $key->{$meth} = pari2pv($value);
  26.             }
  27.             elsif ($value && !(ref $value)) {
  28.                 if ($value =~ /^0x/) {
  29.                     $key->{$meth} = pari2pv(Math::Pari::_hex_cvt($value));
  30.                 }
  31.                 else {
  32.                     $key->{$meth} = $value;
  33.                 }
  34.             }
  35.             my $ret = $key->{$meth} || "";
  36.             $ret = PARI("$ret") if $ret =~ /^\d+$/;
  37.             $ret;
  38.         };
  39.     }
  40. }
  41.  
  42. sub init {
  43.     my $dh = shift;
  44.     my %param = @_;
  45.     for my $w (qw( p g priv_key )) {
  46.         $dh->$w($param{$w});
  47.     }
  48. }
  49.  
  50. sub generate_keys {
  51.     my $dh = shift;
  52.     my $i = bitsize($dh->{p}) - 1;
  53.  
  54.     $dh->{priv_key} = makerandom(Size => $i, Strength => 0)
  55.         unless defined $dh->{priv_key};
  56.  
  57.     $dh->{pub_key} = mod_exp($dh->{g}, $dh->{priv_key}, $dh->{p});
  58. }
  59.  
  60. sub size {
  61.     my $dh = shift;
  62.     bitsize($dh->{p}) / 8;
  63. }
  64.  
  65. sub compute_key {
  66.     my $dh = shift;
  67.     my $pub_key = shift;
  68.     mod_exp($pub_key, $dh->{priv_key}, $dh->{p});
  69. }
  70.  
  71. sub bitsize {
  72.     return pari2num(floor(Math::Pari::log($_[0])/Math::Pari::log(2)) + 1);
  73. }
  74.  
  75. sub mod_exp {
  76.     my($a, $exp, $n) = @_;
  77.     my $m = Mod($a, $n);
  78.     lift($m ** $exp);
  79. }
  80.  
  81. 1;
  82. __END__
  83.  
  84. =head1 NAME
  85.  
  86. Crypt::DH - Diffie-Hellman key exchange system
  87.  
  88. =head1 SYNOPSIS
  89.  
  90.     use Crypt::DH;
  91.     my $dh = Crypt::DH->new;
  92.     $dh->g($g);
  93.     $dh->p($p);
  94.  
  95.     ## Generate public and private keys.
  96.     $dh->generate_keys;
  97.  
  98.     ## Send public key to "other" party, and receive "other"
  99.     ## public key in return.
  100.  
  101.     ## Now compute shared secret from "other" public key.
  102.     my $shared_secret = $dh->compute_key( $other_pub_key );
  103.  
  104. =head1 DESCRIPTION
  105.  
  106. I<Crypt::DH> is a Perl implementation of the Diffie-Hellman key
  107. exchange system. Diffie-Hellman is an algorithm by which two
  108. parties can agree on a shared secret key, known only to them.
  109. The secret is negotiated over an insecure network without the
  110. two parties ever passing the actual shared secret, or their
  111. private keys, between them.
  112.  
  113. =head1 THE ALGORITHM
  114.  
  115. The algorithm generally works as follows: Party A and Party B
  116. choose a property I<p> and a property I<g>; these properties are
  117. shared by both parties. Each party then computes a random private
  118. key integer I<priv_key>, where the length of I<priv_key> is at
  119. most (number of bits in I<p>) - 1. Each party then computes a
  120. public key based on I<g>, I<priv_key>, and I<p>; the exact value
  121. is
  122.  
  123.     g ^ priv_key mod p
  124.  
  125. The parties exchange these public keys.
  126.  
  127. The shared secret key is generated based on the exchanged public
  128. key, the private key, and I<p>. If the public key of Party B is
  129. denoted I<pub_key_B>, then the shared secret is equal to
  130.  
  131.     pub_key_B ^ priv_key mod p
  132.  
  133. The mathematical principles involved insure that both parties will
  134. generate the same shared secret key.
  135.  
  136. More information can be found in PKCS #3 (Diffie-Hellman Key
  137. Agreement Standard):
  138.  
  139.     http://www.rsasecurity.com/rsalabs/pkcs/pkcs-3/
  140.  
  141. =head1 USAGE
  142.  
  143. I<Crypt::DH> implements the core routines needed to use
  144. Diffie-Hellman key exchange. To actually use the algorithm,
  145. you'll need to start with values for I<p> and I<g>; I<p> is a
  146. large prime, and I<g> is a base which must be larger than 0
  147. and less than I<p>.
  148.  
  149. I<Crypt::DH> uses I<Math::Pari> internally for big-integer
  150. calculations. All accessor methods (I<p>, I<g>, I<priv_key>, and
  151. I<pub_key>) thus return I<Math::Pari> objects, as does the
  152. I<compute_key> method.
  153.  
  154. =head2 $dh = Crypt::DH->new
  155.  
  156. Constructs a new I<Crypt::DH> object and returns the object.
  157.  
  158. =head2 $dh->p([ $p ])
  159.  
  160. Given an argument I<$p>, sets the I<p> parameter (large prime)
  161. for this I<Crypt::DH> object.
  162.  
  163. Returns the current value of I<p>.
  164.  
  165. =head2 $dh->g([ $g ])
  166.  
  167. Given an argument I<$g>, sets the I<g> parameter (base) for
  168. this I<Crypt::DH> object.
  169.  
  170. Returns the current value of I<g>.
  171.  
  172. =head2 $dh->generate_keys
  173.  
  174. Generates the public and private key portions of the I<Crypt::DH>
  175. object, assuming that you've already filled I<p> and I<g> with
  176. appropriate values.
  177.  
  178. =head2 $dh->compute_key( $public_key )
  179.  
  180. Given the public key I<$public_key> of Party B (the party with which
  181. you're performing key negotiation and exchange), computes the shared
  182. secret key, based on that public key, your own private key, and your
  183. own large prime value (I<p>).
  184.  
  185. Returns the shared secret.
  186.  
  187. =head2 $dh->priv_key([ $priv_key ])
  188.  
  189. Returns the private key.  Given an argument I<$priv_key>, sets the
  190. I<priv_key> parameter for this I<Crypt::DH> object.
  191.  
  192. =head2 $dh->pub_key
  193.  
  194. Returns the public key.
  195.  
  196. =head1 AUTHOR & COPYRIGHT
  197.  
  198. Benjamin Trott, ben@rhumba.pair.com
  199.  
  200. Except where otherwise noted, Crypt::DH is Copyright 2001
  201. Benjamin Trott. All rights reserved. Crypt::DH is free
  202. software; you may redistribute it and/or modify it under
  203. the same terms as Perl itself.
  204.  
  205. =cut
  206.