tcldesjr.tcl

Go to the documentation of this file.
00001 /*  desjr.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 /*  3DES functionality removed, February, 2003*/
00007 /*  July, 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 /*  August, 2003 - 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      keysets = (ndx) 1
00070     /*  Produre: keyset - Create or destroy a keyset created*/
00071     /*                    from a 64-bit DES key.*/
00072     /*  Inputs:*/
00073     /*    oper  : The operation to be performed.  This will be either "create"*/
00074     /*            (make a new keyset) or "destroy" (delete an existing keyset).*/
00075     /*            The meaning of the argument "value" depends of the operation*/
00076     /*            performed.  An error is generated if "oper" is not "create"*/
00077     /*            or "destroy".*/
00078     /*              */
00079     /*    value : If the argument "oper" is "create", then "value" is the 64-bit*/
00080     /*            DES key.  (Note: The lsb of each byte is ignored; odd parity is*/
00081     /*            not required).  If the argument "oper" is "destroy", then*/
00082     /*            "value" is a handle to a keyset that was created previously.*/
00083     /* */
00084     /*    weak:   If true then weak keys are allowed. The default is to raise an*/
00085     /*            error when a weak key is seen.*/
00086     /*  Output:*/
00087     /*    If the argument "oper" is "create", then the output is a handle to the*/
00088     /*    keyset stored in the des namespace.  If the argument "oper" is*/
00089     /*    "destroy", then nothing is returned.*/
00090     ret  keyset (type oper , type value , optional weak =0) {
00091     variable keysets
00092     set newset {}
00093     switch -exact -- $oper {
00094         create {
00095         # Create a new keyset handle.
00096         set newset keyset$keysets(ndx)
00097         # Create key set
00098         set keysets($newset) [createKeys $value $weak]
00099         # Never use that keyset handle index again.
00100         incr keysets(ndx)
00101         }
00102         destroy {
00103         # Determine if the keyset handle is valid.
00104         if {[array names keysets $value] != {}} {
00105             # Delete the handle and corresponding keyset.
00106                     unset keysets($value)
00107         } else {
00108             error "The keyset handle \"$value\" is invalid!"
00109         }
00110         }
00111         default {
00112         error {The operator must be either "create" or "destroy".}
00113         }
00114     }
00115     return $newset
00116     }
00117 
00118     /*  Procedure: encrypt - Encryption front-end for the des procedure*/
00119     /*  Inputs:*/
00120     /*    keyset  : Handle to an existing keyset.*/
00121     /*    message : String to be encrypted.*/
00122     /*    mode    : DES mode ecb (default), cbc, cfb, or ofb.*/
00123     /*    iv      : Name of the initialization vector used in CBC, CFB,*/
00124     /*              and OFB modes.*/
00125     /*    kbits   : Number of bits in a data block (default of 64).*/
00126     /*  Output:*/
00127     /*    The encrypted data string.*/
00128     ret  encrypt (type keyset , type message , optional mode =ecb , optional iv ={) {kbits 64}} {
00129     switch -exact -- $mode {
00130         ecb {
00131         return [block $key $message =  1 0]
00132         }
00133         cbc -
00134         ofb -
00135         cfb {
00136         /*  Is the initialization/feedback vector variable is valid?*/
00137         if {[string length $iv] == 0} {
00138             error "An initialization variable must be specified."
00139         } else {
00140             upvar $iv ivec
00141             if {![info exists ivec]} {
00142             error "The variable $iv does not exist."
00143             }
00144         }
00145         switch -exact -- $mode {
00146             cbc {
00147             return [block $key $message =  1 1 ivec]
00148             }
00149             ofb {
00150             return [stream $key $message =  1 0 ivec $kbits]
00151             }
00152             cfb {
00153             return [stream $key $message =  1 1 ivec $kbits]
00154             }
00155         }
00156         }
00157         default {
00158         error {Mode must be ecb, cbc, cfb, or ofb.}
00159         }
00160     }
00161     }
00162 
00163     /*  Procedure: decrypt - Decryption front-end for the des procedure*/
00164     /*  Inputs:*/
00165     /*    keyset  : Handle to an existing keyset.*/
00166     /*    message : String to be decrypted.*/
00167     /*    mode    : DES mode ecb (default), cbc, cfb, or ofb.*/
00168     /*    iv      : Name of the initialization vector used in CBC, CFB,*/
00169     /*              and OFB modes.*/
00170     /*    kbits   : Number of bits in a data block (default of 64).*/
00171     /*  Output:*/
00172     /*    The encrypted or decrypted data string.*/
00173     ret  decrypt (type keyset , type message , optional mode =ecb , optional iv ={) {kbits 64}} {
00174     switch -exact -- $mode {
00175         ecb {
00176         return [block $key $message =  0 0]
00177         }
00178         cbc -
00179         ofb -
00180         cfb {
00181         /*  Is the initialization/feedback vector variable is valid?*/
00182         if {[string length $iv] < 1} {
00183             error "An initialization variable must be specified."
00184         } else {
00185             upvar $iv ivec
00186             if {![info exists ivec]} {
00187             error "The variable $iv does not exist."
00188             }
00189         }
00190         switch -exact -- $mode {
00191             cbc {
00192             return [block $key $message =  0 1 ivec]
00193             }
00194             ofb {
00195             return [stream $key $message =  0 0 ivec $kbits]
00196             }
00197             cfb {
00198             return [stream $key $message =  0 1 ivec $kbits]
00199             }
00200         }
00201         }
00202         default {
00203         error {Mode must be ecb, cbc, cfb, or ofb.}
00204         }
00205     }
00206     }
00207 
00208     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];
00209     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];
00210     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];
00211     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];
00212     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];
00213     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];
00214     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];
00215     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];
00216 
00217     variable desEncrypt {0 32 2}
00218     variable desDecrypt {30 -2 -2}
00219 
00220     /*  Procedure: block - DES ECB and CBC mode support*/
00221     /*  Inputs:*/
00222     /*    keyset   : Handle to an existing keyset.*/
00223     /*    message  : String to be encrypted or decrypted (Note: For encryption,*/
00224     /*               the string is extended with null characters to an integral*/
00225     /*               multiple of eight bytes.  For decryption, the string length*/
00226     /*               must be an integral multiple of eight bytes.*/
00227     /*    encrypt  : Perform encryption (1) or decryption (0)*/
00228     /*    mode     : DES mode 1=CBC, 0=ECB (default).*/
00229     /*    iv       : Name of the variable containing the initialization vector*/
00230     /*               used in CBC mode.  The value must be 64 bits in length.*/
00231     /*  Output:*/
00232     /*    The encrypted or decrypted data string.*/
00233     ret  block (type keyset , type message , type encrypt , optional mode =0 , optional iv ={)} {
00234     variable spfunction1
00235     variable spfunction2
00236     variable spfunction3
00237     variable spfunction4
00238     variable spfunction5
00239     variable spfunction6
00240     variable spfunction7
00241     variable spfunction8
00242     variable desEncrypt
00243     variable desDecrypt
00244     variable keysets
00245 
00246     # Determine if the keyset handle is valid.
00247     if {[array names keysets $keyset] != {}} {
00248         /*  Acquire the 16 or 48 subkeys we will need*/
00249          keys =  $keys = ($key)
00250     } else =  {
00251         error "The key handle =  \"$key\" is =  invalid!"
00252     }
00253      m =  0
00254      cbcleft =  0x00;  cbcleft2 =  0x00
00255      cbcright =  0x00;  cbcright2 =  0x00
00256      len =  [string length $message];
00257         if {$len == 0} {
00258             return -code error "invalid message size: the message may not be empty"
00259         }
00260      chunk =  0;
00261     /*  Set up the loops for des*/
00262     expr {$encrypt ? [ looping =  $desEncrypt] : [ looping =  $desDecrypt]}
00263 
00264     /*  Pad the message out with null bytes.*/
00265     append message "\0\0\0\0\0\0\0\0"
00266 
00267     /*  Store the result here*/
00268      result =  {};
00269      tempresult =  {};
00270 
00271     /*  CBC mode*/
00272     if {$mode == 1} {
00273         /*  Is the initialization/feedback vector variable is valid?*/
00274         if {[string length $iv] < 1} {
00275         error "An initialization variable must be specified."
00276         } else {
00277         upvar $iv ivec
00278         if {![info exists ivec]} {
00279             error "The variable $iv does not exist."
00280         }
00281                 if {[string length $ivec] != 8} {
00282                     return -code error "invalid initialization vector size:\
00283                         the initialization vector must be 8 bytes"
00284                 }
00285         }
00286         /*  Use the input vector as the intial vector.*/
00287         binary scan $ivec H8H8 cbcleftTemp cbcrightTemp
00288          cbcleft =  "0x$cbcleftTemp"
00289          cbcright =  "0x$cbcrightTemp"
00290     }
00291 
00292     /*  Loop through each 64 bit chunk of the message*/
00293     while {$m < $len} {
00294         binary scan $message x${m}H8H8 lefttemp righttemp
00295          left =  {}
00296         append left "0x" $lefttemp
00297          right =  {}
00298         append right "0x" $righttemp
00299         incr m 8
00300 
00301         /* puts "Left start: $left";*/
00302         /* puts "Right start: $right";*/
00303         /*  For Cipher Block Chaining mode, xor the*/
00304         /*  message with the previous result.*/
00305         if {$mode == 1} {
00306         if {$encrypt} {
00307              left =  [expr {$left ^ $cbcleft}]
00308              right =  [expr {$right ^ $cbcright}]
00309         } else {
00310              cbcleft2 =  $cbcleft;
00311              cbcright2 =  $cbcright;
00312              cbcleft =  $left;
00313              cbcright =  $right;
00314         }
00315         }
00316 
00317         /* puts "Left mode: $left";*/
00318         /* puts "Right mode: $right";*/
00319         /* puts "cbcleft: $cbcleft";*/
00320         /* puts "cbcleft2: $cbcleft2";*/
00321         /* puts "cbcright: $cbcright";*/
00322         /* puts "cbcright2: $cbcright2";*/
00323 
00324         /*  First each 64 but chunk of the message*/
00325         /*  must be permuted according to IP.*/
00326          temp =  [expr {(($left >> 4) ^ $right) & 0x0f0f0f0f}];
00327          right =  [expr {$right ^ $temp}];
00328          left =  [expr {$left ^ ($temp << 4)}];
00329          temp =  [expr {(($left >> 16) ^ $right) & 0x0000ffff}];
00330          right =  [expr {$right ^ $temp}];
00331          left =  [expr {$left ^ ($temp << 16)}];
00332          temp =  [expr {(($right >> 2) ^ $left) & 0x33333333}];
00333          left =  [expr {$left ^ $temp}]
00334          right =  [expr {$right ^ ($temp << 2)}];
00335 
00336          temp =  [expr {(($right >> 8) ^ $left) & 0x00ff00ff}];
00337          left =  [expr {$left ^ $temp}];
00338          right =  [expr {$right ^ ($temp << 8)}];
00339          temp =  [expr {(($left >> 1) ^ $right) & 0x55555555}];
00340          right =  [expr {$right ^ $temp}];
00341          left =  [expr {$left ^ ($temp << 1)}];
00342 
00343          left =  [expr {((($left << 1) & 0xffffffff) | \
00344                  (($left >> 31) & 0x00000001))}]; 
00345          right =  [expr {((($right << 1) & 0xffffffff) | \
00346                   (($right >> 31) & 0x00000001))}]; 
00347 
00348         /* puts "Left IP: [format %x $left]";*/
00349         /* puts "Right IP: [format %x $right]";*/
00350 
00351         /*  Do this 1 time for each chunk of the message.*/
00352          endloop =  [lindex $looping 1];
00353          loopinc =  [lindex $looping 2];
00354 
00355         /* puts "endloop: $endloop";*/
00356         /* puts "loopinc: $loopinc";*/
00357 
00358         /*  Now go through and perform the encryption or decryption. */
00359         for { i =  [lindex $looping 0]} \
00360         {$i != $endloop} {incr i $loopinc} {
00361         /*  For efficiency*/
00362          right1 =  [expr {$right ^ [lindex $keys $i]}]; 
00363          right2 =  [expr {((($right >> 4) & 0x0fffffff) | \
00364                        (($right << 28) & 0xffffffff)) ^ \
00365                       [lindex $keys [expr {$i + 1}]]}];
00366  
00367         /*  puts "right1: [format %x $right1]";*/
00368         /*  puts "right2: [format %x $right2]";*/
00369 
00370         /*  The result is attained by passing these*/
00371         /*  bytes through the S selection functions.*/
00372          temp =  $left;
00373          left =  $right;
00374          right =  [expr {$temp ^ ([lindex $spfunction2 [expr {($right1 >> 24) & 0x3f}]] | \
00375                           [lindex $spfunction4 [expr {($right1 >> 16) & 0x3f}]] | \
00376                           [lindex $spfunction6 [expr {($right1 >>  8) & 0x3f}]] | \
00377                           [lindex $spfunction8 [expr {$right1 & 0x3f}]] | \
00378                           [lindex $spfunction1 [expr {($right2 >> 24) & 0x3f}]] | \
00379                           [lindex $spfunction3 [expr {($right2 >> 16) & 0x3f}]] | \
00380                           [lindex $spfunction5 [expr {($right2 >>  8) & 0x3f}]] | \
00381                           [lindex $spfunction7 [expr {$right2 & 0x3f}]])}];
00382  
00383         /*  puts "Left iter: [format %x $left]";*/
00384         /*  puts "Right iter: [format %x $right]";*/
00385         
00386         }
00387          temp =  $left;
00388          left =  $right;
00389          right =  $temp; /*  Unreverse left and right.*/
00390 
00391         /* puts "Left Iterated: [format %x $left]";*/
00392         /* puts "Right Iterated: [format %x $right]";*/
00393 
00394         /*  Move then each one bit to the right*/
00395          left =  [expr {((($left >> 1) & 0x7fffffff) \
00396                  | (($left << 31) & 0xffffffff))}]; 
00397          right =  [expr {((($right >> 1) & 0x7fffffff) \
00398                   | (($right << 31) & 0xffffffff))}]; 
00399 
00400         /* puts "Left shifted: [format %x $left]";*/
00401         /* puts "Right shifted: [format %x $right]";*/
00402 
00403         /*  Now perform IP-1, which is IP in the opposite direction*/
00404          temp =  [expr {((($left >> 1) & 0x7fffffff) ^ $right) & 0x55555555}];
00405          right =  [expr {$right ^ $temp}];
00406          left =  [expr {$left ^ ($temp << 1)}];
00407          temp =  [expr {((($right >> 8) & 0x00ffffff) ^ $left) & 0x00ff00ff}];
00408          left =  [expr {$left ^ $temp}];
00409          right =  [expr {$right ^ ($temp << 8)}];
00410          temp =  [expr {((($right >> 2) & 0x3fffffff) ^ $left) & 0x33333333}]; 
00411          left =  [expr {$left ^ $temp}];
00412          right =  [expr {$right ^ ($temp << 2)}];
00413          temp =  [expr {((($left >> 16) & 0x0000ffff) ^ $right) & 0x0000ffff}];
00414          right =  [expr {$right ^ $temp}];
00415          left =  [expr {$left ^ ($temp << 16)}];
00416          temp =  [expr {((($left >> 4) & 0x0fffffff) ^ $right) & 0x0f0f0f0f}];
00417          right =  [expr {$right ^ $temp}];
00418          left =  [expr {$left ^ ($temp << 4)}];
00419 
00420         /* puts "Left IP-1: [format %x $left]";*/
00421         /* puts "Right IP-1: [format %x $right]";*/
00422 
00423         /*  For Cipher Block Chaining mode, xor*/
00424         /*  the message with the previous result.*/
00425         if {$mode == 1} {
00426         if {$encrypt} {
00427              cbcleft =  $left;
00428              cbcright =  $right;
00429         } else {
00430              left =  [expr {$left ^ $cbcleft2}];
00431              right =  [expr {$right ^ $cbcright2}];
00432         }
00433         }
00434 
00435         append tempresult \
00436         [binary format H16 [format %08x%08x $left $right]]
00437 
00438         /* puts "Left final: [format %x $left]";*/
00439         /* puts "Right final: [format %x $right]";*/
00440 
00441         incr chunk 8;
00442         if {$chunk == 512} {
00443         append result $tempresult
00444          tempresult =  {};
00445          chunk =  0;
00446         }
00447     }; /*  For every 8 characters, or 64 bits in the message*/
00448 
00449     if {$mode == 1} {
00450         if {$encrypt} {
00451         /*  Save the left and right registers to the feedback vector.*/
00452          ivec =  [binary format H* \
00453                   [format %08x $left][format %08x $right]]
00454         } else {
00455          ivec =  [binary format H* \
00456                   [format %08x $cbcleft][format %08x $cbcright]]
00457         }
00458     }
00459 
00460     /*  Return the result as an array*/
00461     return ${result}$tempresult
00462     }; /*  End of block*/
00463 
00464     /*  Procedure: stream - DES CFB and OFB mode support*/
00465     /*  Inputs:*/
00466     /*    keyset   : Handle to an existing keyset.*/
00467     /*    message  : String to be encrypted or decrypted (Note: The length of the*/
00468     /*               string is dependent upon the value of kbits.  Remember that*/
00469     /*               the string is part of a stream of data, so it must be sized*/
00470     /*               properly for subsequent encryptions/decryptions to be*/
00471     /*               correct.  See the man page for correct message lengths for*/
00472     /*               values of kbits).*/
00473     /*    encrypt  : Perform encryption (1) or decryption (0)*/
00474     /*    mode     : DES mode 0=OFB, 1=CFB.*/
00475     /*    iv       : Name of variable containing the initialization vector.  The*/
00476     /*               value must be 64 bits in length with the first 64-L bits set*/
00477     /*               to zero.*/
00478     /*    kbits    : Number of bits in a data block (default of 64).*/
00479     /*  Output:*/
00480     /*    The encrypted or decrypted data string.*/
00481     ret  stream (type keyset , type message , type encrypt , type mode , type iv , optional kbits =64) {
00482     variable spfunction1
00483     variable spfunction2
00484     variable spfunction3
00485     variable spfunction4
00486     variable spfunction5
00487     variable spfunction6
00488     variable spfunction7
00489     variable spfunction8
00490     variable desEncrypt
00491     variable keysets
00492 
00493     # Determine if the keyset handle is valid.
00494     if {[array names keysets $keyset] != {}} {
00495         # Acquire the 16 subkeys we will need.
00496         set keys $keysets($keyset)
00497     } else {
00498         error "The keyset handle \"$keyset\" is invalid!"
00499     }
00500 
00501     # Is the initialization/feedback vector variable is valid?
00502     if {[string length $iv] < 1} {
00503         error "An initialization variable must be specified."
00504     } else {
00505         upvar $iv ivec
00506         if {![info exists ivec]} {
00507         error "The variable $iv does not exist."
00508         }
00509     }
00510 
00511         # Determine if message length (in bits)
00512     # is not an integral number of kbits.
00513     set len [string length $message];
00514         #puts "len: $len, kbits: $kbits"
00515     if {($kbits < 1) || ($kbits > 64)} {
00516         error "The valid values of kbits are 1 through 64."
00517         } elseif {($kbits % 8) != 0} {
00518         set blockSize [expr {$kbits + (8 - ($kbits % 8))}]
00519         set fail [expr {(($len * 8) / $blockSize) % $kbits}]
00520     } else {
00521         set blockSize [expr {$kbits / 8}]
00522         set fail [expr {$len % $blockSize}]
00523     }
00524         if {$fail} {
00525         error "Data length (in bits) is not an integral number of kbits."
00526     }
00527 
00528     set m 0
00529     set n 0
00530     set chunk 0;
00531     # Set up the loops for des
00532     set looping $desEncrypt
00533 
00534         # Set up shifting values.  Used for both CFB and OFB modes.
00535         if {$kbits < 32} {
00536         # Only some bits from left output are needed.
00537         set kOutShift [expr {32 - $kbits}]
00538         set kOutMask [expr {0x7fffffff >> (31 - $kbits)}]
00539         # Determine number of message bytes needed per iteration.
00540         set msgBytes [expr {int(ceil(double($kbits) / 8.0))}]
00541         # Determine number of message bits needed per iteration.
00542         set msgBits [expr {$msgBytes * 8}]
00543         set msgBitsSub1 [expr {$msgBits - 1}]
00544         # Define bit caches.
00545         set bitCacheIn {}
00546         set bitCacheOut {}
00547         # Variable used to remove bits 0 through
00548         # kbits-1 in the input bit cache.
00549         set kbitsSub1 [expr {$kbits - 1}]
00550         # Variable used to remove leading dummy binary bits.
00551         set xbits [expr {32 - $kbits}]
00552     } elseif {$kbits == 32} {
00553         # Only bits of left output are used.
00554         # Four messages bytes are needed per iteration.
00555         set msgBytes 4
00556         set xbits 32
00557     } elseif {$kbits < 64} {
00558         # All bits from left output are needed.
00559         set kOutShiftLeft [expr {$kbits - 32}]
00560         # Some bits from right output are needed.
00561         set kOutShiftRight [expr {64 - $kbits}]
00562         set kOutMaskRight [expr {0x7fffffff >> (63 - $kbits)}]
00563         # Determine number of message bytes needed per iteration.
00564         set msgBytes [expr {int(ceil(double($kbits) / 8.0))}]
00565         # Determine number of message bits needed per iteration.
00566         set msgBits [expr {$msgBytes * 8}]
00567         set msgBitsSub1 [expr {$msgBits - 1}]
00568         # Define bit caches.
00569         set bitCacheIn {}
00570         set bitCacheOut {}
00571         # Variable used to remove bits 0 through
00572         # kbits-1 in the input bit cache.
00573         set kbitsSub1 [expr {$kbits - 1}]
00574         # Variable used to remove leading dummy binary bits.
00575         set xbits [expr {64 - $kbits}]
00576     } else {
00577         # All 64 bits of output are used.
00578         # Eight messages bytes are needed per iteration.
00579         set msgBytes 8
00580         set xbits 0
00581     }
00582 
00583     # Store the result here
00584     set result {}
00585     set tempresult {}
00586 
00587     # Set up the initialization vector bitstream
00588     binary scan $ivec H8H8 leftTemp rightTemp
00589     set left "0x$leftTemp"
00590     set right "0x$rightTemp"
00591         #puts "Retrieved Feedback vector: $fbvec"
00592         #puts "Start: |$left| |$right|"
00593     
00594     # Loop through each 64 bit chunk of the message
00595     while {$m < $len} {
00596         # puts "Left start: $left";
00597         # puts "Right start: $right";
00598 
00599         # First each 64 but chunk of the
00600         # message must be permuted according to IP.
00601         set temp [expr {(($left >> 4) ^ $right) & 0x0f0f0f0f}];
00602         set right [expr {$right ^ $temp}];
00603         set left [expr {$left ^ ($temp << 4)}];
00604         set temp [expr {(($left >> 16) ^ $right) & 0x0000ffff}];
00605         set right [expr {$right ^ $temp}];
00606         set left [expr {$left ^ ($temp << 16)}];
00607         set temp [expr {(($right >> 2) ^ $left) & 0x33333333}];
00608         set left [expr {$left ^ $temp}];
00609         set right [expr {$right ^ ($temp << 2)}];
00610 
00611         set temp [expr {(($right >> 8) ^ $left) & 0x00ff00ff}];
00612         set left [expr {$left ^ $temp}];
00613         set right [expr {$right ^ ($temp << 8)}];
00614         set temp [expr {(($left >> 1) ^ $right) & 0x55555555}];
00615         set right [expr {$right ^ $temp}];
00616         set left [expr {$left ^ ($temp << 1)}];
00617 
00618         set left [expr {((($left << 1) & 0xffffffff) | \
00619                  (($left >> 31) & 0x00000001))}]; 
00620         set right [expr {((($right << 1) & 0xffffffff) | \
00621                   (($right >> 31) & 0x00000001))}]; 
00622 
00623         #puts "Left IP: [format %x $left]";
00624         #puts "Right IP: [format %x $right]";
00625 
00626         # Do this 1 time for each chunk of the message
00627         set endloop [lindex $looping 1];
00628         set loopinc [lindex $looping 2];
00629 
00630         # puts "endloop: $endloop";
00631         # puts "loopinc: $loopinc";
00632 
00633         # Now go through and perform the encryption or decryption  
00634         for {set i [lindex $looping 0]} \
00635         {$i != $endloop} {incr i $loopinc} {
00636         # For efficiency
00637         set right1 [expr {$right ^ [lindex $keys $i]}]; 
00638         set right2 [expr {((($right >> 4) & 0x0fffffff) | \
00639                        (($right << 28) & 0xffffffff)) ^ \
00640                       [lindex $keys [expr {$i + 1}]]}];
00641  
00642         # puts "right1: [format %x $right1]";
00643         # puts "right2: [format %x $right2]";
00644 
00645         # The result is attained by passing these
00646         # bytes through the S selection functions.
00647         set temp $left;
00648         set left $right;
00649         set right [expr {$temp ^ ([lindex $spfunction2 [expr {($right1 >> 24) & 0x3f}]] | \
00650                           [lindex $spfunction4 [expr {($right1 >> 16) & 0x3f}]] | \
00651                           [lindex $spfunction6 [expr {($right1 >>  8) & 0x3f}]] | \
00652                           [lindex $spfunction8 [expr {$right1 & 0x3f}]] | \
00653                           [lindex $spfunction1 [expr {($right2 >> 24) & 0x3f}]] | \
00654                           [lindex $spfunction3 [expr {($right2 >> 16) & 0x3f}]] | \
00655                           [lindex $spfunction5 [expr {($right2 >>  8) & 0x3f}]] | \
00656                           [lindex $spfunction7 [expr {$right2 & 0x3f}]])}];
00657  
00658         # puts "Left iter: [format %x $left]";
00659         # puts "Right iter: [format %x $right]";
00660         }
00661         set temp $left;
00662         set left $right;
00663         set right $temp; # Unreverse left and right
00664 
00665         #puts "Left Iterated: [format %x $left]";
00666         #puts "Right Iterated: [format %x $right]";
00667 
00668         # Move then each one bit to the right
00669         set left [expr {((($left >> 1) & 0x7fffffff) | \
00670                  (($left << 31) & 0xffffffff))}]; 
00671         set right [expr {((($right >> 1) & 0x7fffffff) | \
00672                   (($right << 31) & 0xffffffff))}]; 
00673 
00674         #puts "Left shifted: [format %x $left]";
00675         #puts "Right shifted: [format %x $right]";
00676 
00677         # Now perform IP-1, which is IP in the opposite direction
00678         set temp [expr {((($left >> 1) & 0x7fffffff) ^ $right) & 0x55555555}];
00679         set right [expr {$right ^ $temp}];
00680         set left [expr {$left ^ ($temp << 1)}];
00681         set temp [expr {((($right >> 8) & 0x00ffffff) ^ $left) & 0x00ff00ff}];
00682         set left [expr {$left ^ $temp}];
00683         set right [expr {$right ^ ($temp << 8)}];
00684         set temp [expr {((($right >> 2) & 0x3fffffff) ^ $left) & 0x33333333}]; 
00685         set left [expr {$left ^ $temp}];
00686         set right [expr {$right ^ ($temp << 2)}];
00687         set temp [expr {((($left >> 16) & 0x0000ffff) ^ $right) & 0x0000ffff}];
00688         set right [expr {$right ^ $temp}];
00689         set left [expr {$left ^ ($temp << 16)}];
00690         set temp [expr {((($left >> 4) & 0x0fffffff) ^ $right) & 0x0f0f0f0f}];
00691         set right [expr {$right ^ $temp}];
00692         set left [expr {$left ^ ($temp << 4)}];
00693 
00694         #puts "Left IP-1: [format %x $left]";
00695         #puts "Right IP-1: [format %x $right]";
00696 
00697         # Extract the "kbits" most significant bits from the output block.
00698         if {$kbits < 32} {
00699         # Only some bits from left output are needed.
00700         set kData [expr {($left >> $kOutShift) & $kOutMask}]
00701         set newBits {}
00702         # If necessary, copy message bytes into input bit cache.
00703         if {([string length $bitCacheIn] < $kbits) && ($n < $len)} {
00704             if {$len - $n < $msgBytes} {
00705             set lastBits [expr {($len - $n) * 8}]
00706             ###puts -nonewline [binary scan $message x${n}B$lastBits newBits]
00707             binary scan $message x${n}B$lastBits newBits
00708             } else {
00709             # Extract "msgBytes" whole bytes as bits
00710             ###puts -nonewline [binary scan $message x${n}B$msgBits newBits]
00711             binary scan $message x${n}B$msgBits newBits
00712             }
00713             incr n $msgBytes
00714             #puts " $newBits  $n [expr {$len - $n}]"
00715             # Add the bits to the input bit cache.
00716             append bitCacheIn $newBits
00717         }
00718         #puts -nonewline "In bit cache: $bitCacheIn"
00719         # Set up message data from input bit cache.
00720         binary scan [binary format B32 [format %032s [string range $bitCacheIn 0 $kbitsSub1]]] H8 temp
00721         set msgData "0x$temp"
00722         # Mix message bits with crypto bits.
00723         set mixData [expr {$msgData ^ $kData}]
00724         # Discard collected bits from the input bit cache.
00725         set bitCacheIn [string range $bitCacheIn $kbits end]
00726         #puts "  After: $bitCacheIn"
00727         # Convert back to a bit stream and append to the output bit cache.
00728         # Only the lower kbits are wanted.
00729         binary scan [binary format H8 [format %08x $mixData]] B32 msgOut
00730         append bitCacheOut [string range $msgOut $xbits end]
00731         #puts -nonewline "Out bit cache: $bitCacheOut"
00732         # If there are sufficient bits, move bytes to the temporary holding string.
00733         if {[string length $bitCacheOut] >= $msgBits} {
00734             append tempresult [binary format B$msgBits [string range $bitCacheOut 0 $msgBitsSub1]]
00735             set bitCacheOut [string range $bitCacheOut $msgBits end]
00736                     #puts -nonewline "  After: $bitCacheOut"
00737             incr m $msgBytes
00738             ###puts "$m bytes output"
00739             incr chunk $msgBytes
00740         }
00741         #puts ""
00742         # For CFB mode
00743         if {$mode == 1} {
00744             if {$encrypt} {
00745             set temp [expr {($right << $kbits) & 0xffffffff}]
00746             set left [expr {(($left << $kbits) & 0xffffffff) | (($right >> $kOutShift) & $kOutMask)}]
00747             set right [expr {$temp | $mixData}]
00748             } else {
00749             set temp [expr {($right << $kbits) & 0xffffffff}]
00750             set left [expr {(($left << $kbits) & 0xffffffff) | (($right >> $kOutShift) & $kOutMask)}]
00751             set right [expr {$temp | $msgData}]
00752             }
00753         }
00754         } elseif {$kbits == 32} {
00755         # Only bits of left output are used.
00756         set kData $left
00757         # Four messages bytes are needed per iteration.
00758         binary scan $message x${m}H8 temp
00759         incr m 4
00760         incr chunk 4
00761         set msgData "0x$temp"
00762         # Mix message bits with crypto bits.
00763         set mixData [expr {$msgData ^ $kData}]
00764         # Move bytes to the temporary holding string.
00765         append tempresult [binary format H8 [format %08x $mixData]]
00766         # For CFB mode
00767         if {$mode == 1} {
00768             set left $right
00769             if {$encrypt} {
00770             set right $mixData
00771             } else {
00772             set right $msgData
00773             }
00774         }
00775         } elseif {$kbits < 64} {
00776         set kDataLeft [expr {($left >> $kOutShiftRight) & $kOutMaskRight}]
00777         set temp [expr {($left << $kOutShiftLeft) & 0xffffffff}]
00778         set kDataRight [expr {(($right >> $kOutShiftRight) & $kOutMaskRight) | $temp}]
00779         # If necessary, copy message bytes into input bit cache.
00780         if {([string length $bitCacheIn] < $kbits)  && ($n < $len)} {
00781             if {$len - $n < $msgBytes} {
00782             set lastBits [expr {($len - $n) * 8}]
00783             ###puts -nonewline [binary scan $message x${n}B$lastBits newBits]
00784             binary scan $message x${n}B$lastBits newBits
00785             } else {
00786             # Extract "msgBytes" whole bytes as bits
00787             ###puts -nonewline [binary scan $message x${n}B$msgBits newBits]
00788             binary scan $message x${n}B$msgBits newBits
00789             }
00790             incr n $msgBytes
00791             # Add the bits to the input bit cache.
00792             append bitCacheIn $newBits
00793         }
00794         # Set up message data from input bit cache.
00795         # puts "Bits from cache: [set temp [string range $bitCacheIn 0 $kbitsSub1]]"
00796         # puts "Length of bit string: [string length $temp]"
00797         binary scan [binary format B64 [format %064s [string range $bitCacheIn 0 $kbitsSub1]]] H8H8 leftTemp rightTemp
00798         set msgDataLeft "0x$leftTemp"
00799         set msgDataRight "0x$rightTemp"
00800         # puts "msgDataLeft: $msgDataLeft"
00801         # puts "msgDataRight: $msgDataRight"
00802         # puts "kDataLeft: [format 0x%08x $kDataLeft]"
00803         # puts "kDataRight: [format 0x%08x $kDataRight]"
00804         # Mix message bits with crypto bits.
00805         set mixDataLeft [expr {$msgDataLeft ^ $kDataLeft}]
00806         set mixDataRight [expr {$msgDataRight ^ $kDataRight}]
00807         # puts "mixDataLeft: $mixDataLeft"
00808         # puts "mixDataRight: $mixDataRight"
00809         # puts "mixDataLeft: [format 0x%08x $mixDataLeft]"
00810         # puts "mixDataRight: [format 0x%08x $mixDataRight]"
00811         # Discard collected bits from the input bit cache.
00812         set bitCacheIn [string range $bitCacheIn $kbits end]
00813         # Convert back to a bit stream and
00814         # append to the output bit cache.
00815         # Only the lower kbits are wanted.
00816         binary scan \
00817             [binary format H8H8 \
00818              [format %08x $mixDataLeft] \
00819              [format %08x $mixDataRight]] B64 msgOut
00820         append bitCacheOut [string range $msgOut $xbits end]
00821         # If there are sufficient bits, move
00822         # bytes to the temporary holding string.
00823         if {[string length $bitCacheOut] >= $msgBits} {
00824             append tempresult \
00825             [binary format B$msgBits \
00826                  [string range $bitCacheOut 0 $msgBitsSub1]]
00827             set bitCacheOut [string range $bitCacheOut $msgBits end]
00828             incr m $msgBytes
00829             incr chunk $msgBytes
00830         }
00831         # For CFB mode
00832         if {$mode == 1} {
00833             if {$encrypt} {
00834             set temp \
00835                 [expr {($right << $kOutShiftRight) & 0xffffffff}]
00836             set left [expr {$temp | $mixDataLeft}]
00837             set right $mixDataRight
00838             } else {
00839             set temp \
00840                 [expr {($right << $kOutShiftRight) & 0xffffffff}]
00841             set left [expr {$temp | $msgDataLeft}]
00842             set right $msgDataRight
00843             }
00844         }
00845         } else {
00846         # All 64 bits of output are used.
00847         set kDataLeft $left
00848         set kDataRight $right
00849         # Eight messages bytes are needed per iteration.
00850         binary scan $message x${m}H8H8 leftTemp rightTemp
00851         incr m 8
00852         incr chunk 8
00853         set msgDataLeft "0x$leftTemp"
00854         set msgDataRight "0x$rightTemp"
00855         # Mix message bits with crypto bits.
00856         set mixDataLeft [expr {$msgDataLeft ^ $kDataLeft}]
00857         set mixDataRight [expr {$msgDataRight ^ $kDataRight}]
00858         # Move bytes to the temporary holding string.
00859         append tempresult \
00860             [binary format H16 \
00861              [format %08x%08x $mixDataLeft $mixDataRight]]
00862         # For CFB mode
00863         if {$mode == 1} {
00864             if {$encrypt} {
00865             set left $mixDataLeft
00866             set right $mixDataRight
00867             } else {
00868             set left $msgDataLeft
00869             set right $msgDataRight
00870             }
00871         }
00872         }
00873 
00874         #puts "Left final: [format %08x $left]";
00875         #puts "Right final: [format %08x $right]"
00876 
00877         if {$chunk >= 512} {
00878         append result $tempresult
00879         set tempresult {};
00880         set chunk 0;
00881         }
00882     }; # For every 8 characters, or 64 bits in the message
00883         #puts "End: |[format 0x%08x $left]| |[format 0x%08x $right]|"
00884     # Save the left and right registers to the feedback vector.
00885     set ivec [binary format H* [format %08x $left][format %08x $right]]
00886     #puts "Saved Feedback vector: $fbvectors($fbvector)"
00887 
00888         append result $tempresult
00889     if {[string length $result] > $len} {
00890         set result [string replace $result $len end]
00891     }
00892     # Return the result as an array
00893     return $result
00894     }; /*  End of stream*/
00895 
00896     variable pc2bytes0 [list 0 0x4 0x20000000 0x20000004 0x10000 0x10004 0x20010000 0x20010004 0x200 0x204 0x20000200 0x20000204 0x10200 0x10204 0x20010200 0x20010204]
00897     variable pc2bytes1 [list 0 0x1 0x100000 0x100001 0x4000000 0x4000001 0x4100000 0x4100001 0x100 0x101 0x100100 0x100101 0x4000100 0x4000101 0x4100100 0x4100101]
00898     variable pc2bytes2 [list 0 0x8 0x800 0x808 0x1000000 0x1000008 0x1000800 0x1000808 0 0x8 0x800 0x808 0x1000000 0x1000008 0x1000800 0x1000808]
00899     variable pc2bytes3 [list 0 0x200000 0x8000000 0x8200000 0x2000 0x202000 0x8002000 0x8202000 0x20000 0x220000 0x8020000 0x8220000 0x22000 0x222000 0x8022000 0x8222000]
00900     variable pc2bytes4 [list 0 0x40000 0x10 0x40010 0 0x40000 0x10 0x40010 0x1000 0x41000 0x1010 0x41010 0x1000 0x41000 0x1010 0x41010]
00901     variable pc2bytes5 [list 0 0x400 0x20 0x420 0 0x400 0x20 0x420 0x2000000 0x2000400 0x2000020 0x2000420 0x2000000 0x2000400 0x2000020 0x2000420]
00902     variable pc2bytes6 [list 0 0x10000000 0x80000 0x10080000 0x2 0x10000002 0x80002 0x10080002 0 0x10000000 0x80000 0x10080000 0x2 0x10000002 0x80002 0x10080002]
00903     variable pc2bytes7 [list 0 0x10000 0x800 0x10800 0x20000000 0x20010000 0x20000800 0x20010800 0x20000 0x30000 0x20800 0x30800 0x20020000 0x20030000 0x20020800 0x20030800]
00904     variable pc2bytes8 [list 0 0x40000 0 0x40000 0x2 0x40002 0x2 0x40002 0x2000000 0x2040000 0x2000000 0x2040000 0x2000002 0x2040002 0x2000002 0x2040002]
00905     variable pc2bytes9 [list 0 0x10000000 0x8 0x10000008 0 0x10000000 0x8 0x10000008 0x400 0x10000400 0x408 0x10000408 0x400 0x10000400 0x408 0x10000408]
00906     variable pc2bytes10 [list 0 0x20 0 0x20 0x100000 0x100020 0x100000 0x100020 0x2000 0x2020 0x2000 0x2020 0x102000 0x102020 0x102000 0x102020]
00907     variable pc2bytes11 [list 0 0x1000000 0x200 0x1000200 0x200000 0x1200000 0x200200 0x1200200 0x4000000 0x5000000 0x4000200 0x5000200 0x4200000 0x5200000 0x4200200 0x5200200]
00908     variable pc2bytes12 [list 0 0x1000 0x8000000 0x8001000 0x80000 0x81000 0x8080000 0x8081000 0x10 0x1010 0x8000010 0x8001010 0x80010 0x81010 0x8080010 0x8081010]
00909     variable pc2bytes13 [list 0 0x4 0x100 0x104 0 0x4 0x100 0x104 0x1 0x5 0x101 0x105 0x1 0x5 0x101 0x105]
00910 
00911     /*  Now define the left shifts which need to be done*/
00912     variable shifts {0  0  1  1  1  1  1  1  0  1  1  1  1  1  1  0};
00913 
00914     /*  Procedure: createKeys*/
00915     /*  Input:*/
00916     /*    key     : The 64-bit DES key (Note: The lsb of each byte*/
00917     /*              is ignored; odd parity is not required).*/
00918     /* */
00919     /*    weak:   If true then weak keys are allowed. The default is to raise an*/
00920     /*            error when a weak key is seen.*/
00921     /*  Output:*/
00922     /*  The 16 (DES) subkeys.*/
00923     ret  createKeys (type key , optional weak =0) {
00924     variable pc2bytes0
00925     variable pc2bytes1
00926     variable pc2bytes2
00927     variable pc2bytes3
00928     variable pc2bytes4
00929     variable pc2bytes5
00930     variable pc2bytes6
00931     variable pc2bytes7
00932     variable pc2bytes8
00933     variable pc2bytes9
00934     variable pc2bytes10
00935     variable pc2bytes11
00936     variable pc2bytes12
00937     variable pc2bytes13
00938     variable shifts
00939 
00940     # Stores the return keys
00941     set keys {}
00942     # Other variables
00943     set lefttemp {}; set righttemp {}
00944     binary scan $key H8H8 lefttemp righttemp
00945     set left {}
00946     append left "0x" $lefttemp
00947     set right {}
00948     append right "0x" $righttemp
00949 
00950     #puts "Left key: $left"
00951     #puts "Right key: $right"
00952 
00953     # Test for weak keys
00954         if {! $weak} {
00955             set maskedLeft [expr {$left & 0xfefefefe}]
00956             set maskedRight [expr {$right & 0xfefefefe}]
00957             if {($maskedLeft == 0x00000000) \
00958                     && ($maskedRight == 0x00000000)} {
00959                 error "The key is weak!"
00960             } elseif {($maskedLeft == 0x1e1e1e1e) \
00961                           && ($maskedRight == 0x0e0e0e0e)} {
00962                 error "The key is weak!"
00963             } elseif {($maskedLeft == 0xe0e0e0e0) \
00964                           && ($maskedRight == 0xf0f0f0f0)} {
00965                 error "The key is weak!"
00966             } elseif {($maskedLeft == 0xfefefefe) \
00967                           && ($maskedRight == 0xfefefefe)} {
00968                 error "The key is weak!"
00969             }
00970         }
00971 
00972     set temp [expr {(($left >> 4) ^ $right) & 0x0f0f0f0f}]
00973     set right [expr {$right ^ $temp}]
00974     set left [expr {$left ^ ($temp << 4)}]
00975     set temp [expr {(($right >> 16) ^ $left) & 0x0000ffff}]
00976     set left [expr {$left ^ $temp}]
00977     set right [expr {$right ^ ($temp << 16)}]
00978     set temp [expr {(($left >> 2) ^ $right) & 0x33333333}]
00979     set right [expr {$right ^ $temp}]
00980     set left [expr {$left ^ ($temp << 2)}]
00981     set temp [expr {(($right >> 16) ^ $left) & 0x0000ffff}]
00982     set left [expr {$left ^ $temp}]
00983     set right [expr {$right ^ ($temp << 16)}]
00984     set temp [expr {(($left >> 1) ^ $right) & 0x55555555}]
00985     set right [expr {$right ^ $temp}]
00986     set left [expr {$left ^ ($temp << 1)}]
00987     set temp [expr {(($right >> 8) ^ $left) & 0x00ff00ff}]
00988     set left [expr {$left ^ $temp}]
00989     set right [expr {$right ^ ($temp << 8)}]
00990     set temp [expr {(($left >> 1) ^ $right) & 0x55555555}]
00991     set right [expr $right ^ $temp]
00992     set left [expr {$left ^ ($temp << 1)}]
00993         
00994     # puts "Left key PC1: [format %x $left]"
00995     # puts "Right key PC1: [format %x $right]"
00996 
00997     # The right side needs to be shifted and to get
00998     # the last four bits of the left side
00999     set temp [expr {($left << 8) | (($right >> 20) & 0x000000f0)}];
01000     # Left needs to be put upside down
01001     set left [expr {($right << 24) | (($right << 8) & 0x00ff0000) | \
01002                 (($right >> 8) & 0x0000ff00) \
01003                 | (($right >> 24) & 0x000000f0)}];
01004     set right $temp;
01005 
01006     #puts "Left key juggle: [format %x $left]"
01007     #puts "Right key juggle: [format %x $right]"
01008 
01009     # Now go through and perform these
01010     # shifts on the left and right keys.
01011     foreach i $shifts  {
01012         # Shift the keys either one or two bits to the left.
01013         if {$i} {
01014         set left [expr {($left << 2) \
01015                     | (($left >> 26) & 0x0000003f)}];
01016         set right [expr {($right << 2) \
01017                      | (($right >> 26) & 0x0000003f)}];
01018         } else {
01019         set left [expr {($left << 1) \
01020                     | (($left >> 27) & 0x0000001f)}];
01021         set right [expr {($right << 1) \
01022                      | (($right >> 27) & 0x0000001f)}];
01023         }
01024         set left [expr {$left & 0xfffffff0}];
01025         set right [expr {$right & 0xfffffff0}];
01026 
01027         # Now apply PC-2, in such a way that E is easier when encrypting or
01028         # decrypting this conversion will look like PC-2 except only the
01029         # last 6 bits of each byte are used rather than 48 consecutive bits
01030         # and the order of lines will be according to how the S selection
01031         # functions will be applied: S2, S4, S6, S8, S1, S3, S5, S7.
01032         set lefttemp [expr {[lindex $pc2bytes0 [expr {($left >> 28) & 0x0000000f}]] | \
01033                     [lindex $pc2bytes1 [expr {($left >> 24) & 0x0000000f}]] | \
01034                     [lindex $pc2bytes2 [expr {($left >> 20) & 0x0000000f}]] | \
01035                     [lindex $pc2bytes3 [expr {($left >> 16) & 0x0000000f}]] | \
01036                     [lindex $pc2bytes4 [expr {($left >> 12) & 0x0000000f}]] | \
01037                     [lindex $pc2bytes5 [expr {($left >> 8) & 0x0000000f}]] | \
01038                     [lindex $pc2bytes6 [expr {($left >> 4) & 0x0000000f}]]}];
01039         set righttemp [expr {[lindex $pc2bytes7 [expr {($right >> 28) & 0x0000000f}]] | \
01040                      [lindex $pc2bytes8 [expr {($right >> 24) & 0x0000000f}]] | \
01041                      [lindex $pc2bytes9 [expr {($right >> 20) & 0x0000000f}]] | \
01042                      [lindex $pc2bytes10 [expr {($right >> 16) & 0x0000000f}]] | \
01043                      [lindex $pc2bytes11 [expr {($right >> 12) & 0x0000000f}]] | \
01044                      [lindex $pc2bytes12 [expr {($right >> 8) & 0x0000000f}]] | \
01045                      [lindex $pc2bytes13 [expr {($right >> 4) & 0x0000000f}]]}];
01046         set temp [expr {(($righttemp >> 16) ^ $lefttemp) & 0x0000ffff}];
01047         lappend keys [expr {$lefttemp ^ $temp}];
01048         lappend keys [expr {$righttemp ^ ($temp << 16)}];
01049     }
01050     # Return the keys we've created.
01051     return $keys;
01052     }; /*  End of createKeys.*/
01053 }; /*  End of des namespace eval.*/
01054 
01055 package provide tclDESjr 1.0.0
01056 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1