v
Зеркало из https://github.com/vlang/v
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4// This implementation is derived from the golang implementation
5// which itself is derived in part from the reference
6// ANSI C implementation, which carries the following notice:
7//
8// rijndael-alg-fst.c
9//
10// @version 3.0 (December 2000)
11//
12// Optimised ANSI C code for the Rijndael cipher (now AES)
13//
14// @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
15// @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
16// @author Paulo Barreto <paulo.barreto@Terra.com.br>
17//
18// This code is hereby placed in the public domain.
19//
20// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
21// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
24// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//
32// See FIPS 197 for specification, and see Daemen and Rijmen's Rijndael submission
33// for implementation details.
34// https://csrc.nist.gov/csrc/media/publications/fips/197/final/documents/fips-197.pdf
35// https://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf
36module aes
37
38import encoding.binary
39
40// Encrypt one block from src into dst, using the expanded key xk.
41@[direct_array_access]
42fn encrypt_block_generic(xk []u32, mut dst []u8, src []u8) {
43_ = src[15] // early bounds check
44mut s0 := binary.big_endian_u32(src[..4])
45mut s1 := binary.big_endian_u32(src[4..8])
46mut s2 := binary.big_endian_u32(src[8..12])
47mut s3 := binary.big_endian_u32(src[12..16])
48// First round just XORs input with key.
49s0 ^= xk[0]
50s1 ^= xk[1]
51s2 ^= xk[2]
52s3 ^= xk[3]
53// Middle rounds shuffle using tables.
54// Number of rounds is set by length of expanded key.
55nr := xk.len / 4 - 2 // - 2: one above, one more below
56mut k := 4
57mut t0 := u32(0)
58mut t1 := u32(0)
59mut t2 := u32(0)
60mut t3 := u32(0)
61for _ in 0 .. nr {
62t0 = xk[k + 0] ^ te0[u8(s0 >> 24)] ^ te1[u8(s1 >> 16)] ^ te2[u8(s2 >> 8)] ^ u32(te3[u8(s3)])
63t1 = xk[k + 1] ^ te0[u8(s1 >> 24)] ^ te1[u8(s2 >> 16)] ^ te2[u8(s3 >> 8)] ^ u32(te3[u8(s0)])
64t2 = xk[k + 2] ^ te0[u8(s2 >> 24)] ^ te1[u8(s3 >> 16)] ^ te2[u8(s0 >> 8)] ^ u32(te3[u8(s1)])
65t3 = xk[k + 3] ^ te0[u8(s3 >> 24)] ^ te1[u8(s0 >> 16)] ^ te2[u8(s1 >> 8)] ^ u32(te3[u8(s2)])
66k += 4
67s0 = t0
68s1 = t1
69s2 = t2
70s3 = t3
71}
72// Last round uses s-box directly and XORs to produce output.
73s0 = u32(s_box0[t0 >> 24]) << 24 | u32(s_box0[t1 >> 16 & 0xff]) << 16 | u32(s_box0[t2 >> 8 & 0xff]) << 8 | u32(s_box0[t3 & u32(0xff)])
74s1 = u32(s_box0[t1 >> 24]) << 24 | u32(s_box0[t2 >> 16 & 0xff]) << 16 | u32(s_box0[t3 >> 8 & 0xff]) << 8 | u32(s_box0[t0 & u32(0xff)])
75s2 = u32(s_box0[t2 >> 24]) << 24 | u32(s_box0[t3 >> 16 & 0xff]) << 16 | u32(s_box0[t0 >> 8 & 0xff]) << 8 | u32(s_box0[t1 & u32(0xff)])
76s3 = u32(s_box0[t3 >> 24]) << 24 | u32(s_box0[t0 >> 16 & 0xff]) << 16 | u32(s_box0[t1 >> 8 & 0xff]) << 8 | u32(s_box0[t2 & u32(0xff)])
77s0 ^= xk[k + 0]
78s1 ^= xk[k + 1]
79s2 ^= xk[k + 2]
80s3 ^= xk[k + 3]
81_ := dst[15] // early bounds check
82binary.big_endian_put_u32(mut (*dst)[0..4], s0)
83binary.big_endian_put_u32(mut (*dst)[4..8], s1)
84binary.big_endian_put_u32(mut (*dst)[8..12], s2)
85binary.big_endian_put_u32(mut (*dst)[12..16], s3)
86}
87
88// Decrypt one block from src into dst, using the expanded key xk.
89@[direct_array_access]
90fn decrypt_block_generic(xk []u32, mut dst []u8, src []u8) {
91_ = src[15] // early bounds check
92mut s0 := binary.big_endian_u32(src[0..4])
93mut s1 := binary.big_endian_u32(src[4..8])
94mut s2 := binary.big_endian_u32(src[8..12])
95mut s3 := binary.big_endian_u32(src[12..16])
96// First round just XORs input with key.
97s0 ^= xk[0]
98s1 ^= xk[1]
99s2 ^= xk[2]
100s3 ^= xk[3]
101// Middle rounds shuffle using tables.
102// Number of rounds is set by length of expanded key.
103nr := xk.len / 4 - 2 // - 2: one above, one more below
104mut k := 4
105mut t0 := u32(0)
106mut t1 := u32(0)
107mut t2 := u32(0)
108mut t3 := u32(0)
109for _ in 0 .. nr {
110t0 = xk[k + 0] ^ td0[u8(s0 >> 24)] ^ td1[u8(s3 >> 16)] ^ td2[u8(s2 >> 8)] ^ u32(td3[u8(s1)])
111t1 = xk[k + 1] ^ td0[u8(s1 >> 24)] ^ td1[u8(s0 >> 16)] ^ td2[u8(s3 >> 8)] ^ u32(td3[u8(s2)])
112t2 = xk[k + 2] ^ td0[u8(s2 >> 24)] ^ td1[u8(s1 >> 16)] ^ td2[u8(s0 >> 8)] ^ u32(td3[u8(s3)])
113t3 = xk[k + 3] ^ td0[u8(s3 >> 24)] ^ td1[u8(s2 >> 16)] ^ td2[u8(s1 >> 8)] ^ u32(td3[u8(s0)])
114k += 4
115s0 = t0
116s1 = t1
117s2 = t2
118s3 = t3
119}
120// Last round uses s-box directly and XORs to produce output.
121s0 = u32(s_box1[t0 >> 24]) << 24 | u32(s_box1[t3 >> 16 & 0xff]) << 16 | u32(s_box1[t2 >> 8 & 0xff]) << 8 | u32(s_box1[t1 & u32(0xff)])
122s1 = u32(s_box1[t1 >> 24]) << 24 | u32(s_box1[t0 >> 16 & 0xff]) << 16 | u32(s_box1[t3 >> 8 & 0xff]) << 8 | u32(s_box1[t2 & u32(0xff)])
123s2 = u32(s_box1[t2 >> 24]) << 24 | u32(s_box1[t1 >> 16 & 0xff]) << 16 | u32(s_box1[t0 >> 8 & 0xff]) << 8 | u32(s_box1[t3 & u32(0xff)])
124s3 = u32(s_box1[t3 >> 24]) << 24 | u32(s_box1[t2 >> 16 & 0xff]) << 16 | u32(s_box1[t1 >> 8 & 0xff]) << 8 | u32(s_box1[t0 & u32(0xff)])
125s0 ^= xk[k + 0]
126s1 ^= xk[k + 1]
127s2 ^= xk[k + 2]
128s3 ^= xk[k + 3]
129_ = dst[15] // early bounds check
130binary.big_endian_put_u32(mut (*dst)[..4], s0)
131binary.big_endian_put_u32(mut (*dst)[4..8], s1)
132binary.big_endian_put_u32(mut (*dst)[8..12], s2)
133binary.big_endian_put_u32(mut (*dst)[12..16], s3)
134}
135
136// Apply s_box0 to each byte in w.
137@[direct_array_access; inline]
138fn subw(w u32) u32 {
139return u32(s_box0[w >> 24]) << 24 | u32(s_box0[w >> 16 & 0xff]) << 16 | u32(s_box0[w >> 8 & 0xff]) << 8 | u32(s_box0[w & u32(0xff)])
140}
141
142// Rotate
143@[inline]
144fn rotw(w u32) u32 {
145return (w << 8) | (w >> 24)
146}
147
148// Key expansion algorithm. See FIPS-197, Figure 11.
149// Their rcon[i] is our powx[i-1] << 24.
150@[direct_array_access]
151fn expand_key_generic(key []u8, mut enc []u32, mut dec []u32) {
152// Encryption key setup.
153mut i := 0
154nk := key.len / 4
155for i = 0; i < nk; i++ {
156if 4 * i >= key.len {
157break
158}
159enc[i] = binary.big_endian_u32(key[4 * i..])
160}
161for i < enc.len {
162mut t := enc[i - 1]
163if i % nk == 0 {
164t = subw(rotw(t)) ^ u32(pow_x[i / nk - 1]) << 24
165} else if nk > 6 && i % nk == 4 {
166t = subw(t)
167}
168enc[i] = enc[i - nk] ^ t
169i++
170}
171// Derive decryption key from encryption key.
172// Reverse the 4-word round key sets from enc to produce dec.
173// All sets but the first and last get the MixColumn transform applied.
174if dec.len == 0 {
175return
176}
177n := enc.len
178for i = 0; i < n; i += 4 {
179ei := n - i - 4
180for j in 0 .. 4 {
181mut x := enc[ei + j]
182if i > 0 && i + 4 < n {
183x = td0[s_box0[x >> 24]] ^ td1[s_box0[x >> 16 & 0xff]] ^ td2[s_box0[x >> 8 & 0xff]] ^ td3[s_box0[x & u32(0xff)]]
184}
185dec[i + j] = x
186}
187}
188}
189