]> gitweb.factorcode.org Git - factor.git/blob - extra/crypto/aes/aes.factor
75cd5706263933009a41a169d2b6187029cd0981
[factor.git] / extra / crypto / aes / aes.factor
1 ! Copyright (C) 2013 Fred Alger
2 ! Some parts Copyright (C) 2008 Doug Coleman.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays assocs byte-arrays crypto.aes.utils
5 generalizations kernel literals math math.bitwise math.ranges
6 namespaces sequences sequences.private sequences.unrolled ;
7 IN: crypto.aes
8
9 CONSTANT: AES_BLOCK_SIZE 16
10
11 ! FIPS-197 AES
12 ! input block, state, output block -- 4 32-bit words
13 CONSTANT: FIPS-197 {
14     { 128 10 } ! aes-128 -- Key(4) Block(4) Rounds(10)
15     { 192 12 } ! aes-192 -- Key(6) Block(4) Rounds(12)
16     { 256 14 } ! aes-256 -- Key(8) Block(4) Rounds(14)
17 }
18
19 <PRIVATE
20
21 : (nrounds) ( byte-array -- rounds )
22     length 8 * FIPS-197 at ;
23
24 <<
25 CONSTANT: sbox B{
26     0x63 0x7c 0x77 0x7b 0xf2 0x6b 0x6f 0xc5
27     0x30 0x01 0x67 0x2b 0xfe 0xd7 0xab 0x76
28     0xca 0x82 0xc9 0x7d 0xfa 0x59 0x47 0xf0
29     0xad 0xd4 0xa2 0xaf 0x9c 0xa4 0x72 0xc0
30     0xb7 0xfd 0x93 0x26 0x36 0x3f 0xf7 0xcc
31     0x34 0xa5 0xe5 0xf1 0x71 0xd8 0x31 0x15
32     0x04 0xc7 0x23 0xc3 0x18 0x96 0x05 0x9a
33     0x07 0x12 0x80 0xe2 0xeb 0x27 0xb2 0x75
34     0x09 0x83 0x2c 0x1a 0x1b 0x6e 0x5a 0xa0
35     0x52 0x3b 0xd6 0xb3 0x29 0xe3 0x2f 0x84
36     0x53 0xd1 0x00 0xed 0x20 0xfc 0xb1 0x5b
37     0x6a 0xcb 0xbe 0x39 0x4a 0x4c 0x58 0xcf
38     0xd0 0xef 0xaa 0xfb 0x43 0x4d 0x33 0x85
39     0x45 0xf9 0x02 0x7f 0x50 0x3c 0x9f 0xa8
40     0x51 0xa3 0x40 0x8f 0x92 0x9d 0x38 0xf5
41     0xbc 0xb6 0xda 0x21 0x10 0xff 0xf3 0xd2
42     0xcd 0x0c 0x13 0xec 0x5f 0x97 0x44 0x17
43     0xc4 0xa7 0x7e 0x3d 0x64 0x5d 0x19 0x73
44     0x60 0x81 0x4f 0xdc 0x22 0x2a 0x90 0x88
45     0x46 0xee 0xb8 0x14 0xde 0x5e 0x0b 0xdb
46     0xe0 0x32 0x3a 0x0a 0x49 0x06 0x24 0x5c
47     0xc2 0xd3 0xac 0x62 0x91 0x95 0xe4 0x79
48     0xe7 0xc8 0x37 0x6d 0x8d 0xd5 0x4e 0xa9
49     0x6c 0x56 0xf4 0xea 0x65 0x7a 0xae 0x08
50     0xba 0x78 0x25 0x2e 0x1c 0xa6 0xb4 0xc6
51     0xe8 0xdd 0x74 0x1f 0x4b 0xbd 0x8b 0x8a
52     0x70 0x3e 0xb5 0x66 0x48 0x03 0xf6 0x0e
53     0x61 0x35 0x57 0xb9 0x86 0xc1 0x1d 0x9e
54     0xe1 0xf8 0x98 0x11 0x69 0xd9 0x8e 0x94
55     0x9b 0x1e 0x87 0xe9 0xce 0x55 0x28 0xdf
56     0x8c 0xa1 0x89 0x0d 0xbf 0xe6 0x42 0x68
57     0x41 0x99 0x2d 0x0f 0xb0 0x54 0xbb 0x16
58 }
59 >>
60
61 CONSTANT: inv-sbox $[
62     256 <byte-array>
63     dup 256 [ dup sbox nth rot set-nth ] with each-integer
64 ]
65
66 ! applies sbox to each byte of word
67 : subword ( word -- word' )
68     [ gb0 sbox nth ] keep [ gb1 sbox nth ] keep
69     [ gb2 sbox nth ] keep gb3 sbox nth >ui32 ;
70
71 ! applies inverse sbox to each byte of word
72 : inv-subword ( word -- word' )
73     [ gb0 inv-sbox nth ] keep [ gb1 inv-sbox nth ] keep
74     [ gb2 inv-sbox nth ] keep gb3 inv-sbox nth >ui32 ;
75
76 : rotword ( n -- n ) 8 bitroll-32 ;
77
78 ! round constants, 2^n over GF(2^8)
79 : rcon ( -- array )
80     {
81         0x00 0x01 0x02 0x04 0x08 0x10
82         0x20 0x40 0x80 0x1b 0x36
83     } ;
84
85 : (rcon-nth) ( n -- rcon[n] ) rcon nth 24 shift ;
86
87 ! Galois field product related
88 : xtime ( x -- x' )
89     [ 1 shift ]
90     [ 0x80 bitand 0 = 0 0x1b ? ] bi bitxor 8 bits ;
91
92 ! generate t-box
93 :: set-t ( T i -- )
94     i sbox nth :> a1
95     a1 xtime :> a2
96     a1 a2 bitxor :> a3
97
98     a3 a1 a1 a2 >ui32 i T set-nth
99     a1 a1 a2 a3 >ui32 i 0x100 + T set-nth
100     a1 a2 a3 a1 >ui32 i 0x200 + T set-nth
101     a2 a3 a1 a1 >ui32 i 0x300 + T set-nth ;
102
103 MEMO:: t-table ( -- array )
104     1024 0 <array>
105     dup 256 [ set-t ] with each-integer ;
106
107 ! generate inverse t-box
108 :: set-d ( D i -- )
109     i inv-sbox nth :> a1
110     a1 xtime :> a2
111     a2 xtime :> a4
112     a4 xtime :> a8
113     a8 a1 bitxor :> a9
114     a9 a2 bitxor :> ab
115     a9 a4 bitxor :> ad
116     a8 a4 a2 bitxor bitxor :> ae
117
118     ab ad a9 ae >ui32 i D set-nth
119     ad a9 ae ab >ui32 i 0x100 + D set-nth
120     a9 ae ab ad >ui32 i 0x200 + D set-nth
121     ae ab ad a9 >ui32 i 0x300 + D set-nth ;
122
123 MEMO:: d-table ( -- array )
124     1024 0 <array>
125     dup 256 [ set-d ] with each-integer ;
126
127
128 :: (transform) ( a0 a1 a2 a3 table -- word' )
129   a0 a1 a2 a3
130   [ 0x100 + ] [ 0x200 + ] [ 0x300 + ] tri*
131   [ table nth ] 4 napply
132   bitxor bitxor bitxor ; inline
133
134 : t-transform ( a0 a1 a2 a3 -- word' ) t-table (transform) ;
135 : d-transform ( a0 a1 a2 a3 -- word' ) d-table (transform) ;
136
137 ! key schedule
138 ! expands an 128/192/256 bit key into an 176/208/240 byte schedule
139
140 SYMBOL: aes-expand-inner
141 HOOK: key-expand-round aes-expand-inner  ( temp i -- temp' )
142
143 SINGLETON: aes-128-key
144 SINGLETON: aes-256-key
145
146 : (add-rcon) ( word rcon-ndx -- word' )
147     (rcon-nth) [ rotword subword ] dip bitxor ;
148
149 M: aes-128-key key-expand-round ( temp i -- temp' )
150     4 /mod 0 = swap and [ (add-rcon) ] when* ;
151
152 ERROR: aes-192-256-not-implemented ;
153 M: aes-256-key key-expand-round ( temp i -- temp' )
154     aes-192-256-not-implemented ;
155
156 : (key-sched-round) ( output temp i -- output' )
157     key-expand-round
158     [ dup 4th-from-end ] dip bitxor suffix! ; inline
159
160 : (sched-interval) ( K Nr -- seq )
161     [ length ] dip 1 + 4 * [a..b) ;    ! over the interval Nk...Nb(Nr + 1)
162
163 : (init-round) ( out -- out temp quot )
164     [ ]
165     [ last ]
166     [
167         length
168         6 > [ aes-256-key ] [ aes-128-key ] if
169     ] tri ;
170
171 ! K -- input key (byte-array), Nr -- number of rounds
172 ! output: sched, Nb(Nr+1) byte key schedule
173 : (expand-enc-key) ( K Nr -- sched )
174     [ bytes>words ] dip
175     [ drop (init-round) ]
176     [ (sched-interval) ] 2bi
177     [
178         [ aes-expand-inner set ] dip
179         [ (key-sched-round) dup last ] each
180     ] with-scope
181     drop ;
182
183 TUPLE: aes-state nrounds key state ;
184
185 : <aes> ( nrounds key state -- aes-state ) \ aes-state boa ;
186
187 ! grabs the 4n...4(n+1) words of the key
188 : (key-at-nth-round) ( nth aes -- seq )
189     [ 4 * dup 4 + ] [ key>> ] bi* <slice> ;
190
191 SYMBOL: aes-strategy
192 HOOK: (expand-key) aes-strategy ( K Nr -- sched )
193 HOOK: (first-round) aes-strategy ( aes -- aes' )
194 HOOK: (counter) aes-strategy ( nrounds -- seq )
195 HOOK: (round) aes-strategy ( state -- )
196 HOOK: (add-key) aes-strategy ( aes -- aes' )
197 HOOK: (final-round) aes-strategy ( aes -- aes' )
198
199 SINGLETON: aes-decrypt
200 SINGLETON: aes-encrypt
201
202
203 ! rotates the 2nd row left by one element
204 ! rotates the 3rd row left by two elements
205 ! rotates the 4th row left by three elements
206 !
207 ! Kind of ugly because the algorithm is specified and
208 ! implemented in terms of columns. This approach is very
209 ! efficient in terms of execution and only requires one new
210 ! word to implement.
211 !
212 ! The alternative is to split into arrays of bytes, transpose,
213 ! rotate each row n times, transpose again, and then
214 ! smash them back into 4-byte words.
215 :: (shift-rows) ( c0 c1 c2 c3 -- c0' c1' c2' c3' )
216     c3 gb0   c2 gb1   c1 gb2   c0 gb3   >ui32   ! c0'
217     c0 gb0   c3 gb1   c2 gb2   c1 gb3   >ui32   ! c1'
218     c1 gb0   c0 gb1   c3 gb2   c2 gb3   >ui32   ! c2'
219     c2 gb0   c1 gb1   c0 gb2   c3 gb3   >ui32 ; ! c3'
220
221 :: (unshift-rows) ( c0 c1 c2 c3 -- c0' c1' c2' c3' )
222     c1 gb0   c2 gb1   c3 gb2   c0 gb3   >ui32   ! c0'
223     c2 gb0   c3 gb1   c0 gb2   c1 gb3   >ui32   ! c1'
224     c3 gb0   c0 gb1   c1 gb2   c2 gb3   >ui32   ! c2'
225     c0 gb0   c1 gb1   c2 gb2   c3 gb3   >ui32 ; ! c3'
226
227 : (add-round-key) ( key state -- state' )
228    4 [ bitxor ] unrolled-2map ;
229
230 : add-round-key ( aes n -- aes' )
231     over (key-at-nth-round) swap
232     [ (add-round-key) ] change-state ;
233
234 : add-final-round-key ( aes -- aes' )
235     dup nrounds>> add-round-key ;
236
237 : add-first-round-key ( aes -- aes' )
238     0 add-round-key ;
239
240 : aes-round ( state -- )
241     dup first4-unsafe
242     { [ first-diag t-transform ]
243       [ second-diag t-transform ]
244       [ third-diag t-transform ]
245       [ fourth-diag t-transform ] } 4 ncleave
246       set-first4-unsafe ;
247
248
249 : shift-rows ( state -- state' )
250     first4 (shift-rows) 4array ;
251
252 : unshift-rows ( state -- state' )
253     first4 (unshift-rows) 4array ;
254
255 : final-round ( state -- state' )
256     4 [ subword ] unrolled-map shift-rows ;
257
258 : (do-round) ( aes -- aes' )
259     [ state>> (round) ] keep ;
260
261 M: aes-encrypt (expand-key) (expand-enc-key) ;
262 M: aes-encrypt (first-round) add-first-round-key ;
263 M: aes-encrypt (counter) [1..b) ;
264 M: aes-encrypt (round) aes-round ;
265 M: aes-encrypt (final-round) [ final-round ] change-state add-final-round-key ;
266
267 M:: aes-decrypt (expand-key) ( K Nr -- sched )
268     K Nr (expand-enc-key) dup length :> key-length
269     [
270         [ 4 >= ] [ key-length 4 - < ] bi and
271         [ subword ui32-rev> d-transform ] when
272     ] map-index ;
273
274 M: aes-decrypt (first-round) ( aes -- aes' )
275     add-final-round-key ;
276
277 M: aes-decrypt (counter) ( nrounds -- seq ) [1..b) <reversed> ;
278 M: aes-decrypt (final-round) ( aes -- aes' )
279     [ [ inv-subword ] map unshift-rows  ] change-state
280     add-first-round-key ;
281
282 M: aes-decrypt (round) ( state -- )
283     dup first4-unsafe
284     { [ -first-diag d-transform ]
285       [ -fourth-diag d-transform ]
286       [ -third-diag d-transform ]
287       [ -second-diag d-transform ] } 4 ncleave
288       set-first4-unsafe ;
289
290
291 : (aes-crypt) ( aes -- aes' )
292     (first-round) [
293         dup nrounds>> (counter)
294         [ [ (do-round) ] dip add-round-key drop ] with each
295     ] keep
296     (final-round) ;
297
298 : (aes-expand-key) ( key -- nrounds expanded-key )
299     [ (nrounds) ] keep over (expand-key) ;
300
301 : (aes-crypt-block-inner) ( nrounds key block -- crypted-block )
302     <aes> (aes-crypt) state>> ;
303
304 : (aes-crypt-block) ( key block -- output-block )
305     [ (aes-expand-key) ] dip bytes>words (aes-crypt-block-inner) ;
306
307 PRIVATE>
308
309 : aes-encrypt-block ( key block -- output )
310     [ aes-encrypt aes-strategy set (aes-crypt-block) ] with-scope
311     [ ui32> 4array reverse ] map concat ;
312
313 : aes-decrypt-block ( key block -- output )
314     [ aes-decrypt aes-strategy set (aes-crypt-block) ] with-scope
315     [ ui32> 4array reverse ] map concat ;