forked from standardml/cmlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.sml
executable file
·626 lines (567 loc) · 32.6 KB
/
aes.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
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
581
582
583
584
585
586
587
588
589
590
591
592
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
structure AES
:>
CIPHER
where type prekey = Bytestring.string
where type init = unit
=
struct
structure A = Word8Array
structure B = Bytestring
val w2i = Word8.toInt
val xorb = Word8.xorb
val sub = Word8Vector.sub
val asub = Word8Array.sub
val bsub = Bytestring.sub
val rcon =
Word8Vector.fromList
[0wx8D,0wx1,0wx2,0wx4,0wx8,0wx10,0wx20,0wx40,0wx80,0wx1B,0wx36,0wx6C,0wxD8,
0wxAB,0wx4D,0wx9A,0wx2F,0wx5E,0wxBC,0wx63,0wxC6,0wx97,0wx35,0wx6A,0wxD4,
0wxB3,0wx7D,0wxFA,0wxEF,0wxC5,0wx91,0wx39,0wx72,0wxE4,0wxD3,0wxBD,0wx61,
0wxC2,0wx9F,0wx25,0wx4A,0wx94,0wx33,0wx66,0wxCC,0wx83,0wx1D,0wx3A,0wx74,
0wxE8,0wxCB,0wx8D,0wx1,0wx2,0wx4,0wx8,0wx10,0wx20,0wx40,0wx80,0wx1B,0wx36,
0wx6C,0wxD8,0wxAB,0wx4D,0wx9A,0wx2F,0wx5E,0wxBC,0wx63,0wxC6,0wx97,0wx35,
0wx6A,0wxD4,0wxB3,0wx7D,0wxFA,0wxEF,0wxC5,0wx91,0wx39,0wx72,0wxE4,0wxD3,
0wxBD,0wx61,0wxC2,0wx9F,0wx25,0wx4A,0wx94,0wx33,0wx66,0wxCC,0wx83,0wx1D,
0wx3A,0wx74,0wxE8,0wx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wxD4,0wxDA,0wxC8,0wxC6,0wx9C,0wx92,0wx80,0wx8E,
0wxA4,0wxAA,0wxB8,0wxB6,0wxC,0wx2,0wx10,0wx1E,0wx34,0wx3A,0wx28,0wx26,
0wx7C,0wx72,0wx60,0wx6E,0wx44,0wx4A,0wx58,0wx56,0wx37,0wx39,0wx2B,0wx25,
0wxF,0wx1,0wx13,0wx1D,0wx47,0wx49,0wx5B,0wx55,0wx7F,0wx71,0wx63,0wx6D,
0wxD7,0wxD9,0wxCB,0wxC5,0wxEF,0wxE1,0wxF3,0wxFD,0wxA7,0wxA9,0wxBB,0wxB5,
0wx9F,0wx91,0wx83,0wx8D]
(*
fun ksc (a, b, c, d, i) =
(xorb (sub (sbox, w2i b), sub (rcon, i)),
sub (sbox, w2i c),
sub (sbox, w2i d),
sub (sbox, w2i a))
*)
fun mixColumns (a, b, c, d) =
let
val a' = w2i a
val b' = w2i b
val c' = w2i c
val d' = w2i d
in
(xorb (xorb (xorb (sub (times2, a'), sub (times3, b')), c), d),
xorb (xorb (xorb (a, sub (times2, b')), sub (times3, c')), d),
xorb (xorb (xorb (a, b), sub (times2, c')), sub (times3, d')),
xorb (xorb (xorb (sub (times3, a'), b), c), sub (times2, d')))
end
fun invMixColumns (a, b, c, d) =
let
val a' = w2i a
val b' = w2i b
val c' = w2i c
val d' = w2i d
in
(xorb (xorb (xorb (sub (times14, a'), sub (times11, b')), sub (times13, c')), sub (times9, d')),
xorb (xorb (xorb (sub (times9, a'), sub (times14, b')), sub (times11, c')), sub (times13, d')),
xorb (xorb (xorb (sub (times13, a'), sub (times9, b')), sub (times14, c')), sub (times11, d')),
xorb (xorb (xorb (sub (times11, a'), sub (times13, b')), sub (times9, c')), sub (times14, d')))
end
type prekey = Bytestring.string
type key = Word8Array.array
type init = unit
val messageSize = SOME 16
fun fullkey128 prekey =
let
val keyv = B.toWord8Vector prekey
val fkey = Word8Array.array (176, 0w0)
val () = Word8Array.copyVec {src=keyv, dst=fkey, di=0}
fun loop1 (pos, i, t0, t1, t2, t3) =
if pos >= 176 then
()
else
let
val t0' = xorb (sub (sbox, w2i t1), sub (rcon, i))
val t1' = sub (sbox, w2i t2)
val t2' = sub (sbox, w2i t3)
val t3' = sub (sbox, w2i t0)
in
loop2 (4, pos, i+1, t0', t1', t2', t3')
end
and loop2 (n, pos, i, t0, t1, t2, t3) =
if n <= 0 then
loop1 (pos, i, t0, t1, t2, t3)
else
let
val t0' = xorb (t0, asub (fkey, pos-16))
val t1' = xorb (t1, asub (fkey, pos-15))
val t2' = xorb (t2, asub (fkey, pos-14))
val t3' = xorb (t3, asub (fkey, pos-13))
val () = A.update (fkey, pos, t0')
val () = A.update (fkey, pos+1, t1')
val () = A.update (fkey, pos+2, t2')
val () = A.update (fkey, pos+3, t3')
in
loop2 (n-1, pos+4, i, t0', t1', t2', t3')
end
val () =
loop1 (16, 1, sub (keyv, 12), sub (keyv, 13), sub (keyv, 14), sub (keyv, 15))
in
fkey
end
fun fullkey192 prekey =
let
val keyv = B.toWord8Vector prekey
val fkey = Word8Array.array (208, 0w0)
val () = Word8Array.copyVec {src=keyv, dst=fkey, di=0}
fun loop1 (pos, i, t0, t1, t2, t3) =
let
val t0' = xorb (sub (sbox, w2i t1), sub (rcon, i))
val t1' = sub (sbox, w2i t2)
val t2' = sub (sbox, w2i t3)
val t3' = sub (sbox, w2i t0)
in
loop2 (6, pos, i+1, t0', t1', t2', t3')
end
(* 24 doesn't divide 208, so we have to check termination down here. *)
and loop2 (n, pos, i, t0, t1, t2, t3) =
if pos >= 208 then
()
else if n <= 0 then
loop1 (pos, i, t0, t1, t2, t3)
else
let
val t0' = xorb (t0, asub (fkey, pos-24))
val t1' = xorb (t1, asub (fkey, pos-23))
val t2' = xorb (t2, asub (fkey, pos-22))
val t3' = xorb (t3, asub (fkey, pos-21))
val () = A.update (fkey, pos, t0')
val () = A.update (fkey, pos+1, t1')
val () = A.update (fkey, pos+2, t2')
val () = A.update (fkey, pos+3, t3')
in
loop2 (n-1, pos+4, i, t0', t1', t2', t3')
end
val () =
loop1 (24, 1, sub (keyv, 20), sub (keyv, 21), sub (keyv, 22), sub (keyv, 23))
in
fkey
end
fun fullkey256 prekey =
let
val keyv = B.toWord8Vector prekey
val fkey = Word8Array.array (240, 0w0)
val () = Word8Array.copyVec {src=keyv, dst=fkey, di=0}
fun loop1 (pos, i, t0, t1, t2, t3) =
let
val t0' = xorb (sub (sbox, w2i t1), sub (rcon, i))
val t1' = sub (sbox, w2i t2)
val t2' = sub (sbox, w2i t3)
val t3' = sub (sbox, w2i t0)
in
loop2 (4, pos, i+1, t0', t1', t2', t3')
end
and loop2 (n, pos, i, t0, t1, t2, t3) =
if pos >= 240 then
()
else if n <= 0 then
loop3 (pos, i, t0, t1, t2, t3)
else
let
val t0' = xorb (t0, asub (fkey, pos-32))
val t1' = xorb (t1, asub (fkey, pos-31))
val t2' = xorb (t2, asub (fkey, pos-30))
val t3' = xorb (t3, asub (fkey, pos-29))
val () = A.update (fkey, pos, t0')
val () = A.update (fkey, pos+1, t1')
val () = A.update (fkey, pos+2, t2')
val () = A.update (fkey, pos+3, t3')
in
loop2 (n-1, pos+4, i, t0', t1', t2', t3')
end
and loop3 (pos, i, t0, t1, t2, t3) =
let
val t0' = sub (sbox, w2i t0)
val t1' = sub (sbox, w2i t1)
val t2' = sub (sbox, w2i t2)
val t3' = sub (sbox, w2i t3)
in
loop4 (4, pos, i, t0', t1', t2', t3')
end
and loop4 (n, pos, i, t0, t1, t2, t3) =
if n <= 0 then
loop1 (pos, i, t0, t1, t2, t3)
else
let
val t0' = xorb (t0, asub (fkey, pos-32))
val t1' = xorb (t1, asub (fkey, pos-31))
val t2' = xorb (t2, asub (fkey, pos-30))
val t3' = xorb (t3, asub (fkey, pos-29))
val () = A.update (fkey, pos, t0')
val () = A.update (fkey, pos+1, t1')
val () = A.update (fkey, pos+2, t2')
val () = A.update (fkey, pos+3, t3')
in
loop4 (n-1, pos+4, i, t0', t1', t2', t3')
end
val () =
loop1 (32, 1, sub (keyv, 28), sub (keyv, 29), sub (keyv, 30), sub (keyv, 31))
in
fkey
end
fun makeKey (prekey, ()) =
(case B.size prekey of
16 => fullkey128 prekey
| 24 => fullkey192 prekey
| 32 => fullkey256 prekey
| _ => raise Domain)
fun encrypt (fkey, msg) =
if Bytestring.size msg <> 16 then
raise Domain
else
let
val count = A.length fkey - 16
fun loop (i, a00, a10, a20, a30, a01, a11, a21, a31, a02, a12, a22, a32, a03, a13, a23, a33) =
if i = count then
Bytestring.implode
[xorb (sub (sbox, w2i a00), asub (fkey, i)),
xorb (sub (sbox, w2i a11), asub (fkey, i+1)),
xorb (sub (sbox, w2i a22), asub (fkey, i+2)),
xorb (sub (sbox, w2i a33), asub (fkey, i+3)),
xorb (sub (sbox, w2i a01), asub (fkey, i+4)),
xorb (sub (sbox, w2i a12), asub (fkey, i+5)),
xorb (sub (sbox, w2i a23), asub (fkey, i+6)),
xorb (sub (sbox, w2i a30), asub (fkey, i+7)),
xorb (sub (sbox, w2i a02), asub (fkey, i+8)),
xorb (sub (sbox, w2i a13), asub (fkey, i+9)),
xorb (sub (sbox, w2i a20), asub (fkey, i+10)),
xorb (sub (sbox, w2i a31), asub (fkey, i+11)),
xorb (sub (sbox, w2i a03), asub (fkey, i+12)),
xorb (sub (sbox, w2i a10), asub (fkey, i+13)),
xorb (sub (sbox, w2i a21), asub (fkey, i+14)),
xorb (sub (sbox, w2i a32), asub (fkey, i+15))]
else
let
val (b00, b10, b20, b30) =
mixColumns
(sub (sbox, w2i a00), sub (sbox, w2i a11), sub (sbox, w2i a22), sub (sbox, w2i a33))
val (b01, b11, b21, b31) =
mixColumns
(sub (sbox, w2i a01), sub (sbox, w2i a12), sub (sbox, w2i a23), sub (sbox, w2i a30))
val (b02, b12, b22, b32) =
mixColumns
(sub (sbox, w2i a02), sub (sbox, w2i a13), sub (sbox, w2i a20), sub (sbox, w2i a31))
val (b03, b13, b23, b33) =
mixColumns
(sub (sbox, w2i a03), sub (sbox, w2i a10), sub (sbox, w2i a21), sub (sbox, w2i a32))
in
loop (i+16,
xorb (b00, asub (fkey, i)),
xorb (b10, asub (fkey, i+1)),
xorb (b20, asub (fkey, i+2)),
xorb (b30, asub (fkey, i+3)),
xorb (b01, asub (fkey, i+4)),
xorb (b11, asub (fkey, i+5)),
xorb (b21, asub (fkey, i+6)),
xorb (b31, asub (fkey, i+7)),
xorb (b02, asub (fkey, i+8)),
xorb (b12, asub (fkey, i+9)),
xorb (b22, asub (fkey, i+10)),
xorb (b32, asub (fkey, i+11)),
xorb (b03, asub (fkey, i+12)),
xorb (b13, asub (fkey, i+13)),
xorb (b23, asub (fkey, i+14)),
xorb (b33, asub (fkey, i+15)))
end
in
loop (16,
xorb (bsub (msg, 0), asub (fkey, 0)),
xorb (bsub (msg, 1), asub (fkey, 1)),
xorb (bsub (msg, 2), asub (fkey, 2)),
xorb (bsub (msg, 3), asub (fkey, 3)),
xorb (bsub (msg, 4), asub (fkey, 4)),
xorb (bsub (msg, 5), asub (fkey, 5)),
xorb (bsub (msg, 6), asub (fkey, 6)),
xorb (bsub (msg, 7), asub (fkey, 7)),
xorb (bsub (msg, 8), asub (fkey, 8)),
xorb (bsub (msg, 9), asub (fkey, 9)),
xorb (bsub (msg, 10), asub (fkey, 10)),
xorb (bsub (msg, 11), asub (fkey, 11)),
xorb (bsub (msg, 12), asub (fkey, 12)),
xorb (bsub (msg, 13), asub (fkey, 13)),
xorb (bsub (msg, 14), asub (fkey, 14)),
xorb (bsub (msg, 15), asub (fkey, 15)))
end
fun decrypt (fkey, msg) =
if Bytestring.size msg <> 16 then
raise Domain
else
let
val count = A.length fkey - 16
fun loop (i, a00, a10, a20, a30, a01, a11, a21, a31, a02, a12, a22, a32, a03, a13, a23, a33) =
if i = 0 then
Bytestring.implode
[xorb (a00, asub (fkey, 0)),
xorb (a10, asub (fkey, 1)),
xorb (a20, asub (fkey, 2)),
xorb (a30, asub (fkey, 3)),
xorb (a01, asub (fkey, 4)),
xorb (a11, asub (fkey, 5)),
xorb (a21, asub (fkey, 6)),
xorb (a31, asub (fkey, 7)),
xorb (a02, asub (fkey, 8)),
xorb (a12, asub (fkey, 9)),
xorb (a22, asub (fkey, 10)),
xorb (a32, asub (fkey, 11)),
xorb (a03, asub (fkey, 12)),
xorb (a13, asub (fkey, 13)),
xorb (a23, asub (fkey, 14)),
xorb (a33, asub (fkey, 15))]
else
let
val (b00, b10, b20, b30) =
invMixColumns
(xorb (a00, asub (fkey, i)), xorb (a10, asub (fkey, i+1)), xorb (a20, asub (fkey, i+2)), xorb (a30, asub (fkey, i+3)))
val (b01, b11, b21, b31) =
invMixColumns
(xorb (a01, asub (fkey, i+4)), xorb (a11, asub (fkey, i+5)), xorb (a21, asub (fkey, i+6)), xorb (a31, asub (fkey, i+7)))
val (b02, b12, b22, b32) =
invMixColumns
(xorb (a02, asub (fkey, i+8)), xorb (a12, asub (fkey, i+9)), xorb (a22, asub (fkey, i+10)), xorb (a32, asub (fkey, i+11)))
val (b03, b13, b23, b33) =
invMixColumns
(xorb (a03, asub (fkey, i+12)), xorb (a13, asub (fkey, i+13)), xorb (a23, asub (fkey, i+14)), xorb (a33, asub (fkey, i+15)))
in
loop (i-16,
sub (invSbox, w2i b00),
sub (invSbox, w2i b13),
sub (invSbox, w2i b22),
sub (invSbox, w2i b31),
sub (invSbox, w2i b01),
sub (invSbox, w2i b10),
sub (invSbox, w2i b23),
sub (invSbox, w2i b32),
sub (invSbox, w2i b02),
sub (invSbox, w2i b11),
sub (invSbox, w2i b20),
sub (invSbox, w2i b33),
sub (invSbox, w2i b03),
sub (invSbox, w2i b12),
sub (invSbox, w2i b21),
sub (invSbox, w2i b30))
end
in
loop (count-16,
sub (invSbox, w2i (xorb (bsub (msg, 0), asub (fkey, count)))),
sub (invSbox, w2i (xorb (bsub (msg, 13), asub (fkey, count+13)))),
sub (invSbox, w2i (xorb (bsub (msg, 10), asub (fkey, count+10)))),
sub (invSbox, w2i (xorb (bsub (msg, 7), asub (fkey, count+7)))),
sub (invSbox, w2i (xorb (bsub (msg, 4), asub (fkey, count+4)))),
sub (invSbox, w2i (xorb (bsub (msg, 1), asub (fkey, count+1)))),
sub (invSbox, w2i (xorb (bsub (msg, 14), asub (fkey, count+14)))),
sub (invSbox, w2i (xorb (bsub (msg, 11), asub (fkey, count+11)))),
sub (invSbox, w2i (xorb (bsub (msg, 8), asub (fkey, count+8)))),
sub (invSbox, w2i (xorb (bsub (msg, 5), asub (fkey, count+5)))),
sub (invSbox, w2i (xorb (bsub (msg, 2), asub (fkey, count+2)))),
sub (invSbox, w2i (xorb (bsub (msg, 15), asub (fkey, count+15)))),
sub (invSbox, w2i (xorb (bsub (msg, 12), asub (fkey, count+12)))),
sub (invSbox, w2i (xorb (bsub (msg, 9), asub (fkey, count+9)))),
sub (invSbox, w2i (xorb (bsub (msg, 6), asub (fkey, count+6)))),
sub (invSbox, w2i (xorb (bsub (msg, 3), asub (fkey, count+3)))))
end
end
structure AESCBC = CBCCipherFun (structure Cipher = AES)
structure AESCFB = CFBCipherFun (structure Cipher = AES)
structure AESOFB = OFBCipherFun (structure Cipher = AES)
structure AESCTR = CTRCipherFun (structure Cipher = AES)