Changes to modules/aes/aes.man.
Changes to modules/aes/aes.tcl.
︙ | | |
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
-
+
|
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------
package require Tcl 8.5
namespace eval ::aes {
variable version 1.2
variable version 1.2.1
variable rcsid {$Id: aes.tcl,v 1.7 2010/07/06 19:39:00 andreas_kupries Exp $}
variable uid ; if {![info exists uid]} { set uid 0 }
namespace export aes
# constants
|
︙ | | |
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
-
-
-
+
+
+
+
-
-
+
+
+
+
+
+
-
+
-
-
-
+
-
-
+
-
-
-
+
+
+
-
-
+
+
+
+
+
-
-
+
-
-
-
-
-
+
-
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
+
+
-
-
-
-
-
+
+
-
-
|
# 5.1 Cipher: Encipher a single block of 128 bits.
proc ::aes::EncryptBlock {Key block} {
upvar #0 $Key state
if {[binary scan $block Iu4 data] != 1} {
return -code error "invalid block size: blocks must be 16 bytes"
}
if {[string equal $state(M) cbc]} {
for {set n 0} {$n < 4} {incr n} {
lappend data2 [expr {0xffffffff & ([lindex $data $n] ^ [lindex $state(I) $n])}]
if {$state(M) eq {cbc}} {
# Loop unrolled.
lassign $data d0 d1 d2 d3
lassign $state(I) s0 s1 s2 s3
}
set data $data2
set data [list \
[expr {$d0 ^ $s0}] \
[expr {$d1 ^ $s1}] \
[expr {$d2 ^ $s2}] \
[expr {$d3 ^ $s3}] ]
}
set data [AddRoundKey $Key 0 $data]
for {set n 1} {$n < $state(Nr)} {incr n} {
set data [AddRoundKey $Key $n [MixColumns [ShiftRows [SubBytes $data]]]]
}
set data [AddRoundKey $Key $n [ShiftRows [SubBytes $data]]]
# Bug 2993029:
# Force all elements of data into the 32bit range.
# Loop unrolled
set res {}
set res [Clamp32 $data]
foreach d $data {
lappend res [expr {$d & 0xffffffff}]
}
set state(I) $res
return [binary format Iu4 $res]
binary format Iu4 $res
}
# 5.3: Inverse Cipher: Decipher a single 128 bit block.
proc ::aes::DecryptBlock {Key block} {
upvar #0 $Key state
if {[binary scan $block Iu4 data] != 1} {
return -code error "invalid block size: block must be 16 bytes"
}
set iv $data
set n $state(Nr)
set data [AddRoundKey $Key $state(Nr) $data]
for {incr n -1} {$n > 0} {incr n -1} {
set data [InvMixColumns [AddRoundKey $Key $n [InvSubBytes [InvShiftRows $data]]]]
}
set data [AddRoundKey $Key $n [InvSubBytes [InvShiftRows $data]]]
if {[string equal $state(M) cbc]} {
for {set n 0} {$n < 4} {incr n} {
lappend data2 [expr {0xffffffff & ([lindex $data $n] ^ [lindex $state(I) $n])}]
if {$state(M) eq {cbc}} {
lassign $data d0 d1 d2 d3
lassign $state(I) s0 s1 s2 s3
}
set data $data2
set data [list \
[expr {($d0 ^ $s0) & 0xffffffff}] \
[expr {($d1 ^ $s1) & 0xffffffff}] \
[expr {($d2 ^ $s2) & 0xffffffff}] \
[expr {($d3 ^ $s3) & 0xffffffff}] ]
} else {
# Bug 2993029:
# Force all elements of data into the 32bit range.
# The trimming we see above only happens for CBC mode.
# The integrated clamping we see above only happens for CBC mode.
set res {}
foreach d $data {
lappend res [expr {$d & 0xffffffff}]
}
set data $res
set data [Clamp32 $data]
}
set state(I) $iv
return [binary format Iu4 $data]
binary format Iu4 $data
}
proc ::aes::Clamp32 {data} {
# Force all elements into 32bit range.
lassign $data d0 d1 d2 d3
list \
[expr {$d0 & 0xffffffff}] \
[expr {$d1 & 0xffffffff}] \
[expr {$d2 & 0xffffffff}] \
[expr {$d3 & 0xffffffff}]
}
# 5.2: KeyExpansion
proc ::aes::ExpandKey {Key} {
upvar #0 $Key state
set Rcon [list 0x00000000 0x01000000 0x02000000 0x04000000 0x08000000 \
0x10000000 0x20000000 0x40000000 0x80000000 0x1b000000 \
0x36000000 0x6c000000 0xd8000000 0xab000000 0x4d000000]
0x10000000 0x20000000 0x40000000 0x80000000 0x1b000000 \
0x36000000 0x6c000000 0xd8000000 0xab000000 0x4d000000]
# Split the key into Nk big-endian words
binary scan $state(K) I* W
set max [expr {$state(Nb) * ($state(Nr) + 1)}]
set i $state(Nk)
set h $state(Nk) ; incr h -1
set h [expr {$i - 1}]
set j 0
for {} {$i < $max} {incr i; incr h; incr j} {
set temp [lindex $W $h]
if {($i % $state(Nk)) == 0} {
set sub [SubWord [RotWord $temp]]
set rc [lindex $Rcon [expr {$i/$state(Nk)}]]
set temp [expr {$sub ^ $rc}]
} elseif {$state(Nk) > 6 && ($i % $state(Nk)) == 4} {
set temp [SubWord $temp]
}
lappend W [expr {[lindex $W $j] ^ $temp}]
}
set state(W) $W
return
}
# 5.2: Key Expansion: Apply S-box to each byte in the 32 bit word
proc ::aes::SubWord {w} {
variable sbox
set s3 [lindex $sbox [expr {(($w >> 24) & 255)}]]
set s2 [lindex $sbox [expr {(($w >> 16) & 255)}]]
set s1 [lindex $sbox [expr {(($w >> 8 ) & 255)}]]
set s0 [lindex $sbox [expr {( $w & 255)}]]
set s3 [lindex $sbox [expr {($w >> 24) & 255}]]
set s2 [lindex $sbox [expr {($w >> 16) & 255}]]
set s1 [lindex $sbox [expr {($w >> 8 ) & 255}]]
set s0 [lindex $sbox [expr { $w & 255}]]
return [expr {($s3 << 24) | ($s2 << 16) | ($s1 << 8) | $s0}]
}
proc ::aes::InvSubWord {w} {
variable xobs
set s3 [lindex $xobs [expr {(($w >> 24) & 255)}]]
set s2 [lindex $xobs [expr {(($w >> 16) & 255)}]]
set s1 [lindex $xobs [expr {(($w >> 8 ) & 255)}]]
set s0 [lindex $xobs [expr {( $w & 255)}]]
set s3 [lindex $xobs [expr {($w >> 24) & 255}]]
set s2 [lindex $xobs [expr {($w >> 16) & 255}]]
set s1 [lindex $xobs [expr {($w >> 8 ) & 255}]]
set s0 [lindex $xobs [expr { $w & 255}]]
return [expr {($s3 << 24) | ($s2 << 16) | ($s1 << 8) | $s0}]
}
# 5.2: Key Expansion: Rotate a 32bit word by 8 bits
proc ::aes::RotWord {w} {
return [expr {(($w << 8) | (($w >> 24) & 0xff)) & 0xffffffff}]
}
# 5.1.1: SubBytes() Transformation
proc ::aes::SubBytes {words} {
set r {}
foreach w $words {
lappend r [SubWord $w]
lassign $words w0 w1 w2 w3
list [SubWord $w0] [SubWord $w1] [SubWord $w2] [SubWord $w3]
}
return $r
}
# 5.3.2: InvSubBytes() Transformation
proc ::aes::InvSubBytes {words} {
set r {}
foreach w $words {
lappend r [InvSubWord $w]
lassign $words w0 w1 w2 w3
list [InvSubWord $w0] [InvSubWord $w1] [InvSubWord $w2] [InvSubWord $w3]
}
return $r
}
# 5.1.2: ShiftRows() Transformation
proc ::aes::ShiftRows {words} {
for {set n0 0} {$n0 < 4} {incr n0} {
set n1 [expr {($n0 + 1) % 4}]
set n2 [expr {($n0 + 2) % 4}]
|
︙ | | |
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
-
+
-
+
-
+
-
+
-
+
-
+
|
0x5b 0x59 0x5f 0x5d 0x53 0x51 0x57 0x55 0x4b 0x49 0x4f 0x4d 0x43 0x41 0x47 0x45
0x7b 0x79 0x7f 0x7d 0x73 0x71 0x77 0x75 0x6b 0x69 0x6f 0x6d 0x63 0x61 0x67 0x65
0x9b 0x99 0x9f 0x9d 0x93 0x91 0x97 0x95 0x8b 0x89 0x8f 0x8d 0x83 0x81 0x87 0x85
0xbb 0xb9 0xbf 0xbd 0xb3 0xb1 0xb7 0xb5 0xab 0xa9 0xaf 0xad 0xa3 0xa1 0xa7 0xa5
0xdb 0xd9 0xdf 0xdd 0xd3 0xd1 0xd7 0xd5 0xcb 0xc9 0xcf 0xcd 0xc3 0xc1 0xc7 0xc5
0xfb 0xf9 0xff 0xfd 0xf3 0xf1 0xf7 0xf5 0xeb 0xe9 0xef 0xed 0xe3 0xe1 0xe7 0xe5
}
return [lindex $xtime $number]
lindex $xtime $number
}
proc ::aes::GFMult3 {number} {
# multliply by 2 (via GFMult2) and add the number again on the result (via XOR)
return [expr {$number ^ [GFMult2 $number]}]
expr {$number ^ [GFMult2 $number]}
}
proc ::aes::GFMult09 {number} {
# 09 is: (02*02*02) + 01
return [expr {[GFMult2 [GFMult2 [GFMult2 $number]]] ^ $number}]
expr {[GFMult2 [GFMult2 [GFMult2 $number]]] ^ $number}
}
proc ::aes::GFMult0b {number} {
# 0b is: (02*02*02) + 02 + 01
#return [expr [GFMult2 [GFMult2 [GFMult2 $number]]] ^ [GFMult2 $number] ^ $number]
#set g0 [GFMult2 $number]
return [expr {[GFMult09 $number] ^ [GFMult2 $number]}]
expr {[GFMult09 $number] ^ [GFMult2 $number]}
}
proc ::aes::GFMult0d {number} {
# 0d is: (02*02*02) + (02*02) + 01
set temp [GFMult2 [GFMult2 $number]]
return [expr {[GFMult2 $temp] ^ ($temp ^ $number)}]
expr {[GFMult2 $temp] ^ ($temp ^ $number)}
}
proc ::aes::GFMult0e {number} {
# 0e is: (02*02*02) + (02*02) + 02
set temp [GFMult2 [GFMult2 $number]]
return [expr {[GFMult2 $temp] ^ ($temp ^ [GFMult2 $number])}]
expr {[GFMult2 $temp] ^ ($temp ^ [GFMult2 $number])}
}
# -------------------------------------------------------------------------
# aes::Encrypt --
#
# Encrypt a blocks of plain text and returns blocks of cipher text.
|
︙ | | |
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
-
+
-
+
-
+
-
+
-
+
|
set block [string range $data $i [incr i 15]]
append result [DecryptBlock $Key $block]
}
return $result
}
# -------------------------------------------------------------------------
# Fileevent handler for chunked file reading.
# chan event handler for chunked file reading.
#
proc ::aes::Chunk {Key in {out {}} {chunksize 4096}} {
upvar #0 $Key state
#puts ||CHUNK.X||i=$in|o=$out|c=$chunksize|eof=[eof $in]
if {[eof $in]} {
fileevent $in readable {}
chan event $in readable {}
set state(reading) 0
}
set data [read $in $chunksize]
#puts ||CHUNK.R||i=$in|o=$out|c=$chunksize|eof=[eof $in]||[string length $data]||$data||
# Do nothing when data was read at all.
if {![string length $data]} return
if {$data eq {}} return
if {[eof $in]} {
#puts CHUNK.Z
set data [Pad $data 16]
}
#puts ||CHUNK.P||i=$in|o=$out|c=$chunksize|eof=[eof $in]||[string length $data]||$data||
if {$out == {}} {
if {$out eq {}} {
append state(output) [$state(cmd) $Key $data]
} else {
puts -nonewline $out [$state(cmd) $Key $data]
}
}
proc ::aes::SetOneOf {lst item} {
set ndx [lsearch -glob $lst "${item}*"]
if {$ndx == -1} {
set err [join $lst ", "]
return -code error "invalid mode \"$item\": must be one of $err"
}
return [lindex $lst $ndx]
lindex $lst $ndx
}
proc ::aes::CheckSize {what size thing} {
if {[string length $thing] != $size} {
return -code error "invalid value for $what: must be $size bytes long"
}
return $thing
|
︙ | | |
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
-
-
-
-
-
|
proc ::aes::Pop {varname {nth 0}} {
upvar 1 $varname args
set r [lindex $args $nth]
set args [lreplace $args $nth $nth]
return $r
}
proc ::aes::Hex {data} {
binary scan $data H* r
return $r
}
proc ::aes::aes {args} {
array set opts {-dir encrypt -mode cbc -key {} -in {} -out {} -chunksize 4096 -hex 0}
set opts(-iv) [string repeat \0 16]
set modes {ecb cbc}
set dirs {encrypt decrypt}
while {([llength $args] > 1) && [string match -* [set option [lindex $args 0]]]} {
switch -exact -- $option {
|
︙ | | |
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
-
+
-
+
-
+
|
return -code error "bad option \"$option\":\
must be one of $err"
}
}
Pop args
}
if {$opts(-key) == {}} {
if {$opts(-key) eq {}} {
return -code error "no key provided: the -key option is required"
}
set r {}
if {$opts(-in) == {}} {
if {$opts(-in) eq {}} {
if {[llength $args] != 1} {
return -code error "wrong \# args:\
should be \"aes ?options...? -key keydata plaintext\""
}
set data [Pad [lindex $args 0] 16]
set Key [Init $opts(-mode) $opts(-key) $opts(-iv)]
if {[string equal $opts(-dir) "encrypt"]} {
set r [Encrypt $Key $data]
} else {
set r [Decrypt $Key $data]
}
if {$opts(-out) != {}} {
if {$opts(-out) ne {}} {
puts -nonewline $opts(-out) $r
set r {}
}
Final $Key
} else {
|
︙ | | |
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
-
+
-
+
|
set state(reading) 1
if {[string equal $opts(-dir) "encrypt"]} {
set state(cmd) Encrypt
} else {
set state(cmd) Decrypt
}
set state(output) ""
fileevent $opts(-in) readable $readcmd
chan event $opts(-in) readable $readcmd
if {[info commands ::tkwait] != {}} {
tkwait variable [subst $Key](reading)
} else {
vwait [subst $Key](reading)
}
if {$opts(-out) == {}} {
set r $state(output)
}
Final $Key
}
if {$opts(-hex)} {
set r [Hex $r]
binary scan $r H* r
}
return $r
}
# -------------------------------------------------------------------------
package provide aes $::aes::version
# -------------------------------------------------------------------------
# Local variables:
# mode: tcl
# indent-tabs-mode: nil
# End:
|
Changes to modules/aes/pkgIndex.tcl.