Paste: fips197
Author: | Gabriel Kerneis |
Mode: | factor |
Date: | Sun, 26 May 2013 21:24:18 |
Plain Text |
USING: kernel sequences grouping math math.order math.parser math.bitwise
math.bits strings locals arrays make crypto.aes tools.test ;
IN: fips197
: sub-word
[ sbox nth ] map ;
: rot-word
cut-slice prepend ;
: xor-word
[ bitxor ] 2map ;
: key-step
{ 0 0 0 } swap prefix
over last 1 rot-word sub-word
xor-word
[ xor-word ] accumulate swap suffix 1 tail ;
: expand-key
11 0 <array> 0x01 [ drop xtime ] accumulate nip swap
[ key-step ] accumulate nip ;
: sub-bytes
[ sub-word ] map ;
: shift-rows
flip 4 iota [ rot-word ] 2map flip ;
: nxtime
make-bits
[ [ xtime ] times 0 ? ] with map-index
0 [ bitxor ] reduce ;
: word-product
[ nxtime ] 2map
0 [ bitxor ] reduce ;
: matrix-product
[ word-product ] with map ;
: mix-column
{ { 2 3 1 1 }
{ 1 2 3 1 }
{ 1 1 2 3 }
{ 3 1 1 2 } } matrix-product ;
: mix-columns
[ mix-column ] map ;
: add-round-key
[ xor-word ] 2map ;
: aes-round
sub-bytes shift-rows mix-columns add-round-key ;
: aes-128-encrypt
[ unclip ] dip add-round-key
[ unclip-last swap ] dip
[ swap aes-round ] reduce
sub-bytes shift-rows add-round-key ;
: inv-shift-rows
flip { 0 3 2 1 } [ rot-word ] 2map flip ;
: inv-sub-bytes
[ [ inv-sbox nth ] map ] map ;
: inv-mix-column
{ { 0xe 0xb 0xd 0x9 }
{ 0x9 0xe 0xb 0xd }
{ 0xd 0x9 0xe 0xb }
{ 0xb 0xd 0x9 0xe } } matrix-product ;
: inv-mix-columns
[ inv-mix-column ] map ;
: inv-aes-round
inv-shift-rows inv-sub-bytes add-round-key inv-mix-columns ;
: aes-128-decrypt
[ reverse ] dip
[ unclip ] dip add-round-key
[ unclip-last swap ] dip
[ swap inv-aes-round ] reduce
inv-shift-rows inv-sub-bytes add-round-key ;
: cut-padding
dup last head* ;
: aes-128-ecb-decrypt
[ 4 group expand-key ] dip
2 [ 4 group ] times
[ aes-128-decrypt ] with map
concat concat
cut-padding ;
New Annotation