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 / DES3.pm < prev    next >
Encoding:
Perl POD Document  |  2001-05-02  |  3.6 KB  |  156 lines

  1. # $Id: DES3.pm,v 1.10 2001/05/02 21:58:23 btrott Exp $
  2.  
  3. package Net::SSH::Perl::Cipher::DES3;
  4.  
  5. use strict;
  6.  
  7. use Net::SSH::Perl::Cipher;
  8. use base qw( Net::SSH::Perl::Cipher );
  9.  
  10. use Net::SSH::Perl::Cipher::CBC;
  11. use Crypt::DES;
  12.  
  13. sub new {
  14.     my $class = shift;
  15.     my $ciph = bless { }, $class;
  16.     $ciph->init(@_) if @_;
  17.     $ciph;
  18. }
  19.  
  20. sub keysize { 24 }
  21. sub blocksize { 8 }
  22.  
  23. sub init {
  24.     my $ciph = shift;
  25.     my($key, $iv, $is_ssh2) = @_;
  26.     $ciph->{is_ssh2} = defined $is_ssh2 ? $is_ssh2 : 0;
  27.  
  28.     if ($is_ssh2) {
  29.         my $ede3 = Net::SSH::Perl::Cipher::DES3::EDE3->new($key);
  30.         $ciph->{cbc} = Net::SSH::Perl::Cipher::CBC->new($ede3,
  31.             substr($iv, 0, 8));
  32.     }
  33.     else {
  34.         for my $i (1..3) {
  35.             my $this_key = $i == 3 && length($key) <= 16 ?
  36.                 substr $key, 0, 8 :
  37.                 substr $key, 8*($i-1), 8;
  38.             $ciph->{"cbc$i"} = Net::SSH::Perl::Cipher::CBC->new(
  39.                 Crypt::DES->new($this_key)
  40.             );
  41.         }
  42.     }
  43. }
  44.  
  45. sub encrypt {
  46.     my($ciph, $text) = @_;
  47.     if ($ciph->{is_ssh2}) {
  48.         return $ciph->{cbc}->encrypt($text);
  49.     }
  50.     else {
  51.         return $ciph->{cbc3}->encrypt(
  52.             $ciph->{cbc2}->decrypt(
  53.                 $ciph->{cbc1}->encrypt($text)
  54.             )
  55.         );
  56.     }
  57. }
  58.  
  59. sub decrypt {
  60.     my($ciph, $text) = @_;
  61.     if ($ciph->{is_ssh2}) {
  62.         return $ciph->{cbc}->decrypt($text);
  63.     }
  64.     else {
  65.         return $ciph->{cbc1}->decrypt(
  66.             $ciph->{cbc2}->encrypt(
  67.                 $ciph->{cbc3}->decrypt($text)
  68.             )
  69.         );
  70.     }
  71. }
  72.  
  73. package Net::SSH::Perl::Cipher::DES3::EDE3;
  74. use strict;
  75.  
  76. sub new {
  77.     my $class = shift;
  78.     my $ede3 = bless {}, $class;
  79.     $ede3->init(@_) if @_;
  80.     $ede3;
  81. }
  82.  
  83. sub keysize { 24 }
  84. sub blocksize { 8 }
  85.  
  86. sub init {
  87.     my $ede3 = shift;
  88.     my($key) = @_;
  89.     for my $i (1..3) {
  90.         $ede3->{"des$i"} = Crypt::DES->new(substr $key, 8*($i-1), 8);
  91.     }
  92. }
  93.  
  94. sub encrypt {
  95.     my($ede3, $block) = @_;
  96.     $ede3->{des3}->encrypt(
  97.         $ede3->{des2}->decrypt(
  98.             $ede3->{des1}->encrypt($block)
  99.         )
  100.     );
  101. }
  102.  
  103. sub decrypt {
  104.     my($ede3, $block) = @_;
  105.     $ede3->{des1}->decrypt(
  106.         $ede3->{des2}->encrypt(
  107.             $ede3->{des3}->decrypt($block)
  108.         )
  109.     );
  110. }
  111.  
  112. 1;
  113. __END__
  114.  
  115. =head1 NAME
  116.  
  117. Net::SSH::Perl::Cipher::DES3 - Wrapper for SSH 3DES support
  118.  
  119. =head1 SYNOPSIS
  120.  
  121.     use Net::SSH::Perl::Cipher;
  122.     my $cipher = Net::SSH::Perl::Cipher->new('DES3', $key);
  123.     print $cipher->encrypt($plaintext);
  124.  
  125. =head1 DESCRIPTION
  126.  
  127. I<Net::SSH::Perl::Cipher::DES3> provides 3DES encryption
  128. support for I<Net::SSH::Perl>. To do so it wraps around
  129. I<Crypt::DES>, a C/XS implementation of the DES algorithm.
  130.  
  131. The 3DES (three-key triple-DES) algorithm used here differs
  132. based on the SSH protocol being used. SSH1 uses 3DES in
  133. inner CBC mode, meaning that there are three CBC objects,
  134. and each CBC object is paired with a DES object and key.
  135.  
  136. SSH2 uses 3DES in outer CBC mode; this uses one CBC object
  137. wrapped around a DES-EDE3 object (also included in this
  138. library); that object contains three DES ciphers with three
  139. different keys. Each encrypt operation is actually
  140. encrypt-decrypt-encrypt with the three DES keys; decrypt
  141. is actually decrypt-encrypt-decrypt with the DES keys.
  142.  
  143. The key length for both implementations is 24 bytes.
  144. The first 8 bytes of the key are used as the first DES
  145. key, the second 8 bytes for the second key, etc. If the
  146. key I<$key> that you pass to I<new> is only 16 bytes, the
  147. first 8 bytes of I<$key> will be used as the key for both
  148. the first and third DES ciphers.
  149.  
  150. =head1 AUTHOR & COPYRIGHTS
  151.  
  152. Please see the Net::SSH::Perl manpage for author, copyright,
  153. and license information.
  154.  
  155. =cut
  156.