-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes.S
executable file
·132 lines (109 loc) · 1.98 KB
/
aes.S
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
.text
.global aes_encrypt, aes_expand_key
/*
* void aes_expand_key(const uint64_t *key, uint64_t *rk);
*/
aes_expand_key:
ld t0, 0(a0)
ld t1, 8(a0)
/* rk[0] */
sd t0, 0(a1)
sd t1, 8(a1)
/* rk[1] */
aes64ks1i t2, t1, 0
aes64ks2 t0, t2, t0
aes64ks2 t1, t0, t1
sd t0, 16(a1)
sd t1, 24(a1)
/* rk[2] */
aes64ks1i t2, t1, 1
aes64ks2 t0, t2, t0
aes64ks2 t1, t0, t1
sd t0, 32(a1)
sd t1, 40(a1)
/* rk[3] */
aes64ks1i t2, t1, 2
aes64ks2 t0, t2, t0
aes64ks2 t1, t0, t1
sd t0, 48(a1)
sd t1, 56(a1)
/* rk[4] */
aes64ks1i t2, t1, 3
aes64ks2 t0, t2, t0
aes64ks2 t1, t0, t1
sd t0, 64(a1)
sd t1, 72(a1)
/* rk[5] */
aes64ks1i t2, t1, 4
aes64ks2 t0, t2, t0
aes64ks2 t1, t0, t1
sd t0, 80(a1)
sd t1, 88(a1)
/* rk[6] */
aes64ks1i t2, t1, 5
aes64ks2 t0, t2, t0
aes64ks2 t1, t0, t1
sd t0, 96(a1)
sd t1, 104(a1)
/* rk[7] */
aes64ks1i t2, t1, 6
aes64ks2 t0, t2, t0
aes64ks2 t1, t0, t1
sd t0, 112(a1)
sd t1, 120(a1)
/* rk[8] */
aes64ks1i t2, t1, 7
aes64ks2 t0, t2, t0
aes64ks2 t1, t0, t1
sd t0, 128(a1)
sd t1, 136(a1)
/* rk[9] */
aes64ks1i t2, t1, 8
aes64ks2 t0, t2, t0
aes64ks2 t1, t0, t1
sd t0, 144(a1)
sd t1, 152(a1)
/* rk[10] */
aes64ks1i t2, t1, 9
aes64ks2 t0, t2, t0
aes64ks2 t1, t0, t1
sd t0, 160(a1)
sd t1, 168(a1)
ret
/*
* void aes_encrypt(const uint64_t *rk, uint64_t *plaintext, uint64_t *ciphertext);
*/
aes_encrypt:
ld t2, 0(a1)
ld t3, 8(a1)
li t4, 9
mv t5, a0
/* Add round key */
ld t0, 0(t5)
ld t1, 8(t5)
addi t5, t5, 16
xor t2, t2, t0
xor t3, t3, t1
loop:
/* Rest of the round */
aes64esm t0, t2, t3
aes64esm t1, t3, t2
/* Add round key */
ld t2, 0(t5)
ld t3, 8(t5)
addi t5, t5, 16
xor t2, t2, t0
xor t3, t3, t1
addi t4, t4, -1
# It should be bne instruction for normal AES.
beq t4, zero, loop
/* Final round */
aes64es t0, t2, t3
aes64es t1, t3, t2
ld t2, 0(t5)
ld t3, 8(t5)
xor t2, t2, t0
xor t3, t3, t1
sd t2, 0(a2)
sd t3, 8(a2)
ret