aes.tcl

Go to the documentation of this file.
00001 /*  aes.tcl - */
00002 /* */
00003 /*  Copyright (c) 2005 Thorsten Schloermann*/
00004 /*  Copyright (c) 2005 Pat Thoyts <patthoyts@users.sourceforge.net>*/
00005 /* */
00006 /*  A Tcl implementation of the Advanced Encryption Standard (US FIPS PUB 197)*/
00007 /* */
00008 /*  AES is a block cipher with a block size of 128 bits and a variable*/
00009 /*  key size of 128, 192 or 256 bits.*/
00010 /*  The algorithm works on each block as a 4x4 state array. There are 4 steps*/
00011 /*  in each round:*/
00012 /*    SubBytes    a non-linear substitution step using a predefined S-box*/
00013 /*    ShiftRows   cyclic transposition of rows in the state matrix*/
00014 /*    MixColumns  transformation upon columns in the state matrix*/
00015 /*    AddRoundKey application of round specific sub-key*/
00016 /* */
00017 /*  -------------------------------------------------------------------------*/
00018 /*  See the file "license.terms" for information on usage and redistribution*/
00019 /*  of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00020 /*  -------------------------------------------------------------------------*/
00021 
00022 package require Tcl 8.2
00023 
00024 namespace ::aes {
00025     variable version 1.0.0
00026     variable rcsid {$Id: aes.tcl,v 1.5 2005/09/04 17:15:56 patthoyts Exp $}
00027     variable uid ; if {![info exists uid]} {  uid =  0 }
00028 
00029     namespace export {aes}
00030 
00031     /*  constants*/
00032 
00033     /*  S-box*/
00034     variable sbox {
00035         0x63 0x7c 0x77 0x7b 0xf2 0x6b 0x6f 0xc5 0x30 0x01 0x67 0x2b 0xfe 0xd7 0xab 0x76
00036         0xca 0x82 0xc9 0x7d 0xfa 0x59 0x47 0xf0 0xad 0xd4 0xa2 0xaf 0x9c 0xa4 0x72 0xc0
00037         0xb7 0xfd 0x93 0x26 0x36 0x3f 0xf7 0xcc 0x34 0xa5 0xe5 0xf1 0x71 0xd8 0x31 0x15
00038         0x04 0xc7 0x23 0xc3 0x18 0x96 0x05 0x9a 0x07 0x12 0x80 0xe2 0xeb 0x27 0xb2 0x75
00039         0x09 0x83 0x2c 0x1a 0x1b 0x6e 0x5a 0xa0 0x52 0x3b 0xd6 0xb3 0x29 0xe3 0x2f 0x84
00040         0x53 0xd1 0x00 0xed 0x20 0xfc 0xb1 0x5b 0x6a 0xcb 0xbe 0x39 0x4a 0x4c 0x58 0xcf
00041         0xd0 0xef 0xaa 0xfb 0x43 0x4d 0x33 0x85 0x45 0xf9 0x02 0x7f 0x50 0x3c 0x9f 0xa8
00042         0x51 0xa3 0x40 0x8f 0x92 0x9d 0x38 0xf5 0xbc 0xb6 0xda 0x21 0x10 0xff 0xf3 0xd2
00043         0xcd 0x0c 0x13 0xec 0x5f 0x97 0x44 0x17 0xc4 0xa7 0x7e 0x3d 0x64 0x5d 0x19 0x73
00044         0x60 0x81 0x4f 0xdc 0x22 0x2a 0x90 0x88 0x46 0xee 0xb8 0x14 0xde 0x5e 0x0b 0xdb
00045         0xe0 0x32 0x3a 0x0a 0x49 0x06 0x24 0x5c 0xc2 0xd3 0xac 0x62 0x91 0x95 0xe4 0x79
00046         0xe7 0xc8 0x37 0x6d 0x8d 0xd5 0x4e 0xa9 0x6c 0x56 0xf4 0xea 0x65 0x7a 0xae 0x08
00047         0xba 0x78 0x25 0x2e 0x1c 0xa6 0xb4 0xc6 0xe8 0xdd 0x74 0x1f 0x4b 0xbd 0x8b 0x8a
00048         0x70 0x3e 0xb5 0x66 0x48 0x03 0xf6 0x0e 0x61 0x35 0x57 0xb9 0x86 0xc1 0x1d 0x9e
00049         0xe1 0xf8 0x98 0x11 0x69 0xd9 0x8e 0x94 0x9b 0x1e 0x87 0xe9 0xce 0x55 0x28 0xdf
00050         0x8c 0xa1 0x89 0x0d 0xbf 0xe6 0x42 0x68 0x41 0x99 0x2d 0x0f 0xb0 0x54 0xbb 0x16
00051     }
00052     /*  inverse S-box*/
00053     variable xobs {
00054         0x52 0x09 0x6a 0xd5 0x30 0x36 0xa5 0x38 0xbf 0x40 0xa3 0x9e 0x81 0xf3 0xd7 0xfb
00055         0x7c 0xe3 0x39 0x82 0x9b 0x2f 0xff 0x87 0x34 0x8e 0x43 0x44 0xc4 0xde 0xe9 0xcb
00056         0x54 0x7b 0x94 0x32 0xa6 0xc2 0x23 0x3d 0xee 0x4c 0x95 0x0b 0x42 0xfa 0xc3 0x4e
00057         0x08 0x2e 0xa1 0x66 0x28 0xd9 0x24 0xb2 0x76 0x5b 0xa2 0x49 0x6d 0x8b 0xd1 0x25
00058         0x72 0xf8 0xf6 0x64 0x86 0x68 0x98 0x16 0xd4 0xa4 0x5c 0xcc 0x5d 0x65 0xb6 0x92
00059         0x6c 0x70 0x48 0x50 0xfd 0xed 0xb9 0xda 0x5e 0x15 0x46 0x57 0xa7 0x8d 0x9d 0x84
00060         0x90 0xd8 0xab 0x00 0x8c 0xbc 0xd3 0x0a 0xf7 0xe4 0x58 0x05 0xb8 0xb3 0x45 0x06
00061         0xd0 0x2c 0x1e 0x8f 0xca 0x3f 0x0f 0x02 0xc1 0xaf 0xbd 0x03 0x01 0x13 0x8a 0x6b
00062         0x3a 0x91 0x11 0x41 0x4f 0x67 0xdc 0xea 0x97 0xf2 0xcf 0xce 0xf0 0xb4 0xe6 0x73
00063         0x96 0xac 0x74 0x22 0xe7 0xad 0x35 0x85 0xe2 0xf9 0x37 0xe8 0x1c 0x75 0xdf 0x6e
00064         0x47 0xf1 0x1a 0x71 0x1d 0x29 0xc5 0x89 0x6f 0xb7 0x62 0x0e 0xaa 0x18 0xbe 0x1b
00065         0xfc 0x56 0x3e 0x4b 0xc6 0xd2 0x79 0x20 0x9a 0xdb 0xc0 0xfe 0x78 0xcd 0x5a 0xf4
00066         0x1f 0xdd 0xa8 0x33 0x88 0x07 0xc7 0x31 0xb1 0x12 0x10 0x59 0x27 0x80 0xec 0x5f
00067         0x60 0x51 0x7f 0xa9 0x19 0xb5 0x4a 0x0d 0x2d 0xe5 0x7a 0x9f 0x93 0xc9 0x9c 0xef
00068         0xa0 0xe0 0x3b 0x4d 0xae 0x2a 0xf5 0xb0 0xc8 0xeb 0xbb 0x3c 0x83 0x53 0x99 0x61
00069         0x17 0x2b 0x04 0x7e 0xba 0x77 0xd6 0x26 0xe1 0x69 0x14 0x63 0x55 0x21 0x0c 0x7d
00070     }
00071 }
00072 
00073 /*  aes::Init --*/
00074 /* */
00075 /*  Initialise our AES state and calculate the key schedule. An initialization*/
00076 /*  vector is maintained in the state for modes that require one. The key must*/
00077 /*  be binary data of the correct size and the IV must be 16 bytes.*/
00078 /* */
00079 /*  Nk: columns of the key-array*/
00080 /*  Nr: number of rounds (depends on key-length)*/
00081 /*  Nb: columns of the text-block, is always 4 in AES*/
00082 /* */
00083 ret  ::aes::Init (type mode , type key , type iv) {
00084     switch -exact -- $mode {
00085         ecb - cbc { }
00086         cfb - ofb {
00087             return -code error "$mode mode not implemented"
00088         }
00089         default {
00090             return -code error "invalid mode \"$mode\":\
00091                 must be one of ecb or cbc."
00092         }
00093     }
00094 
00095     set size [expr {[string length $key] << 3}]
00096     switch -exact -- $size {
00097         128 {set Nk 4; set Nr 10; set Nb 4}
00098         192 {set Nk 6; set Nr 12; set Nb 4}
00099         256 {set Nk 8; set Nr 14; set Nb 4}
00100         default {
00101             return -code error "invalid key size \"$size\":\
00102                 must be one of 128, 192 or 256."
00103         }
00104     }
00105 
00106     variable uid
00107     set Key [namespace current]::[incr uid]
00108     upvar #0 $Key state
00109     array set state [list M $mode K $key I $iv Nk $Nk Nr $Nr Nb $Nb W {}]
00110     ExpandKey $Key
00111     return $Key
00112 }
00113 
00114 /*  aes::Reset --*/
00115 /* */
00116 /*  Reset the initialization vector for the specified key. This permits the*/
00117 /*  key to be reused for encryption or decryption without the expense of*/
00118 /*  re-calculating the key schedule.*/
00119 /* */
00120 ret  ::aes::Reset (type Key , type iv) {
00121     upvar #0 $Key state
00122     set state(I) $iv
00123     return
00124 }
00125     
00126 /*  aes::Final --*/
00127 /* */
00128 /*  Clean up the key state*/
00129 /* */
00130 ret  ::aes::Final (type Key) {
00131     # FRINK: nocheck
00132     unset $Key
00133 }
00134 
00135 /*  -------------------------------------------------------------------------*/
00136 
00137 /*  5.1 Cipher:  Encipher a single block of 128 bits.*/
00138 ret  ::aes::EncryptBlock (type Key , type block) {
00139     upvar #0 $Key state
00140     if {[binary scan $block I4 data] != 1} {
00141         return -code error "invalid block size: blocks must be 16 bytes"
00142     }
00143 
00144     if {[string equal $state(M) cbc]} {
00145         if {[binary scan $state(I) I4 iv] != 1} {
00146             return -code error "invalid initialization vector: must be 16 bytes"
00147         }
00148         for {set n 0} {$n < 4} {incr n} {
00149             lappend data2 [expr {[lindex $data $n] ^ [lindex $iv $n]}]
00150         }
00151         set data $data2
00152     }
00153 
00154     set data [AddRoundKey $Key 0 $data]
00155     for {set n 1} {$n < $state(Nr)} {incr n} {
00156         set data [AddRoundKey $Key $n [MixColumns [ShiftRows [SubBytes $data]]]]
00157     }
00158     set data [AddRoundKey $Key $n [ShiftRows [SubBytes $data]]]
00159     return [set state(I) [binary format I4 $data]]
00160 }
00161 
00162 /*  5.3: Inverse Cipher: Decipher a single 128 bit block.*/
00163 ret  ::aes::DecryptBlock (type Key , type block) {
00164     upvar #0 $Key state
00165     if {[binary scan $block I4 data] != 1} {
00166         return -code error "invalid block size: block must be 16 bytes"
00167     }
00168 
00169     set n $state(Nr)
00170     set data [AddRoundKey $Key $state(Nr) $data]
00171     for {incr n -1} {$n > 0} {incr n -1} {
00172         set data [InvMixColumns [AddRoundKey $Key $n [InvSubBytes [InvShiftRows $data]]]]
00173     }
00174     set data [AddRoundKey $Key $n [InvSubBytes [InvShiftRows $data]]]
00175     
00176     if {[string equal $state(M) cbc]} {
00177         if {[binary scan $state(I) I4 iv] != 1} {
00178             return -code error "invalid initialization vector: must be 16 bytes"
00179         }
00180         for {set n 0} {$n < 4} {incr n} {
00181             lappend data2 [expr {[lindex $data $n] ^ [lindex $iv $n]}]
00182         }
00183         set data $data2
00184     }
00185     
00186     set state(I) $block
00187     return [binary format I4 $data]
00188 }
00189 
00190 /*  5.2: KeyExpansion*/
00191 ret  ::aes::ExpandKey (type Key) {
00192     upvar #0 $Key state
00193     set Rcon [list 0x00000000 0x01000000 0x02000000 0x04000000 0x08000000 \
00194                   0x10000000 0x20000000 0x40000000 0x80000000 0x1b000000 \
00195                   0x36000000 0x6c000000 0xd8000000 0xab000000 0x4d000000]
00196     # Split the key into Nk big-endian words
00197     binary scan $state(K) I* W
00198     set max [expr {$state(Nb) * ($state(Nr) + 1)}]
00199     set i $state(Nk)
00200     set h $state(Nk) ; incr h -1
00201     set j 0
00202     for {} {$i < $max} {incr i; incr h; incr j} {
00203         set temp [lindex $W $h]
00204         if {($i % $state(Nk)) == 0} {
00205             set sub [SubWord [RotWord $temp]]
00206             set rc [lindex $Rcon [expr {$i/$state(Nk)}]]
00207             set temp [expr {$sub ^ $rc}]
00208         } elseif {$state(Nk) > 6 && ($i % $state(Nk)) == 4} { 
00209             set temp [SubWord $temp]
00210         }
00211         lappend W [expr {[lindex $W $j] ^ $temp}]
00212     }
00213     set state(W) $W
00214     return
00215 }
00216 
00217 /*  5.2: Key Expansion: Apply S-box to each byte in the 32 bit word*/
00218 ret  ::aes::SubWord (type w) {
00219     variable sbox
00220     set s3 [lindex $sbox [expr {(($w >> 24) & 255)}]]
00221     set s2 [lindex $sbox [expr {(($w >> 16) & 255)}]]
00222     set s1 [lindex $sbox [expr {(($w >> 8 ) & 255)}]]
00223     set s0 [lindex $sbox [expr {( $w        & 255)}]]
00224     return [expr {($s3 << 24) | ($s2 << 16) | ($s1 << 8) | $s0}]
00225 }
00226 
00227 ret  ::aes::InvSubWord (type w) {
00228     variable xobs
00229     set s3 [lindex $xobs [expr {(($w >> 24) & 255)}]]
00230     set s2 [lindex $xobs [expr {(($w >> 16) & 255)}]]
00231     set s1 [lindex $xobs [expr {(($w >> 8 ) & 255)}]]
00232     set s0 [lindex $xobs [expr {( $w        & 255)}]]
00233     return [expr {($s3 << 24) | ($s2 << 16) | ($s1 << 8) | $s0}]
00234 }
00235 
00236 /*  5.2: Key Expansion: Rotate a 32bit word by 8 bits*/
00237 ret  ::aes::RotWord (type w) {
00238     return [expr {(($w << 8) | (($w >> 24) & 0xff)) & 0xffffffff}]
00239 }
00240 
00241 /*  5.1.1: SubBytes() Transformation*/
00242 ret  ::aes::SubBytes (type words) {
00243     set r {}
00244     foreach w $words {
00245         lappend r [SubWord $w]
00246     }
00247     return $r
00248 }
00249 
00250 /*  5.3.2: InvSubBytes() Transformation*/
00251 ret  ::aes::InvSubBytes (type words) {
00252     set r {}
00253     foreach w $words {
00254         lappend r [InvSubWord $w]
00255     }
00256     return $r
00257 }
00258 
00259 /*  5.1.2: ShiftRows() Transformation*/
00260 ret  ::aes::ShiftRows (type words) {
00261     for {set n0 0} {$n0 < 4} {incr n0} {
00262         set n1 [expr {($n0 + 1) % 4}]
00263         set n2 [expr {($n0 + 2) % 4}]
00264         set n3 [expr {($n0 + 3) % 4}]
00265         lappend r [expr {(  [lindex $words $n0] & 0xff000000)
00266                          | ([lindex $words $n1] & 0x00ff0000)
00267                          | ([lindex $words $n2] & 0x0000ff00)
00268                          | ([lindex $words $n3] & 0x000000ff)
00269                      }]
00270     }
00271     return $r
00272 }
00273 
00274 
00275 /*  5.3.1: InvShiftRows() Transformation*/
00276 ret  ::aes::InvShiftRows (type words) {
00277     for {set n0 0} {$n0 < 4} {incr n0} {
00278         set n1 [expr {($n0 + 1) % 4}]
00279         set n2 [expr {($n0 + 2) % 4}]
00280         set n3 [expr {($n0 + 3) % 4}]
00281         lappend r [expr {(  [lindex $words $n0] & 0xff000000)
00282                          | ([lindex $words $n3] & 0x00ff0000)
00283                          | ([lindex $words $n2] & 0x0000ff00)
00284                          | ([lindex $words $n1] & 0x000000ff)
00285                      }]
00286     }
00287     return $r
00288 }
00289 
00290 /*  5.1.3: MixColumns() Transformation*/
00291 ret  ::aes::MixColumns (type words) {
00292     set r {}
00293     foreach w $words {
00294         set r0 [expr {(($w >> 24) & 255)}]
00295         set r1 [expr {(($w >> 16) & 255)}]
00296         set r2 [expr {(($w >> 8 ) & 255)}]
00297         set r3 [expr {( $w        & 255)}]
00298 
00299         set s0 [expr {[GFMult2 $r0] ^ [GFMult3 $r1] ^ $r2 ^ $r3}]
00300         set s1 [expr {$r0 ^ [GFMult2 $r1] ^ [GFMult3 $r2] ^ $r3}]
00301         set s2 [expr {$r0 ^ $r1 ^ [GFMult2 $r2] ^ [GFMult3 $r3]}]
00302         set s3 [expr {[GFMult3 $r0] ^ $r1 ^ $r2 ^ [GFMult2 $r3]}]
00303 
00304         lappend r [expr {($s0 << 24) | ($s1 << 16) | ($s2 << 8) | $s3}]
00305     }
00306     return $r
00307 }
00308 
00309 /*  5.3.3: InvMixColumns() Transformation*/
00310 ret  ::aes::InvMixColumns (type words) {
00311     set r {}
00312     foreach w $words {
00313         set r0 [expr {(($w >> 24) & 255)}]
00314         set r1 [expr {(($w >> 16) & 255)}]
00315         set r2 [expr {(($w >> 8 ) & 255)}]
00316         set r3 [expr {( $w        & 255)}]
00317 
00318         set s0 [expr {[GFMult0e $r0] ^ [GFMult0b $r1] ^ [GFMult0d $r2] ^ [GFMult09 $r3]}]
00319         set s1 [expr {[GFMult09 $r0] ^ [GFMult0e $r1] ^ [GFMult0b $r2] ^ [GFMult0d $r3]}]
00320         set s2 [expr {[GFMult0d $r0] ^ [GFMult09 $r1] ^ [GFMult0e $r2] ^ [GFMult0b $r3]}]
00321         set s3 [expr {[GFMult0b $r0] ^ [GFMult0d $r1] ^ [GFMult09 $r2] ^ [GFMult0e $r3]}]
00322 
00323         lappend r [expr {($s0 << 24) | ($s1 << 16) | ($s2 << 8) | $s3}]
00324     }
00325     return $r
00326 }
00327 
00328 /*  5.1.4: AddRoundKey() Transformation*/
00329 ret  ::aes::AddRoundKey (type Key , type round , type words) {
00330     upvar #0 $Key state
00331     set r {}
00332     set n [expr {$round * $state(Nb)}]
00333     foreach w $words {
00334         lappend r [expr {$w ^ [lindex $state(W) $n]}]
00335         incr n
00336     }
00337     return $r
00338 }
00339     
00340 /*  -------------------------------------------------------------------------*/
00341 /*  ::aes::GFMult**/
00342 /* */
00343 /*  some needed functions for multiplication in a Galois-field*/
00344 /* */
00345 ret  ::aes::GFMult2 (type number) {
00346     # this is a tabular representation of xtime (multiplication by 2)
00347     # it is used instead of calculation to prevent timing attacks
00348     set xtime {
00349         0x00 0x02 0x04 0x06 0x08 0x0a 0x0c 0x0e 0x10 0x12 0x14 0x16 0x18 0x1a 0x1c 0x1e
00350         0x20 0x22 0x24 0x26 0x28 0x2a 0x2c 0x2e 0x30 0x32 0x34 0x36 0x38 0x3a 0x3c 0x3e 
00351         0x40 0x42 0x44 0x46 0x48 0x4a 0x4c 0x4e 0x50 0x52 0x54 0x56 0x58 0x5a 0x5c 0x5e
00352         0x60 0x62 0x64 0x66 0x68 0x6a 0x6c 0x6e 0x70 0x72 0x74 0x76 0x78 0x7a 0x7c 0x7e 
00353         0x80 0x82 0x84 0x86 0x88 0x8a 0x8c 0x8e 0x90 0x92 0x94 0x96 0x98 0x9a 0x9c 0x9e 
00354         0xa0 0xa2 0xa4 0xa6 0xa8 0xaa 0xac 0xae 0xb0 0xb2 0xb4 0xb6 0xb8 0xba 0xbc 0xbe 
00355         0xc0 0xc2 0xc4 0xc6 0xc8 0xca 0xcc 0xce 0xd0 0xd2 0xd4 0xd6 0xd8 0xda 0xdc 0xde 
00356         0xe0 0xe2 0xe4 0xe6 0xe8 0xea 0xec 0xee 0xf0 0xf2 0xf4 0xf6 0xf8 0xfa 0xfc 0xfe 
00357         0x1b 0x19 0x1f 0x1d 0x13 0x11 0x17 0x15 0x0b 0x09 0x0f 0x0d 0x03 0x01 0x07 0x05 
00358         0x3b 0x39 0x3f 0x3d 0x33 0x31 0x37 0x35 0x2b 0x29 0x2f 0x2d 0x23 0x21 0x27 0x25 
00359         0x5b 0x59 0x5f 0x5d 0x53 0x51 0x57 0x55 0x4b 0x49 0x4f 0x4d 0x43 0x41 0x47 0x45 
00360         0x7b 0x79 0x7f 0x7d 0x73 0x71 0x77 0x75 0x6b 0x69 0x6f 0x6d 0x63 0x61 0x67 0x65 
00361         0x9b 0x99 0x9f 0x9d 0x93 0x91 0x97 0x95 0x8b 0x89 0x8f 0x8d 0x83 0x81 0x87 0x85 
00362         0xbb 0xb9 0xbf 0xbd 0xb3 0xb1 0xb7 0xb5 0xab 0xa9 0xaf 0xad 0xa3 0xa1 0xa7 0xa5 
00363         0xdb 0xd9 0xdf 0xdd 0xd3 0xd1 0xd7 0xd5 0xcb 0xc9 0xcf 0xcd 0xc3 0xc1 0xc7 0xc5 
00364         0xfb 0xf9 0xff 0xfd 0xf3 0xf1 0xf7 0xf5 0xeb 0xe9 0xef 0xed 0xe3 0xe1 0xe7 0xe5
00365     }
00366     return [lindex $xtime $number]
00367 }
00368 
00369 ret  ::aes::GFMult3 (type number) {
00370     # multliply by 2 (via GFMult2) and add the number again on the result (via XOR)
00371     return [expr {$number ^ [GFMult2 $number]}]
00372 }
00373 
00374 ret  ::aes::GFMult09 (type number) {
00375     # 09 is: (02*02*02) + 01
00376     return [expr {[GFMult2 [GFMult2 [GFMult2 $number]]] ^ $number}]
00377 }
00378 
00379 ret  ::aes::GFMult0b (type number) {
00380     # 0b is: (02*02*02) + 02 + 01
00381     #return [expr [GFMult2 [GFMult2 [GFMult2 $number]]] ^ [GFMult2 $number] ^ $number]
00382     #set g0 [GFMult2 $number]
00383     return [expr {[GFMult09 $number] ^ [GFMult2 $number]}]
00384 }
00385 
00386 ret  ::aes::GFMult0d (type number) {
00387     # 0d is: (02*02*02) + (02*02) + 01
00388     set temp [GFMult2 [GFMult2 $number]]
00389     return [expr {[GFMult2 $temp] ^ ($temp ^ $number)}]
00390 }
00391 
00392 ret  ::aes::GFMult0e (type number) {
00393     # 0e is: (02*02*02) + (02*02) + 02
00394     set temp [GFMult2 [GFMult2 $number]]
00395     return [expr {[GFMult2 $temp] ^ ($temp ^ [GFMult2 $number])}]
00396 }
00397 
00398 /*  -------------------------------------------------------------------------*/
00399 
00400 /*  aes::Encrypt --*/
00401 /* */
00402 /*  Encrypt a blocks of plain text and returns blocks of cipher text.*/
00403 /*  The input data must be a multiple of the block size (16).*/
00404 /* */
00405 ret  ::aes::Encrypt (type Key , type data) {
00406     set len [string length $data]
00407     if {($len % 16) != 0} {
00408         return -code error "invalid block size: AES requires 16 byte blocks"
00409     }
00410 
00411     set result {}
00412     for {set i 0} {$i < $len} {incr i 1} {
00413         set block [string range $data $i [incr i 15]]
00414         append result [EncryptBlock $Key $block]
00415     }
00416     return $result
00417 }
00418 
00419 /*  aes::DecryptBlock --*/
00420 /* */
00421 /*  Decrypt a blocks of cipher text and returns blocks of plain text.*/
00422 /*  The input data must be a multiple of the block size (16).*/
00423 /* */
00424 ret  ::aes::Decrypt (type Key , type data) {
00425     set len [string length $data]
00426     if {($len % 16) != 0} {
00427         return -code error "invalid block size: AES requires 16 byte blocks"
00428     }
00429 
00430     set result {}
00431     for {set i 0} {$i < $len} {incr i 1} {
00432         set block [string range $data $i [incr i 15]]
00433         append result [DecryptBlock $Key $block]
00434     }
00435     return $result
00436 }
00437 
00438 /*  -------------------------------------------------------------------------*/
00439 /*  Fileevent handler for chunked file reading.*/
00440 /* */
00441 ret  ::aes::Chunk (type Key , type in , optional out ={) {chunksize 4096}} {
00442     upvar /* 0 $Key state*/
00443     
00444     if {[eof $in]} {
00445         fileevent $in readable {}
00446          state = (reading) 0
00447     }
00448 
00449      data =  [read $in $chunksize]
00450     /*  FIX ME: we should ony pad after eof*/
00451      data =  [Pad $data 16]
00452     
00453     if {$out == {}} {
00454         append state(output) [$state(cmd) $Key $data]
00455     } else {
00456         puts -nonewline $out [$state(cmd) $Key $data]
00457     }
00458 }
00459 
00460 ret  ::aes::SetOneOf (type lst , type item) {
00461     set ndx [lsearch -glob $lst "${item}*"]
00462     if {$ndx == -1} {
00463         set err [join $lst ", "]
00464         return -code error "invalid mode \"$item\": must be one of $err"
00465     }
00466     return [lindex $lst $ndx]
00467 }
00468 
00469 ret  ::aes::CheckSize (type what , type size , type thing) {
00470     if {[string length $thing] != $size} {
00471         return -code error "invalid value for $what: must be $size bytes long"
00472     }
00473     return $thing
00474 }
00475 
00476 ret  ::aes::Pad (type data , type blocksize , optional fill =\0) {
00477     set len [string length $data]
00478     if {$len == 0} {
00479         set data [string repeat $fill $blocksize]
00480     } elseif {($len % $blocksize) != 0} {
00481         set pad [expr {$blocksize - ($len % $blocksize)}]
00482         append data [string repeat $fill $pad]
00483     }
00484     return $data
00485 }
00486 
00487 ret  ::aes::Pop (type varname , optional nth =0) {
00488     upvar $varname args
00489     set r [lindex $args $nth]
00490     set args [lreplace $args $nth $nth]
00491     return $r
00492 }
00493 
00494 ret  ::aes::Hex (type data) {
00495     binary scan $data H* r
00496     return $r 
00497 }
00498 
00499 ret  ::aes::aes (type args) {
00500     array set opts {-dir encrypt -mode cbc -key {} -in {} -out {} -chunksize 4096 -hex 0}
00501     set opts(-iv) [string repeat \0 16]
00502     set modes {ecb cbc}
00503     set dirs {encrypt decrypt}
00504     while {[string match -* [set option [lindex $args 0]]]} {
00505         switch -exact -- $option {
00506             -mode      { set opts(-mode) [SetOneOf $modes [Pop args 1]] }
00507             -dir       { set opts(-dir) [SetOneOf $dirs [Pop args 1]] }
00508             -iv        { set opts(-iv) [CheckSize -iv 16 [Pop args 1]] }
00509             -key       { set opts(-key) [Pop args 1] }
00510             -in        { set opts(-in) [Pop args 1] }
00511             -out       { set opts(-out) [Pop args 1] }
00512             -chunksize { set opts(-chunksize) [Pop args 1] }
00513             -hex       { set opts(-hex) 1 }
00514             --         { Pop args ; break }
00515             default {
00516                 set err [join [lsort [array names opts]] ", "]
00517                 return -code error "bad option \"$option\":\
00518                     must be one of $err"
00519             }
00520         }
00521         Pop args
00522     }
00523 
00524     if {$opts(-key) == {}} {
00525         return -code error "no key provided: the -key option is required"
00526     }
00527 
00528     set r {}
00529     if {$opts(-in) == {}} {
00530 
00531         if {[llength $args] != 1} {
00532             return -code error "wrong \# args:\
00533                 should be \"aes ?options...? -key keydata plaintext\""
00534         }
00535 
00536         set data [Pad [lindex $args 0] 16]
00537         set Key [Init $opts(-mode) $opts(-key) $opts(-iv)]
00538         if {[string equal $opts(-dir) "encrypt"]} {
00539             set r [Encrypt $Key $data]
00540         } else {
00541             set r [Decrypt $Key $data]
00542         }
00543 
00544         if {$opts(-out) != {}} {
00545             puts -nonewline $opts(-out) $r
00546             set r {}
00547         }
00548         Final $Key
00549 
00550     } else {
00551 
00552         if {[llength $args] != 0} {
00553             return -code error "wrong \# args:\
00554                 should be \"aes ?options...? -key keydata -in channel\""
00555         }
00556 
00557         set Key [Init $opts(-mode) $opts(-key) $opts(-iv)]
00558         upvar $Key state
00559         set state(reading) 1
00560         if {[string equal $opts(-dir) "encrypt"]} {
00561             set state(cmd) Encrypt
00562         } else {
00563             set state(cmd) Decrypt
00564         }
00565         set state(output) ""
00566         fileevent $opts(-in) readable \
00567             [list [namespace origin Chunk] \
00568                  $Key $opts(-in) $opts(-out) $opts(-chunksize)]
00569         if {[info commands ::tkwait] != {}} {
00570             tkwait variable [subst $Key](reading)
00571         } else {
00572             vwait [subst $Key](reading)
00573         }
00574         if {$opts(-out) == {}} {
00575             set r $state(output)
00576         }
00577         Final $Key
00578 
00579     }
00580 
00581     if {$opts(-hex)} {
00582         set r [Hex $r]
00583     }
00584     return $r
00585 }
00586 
00587 /*  -------------------------------------------------------------------------*/
00588 
00589 package provide aes $::aes::version
00590 
00591 /*  -------------------------------------------------------------------------*/
00592 /*  Local variables:*/
00593 /*  mode: tcl*/
00594 /*  indent-tabs-mode: nil*/
00595 /*  End:*/
00596 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1