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 package aes
38
39 import "internal/byteorder"
40
41
42 func encryptBlockGo(xk []uint32, dst, src []byte) {
43 _ = src[15]
44 s0 := byteorder.BeUint32(src[0:4])
45 s1 := byteorder.BeUint32(src[4:8])
46 s2 := byteorder.BeUint32(src[8:12])
47 s3 := byteorder.BeUint32(src[12:16])
48
49
50 s0 ^= xk[0]
51 s1 ^= xk[1]
52 s2 ^= xk[2]
53 s3 ^= xk[3]
54
55
56
57 nr := len(xk)/4 - 2
58 k := 4
59 var t0, t1, t2, t3 uint32
60 for r := 0; r < nr; r++ {
61 t0 = xk[k+0] ^ te0[uint8(s0>>24)] ^ te1[uint8(s1>>16)] ^ te2[uint8(s2>>8)] ^ te3[uint8(s3)]
62 t1 = xk[k+1] ^ te0[uint8(s1>>24)] ^ te1[uint8(s2>>16)] ^ te2[uint8(s3>>8)] ^ te3[uint8(s0)]
63 t2 = xk[k+2] ^ te0[uint8(s2>>24)] ^ te1[uint8(s3>>16)] ^ te2[uint8(s0>>8)] ^ te3[uint8(s1)]
64 t3 = xk[k+3] ^ te0[uint8(s3>>24)] ^ te1[uint8(s0>>16)] ^ te2[uint8(s1>>8)] ^ te3[uint8(s2)]
65 k += 4
66 s0, s1, s2, s3 = t0, t1, t2, t3
67 }
68
69
70 s0 = uint32(sbox0[t0>>24])<<24 | uint32(sbox0[t1>>16&0xff])<<16 | uint32(sbox0[t2>>8&0xff])<<8 | uint32(sbox0[t3&0xff])
71 s1 = uint32(sbox0[t1>>24])<<24 | uint32(sbox0[t2>>16&0xff])<<16 | uint32(sbox0[t3>>8&0xff])<<8 | uint32(sbox0[t0&0xff])
72 s2 = uint32(sbox0[t2>>24])<<24 | uint32(sbox0[t3>>16&0xff])<<16 | uint32(sbox0[t0>>8&0xff])<<8 | uint32(sbox0[t1&0xff])
73 s3 = uint32(sbox0[t3>>24])<<24 | uint32(sbox0[t0>>16&0xff])<<16 | uint32(sbox0[t1>>8&0xff])<<8 | uint32(sbox0[t2&0xff])
74
75 s0 ^= xk[k+0]
76 s1 ^= xk[k+1]
77 s2 ^= xk[k+2]
78 s3 ^= xk[k+3]
79
80 _ = dst[15]
81 byteorder.BePutUint32(dst[0:4], s0)
82 byteorder.BePutUint32(dst[4:8], s1)
83 byteorder.BePutUint32(dst[8:12], s2)
84 byteorder.BePutUint32(dst[12:16], s3)
85 }
86
87
88 func decryptBlockGo(xk []uint32, dst, src []byte) {
89 _ = src[15]
90 s0 := byteorder.BeUint32(src[0:4])
91 s1 := byteorder.BeUint32(src[4:8])
92 s2 := byteorder.BeUint32(src[8:12])
93 s3 := byteorder.BeUint32(src[12:16])
94
95
96 s0 ^= xk[0]
97 s1 ^= xk[1]
98 s2 ^= xk[2]
99 s3 ^= xk[3]
100
101
102
103 nr := len(xk)/4 - 2
104 k := 4
105 var t0, t1, t2, t3 uint32
106 for r := 0; r < nr; r++ {
107 t0 = xk[k+0] ^ td0[uint8(s0>>24)] ^ td1[uint8(s3>>16)] ^ td2[uint8(s2>>8)] ^ td3[uint8(s1)]
108 t1 = xk[k+1] ^ td0[uint8(s1>>24)] ^ td1[uint8(s0>>16)] ^ td2[uint8(s3>>8)] ^ td3[uint8(s2)]
109 t2 = xk[k+2] ^ td0[uint8(s2>>24)] ^ td1[uint8(s1>>16)] ^ td2[uint8(s0>>8)] ^ td3[uint8(s3)]
110 t3 = xk[k+3] ^ td0[uint8(s3>>24)] ^ td1[uint8(s2>>16)] ^ td2[uint8(s1>>8)] ^ td3[uint8(s0)]
111 k += 4
112 s0, s1, s2, s3 = t0, t1, t2, t3
113 }
114
115
116 s0 = uint32(sbox1[t0>>24])<<24 | uint32(sbox1[t3>>16&0xff])<<16 | uint32(sbox1[t2>>8&0xff])<<8 | uint32(sbox1[t1&0xff])
117 s1 = uint32(sbox1[t1>>24])<<24 | uint32(sbox1[t0>>16&0xff])<<16 | uint32(sbox1[t3>>8&0xff])<<8 | uint32(sbox1[t2&0xff])
118 s2 = uint32(sbox1[t2>>24])<<24 | uint32(sbox1[t1>>16&0xff])<<16 | uint32(sbox1[t0>>8&0xff])<<8 | uint32(sbox1[t3&0xff])
119 s3 = uint32(sbox1[t3>>24])<<24 | uint32(sbox1[t2>>16&0xff])<<16 | uint32(sbox1[t1>>8&0xff])<<8 | uint32(sbox1[t0&0xff])
120
121 s0 ^= xk[k+0]
122 s1 ^= xk[k+1]
123 s2 ^= xk[k+2]
124 s3 ^= xk[k+3]
125
126 _ = dst[15]
127 byteorder.BePutUint32(dst[0:4], s0)
128 byteorder.BePutUint32(dst[4:8], s1)
129 byteorder.BePutUint32(dst[8:12], s2)
130 byteorder.BePutUint32(dst[12:16], s3)
131 }
132
133
134 func subw(w uint32) uint32 {
135 return uint32(sbox0[w>>24])<<24 |
136 uint32(sbox0[w>>16&0xff])<<16 |
137 uint32(sbox0[w>>8&0xff])<<8 |
138 uint32(sbox0[w&0xff])
139 }
140
141
142 func rotw(w uint32) uint32 { return w<<8 | w>>24 }
143
144
145
146 func expandKeyGo(key []byte, enc, dec []uint32) {
147
148 var i int
149 nk := len(key) / 4
150 for i = 0; i < nk; i++ {
151 enc[i] = byteorder.BeUint32(key[4*i:])
152 }
153 for ; i < len(enc); i++ {
154 t := enc[i-1]
155 if i%nk == 0 {
156 t = subw(rotw(t)) ^ (uint32(powx[i/nk-1]) << 24)
157 } else if nk > 6 && i%nk == 4 {
158 t = subw(t)
159 }
160 enc[i] = enc[i-nk] ^ t
161 }
162
163
164
165
166 if dec == nil {
167 return
168 }
169 n := len(enc)
170 for i := 0; i < n; i += 4 {
171 ei := n - i - 4
172 for j := 0; j < 4; j++ {
173 x := enc[ei+j]
174 if i > 0 && i+4 < n {
175 x = td0[sbox0[x>>24]] ^ td1[sbox0[x>>16&0xff]] ^ td2[sbox0[x>>8&0xff]] ^ td3[sbox0[x&0xff]]
176 }
177 dec[i+j] = x
178 }
179 }
180 }
181
View as plain text