tcldesjr.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 namespace des {
00068 variable keys =
00069 keysets = (ndx) 1
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
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
00119
00120
00121
00122
00123
00124
00125
00126
00127
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
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
00164
00165
00166
00167
00168
00169
00170
00171
00172
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
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
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
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
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
00262 expr {$encrypt ? [ looping = $desEncrypt] : [ looping = $desDecrypt]}
00263
00264
00265 append message "\0\0\0\0\0\0\0\0"
00266
00267
00268 result = {};
00269 tempresult = {};
00270
00271
00272 if {$mode == 1} {
00273
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
00287 binary scan $ivec H8H8 cbcleftTemp cbcrightTemp
00288 cbcleft = "0x$cbcleftTemp"
00289 cbcright = "0x$cbcrightTemp"
00290 }
00291
00292
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
00302
00303
00304
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
00318
00319
00320
00321
00322
00323
00324
00325
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
00349
00350
00351
00352 endloop = [lindex $looping 1];
00353 loopinc = [lindex $looping 2];
00354
00355
00356
00357
00358
00359 for { i = [lindex $looping 0]} \
00360 {$i != $endloop} {incr i $loopinc} {
00361
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
00368
00369
00370
00371
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
00384
00385
00386 }
00387 temp = $left;
00388 left = $right;
00389 right = $temp;
00390
00391
00392
00393
00394
00395 left = [expr {((($left >> 1) & 0x7fffffff) \
00396 | (($left << 31) & 0xffffffff))}];
00397 right = [expr {((($right >> 1) & 0x7fffffff) \
00398 | (($right << 31) & 0xffffffff))}];
00399
00400
00401
00402
00403
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
00421
00422
00423
00424
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
00439
00440
00441 incr chunk 8;
00442 if {$chunk == 512} {
00443 append result $tempresult
00444 tempresult = {};
00445 chunk = 0;
00446 }
00447 };
00448
00449 if {$mode == 1} {
00450 if {$encrypt} {
00451
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
00461 return ${result}$tempresult
00462 };
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
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 };
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
00912 variable shifts {0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0};
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
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 };
01053 };
01054
01055 package provide tclDESjr 1.0.0
01056