Functions/Subroutines | |
ret | stream (type keyset, type message, type encrypt, type mode, type iv, optional kbits=64) |
ret | createKeys (type key, optional weak=0) |
Variables | |
variable | keys |
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] |
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] |
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] |
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] |
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] |
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] |
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] |
ret des::createKeys | ( | type | key, | |
optional | weak = 0 | |||
) |
Definition at line 923 of file tcldesjr.tcl.
00923 { 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.*/
ret des::stream | ( | type | keyset, | |
type | message, | |||
type | encrypt, | |||
type | mode, | |||
type | iv, | |||
optional | kbits = 64 | |||
) |
Definition at line 481 of file tcldesjr.tcl.
References error(), keys, spfunction2, spfunction3, spfunction4, spfunction5, spfunction6, spfunction7, and spfunction8.
00481 { 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*/
variable des::keys |
Definition at line 68 of file tcldes.tcl.
Referenced by createKeys(), and stream().
variable des::spfunction2 |
Definition at line 212 of file tcldes.tcl.
Referenced by stream().
variable des::spfunction3 |
Definition at line 213 of file tcldes.tcl.
Referenced by stream().
variable des::spfunction4 |
Definition at line 214 of file tcldes.tcl.
Referenced by stream().
variable des::spfunction5 |
Definition at line 215 of file tcldes.tcl.
Referenced by stream().
variable des::spfunction6 |
Definition at line 216 of file tcldes.tcl.
Referenced by stream().
variable des::spfunction7 |
Definition at line 217 of file tcldes.tcl.
Referenced by stream().
variable des::spfunction8 |
Definition at line 218 of file tcldes.tcl.
Referenced by stream().