2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-07-25 18:49:57 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
// This implementation is derived from the golang implementation
|
2019-07-25 19:07:30 +03:00
|
|
|
// which itself is derived in part from the reference
|
2019-07-25 18:49:57 +03:00
|
|
|
// ANSI C implementation, which carries the following notice:
|
|
|
|
//
|
2020-11-11 19:16:45 +03:00
|
|
|
// rijndael-alg-fst.c
|
2019-07-25 18:49:57 +03:00
|
|
|
//
|
2020-11-11 19:16:45 +03:00
|
|
|
// @version 3.0 (December 2000)
|
2019-07-25 18:49:57 +03:00
|
|
|
//
|
2020-11-11 19:16:45 +03:00
|
|
|
// Optimised ANSI C code for the Rijndael cipher (now AES)
|
2019-07-25 18:49:57 +03:00
|
|
|
//
|
2020-11-11 19:16:45 +03:00
|
|
|
// @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
|
|
|
|
// @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
|
|
|
|
// @author Paulo Barreto <paulo.barreto@Terra.com.br>
|
2019-07-25 18:49:57 +03:00
|
|
|
//
|
2020-11-11 19:16:45 +03:00
|
|
|
// This code is hereby placed in the public domain.
|
2019-07-25 18:49:57 +03:00
|
|
|
//
|
2020-11-11 19:16:45 +03:00
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
|
|
|
|
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
|
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2019-07-25 18:49:57 +03:00
|
|
|
//
|
|
|
|
// See FIPS 197 for specification, and see Daemen and Rijmen's Rijndael submission
|
|
|
|
// for implementation details.
|
2020-11-11 19:16:45 +03:00
|
|
|
// https://csrc.nist.gov/csrc/media/publications/fips/197/final/documents/fips-197.pdf
|
|
|
|
// https://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf
|
2019-07-25 18:49:57 +03:00
|
|
|
module aes
|
|
|
|
|
2020-04-26 14:49:31 +03:00
|
|
|
import encoding.binary
|
2019-07-25 18:49:57 +03:00
|
|
|
|
|
|
|
// Encrypt one block from src into dst, using the expanded key xk.
|
2022-04-15 15:35:35 +03:00
|
|
|
fn encrypt_block_generic(xk []u32, mut dst []u8, src []u8) {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = src[15] // early bounds check
|
2019-10-27 10:03:15 +03:00
|
|
|
mut s0 := binary.big_endian_u32(src[..4])
|
2021-01-19 15:34:25 +03:00
|
|
|
mut s1 := binary.big_endian_u32(src[4..8])
|
|
|
|
mut s2 := binary.big_endian_u32(src[8..12])
|
|
|
|
mut s3 := binary.big_endian_u32(src[12..16])
|
2019-07-25 18:49:57 +03:00
|
|
|
// First round just XORs input with key.
|
|
|
|
s0 ^= xk[0]
|
|
|
|
s1 ^= xk[1]
|
|
|
|
s2 ^= xk[2]
|
|
|
|
s3 ^= xk[3]
|
|
|
|
// Middle rounds shuffle using tables.
|
|
|
|
// Number of rounds is set by length of expanded key.
|
2020-11-11 19:16:45 +03:00
|
|
|
nr := xk.len / 4 - 2 // - 2: one above, one more below
|
2019-07-25 18:49:57 +03:00
|
|
|
mut k := 4
|
|
|
|
mut t0 := u32(0)
|
|
|
|
mut t1 := u32(0)
|
|
|
|
mut t2 := u32(0)
|
|
|
|
mut t3 := u32(0)
|
2020-11-11 19:16:45 +03:00
|
|
|
for _ in 0 .. nr {
|
2022-04-15 14:58:56 +03:00
|
|
|
t0 = xk[k + 0] ^ te0[u8(s0 >> 24)] ^ te1[u8(s1 >> 16)] ^ te2[u8(s2 >> 8)] ^ u32(te3[u8(s3)])
|
|
|
|
t1 = xk[k + 1] ^ te0[u8(s1 >> 24)] ^ te1[u8(s2 >> 16)] ^ te2[u8(s3 >> 8)] ^ u32(te3[u8(s0)])
|
|
|
|
t2 = xk[k + 2] ^ te0[u8(s2 >> 24)] ^ te1[u8(s3 >> 16)] ^ te2[u8(s0 >> 8)] ^ u32(te3[u8(s1)])
|
|
|
|
t3 = xk[k + 3] ^ te0[u8(s3 >> 24)] ^ te1[u8(s0 >> 16)] ^ te2[u8(s1 >> 8)] ^ u32(te3[u8(s2)])
|
2019-07-25 18:49:57 +03:00
|
|
|
k += 4
|
|
|
|
s0 = t0
|
|
|
|
s1 = t1
|
|
|
|
s2 = t2
|
|
|
|
s3 = t3
|
|
|
|
}
|
|
|
|
// Last round uses s-box directly and XORs to produce output.
|
2021-11-29 03:48:49 +03:00
|
|
|
s0 = 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)])
|
|
|
|
s1 = 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)])
|
|
|
|
s2 = 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)])
|
|
|
|
s3 = 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)])
|
2020-11-11 19:16:45 +03:00
|
|
|
s0 ^= xk[k + 0]
|
|
|
|
s1 ^= xk[k + 1]
|
|
|
|
s2 ^= xk[k + 2]
|
|
|
|
s3 ^= xk[k + 3]
|
2020-06-08 01:47:04 +03:00
|
|
|
_ := dst[15] // early bounds check
|
2020-06-30 15:19:22 +03:00
|
|
|
binary.big_endian_put_u32(mut (*dst)[0..4], s0)
|
2021-01-19 15:34:25 +03:00
|
|
|
binary.big_endian_put_u32(mut (*dst)[4..8], s1)
|
|
|
|
binary.big_endian_put_u32(mut (*dst)[8..12], s2)
|
|
|
|
binary.big_endian_put_u32(mut (*dst)[12..16], s3)
|
2019-07-25 18:49:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt one block from src into dst, using the expanded key xk.
|
2022-04-15 15:35:35 +03:00
|
|
|
fn decrypt_block_generic(xk []u32, mut dst []u8, src []u8) {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = src[15] // early bounds check
|
2020-06-30 15:19:22 +03:00
|
|
|
mut s0 := binary.big_endian_u32(src[0..4])
|
2021-01-19 15:34:25 +03:00
|
|
|
mut s1 := binary.big_endian_u32(src[4..8])
|
|
|
|
mut s2 := binary.big_endian_u32(src[8..12])
|
|
|
|
mut s3 := binary.big_endian_u32(src[12..16])
|
2019-07-25 18:49:57 +03:00
|
|
|
// First round just XORs input with key.
|
|
|
|
s0 ^= xk[0]
|
|
|
|
s1 ^= xk[1]
|
|
|
|
s2 ^= xk[2]
|
|
|
|
s3 ^= xk[3]
|
|
|
|
// Middle rounds shuffle using tables.
|
|
|
|
// Number of rounds is set by length of expanded key.
|
2020-11-11 19:16:45 +03:00
|
|
|
nr := xk.len / 4 - 2 // - 2: one above, one more below
|
2019-07-25 18:49:57 +03:00
|
|
|
mut k := 4
|
|
|
|
mut t0 := u32(0)
|
|
|
|
mut t1 := u32(0)
|
|
|
|
mut t2 := u32(0)
|
|
|
|
mut t3 := u32(0)
|
2020-11-11 19:16:45 +03:00
|
|
|
for _ in 0 .. nr {
|
2022-04-15 14:58:56 +03:00
|
|
|
t0 = xk[k + 0] ^ td0[u8(s0 >> 24)] ^ td1[u8(s3 >> 16)] ^ td2[u8(s2 >> 8)] ^ u32(td3[u8(s1)])
|
|
|
|
t1 = xk[k + 1] ^ td0[u8(s1 >> 24)] ^ td1[u8(s0 >> 16)] ^ td2[u8(s3 >> 8)] ^ u32(td3[u8(s2)])
|
|
|
|
t2 = xk[k + 2] ^ td0[u8(s2 >> 24)] ^ td1[u8(s1 >> 16)] ^ td2[u8(s0 >> 8)] ^ u32(td3[u8(s3)])
|
|
|
|
t3 = xk[k + 3] ^ td0[u8(s3 >> 24)] ^ td1[u8(s2 >> 16)] ^ td2[u8(s1 >> 8)] ^ u32(td3[u8(s0)])
|
2019-07-25 18:49:57 +03:00
|
|
|
k += 4
|
|
|
|
s0 = t0
|
|
|
|
s1 = t1
|
|
|
|
s2 = t2
|
|
|
|
s3 = t3
|
|
|
|
}
|
|
|
|
// Last round uses s-box directly and XORs to produce output.
|
2021-11-29 03:48:49 +03:00
|
|
|
s0 = 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)])
|
|
|
|
s1 = 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)])
|
|
|
|
s2 = 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)])
|
|
|
|
s3 = 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)])
|
2020-11-11 19:16:45 +03:00
|
|
|
s0 ^= xk[k + 0]
|
|
|
|
s1 ^= xk[k + 1]
|
|
|
|
s2 ^= xk[k + 2]
|
|
|
|
s3 ^= xk[k + 3]
|
2019-07-25 18:49:57 +03:00
|
|
|
_ = dst[15] // early bounds check
|
2020-06-30 15:19:22 +03:00
|
|
|
binary.big_endian_put_u32(mut (*dst)[..4], s0)
|
2021-01-19 15:34:25 +03:00
|
|
|
binary.big_endian_put_u32(mut (*dst)[4..8], s1)
|
|
|
|
binary.big_endian_put_u32(mut (*dst)[8..12], s2)
|
|
|
|
binary.big_endian_put_u32(mut (*dst)[12..16], s3)
|
2019-07-25 18:49:57 +03:00
|
|
|
}
|
|
|
|
|
2019-10-24 14:48:20 +03:00
|
|
|
// Apply s_box0 to each byte in w.
|
2019-07-25 18:49:57 +03:00
|
|
|
fn subw(w u32) u32 {
|
2021-11-29 03:48:49 +03:00
|
|
|
return 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)])
|
2019-07-25 18:49:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate
|
2020-11-11 19:16:45 +03:00
|
|
|
fn rotw(w u32) u32 {
|
|
|
|
return (w << 8) | (w >> 24)
|
|
|
|
}
|
2019-07-25 18:49:57 +03:00
|
|
|
|
|
|
|
// Key expansion algorithm. See FIPS-197, Figure 11.
|
|
|
|
// Their rcon[i] is our powx[i-1] << 24.
|
2022-04-15 15:35:35 +03:00
|
|
|
fn expand_key_generic(key []u8, mut enc []u32, mut dec []u32) {
|
2019-07-25 18:49:57 +03:00
|
|
|
// Encryption key setup.
|
|
|
|
mut i := 0
|
|
|
|
nk := key.len / 4
|
|
|
|
for i = 0; i < nk; i++ {
|
2020-11-11 19:16:45 +03:00
|
|
|
if 4 * i >= key.len {
|
2019-07-25 18:49:57 +03:00
|
|
|
break
|
|
|
|
}
|
2020-11-11 19:16:45 +03:00
|
|
|
enc[i] = binary.big_endian_u32(key[4 * i..])
|
2019-07-25 18:49:57 +03:00
|
|
|
}
|
|
|
|
for i < enc.len {
|
2020-11-11 19:16:45 +03:00
|
|
|
mut t := enc[i - 1]
|
|
|
|
if i % nk == 0 {
|
|
|
|
t = subw(rotw(t)) ^ u32(pow_x[i / nk - 1]) << 24
|
|
|
|
} else if nk > 6 && i % nk == 4 {
|
2019-07-25 18:49:57 +03:00
|
|
|
t = subw(t)
|
|
|
|
}
|
2020-11-11 19:16:45 +03:00
|
|
|
enc[i] = enc[i - nk] ^ t
|
2019-07-25 18:49:57 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
// Derive decryption key from encryption key.
|
|
|
|
// Reverse the 4-word round key sets from enc to produce dec.
|
|
|
|
// All sets but the first and last get the MixColumn transform applied.
|
|
|
|
if dec.len == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n := enc.len
|
|
|
|
for i = 0; i < n; i += 4 {
|
|
|
|
ei := n - i - 4
|
2020-11-11 19:16:45 +03:00
|
|
|
for j in 0 .. 4 {
|
|
|
|
mut x := enc[ei + j]
|
|
|
|
if i > 0 && i + 4 < n {
|
2021-05-08 13:32:29 +03:00
|
|
|
x = td0[s_box0[x >> 24]] ^ td1[s_box0[x >> 16 & 0xff]] ^ td2[s_box0[x >> 8 & 0xff]] ^ td3[s_box0[x & u32(0xff)]]
|
2019-07-25 18:49:57 +03:00
|
|
|
}
|
2020-11-11 19:16:45 +03:00
|
|
|
dec[i + j] = x
|
2019-07-25 18:49:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|