tcldes.tcl

Go to the documentation of this file.
00001 /*  des.tcl*/
00002 /*  $Revision: 1.1 $*/
00003 /*  $Date: 2005/09/26 09:16:59 $*/
00004 /* */
00005 /*  Port of Javascript implementation to Tcl 8.4 by Mac A. Cody,*/
00006 /*  October, 2002 - February, 2003*/
00007 /*  August, 2003 - Separated key set generation from encryption/decryption.*/
00008 /*                 Renamed "des" procedure to "block" to differentiate from the*/
00009 /*                 "stream" procedure used for CFB and OFB modes.*/
00010 /*                 Modified the "encrypt" and "decrypt" procedures to support*/
00011 /*                 CFB and OFB modes. Changed the procedure arguments.*/
00012 /*                 Added the "stream" procedure to support CFB and OFB modes.*/
00013 /*  June, 2004 - Corrected input vector bug in stream-mode processing.  Added*/
00014 /*               support for feedback vector storage and management function.*/
00015 /*               This enables a stream of data to be processed over several calls*/
00016 /*               to the encryptor or decryptor.*/
00017 /*  September, 2004 - Added feedback vector to the CBC mode of operation to allow*/
00018 /*                    a large data set to be processed over several calls to the*/
00019 /*                    encryptor or decryptor.*/
00020 /*  October, 2004 - Added test for weak keys in the createKeys procedure.*/
00021 /* */
00022 /*  Paul Tero, July 2001*/
00023 /*  http://www.shopable.co.uk/des.html*/
00024 /* */
00025 /*  Optimised for performance with large blocks by Michael Hayworth,*/
00026 /*  November 2001, http://www.netdealing.com*/
00027 /* */
00028 /*  This software is copyrighted (c) 2003, 2004 by Mac A. Cody.  All rights*/
00029 /*  reserved.  The following terms apply to all files associated with*/
00030 /*  the software unless explicitly disclaimed in individual files or*/
00031 /*  directories.*/
00032 
00033 /*  The authors hereby grant permission to use, copy, modify, distribute,*/
00034 /*  and license this software for any purpose, provided that existing*/
00035 /*  copyright notices are retained in all copies and that this notice is*/
00036 /*  included verbatim in any distributions. No written agreement, license,*/
00037 /*  or royalty fee is required for any of the authorized uses.*/
00038 /*  Modifications to this software may be copyrighted by their authors and*/
00039 /*  need not follow the licensing terms described here, provided that the*/
00040 /*  new terms are clearly indicated on the first page of each file where*/
00041 /*  they apply.*/
00042 
00043 /*  IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY*/
00044 /*  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES*/
00045 /*  ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY*/
00046 /*  DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE*/
00047 /*  POSSIBILITY OF SUCH DAMAGE.*/
00048 
00049 /*  THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,*/
00050 /*  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,*/
00051 /*  FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE*/
00052 /*  IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE*/
00053 /*  NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR*/
00054 /*  MODIFICATIONS.*/
00055 
00056 /*  GOVERNMENT USE: If you are acquiring this software on behalf of the*/
00057 /*  U.S. government, the Government shall have only "Restricted Rights"*/
00058 /*  in the software and related documentation as defined in the Federal */
00059 /*  Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you*/
00060 /*  are acquiring the software on behalf of the Department of Defense, the*/
00061 /*  software shall be classified as "Commercial Computer Software" and the*/
00062 /*  Government shall have only "Restricted Rights" as defined in Clause*/
00063 /*  252.227-7013 (c) (1) of DFARs.  Notwithstanding the foregoing, the*/
00064 /*  authors grant the U.S. Government and others acting in its behalf*/
00065 /*  permission to use and distribute the software in accordance with the*/
00066 /*  terms specified in this license. */
00067 namespace des {
00068     variable keys = 
00069     variable WeakKeysError
00070     if {![info exists WeakKeysError]} {  WeakKeysError =  1 }
00071      keysets = (ndx) 1
00072     /*  Produre: keyset - Create or destroy a keyset created from a 64-bit*/
00073     /*                    DES key or a 192-bit 3DES key.*/
00074     /*  Inputs:*/
00075     /*    oper  : The operation to be performed.  This will be either "create"*/
00076     /*            (make a new keyset) or "destroy" (delete an existing keyset).*/
00077     /*            The meaning of the argument "value" depends of the operation*/
00078     /*            performed.  An error is generated if "oper" is not "create"*/
00079     /*            or "destroy".*/
00080     /*              */
00081     /*    value : If the argument "oper" is "create", then "value" is the 64-bit*/
00082     /*            DES key or the 192-bit 3DES key.  (Note: The lsb of each byte*/
00083     /*            is ignored; odd parity is not required).  If the argument*/
00084     /*            "oper" is "destroy", then "value" is a handle to a keyset that*/
00085     /*            was created previously.*/
00086     /* */
00087     /*    weak:   If true then weak keys are allowed. The default is to raise an*/
00088     /*            error when a weak key is seen.*/
00089     /*  Output:*/
00090     /*    If the argument "oper" is "create", then the output is a handle to the*/
00091     /*    keyset stored in the des namespace.  If the argument "oper" is*/
00092     /*    "destroy", then nothing is returned.*/
00093     ret  keyset (type oper , type value , optional weak =0) {
00094     variable keysets
00095     set newset {}
00096     switch -exact -- $oper {
00097         create {
00098         # Create a new keyset handle.
00099         set newset keyset$keysets(ndx)
00100         # Create key set
00101         set keysets($newset) [createKeys $value $weak]
00102         # Never use that keyset handle index again.
00103         incr keysets(ndx)
00104         }
00105         destroy {
00106         # Determine if the keyset handle is valid.
00107         if {[array names keysets $value] != {}} {
00108             # Delete the handle and corresponding keyset.
00109                     unset keysets($value)
00110         } else {
00111             error "The keyset handle \"$value\" is invalid!"
00112         }
00113         }
00114         default {
00115         error {The operator must be either "create" or "destroy".}
00116         }
00117     }
00118     return $newset
00119     }
00120 
00121     /*  Procedure: encrypt - Encryption front-end for the des procedure*/
00122     /*  Inputs:*/
00123     /*    keyset  : Handle to an existing keyset.*/
00124     /*    message : String to be encrypted.*/
00125     /*    mode    : DES mode ecb (default), cbc, cfb, or ofb.*/
00126     /*    iv      : Name of the initialization vector used in CBC, CFB,*/
00127     /*              and OFB modes.*/
00128     /*    kbits   : Number of bits in a data block (default of 64).*/
00129     /*  Output:*/
00130     /*    The encrypted data string.*/
00131     ret  encrypt (type keyset , type message , optional mode =ecb , optional iv ={) {kbits 64}} {
00132     switch -exact -- $mode {
00133         ecb {
00134         return [block $key $message =  1 0]
00135         }
00136         cbc -
00137         ofb -
00138         cfb {
00139         /*  Is the initialization/feedback vector variable is valid?*/
00140         if {[string length $iv] == 0} {
00141             error "An initialization variable must be specified."
00142         } else {
00143             upvar $iv ivec
00144             if {![info exists ivec]} {
00145             error "The variable $iv does not exist."
00146             }
00147         }
00148         switch -exact -- $mode {
00149             cbc {
00150             return [block $key $message =  1 1 ivec]
00151             }
00152             ofb {
00153             return [stream $key $message =  1 0 ivec $kbits]
00154             }
00155             cfb {
00156             return [stream $key $message =  1 1 ivec $kbits]
00157             }
00158         }
00159         }
00160         default {
00161         error {Mode must be ecb, cbc, cfb, or ofb.}
00162         }
00163     }
00164     }
00165 
00166     /*  Procedure: decrypt - Decryption front-end for the des procedure*/
00167     /*  Inputs:*/
00168     /*    keyset  : Handle to an existing keyset.*/
00169     /*    message : String to be decrypted.*/
00170     /*    mode    : DES mode ecb (default), cbc, cfb, or ofb.*/
00171     /*    iv      : Name of the initialization vector used in CBC, CFB,*/
00172     /*              and OFB modes.*/
00173     /*    kbits   : Number of bits in a data block (default of 64).*/
00174     /*  Output:*/
00175     /*    The encrypted or decrypted data string.*/
00176     ret  decrypt (type keyset , type message , optional mode =ecb , optional iv ={) {kbits 64}} {
00177     switch -exact -- $mode {
00178         ecb {
00179         return [block $key $message =  0 0]
00180         }
00181         cbc -
00182         ofb -
00183         cfb {
00184         /*  Is the initialization/feedback vector variable is valid?*/
00185         if {[string length $iv] < 1} {
00186             error "An initialization variable must be specified."
00187         } else {
00188             upvar $iv ivec
00189             if {![info exists ivec]} {
00190             error "The variable $iv does not exist."
00191             }
00192         }
00193         switch -exact -- $mode {
00194             cbc {
00195             return [block $key $message =  0 1 ivec]
00196             }
00197             ofb {
00198             return [stream $key $message =  0 0 ivec $kbits]
00199             }
00200             cfb {
00201             return [stream $key $message =  0 1 ivec $kbits]
00202             }
00203         }
00204         }
00205         default {
00206         error {Mode must be ecb, cbc, cfb, or ofb.}
00207         }
00208     }
00209     }
00210 
00211     variable spfunction1 [list 0x1010400 0 0x10000 0x1010404 0x1010004 0x10404 0x4 0x10000 0x400 0x1010400 0x1010404 0x400 0x1000404 0x1010004 0x1000000 0x4 0x404 0x1000400 0x1000400 0x10400 0x10400 0x1010000 0x1010000 0x1000404 0x10004 0x1000004 0x1000004 0x10004 0 0x404 0x10404 0x1000000 0x10000 0x1010404 0x4 0x1010000 0x1010400 0x1000000 0x1000000 0x400 0x1010004 0x10000 0x10400 0x1000004 0x400 0x4 0x1000404 0x10404 0x1010404 0x10004 0x1010000 0x1000404 0x1000004 0x404 0x10404 0x1010400 0x404 0x1000400 0x1000400 0 0x10004 0x10400 0 0x1010004];
00212     variable spfunction2 [list 0x80108020 0x80008000 0x8000 0x108020 0x100000 0x20 0x80100020 0x80008020 0x80000020 0x80108020 0x80108000 0x80000000 0x80008000 0x100000 0x20 0x80100020 0x108000 0x100020 0x80008020 0 0x80000000 0x8000 0x108020 0x80100000 0x100020 0x80000020 0 0x108000 0x8020 0x80108000 0x80100000 0x8020 0 0x108020 0x80100020 0x100000 0x80008020 0x80100000 0x80108000 0x8000 0x80100000 0x80008000 0x20 0x80108020 0x108020 0x20 0x8000 0x80000000 0x8020 0x80108000 0x100000 0x80000020 0x100020 0x80008020 0x80000020 0x100020 0x108000 0 0x80008000 0x8020 0x80000000 0x80100020 0x80108020 0x108000];
00213     variable spfunction3 [list 0x208 0x8020200 0 0x8020008 0x8000200 0 0x20208 0x8000200 0x20008 0x8000008 0x8000008 0x20000 0x8020208 0x20008 0x8020000 0x208 0x8000000 0x8 0x8020200 0x200 0x20200 0x8020000 0x8020008 0x20208 0x8000208 0x20200 0x20000 0x8000208 0x8 0x8020208 0x200 0x8000000 0x8020200 0x8000000 0x20008 0x208 0x20000 0x8020200 0x8000200 0 0x200 0x20008 0x8020208 0x8000200 0x8000008 0x200 0 0x8020008 0x8000208 0x20000 0x8000000 0x8020208 0x8 0x20208 0x20200 0x8000008 0x8020000 0x8000208 0x208 0x8020000 0x20208 0x8 0x8020008 0x20200];
00214     variable spfunction4 [list 0x802001 0x2081 0x2081 0x80 0x802080 0x800081 0x800001 0x2001 0 0x802000 0x802000 0x802081 0x81 0 0x800080 0x800001 0x1 0x2000 0x800000 0x802001 0x80 0x800000 0x2001 0x2080 0x800081 0x1 0x2080 0x800080 0x2000 0x802080 0x802081 0x81 0x800080 0x800001 0x802000 0x802081 0x81 0 0 0x802000 0x2080 0x800080 0x800081 0x1 0x802001 0x2081 0x2081 0x80 0x802081 0x81 0x1 0x2000 0x800001 0x2001 0x802080 0x800081 0x2001 0x2080 0x800000 0x802001 0x80 0x800000 0x2000 0x802080];
00215     variable spfunction5 [list 0x100 0x2080100 0x2080000 0x42000100 0x80000 0x100 0x40000000 0x2080000 0x40080100 0x80000 0x2000100 0x40080100 0x42000100 0x42080000 0x80100 0x40000000 0x2000000 0x40080000 0x40080000 0 0x40000100 0x42080100 0x42080100 0x2000100 0x42080000 0x40000100 0 0x42000000 0x2080100 0x2000000 0x42000000 0x80100 0x80000 0x42000100 0x100 0x2000000 0x40000000 0x2080000 0x42000100 0x40080100 0x2000100 0x40000000 0x42080000 0x2080100 0x40080100 0x100 0x2000000 0x42080000 0x42080100 0x80100 0x42000000 0x42080100 0x2080000 0 0x40080000 0x42000000 0x80100 0x2000100 0x40000100 0x80000 0 0x40080000 0x2080100 0x40000100];
00216     variable spfunction6 [list 0x20000010 0x20400000 0x4000 0x20404010 0x20400000 0x10 0x20404010 0x400000 0x20004000 0x404010 0x400000 0x20000010 0x400010 0x20004000 0x20000000 0x4010 0 0x400010 0x20004010 0x4000 0x404000 0x20004010 0x10 0x20400010 0x20400010 0 0x404010 0x20404000 0x4010 0x404000 0x20404000 0x20000000 0x20004000 0x10 0x20400010 0x404000 0x20404010 0x400000 0x4010 0x20000010 0x400000 0x20004000 0x20000000 0x4010 0x20000010 0x20404010 0x404000 0x20400000 0x404010 0x20404000 0 0x20400010 0x10 0x4000 0x20400000 0x404010 0x4000 0x400010 0x20004010 0 0x20404000 0x20000000 0x400010 0x20004010];
00217     variable spfunction7 [list 0x200000 0x4200002 0x4000802 0 0x800 0x4000802 0x200802 0x4200800 0x4200802 0x200000 0 0x4000002 0x2 0x4000000 0x4200002 0x802 0x4000800 0x200802 0x200002 0x4000800 0x4000002 0x4200000 0x4200800 0x200002 0x4200000 0x800 0x802 0x4200802 0x200800 0x2 0x4000000 0x200800 0x4000000 0x200800 0x200000 0x4000802 0x4000802 0x4200002 0x4200002 0x2 0x200002 0x4000000 0x4000800 0x200000 0x4200800 0x802 0x200802 0x4200800 0x802 0x4000002 0x4200802 0x4200000 0x200800 0 0x2 0x4200802 0 0x200802 0x4200000 0x800 0x4000002 0x4000800 0x800 0x200002];
00218     variable spfunction8 [list 0x10001040 0x1000 0x40000 0x10041040 0x10000000 0x10001040 0x40 0x10000000 0x40040 0x10040000 0x10041040 0x41000 0x10041000 0x41040 0x1000 0x40 0x10040000 0x10000040 0x10001000 0x1040 0x41000 0x40040 0x10040040 0x10041000 0x1040 0 0 0x10040040 0x10000040 0x10001000 0x41040 0x40000 0x41040 0x40000 0x10041000 0x1000 0x40 0x10040040 0x1000 0x41040 0x10001000 0x40 0x10000040 0x10040000 0x10040040 0x10000000 0x40000 0x10001040 0 0x10041040 0x40040 0x10000040 0x10040000 0x10001000 0x10001040 0 0x10041040 0x41000 0x41000 0x1040 0x1040 0x40040 0x10000000 0x10041000];
00219 
00220     variable desEncrypt {0 32 2}
00221     variable desDecrypt {30 -2 -2}
00222     variable des3Encrypt {0 32 2 62 30 -2 64 96 2}
00223     variable des3Decrypt {94 62 -2 32 64 2 30 -2 -2}
00224 
00225     /*  Procedure: block - DES ECB and CBC mode support*/
00226     /*  Inputs:*/
00227     /*    keyset   : Handle to an existing keyset.*/
00228     /*    message  : String to be encrypted or decrypted (Note: For encryption,*/
00229     /*               the string is extended with null characters to an integral*/
00230     /*               multiple of eight bytes.  For decryption, the string length*/
00231     /*               must be an integral multiple of eight bytes.*/
00232     /*    encrypt  : Perform encryption (1) or decryption (0)*/
00233     /*    mode     : DES mode 1=CBC, 0=ECB (default).*/
00234     /*    iv       : Name of the variable containing the initialization vector*/
00235     /*               used in CBC mode.  The value must be 64 bits in length.*/
00236     /*  Output:*/
00237     /*    The encrypted or decrypted data string.*/
00238     ret  block (type keyset , type message , type encrypt , optional mode =0 , optional iv ={)} {
00239     variable spfunction1
00240     variable spfunction2
00241     variable spfunction3
00242     variable spfunction4
00243     variable spfunction5
00244     variable spfunction6
00245     variable spfunction7
00246     variable spfunction8
00247     variable desEncrypt
00248     variable desDecrypt
00249     variable des3Encrypt
00250     variable des3Decrypt
00251     variable keysets
00252 
00253     # Determine if the keyset handle is valid.
00254     if {[array names keysets $keyset] != {}} {
00255         /*  Acquire the 16 or 48 subkeys we will need*/
00256          keys =  $keys = ($key)
00257     } else =  {
00258         error "The key handle =  \"$key\" is =  invalid!"
00259     }
00260      m =  0
00261      cbcleft =  0x00;  cbcleft2 =  0x00
00262      cbcright =  0x00;  cbcright2 =  0x00
00263      len =  [string length $message];
00264         if {$len == 0} {
00265             return -code error "invalid message size: the message may not be empty"
00266         }
00267      chunk =  0;
00268     /*  Set up the loops for single and triple des*/
00269      iterations =  [expr {[llength $keys] == 32 ? 3 : 9}];
00270     if {$iterations == 3} {
00271         expr {$encrypt ? [ looping =  $desEncrypt] : \
00272               [ looping =  $desDecrypt]}
00273     } else {
00274         expr {$encrypt ? [ looping =  $des3Encrypt] : \
00275               [ looping =  $des3Decrypt]}
00276     }
00277 
00278     /*  Pad the message out with null bytes.*/
00279     append message "\0\0\0\0\0\0\0\0"
00280 
00281     /*  Store the result here*/
00282      result =  {};
00283      tempresult =  {};
00284 
00285     /*  CBC mode*/
00286     if {$mode == 1} {
00287         /*  Is the initialization/feedback vector variable is valid?*/
00288         if {[string length $iv] < 1} {
00289         error "An initialization variable must be specified."
00290         } else {
00291         upvar $iv ivec
00292         if {![info exists ivec]} {
00293             error "The variable $iv does not exist."
00294         }
00295                 if {[string length $ivec] != 8} {
00296                     return -code error "invalid initialization vector size:\
00297                         the initialization vector must be 8 bytes"
00298                 }
00299         }
00300         /*  Use the input vector as the intial vector.*/
00301         binary scan $ivec H8H8 cbcleftTemp cbcrightTemp
00302          cbcleft =  "0x$cbcleftTemp"
00303          cbcright =  "0x$cbcrightTemp"
00304     }
00305 
00306     /*  Loop through each 64 bit chunk of the message*/
00307     while {$m < $len} {
00308         binary scan $message x${m}H8H8 lefttemp righttemp
00309          left =  {}
00310         append left "0x" $lefttemp
00311          right =  {}
00312         append right "0x" $righttemp
00313         incr m 8
00314 
00315         /* puts "Left start: $left";*/
00316         /* puts "Right start: $right";*/
00317         /*  For Cipher Block Chaining mode, xor the*/
00318         /*  message with the previous result.*/
00319         if {$mode == 1} {
00320         if {$encrypt} {
00321              left =  [expr {$left ^ $cbcleft}]
00322              right =  [expr {$right ^ $cbcright}]
00323         } else {
00324              cbcleft2 =  $cbcleft;
00325              cbcright2 =  $cbcright;
00326              cbcleft =  $left;
00327              cbcright =  $right;
00328         }
00329         }
00330 
00331         /* puts "Left mode: $left";*/
00332         /* puts "Right mode: $right";*/
00333         /* puts "cbcleft: $cbcleft";*/
00334         /* puts "cbcleft2: $cbcleft2";*/
00335         /* puts "cbcright: $cbcright";*/
00336         /* puts "cbcright2: $cbcright2";*/
00337 
00338         /*  First each 64 but chunk of the message*/
00339         /*  must be permuted according to IP.*/
00340          temp =  [expr {(($left >> 4) ^ $right) & 0x0f0f0f0f}];
00341          right =  [expr {$right ^ $temp}];
00342          left =  [expr {$left ^ ($temp << 4)}];
00343          temp =  [expr {(($left >> 16) ^ $right) & 0x0000ffff}];
00344          right =  [expr {$right ^ $temp}];
00345          left =  [expr {$left ^ ($temp << 16)}];
00346          temp =  [expr {(($right >> 2) ^ $left) & 0x33333333}];
00347          left =  [expr {$left ^ $temp}]
00348          right =  [expr {$right ^ ($temp << 2)}];
00349 
00350          temp =  [expr {(($right >> 8) ^ $left) & 0x00ff00ff}];
00351          left =  [expr {$left ^ $temp}];
00352          right =  [expr {$right ^ ($temp << 8)}];
00353          temp =  [expr {(($left >> 1) ^ $right) & 0x55555555}];
00354          right =  [expr {$right ^ $temp}];
00355          left =  [expr {$left ^ ($temp << 1)}];
00356 
00357          left =  [expr {((($left << 1) & 0xffffffff) | \
00358                  (($left >> 31) & 0x00000001))}]; 
00359          right =  [expr {((($right << 1) & 0xffffffff) | \
00360                   (($right >> 31) & 0x00000001))}]; 
00361 
00362         /* puts "Left IP: [format %x $left]";*/
00363         /* puts "Right IP: [format %x $right]";*/
00364 
00365         /*  Do this either 1 or 3 times for each chunk of the message*/
00366         for { j =  0} {$j < $iterations} {incr j 3} {
00367          endloop =  [lindex $looping [expr {$j + 1}]];
00368          loopinc =  [lindex $looping [expr {$j + 2}]];
00369 
00370         /* puts "endloop: $endloop";*/
00371         /* puts "loopinc: $loopinc";*/
00372 
00373         /*  Now go through and perform the encryption or decryption  */
00374         for { i =  [lindex $looping $j]} \
00375             {$i != $endloop} {incr i $loopinc} {
00376             /*  For efficiency*/
00377              right1 =  [expr {$right ^ [lindex $keys $i]}]; 
00378              right2 =  [expr {((($right >> 4) & 0x0fffffff) | \
00379                        (($right << 28) & 0xffffffff)) ^ \
00380                       [lindex $keys [expr {$i + 1}]]}];
00381  
00382             /*  puts "right1: [format %x $right1]";*/
00383             /*  puts "right2: [format %x $right2]";*/
00384 
00385             /*  The result is attained by passing these*/
00386             /*  bytes through the S selection functions.*/
00387              temp =  $left;
00388              left =  $right;
00389              right =  [expr {$temp ^ ([lindex $spfunction2 [expr {($right1 >> 24) & 0x3f}]] | \
00390                                                   [lindex $spfunction4 [expr {($right1 >> 16) & 0x3f}]] | \
00391                                                   [lindex $spfunction6 [expr {($right1 >>  8) & 0x3f}]] | \
00392                                                   [lindex $spfunction8 [expr {$right1 & 0x3f}]] | \
00393                                                   [lindex $spfunction1 [expr {($right2 >> 24) & 0x3f}]] | \
00394                                                   [lindex $spfunction3 [expr {($right2 >> 16) & 0x3f}]] | \
00395                                                   [lindex $spfunction5 [expr {($right2 >>  8) & 0x3f}]] | \
00396                           [lindex $spfunction7 [expr {$right2 & 0x3f}]])}];
00397  
00398             /*  puts "Left iter: [format %x $left]";*/
00399             /*  puts "Right iter: [format %x $right]";*/
00400 
00401         }
00402          temp =  $left;
00403          left =  $right;
00404          right =  $temp; /*  Unreverse left and right*/
00405         }; /*  For either 1 or 3 iterations*/
00406 
00407         /* puts "Left Iterated: [format %x $left]";*/
00408         /* puts "Right Iterated: [format %x $right]";*/
00409 
00410         /*  Move then each one bit to the right*/
00411          left =  [expr {((($left >> 1) & 0x7fffffff) \
00412                  | (($left << 31) & 0xffffffff))}]; 
00413          right =  [expr {((($right >> 1) & 0x7fffffff) \
00414                   | (($right << 31) & 0xffffffff))}]; 
00415 
00416         /* puts "Left shifted: [format %x $left]";*/
00417         /* puts "Right shifted: [format %x $right]";*/
00418 
00419         /*  Now perform IP-1, which is IP in the opposite direction*/
00420          temp =  [expr {((($left >> 1) & 0x7fffffff) ^ $right) & 0x55555555}];
00421          right =  [expr {$right ^ $temp}];
00422          left =  [expr {$left ^ ($temp << 1)}];
00423          temp =  [expr {((($right >> 8) & 0x00ffffff) ^ $left) & 0x00ff00ff}];
00424          left =  [expr {$left ^ $temp}];
00425          right =  [expr {$right ^ ($temp << 8)}];
00426          temp =  [expr {((($right >> 2) & 0x3fffffff) ^ $left) & 0x33333333}]; 
00427          left =  [expr {$left ^ $temp}];
00428          right =  [expr {$right ^ ($temp << 2)}];
00429          temp =  [expr {((($left >> 16) & 0x0000ffff) ^ $right) & 0x0000ffff}];
00430          right =  [expr {$right ^ $temp}];
00431          left =  [expr {$left ^ ($temp << 16)}];
00432          temp =  [expr {((($left >> 4) & 0x0fffffff) ^ $right) & 0x0f0f0f0f}];
00433          right =  [expr {$right ^ $temp}];
00434          left =  [expr {$left ^ ($temp << 4)}];
00435 
00436         /* puts "Left IP-1: [format %x $left]";*/
00437         /* puts "Right IP-1: [format %x $right]";*/
00438 
00439         /*  For Cipher Block Chaining mode, xor*/
00440         /*  the message with the previous result.*/
00441         if {$mode == 1} {
00442         if {$encrypt} {
00443              cbcleft =  $left;
00444              cbcright =  $right;
00445         } else {
00446              left =  [expr {$left ^ $cbcleft2}];
00447              right =  [expr {$right ^ $cbcright2}];
00448         }
00449         }
00450 
00451         append tempresult \
00452         [binary format H16 [format %08x%08x $left $right]]
00453 
00454         /* puts "Left final: [format %x $left]";*/
00455         /* puts "Right final: [format %x $right]";*/
00456 
00457         incr chunk 8;
00458         if {$chunk == 512} {
00459         append result $tempresult
00460          tempresult =  {};
00461          chunk =  0;
00462         }
00463     }; /*  For every 8 characters, or 64 bits in the message*/
00464 
00465     if {$mode == 1} {
00466         if {$encrypt} {
00467         /*  Save the left and right registers to the feedback vector.*/
00468          ivec =  [binary format H* \
00469                   [format %08x $left][format %08x $right]]
00470         } else {
00471          ivec =  [binary format H* \
00472                   [format %08x $cbcleft][format %08x $cbcright]]
00473         }
00474     }
00475 
00476     /*  Return the result as an array*/
00477     return ${result}$tempresult
00478     }; /*  End of block*/
00479 
00480     /*  Procedure: stream - DES CFB and OFB mode support*/
00481     /*  Inputs:*/
00482     /*    keyset   : Handle to an existing keyset.*/
00483     /*    message  : String to be encrypted or decrypted (Note: The length of the*/
00484     /*               string is dependent upon the value of kbits.  Remember that*/
00485     /*               the string is part of a stream of data, so it must be sized*/
00486     /*               properly for subsequent encryptions/decryptions to be*/
00487     /*               correct.  See the man page for correct message lengths for*/
00488     /*               values of kbits).*/
00489     /*    encrypt  : Perform encryption (1) or decryption (0)*/
00490     /*    mode     : DES mode 0=OFB, 1=CFB.*/
00491     /*    iv       : Name of variable containing the initialization vector.  The*/
00492     /*               value must be 64 bits in length with the first 64-L bits set*/
00493     /*               to zero.*/
00494     /*    kbits    : Number of bits in a data block (default of 64).*/
00495     /*  Output:*/
00496     /*    The encrypted or decrypted data string.*/
00497     ret  stream (type keyset , type message , type encrypt , type mode , type iv , optional kbits =64) {
00498     variable spfunction1
00499     variable spfunction2
00500     variable spfunction3
00501     variable spfunction4
00502     variable spfunction5
00503     variable spfunction6
00504     variable spfunction7
00505     variable spfunction8
00506     variable desEncrypt
00507     variable des3Encrypt
00508     variable keysets
00509 
00510     # Determine if the keyset handle is valid.
00511     if {[array names keysets $keyset] != {}} {
00512         # Acquire the 16 or 48 subkeys we will need.
00513         set keys $keysets($keyset)
00514     } else {
00515         error "The keyset handle \"$keyset\" is invalid!"
00516     }
00517 
00518     # Is the initialization/feedback vector variable is valid?
00519     if {[string length $iv] < 1} {
00520         error "An initialization variable must be specified."
00521     } else {
00522         upvar $iv ivec
00523         if {![info exists ivec]} {
00524         error "The variable $iv does not exist."
00525         }
00526     }
00527 
00528         # Determine if message length (in bits)
00529     # is not an integral number of kbits.
00530     set len [string length $message];
00531         #puts "len: $len, kbits: $kbits"
00532     if {($kbits < 1) || ($kbits > 64)} {
00533         error "The valid values of kbits are 1 through 64."
00534         } elseif {($kbits % 8) != 0} {
00535         set blockSize [expr {$kbits + (8 - ($kbits % 8))}]
00536         set fail [expr {(($len * 8) / $blockSize) % $kbits}]
00537     } else {
00538         set blockSize [expr {$kbits / 8}]
00539         set fail [expr {$len % $blockSize}]
00540     }
00541         if {$fail} {
00542         error "Data length (in bits) is not an integral number of kbits."
00543     }
00544 
00545     set m 0
00546     set n 0
00547     set chunk 0;
00548     # Set up the loops for single and triple des
00549     set iterations [expr {[llength $keys] == 32 ? 3 : 9}];
00550     if {$iterations == 3} {
00551         set looping $desEncrypt
00552     } else {
00553         set looping $des3Encrypt
00554     }
00555 
00556         # Set up shifting values.  Used for both CFB and OFB modes.
00557         if {$kbits < 32} {
00558         # Only some bits from left output are needed.
00559         set kOutShift [expr {32 - $kbits}]
00560         set kOutMask [expr {0x7fffffff >> (31 - $kbits)}]
00561         # Determine number of message bytes needed per iteration.
00562         set msgBytes [expr {int(ceil(double($kbits) / 8.0))}]
00563         # Determine number of message bits needed per iteration.
00564         set msgBits [expr {$msgBytes * 8}]
00565         set msgBitsSub1 [expr {$msgBits - 1}]
00566         # Define bit caches.
00567         set bitCacheIn {}
00568         set bitCacheOut {}
00569         # Variable used to remove bits 0 through
00570         # kbits-1 in the input bit cache.
00571         set kbitsSub1 [expr {$kbits - 1}]
00572         # Variable used to remove leading dummy binary bits.
00573         set xbits [expr {32 - $kbits}]
00574     } elseif {$kbits == 32} {
00575         # Only bits of left output are used.
00576         # Four messages bytes are needed per iteration.
00577         set msgBytes 4
00578         set xbits 32
00579     } elseif {$kbits < 64} {
00580         # All bits from left output are needed.
00581         set kOutShiftLeft [expr {$kbits - 32}]
00582         # Some bits from right output are needed.
00583         set kOutShiftRight [expr {64 - $kbits}]
00584         set kOutMaskRight [expr {0x7fffffff >> (63 - $kbits)}]
00585         # Determine number of message bytes needed per iteration.
00586         set msgBytes [expr {int(ceil(double($kbits) / 8.0))}]
00587         # Determine number of message bits needed per iteration.
00588         set msgBits [expr {$msgBytes * 8}]
00589         set msgBitsSub1 [expr {$msgBits - 1}]
00590         # Define bit caches.
00591         set bitCacheIn {}
00592         set bitCacheOut {}
00593         # Variable used to remove bits 0 through
00594         # kbits-1 in the input bit cache.
00595         set kbitsSub1 [expr {$kbits - 1}]
00596         # Variable used to remove leading dummy binary bits.
00597         set xbits [expr {64 - $kbits}]
00598     } else {
00599         # All 64 bits of output are used.
00600         # Eight messages bytes are needed per iteration.
00601         set msgBytes 8
00602         set xbits 0
00603     }
00604 
00605     # Store the result here
00606     set result {}
00607     set tempresult {}
00608 
00609     # Set up the initialization vector bitstream
00610     binary scan $ivec H8H8 leftTemp rightTemp
00611     set left "0x$leftTemp"
00612     set right "0x$rightTemp"
00613         #puts "Retrieved Feedback vector: $fbvec"
00614         #puts "Start: |$left| |$right|"
00615     
00616     # Loop through each 64 bit chunk of the message
00617     while {$m < $len} {
00618         # puts "Left start: $left";
00619         # puts "Right start: $right";
00620 
00621         # First each 64 but chunk of the
00622         # message must be permuted according to IP.
00623         set temp [expr {(($left >> 4) ^ $right) & 0x0f0f0f0f}];
00624         set right [expr {$right ^ $temp}];
00625         set left [expr {$left ^ ($temp << 4)}];
00626         set temp [expr {(($left >> 16) ^ $right) & 0x0000ffff}];
00627         set right [expr {$right ^ $temp}];
00628         set left [expr {$left ^ ($temp << 16)}];
00629         set temp [expr {(($right >> 2) ^ $left) & 0x33333333}];
00630         set left [expr {$left ^ $temp}];
00631         set right [expr {$right ^ ($temp << 2)}];
00632 
00633         set temp [expr {(($right >> 8) ^ $left) & 0x00ff00ff}];
00634         set left [expr {$left ^ $temp}];
00635         set right [expr {$right ^ ($temp << 8)}];
00636         set temp [expr {(($left >> 1) ^ $right) & 0x55555555}];
00637         set right [expr {$right ^ $temp}];
00638         set left [expr {$left ^ ($temp << 1)}];
00639 
00640         set left [expr {((($left << 1) & 0xffffffff) | \
00641                  (($left >> 31) & 0x00000001))}]; 
00642         set right [expr {((($right << 1) & 0xffffffff) | \
00643                   (($right >> 31) & 0x00000001))}]; 
00644 
00645         #puts "Left IP: [format %x $left]";
00646         #puts "Right IP: [format %x $right]";
00647 
00648         # Do this either 1 or 3 times for each chunk of the message
00649         for {set j 0} {$j < $iterations} {incr j 3} {
00650         set endloop [lindex $looping [expr {$j + 1}]];
00651         set loopinc [lindex $looping [expr {$j + 2}]];
00652 
00653         #puts "endloop: $endloop";
00654         #puts "loopinc: $loopinc";
00655 
00656         # Now go through and perform the encryption or decryption  
00657         for {set i [lindex $looping $j]} \
00658             {$i != $endloop} {incr i $loopinc} {
00659             # For efficiency
00660             set right1 [expr {$right ^ [lindex $keys $i]}]; 
00661             set right2 [expr {((($right >> 4) & 0x0fffffff) | \
00662                        (($right << 28) & 0xffffffff)) ^ \
00663                       [lindex $keys [expr {$i + 1}]]}];
00664  
00665             # puts "right1: [format %x $right1]";
00666             # puts "right2: [format %x $right2]";
00667 
00668             # The result is attained by passing these
00669             # bytes through the S selection functions.
00670             set temp $left;
00671             set left $right;
00672             set right [expr {$temp ^ ([lindex $spfunction2 [expr {($right1 >> 24) & 0x3f}]] | \
00673                           [lindex $spfunction4 [expr {($right1 >> 16) & 0x3f}]] | \
00674                           [lindex $spfunction6 [expr {($right1 >>  8) & 0x3f}]] | \
00675                           [lindex $spfunction8 [expr {$right1 & 0x3f}]] | \
00676                           [lindex $spfunction1 [expr {($right2 >> 24) & 0x3f}]] | \
00677                           [lindex $spfunction3 [expr {($right2 >> 16) & 0x3f}]] | \
00678                           [lindex $spfunction5 [expr {($right2 >>  8) & 0x3f}]] | \
00679                           [lindex $spfunction7 [expr {$right2 & 0x3f}]])}];
00680  
00681             # puts "Left iter: [format %x $left]";
00682             # puts "Right iter: [format %x $right]";
00683 
00684         }
00685         set temp $left;
00686         set left $right;
00687         set right $temp; # Unreverse left and right
00688         }; # For either 1 or 3 iterations
00689 
00690         #puts "Left Iterated: [format %x $left]";
00691         #puts "Right Iterated: [format %x $right]";
00692 
00693         # Move then each one bit to the right
00694         set left [expr {((($left >> 1) & 0x7fffffff) | \
00695                  (($left << 31) & 0xffffffff))}]; 
00696         set right [expr {((($right >> 1) & 0x7fffffff) | \
00697                   (($right << 31) & 0xffffffff))}]; 
00698 
00699         #puts "Left shifted: [format %x $left]";
00700         #puts "Right shifted: [format %x $right]";
00701 
00702         # Now perform IP-1, which is IP in the opposite direction
00703         set temp [expr {((($left >> 1) & 0x7fffffff) ^ $right) & 0x55555555}];
00704         set right [expr {$right ^ $temp}];
00705         set left [expr {$left ^ ($temp << 1)}];
00706         set temp [expr {((($right >> 8) & 0x00ffffff) ^ $left) & 0x00ff00ff}];
00707         set left [expr {$left ^ $temp}];
00708         set right [expr {$right ^ ($temp << 8)}];
00709         set temp [expr {((($right >> 2) & 0x3fffffff) ^ $left) & 0x33333333}]; 
00710         set left [expr {$left ^ $temp}];
00711         set right [expr {$right ^ ($temp << 2)}];
00712         set temp [expr {((($left >> 16) & 0x0000ffff) ^ $right) & 0x0000ffff}];
00713         set right [expr {$right ^ $temp}];
00714         set left [expr {$left ^ ($temp << 16)}];
00715         set temp [expr {((($left >> 4) & 0x0fffffff) ^ $right) & 0x0f0f0f0f}];
00716         set right [expr {$right ^ $temp}];
00717         set left [expr {$left ^ ($temp << 4)}];
00718 
00719         #puts "Left IP-1: [format %x $left]";
00720         #puts "Right IP-1: [format %x $right]";
00721 
00722         # Extract the "kbits" most significant bits from the output block.
00723         if {$kbits < 32} {
00724         # Only some bits from left output are needed.
00725         set kData [expr {($left >> $kOutShift) & $kOutMask}]
00726         set newBits {}
00727         # If necessary, copy message bytes into input bit cache.
00728         if {([string length $bitCacheIn] < $kbits) && ($n < $len)} {
00729             if {$len - $n < $msgBytes} {
00730             set lastBits [expr {($len - $n) * 8}]
00731             ###puts -nonewline [binary scan $message x${n}B$lastBits newBits]
00732             binary scan $message x${n}B$lastBits newBits
00733             } else {
00734             # Extract "msgBytes" whole bytes as bits
00735             ###puts -nonewline [binary scan $message x${n}B$msgBits newBits]
00736             binary scan $message x${n}B$msgBits newBits
00737             }
00738             incr n $msgBytes
00739             #puts " $newBits  $n [expr {$len - $n}]"
00740             # Add the bits to the input bit cache.
00741             append bitCacheIn $newBits
00742         }
00743         #puts -nonewline "In bit cache: $bitCacheIn"
00744         # Set up message data from input bit cache.
00745         binary scan [binary format B32 [format %032s [string range $bitCacheIn 0 $kbitsSub1]]] H8 temp
00746         set msgData "0x$temp"
00747         # Mix message bits with crypto bits.
00748         set mixData [expr {$msgData ^ $kData}]
00749         # Discard collected bits from the input bit cache.
00750         set bitCacheIn [string range $bitCacheIn $kbits end]
00751         #puts "  After: $bitCacheIn"
00752         # Convert back to a bit stream and append to the output bit cache.
00753         # Only the lower kbits are wanted.
00754         binary scan [binary format H8 [format %08x $mixData]] B32 msgOut
00755         append bitCacheOut [string range $msgOut $xbits end]
00756         #puts -nonewline "Out bit cache: $bitCacheOut"
00757         # If there are sufficient bits, move bytes to the temporary holding string.
00758         if {[string length $bitCacheOut] >= $msgBits} {
00759             append tempresult [binary format B$msgBits [string range $bitCacheOut 0 $msgBitsSub1]]
00760             set bitCacheOut [string range $bitCacheOut $msgBits end]
00761                     #puts -nonewline "  After: $bitCacheOut"
00762             incr m $msgBytes
00763             ###puts "$m bytes output"
00764             incr chunk $msgBytes
00765         }
00766         #puts ""
00767         # For CFB mode
00768         if {$mode == 1} {
00769             if {$encrypt} {
00770             set temp [expr {($right << $kbits) & 0xffffffff}]
00771             set left [expr {(($left << $kbits) & 0xffffffff) | (($right >> $kOutShift) & $kOutMask)}]
00772             set right [expr {$temp | $mixData}]
00773             } else {
00774             set temp [expr {($right << $kbits) & 0xffffffff}]
00775             set left [expr {(($left << $kbits) & 0xffffffff) | (($right >> $kOutShift) & $kOutMask)}]
00776             set right [expr {$temp | $msgData}]
00777             }
00778         }
00779         } elseif {$kbits == 32} {
00780         # Only bits of left output are used.
00781         set kData $left
00782         # Four messages bytes are needed per iteration.
00783         binary scan $message x${m}H8 temp
00784         incr m 4
00785         incr chunk 4
00786         set msgData "0x$temp"
00787         # Mix message bits with crypto bits.
00788         set mixData [expr {$msgData ^ $kData}]
00789         # Move bytes to the temporary holding string.
00790         append tempresult [binary format H8 [format %08x $mixData]]
00791         # For CFB mode
00792         if {$mode == 1} {
00793             set left $right
00794             if {$encrypt} {
00795             set right $mixData
00796             } else {
00797             set right $msgData
00798             }
00799         }
00800         } elseif {$kbits < 64} {
00801         set kDataLeft [expr {($left >> $kOutShiftRight) & $kOutMaskRight}]
00802         set temp [expr {($left << $kOutShiftLeft) & 0xffffffff}]
00803         set kDataRight [expr {(($right >> $kOutShiftRight) & $kOutMaskRight) | $temp}]
00804         # If necessary, copy message bytes into input bit cache.
00805         if {([string length $bitCacheIn] < $kbits)  && ($n < $len)} {
00806             if {$len - $n < $msgBytes} {
00807             set lastBits [expr {($len - $n) * 8}]
00808             ###puts -nonewline [binary scan $message x${n}B$lastBits newBits]
00809             binary scan $message x${n}B$lastBits newBits
00810             } else {
00811             # Extract "msgBytes" whole bytes as bits
00812             ###puts -nonewline [binary scan $message x${n}B$msgBits newBits]
00813             binary scan $message x${n}B$msgBits newBits
00814             }
00815             incr n $msgBytes
00816             # Add the bits to the input bit cache.
00817             append bitCacheIn $newBits
00818         }
00819         # Set up message data from input bit cache.
00820         # puts "Bits from cache: [set temp [string range $bitCacheIn 0 $kbitsSub1]]"
00821         # puts "Length of bit string: [string length $temp]"
00822         binary scan [binary format B64 [format %064s [string range $bitCacheIn 0 $kbitsSub1]]] H8H8 leftTemp rightTemp
00823         set msgDataLeft "0x$leftTemp"
00824         set msgDataRight "0x$rightTemp"
00825         # puts "msgDataLeft: $msgDataLeft"
00826         # puts "msgDataRight: $msgDataRight"
00827         # puts "kDataLeft: [format 0x%08x $kDataLeft]"
00828         # puts "kDataRight: [format 0x%08x $kDataRight]"
00829         # Mix message bits with crypto bits.
00830         set mixDataLeft [expr {$msgDataLeft ^ $kDataLeft}]
00831         set mixDataRight [expr {$msgDataRight ^ $kDataRight}]
00832         # puts "mixDataLeft: $mixDataLeft"
00833         # puts "mixDataRight: $mixDataRight"
00834         # puts "mixDataLeft: [format 0x%08x $mixDataLeft]"
00835         # puts "mixDataRight: [format 0x%08x $mixDataRight]"
00836         # Discard collected bits from the input bit cache.
00837         set bitCacheIn [string range $bitCacheIn $kbits end]
00838         # Convert back to a bit stream and
00839         # append to the output bit cache.
00840         # Only the lower kbits are wanted.
00841         binary scan \
00842             [binary format H8H8 \
00843              [format %08x $mixDataLeft] \
00844              [format %08x $mixDataRight]] B64 msgOut
00845         append bitCacheOut [string range $msgOut $xbits end]
00846         # If there are sufficient bits, move
00847         # bytes to the temporary holding string.
00848         if {[string length $bitCacheOut] >= $msgBits} {
00849             append tempresult \
00850             [binary format B$msgBits \
00851                  [string range $bitCacheOut 0 $msgBitsSub1]]
00852             set bitCacheOut [string range $bitCacheOut $msgBits end]
00853             incr m $msgBytes
00854             incr chunk $msgBytes
00855         }
00856         # For CFB mode
00857         if {$mode == 1} {
00858             if {$encrypt} {
00859             set temp \
00860                 [expr {($right << $kOutShiftRight) & 0xffffffff}]
00861             set left [expr {$temp | $mixDataLeft}]
00862             set right $mixDataRight
00863             } else {
00864             set temp \
00865                 [expr {($right << $kOutShiftRight) & 0xffffffff}]
00866             set left [expr {$temp | $msgDataLeft}]
00867             set right $msgDataRight
00868             }
00869         }
00870         } else {
00871         # All 64 bits of output are used.
00872         set kDataLeft $left
00873         set kDataRight $right
00874         # Eight messages bytes are needed per iteration.
00875         binary scan $message x${m}H8H8 leftTemp rightTemp
00876         incr m 8
00877         incr chunk 8
00878         set msgDataLeft "0x$leftTemp"
00879         set msgDataRight "0x$rightTemp"
00880         # Mix message bits with crypto bits.
00881         set mixDataLeft [expr {$msgDataLeft ^ $kDataLeft}]
00882         set mixDataRight [expr {$msgDataRight ^ $kDataRight}]
00883         # Move bytes to the temporary holding string.
00884         append tempresult \
00885             [binary format H16 \
00886              [format %08x%08x $mixDataLeft $mixDataRight]]
00887         # For CFB mode
00888         if {$mode == 1} {
00889             if {$encrypt} {
00890             set left $mixDataLeft
00891             set right $mixDataRight
00892             } else {
00893             set left $msgDataLeft
00894             set right $msgDataRight
00895             }
00896         }
00897         }
00898 
00899         #puts "Left final: [format %x $left]";
00900         #puts "Right final: [format %x $right]";
00901 
00902         if {$chunk >= 512} {
00903         append result $tempresult
00904         set tempresult {};
00905         set chunk 0;
00906         }
00907     }; # For every 8 characters, or 64 bits in the message
00908         #puts "End: |[format 0x%08x $left]| |[format 0x%08x $right]|"
00909     # Save the left and right registers to the feedback vector.
00910     set ivec [binary format H* [format %08x $left][format %08x $right]]
00911     #puts "Saved Feedback vector: $fbvectors($fbvector)"
00912 
00913         append result $tempresult
00914     if {[string length $result] > $len} {
00915         set result [string replace $result $len end]
00916     }
00917     # Return the result as an array
00918     return $result
00919     }; /*  End of stream*/
00920 
00921     variable pc2bytes0 [list 0 0x4 0x20000000 0x20000004 0x10000 0x10004 0x20010000 0x20010004 0x200 0x204 0x20000200 0x20000204 0x10200 0x10204 0x20010200 0x20010204]
00922     variable pc2bytes1 [list 0 0x1 0x100000 0x100001 0x4000000 0x4000001 0x4100000 0x4100001 0x100 0x101 0x100100 0x100101 0x4000100 0x4000101 0x4100100 0x4100101]
00923     variable pc2bytes2 [list 0 0x8 0x800 0x808 0x1000000 0x1000008 0x1000800 0x1000808 0 0x8 0x800 0x808 0x1000000 0x1000008 0x1000800 0x1000808]
00924     variable pc2bytes3 [list 0 0x200000 0x8000000 0x8200000 0x2000 0x202000 0x8002000 0x8202000 0x20000 0x220000 0x8020000 0x8220000 0x22000 0x222000 0x8022000 0x8222000]
00925     variable pc2bytes4 [list 0 0x40000 0x10 0x40010 0 0x40000 0x10 0x40010 0x1000 0x41000 0x1010 0x41010 0x1000 0x41000 0x1010 0x41010]
00926     variable pc2bytes5 [list 0 0x400 0x20 0x420 0 0x400 0x20 0x420 0x2000000 0x2000400 0x2000020 0x2000420 0x2000000 0x2000400 0x2000020 0x2000420]
00927     variable pc2bytes6 [list 0 0x10000000 0x80000 0x10080000 0x2 0x10000002 0x80002 0x10080002 0 0x10000000 0x80000 0x10080000 0x2 0x10000002 0x80002 0x10080002]
00928     variable pc2bytes7 [list 0 0x10000 0x800 0x10800 0x20000000 0x20010000 0x20000800 0x20010800 0x20000 0x30000 0x20800 0x30800 0x20020000 0x20030000 0x20020800 0x20030800]
00929     variable pc2bytes8 [list 0 0x40000 0 0x40000 0x2 0x40002 0x2 0x40002 0x2000000 0x2040000 0x2000000 0x2040000 0x2000002 0x2040002 0x2000002 0x2040002]
00930     variable pc2bytes9 [list 0 0x10000000 0x8 0x10000008 0 0x10000000 0x8 0x10000008 0x400 0x10000400 0x408 0x10000408 0x400 0x10000400 0x408 0x10000408]
00931     variable pc2bytes10 [list 0 0x20 0 0x20 0x100000 0x100020 0x100000 0x100020 0x2000 0x2020 0x2000 0x2020 0x102000 0x102020 0x102000 0x102020]
00932     variable pc2bytes11 [list 0 0x1000000 0x200 0x1000200 0x200000 0x1200000 0x200200 0x1200200 0x4000000 0x5000000 0x4000200 0x5000200 0x4200000 0x5200000 0x4200200 0x5200200]
00933     variable pc2bytes12 [list 0 0x1000 0x8000000 0x8001000 0x80000 0x81000 0x8080000 0x8081000 0x10 0x1010 0x8000010 0x8001010 0x80010 0x81010 0x8080010 0x8081010]
00934     variable pc2bytes13 [list 0 0x4 0x100 0x104 0 0x4 0x100 0x104 0x1 0x5 0x101 0x105 0x1 0x5 0x101 0x105]
00935 
00936     /*  Now define the left shifts which need to be done*/
00937     variable shifts {0  0  1  1  1  1  1  1  0  1  1  1  1  1  1  0};
00938 
00939     /*  Procedure: createKeys*/
00940     /*  Input:*/
00941     /*    key     : The 64-bit DES key or the 192-bit 3DES key*/
00942     /*              (Note: The lsb of each byte is ignored; odd parity*/
00943     /*              is not required).*/
00944     /* */
00945     /*    weak:   If true then weak keys are allowed. The default is to raise an*/
00946     /*            error when a weak key is seen.*/
00947     /*  Output:*/
00948     /*  The 16 (DES) or 48 (3DES) subkeys.*/
00949     ret  createKeys (type key , optional weak =0) {
00950     variable pc2bytes0
00951     variable pc2bytes1
00952     variable pc2bytes2
00953     variable pc2bytes3
00954     variable pc2bytes4
00955     variable pc2bytes5
00956     variable pc2bytes6
00957     variable pc2bytes7
00958     variable pc2bytes8
00959     variable pc2bytes9
00960     variable pc2bytes10
00961     variable pc2bytes11
00962     variable pc2bytes12
00963     variable pc2bytes13
00964     variable shifts
00965 
00966     # How many iterations (1 for des, 3 for triple des)
00967     set iterations [expr {([string length $key] >= 24) ? 3 : 1}];
00968     # Stores the return keys
00969     set keys {}
00970     # Other variables
00971     set lefttemp {}; set righttemp {}
00972     set m 0
00973     # Either 1 or 3 iterations
00974     for {set j 0} {$j < $iterations} {incr j} {
00975         binary scan $key x${m}H8H8 lefttemp righttemp
00976         set left {}
00977         append left "0x" $lefttemp
00978         set right {}
00979         append right "0x" $righttemp
00980         incr m 8
00981 
00982         #puts "Left key: $left"
00983         #puts "Right key: $right"
00984 
00985         # Test for weak keys
00986             if {! $weak} {
00987                 set maskedLeft [expr {$left & 0xfefefefe}]
00988                 set maskedRight [expr {$right & 0xfefefefe}]
00989                 if {($maskedLeft == 0x00000000) \
00990                         && ($maskedRight == 0x00000000)} {
00991                     error "Key [expr {$j + 1}] is weak!"
00992                 } elseif {($maskedLeft == 0x1e1e1e1e) \
00993                               && ($maskedRight == 0x0e0e0e0e)} {
00994                     error "Key [expr {$j + 1}] is weak!"
00995                 } elseif {($maskedLeft == 0xe0e0e0e0) \
00996                               && ($maskedRight == 0xf0f0f0f0)} {
00997                     error "Key [expr {$j + 1}] is weak!"
00998                 } elseif {($maskedLeft == 0xfefefefe) \
00999                               && ($maskedRight == 0xfefefefe)} {
01000                     error "Key [expr {$j + 1}] is weak!"
01001                 }
01002             }
01003 
01004         set temp [expr {(($left >> 4) ^ $right) & 0x0f0f0f0f}]
01005         set right [expr {$right ^ $temp}]
01006         set left [expr {$left ^ ($temp << 4)}]
01007         set temp [expr {(($right >> 16) ^ $left) & 0x0000ffff}]
01008         set left [expr {$left ^ $temp}]
01009         set right [expr {$right ^ ($temp << 16)}]
01010         set temp [expr {(($left >> 2) ^ $right) & 0x33333333}]
01011         set right [expr {$right ^ $temp}]
01012         set left [expr {$left ^ ($temp << 2)}]
01013         set temp [expr {(($right >> 16) ^ $left) & 0x0000ffff}]
01014         set left [expr {$left ^ $temp}]
01015         set right [expr {$right ^ ($temp << 16)}]
01016         set temp [expr {(($left >> 1) ^ $right) & 0x55555555}]
01017         set right [expr {$right ^ $temp}]
01018         set left [expr {$left ^ ($temp << 1)}]
01019         set temp [expr {(($right >> 8) ^ $left) & 0x00ff00ff}]
01020         set left [expr {$left ^ $temp}]
01021         set right [expr {$right ^ ($temp << 8)}]
01022         set temp [expr {(($left >> 1) ^ $right) & 0x55555555}]
01023         set right [expr {$right ^ $temp}]
01024         set left [expr {$left ^ ($temp << 1)}]
01025         
01026         #puts "Left key PC1: [format %x $left]"
01027         #puts "Right key PC1: [format %x $right]"
01028 
01029         # The right side needs to be shifted and to get
01030         # the last four bits of the left side
01031         set temp [expr {($left << 8) | (($right >> 20) & 0x000000f0)}];
01032         # Left needs to be put upside down
01033         set left [expr {($right << 24) | (($right << 8) & 0x00ff0000) | \
01034                 (($right >> 8) & 0x0000ff00) \
01035                 | (($right >> 24) & 0x000000f0)}];
01036         set right $temp;
01037 
01038         #puts "Left key juggle: [format %x $left]"
01039         #puts "Right key juggle: [format %x $right]"
01040 
01041         # Now go through and perform these
01042         # shifts on the left and right keys.
01043         foreach i $shifts  {
01044         # Shift the keys either one or two bits to the left.
01045         if {$i} {
01046             set left [expr {($left << 2) \
01047                     | (($left >> 26) & 0x0000003f)}];
01048             set right [expr {($right << 2) \
01049                      | (($right >> 26) & 0x0000003f)}];
01050         } else {
01051             set left [expr {($left << 1) \
01052                     | (($left >> 27) & 0x0000001f)}];
01053             set right [expr {($right << 1) \
01054                      | (($right >> 27) & 0x0000001f)}];
01055         }
01056         set left [expr {$left & 0xfffffff0}];
01057         set right [expr {$right & 0xfffffff0}];
01058 
01059         # Now apply PC-2, in such a way that E is easier when
01060         # encrypting or decrypting this conversion will look like PC-2
01061         # except only the last 6 bits of each byte are used rather than
01062         # 48 consecutive bits and the order of lines will be according
01063         # to how the S selection functions will be applied: S2, S4, S6,
01064         # S8, S1, S3, S5, S7.
01065         set lefttemp [expr {[lindex $pc2bytes0 [expr {($left >> 28) & 0x0000000f}]] | \
01066                     [lindex $pc2bytes1 [expr {($left >> 24) & 0x0000000f}]] | \
01067                     [lindex $pc2bytes2 [expr {($left >> 20) & 0x0000000f}]] | \
01068                     [lindex $pc2bytes3 [expr {($left >> 16) & 0x0000000f}]] | \
01069                     [lindex $pc2bytes4 [expr {($left >> 12) & 0x0000000f}]] | \
01070                     [lindex $pc2bytes5 [expr {($left >> 8) & 0x0000000f}]] | \
01071                     [lindex $pc2bytes6 [expr {($left >> 4) & 0x0000000f}]]}];
01072         set righttemp [expr {[lindex $pc2bytes7 [expr {($right >> 28) & 0x0000000f}]] | \
01073                      [lindex $pc2bytes8 [expr {($right >> 24) & 0x0000000f}]] | \
01074                      [lindex $pc2bytes9 [expr {($right >> 20) & 0x0000000f}]] | \
01075                      [lindex $pc2bytes10 [expr {($right >> 16) & 0x0000000f}]] | \
01076                      [lindex $pc2bytes11 [expr {($right >> 12) & 0x0000000f}]] | \
01077                      [lindex $pc2bytes12 [expr {($right >> 8) & 0x0000000f}]] | \
01078                      [lindex $pc2bytes13 [expr {($right >> 4) & 0x0000000f}]]}];
01079         set temp [expr {(($righttemp >> 16) ^ $lefttemp) & 0x0000ffff}];
01080         lappend keys [expr {$lefttemp ^ $temp}];
01081         lappend keys [expr {$righttemp ^ ($temp << 16)}];
01082         }
01083     }; # For each iteration.
01084     # Return the keys we've created.
01085     return $keys;
01086     }; /*  End of createKeys.*/
01087 }; /*  End of des namespace eval.*/
01088 
01089 package provide tclDES 1.0.0
01090 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1