416 lines
13 KiB
MoonBit
416 lines
13 KiB
MoonBit
///|
|
|
/// Pure-MoonBit AES-256 (FIPS-197) and AES-256-CBC (NIST SP 800-38A).
|
|
/// Used for NTCP2 ephemeral key obfuscation and I2P tunnel layer encryption.
|
|
|
|
///|
|
|
let aes_sbox : FixedArray[Byte] = [
|
|
b'\x63', b'\x7c', b'\x77', b'\x7b', b'\xf2', b'\x6b', b'\x6f', b'\xc5', b'\x30',
|
|
b'\x01', b'\x67', b'\x2b', b'\xfe', b'\xd7', b'\xab', b'\x76', b'\xca', b'\x82',
|
|
b'\xc9', b'\x7d', b'\xfa', b'\x59', b'\x47', b'\xf0', b'\xad', b'\xd4', b'\xa2',
|
|
b'\xaf', b'\x9c', b'\xa4', b'\x72', b'\xc0', b'\xb7', b'\xfd', b'\x93', b'\x26',
|
|
b'\x36', b'\x3f', b'\xf7', b'\xcc', b'\x34', b'\xa5', b'\xe5', b'\xf1', b'\x71',
|
|
b'\xd8', b'\x31', b'\x15', b'\x04', b'\xc7', b'\x23', b'\xc3', b'\x18', b'\x96',
|
|
b'\x05', b'\x9a', b'\x07', b'\x12', b'\x80', b'\xe2', b'\xeb', b'\x27', b'\xb2',
|
|
b'\x75', b'\x09', b'\x83', b'\x2c', b'\x1a', b'\x1b', b'\x6e', b'\x5a', b'\xa0',
|
|
b'\x52', b'\x3b', b'\xd6', b'\xb3', b'\x29', b'\xe3', b'\x2f', b'\x84', b'\x53',
|
|
b'\xd1', b'\x00', b'\xed', b'\x20', b'\xfc', b'\xb1', b'\x5b', b'\x6a', b'\xcb',
|
|
b'\xbe', b'\x39', b'\x4a', b'\x4c', b'\x58', b'\xcf', b'\xd0', b'\xef', b'\xaa',
|
|
b'\xfb', b'\x43', b'\x4d', b'\x33', b'\x85', b'\x45', b'\xf9', b'\x02', b'\x7f',
|
|
b'\x50', b'\x3c', b'\x9f', b'\xa8', b'\x51', b'\xa3', b'\x40', b'\x8f', b'\x92',
|
|
b'\x9d', b'\x38', b'\xf5', b'\xbc', b'\xb6', b'\xda', b'\x21', b'\x10', b'\xff',
|
|
b'\xf3', b'\xd2', b'\xcd', b'\x0c', b'\x13', b'\xec', b'\x5f', b'\x97', b'\x44',
|
|
b'\x17', b'\xc4', b'\xa7', b'\x7e', b'\x3d', b'\x64', b'\x5d', b'\x19', b'\x73',
|
|
b'\x60', b'\x81', b'\x4f', b'\xdc', b'\x22', b'\x2a', b'\x90', b'\x88', b'\x46',
|
|
b'\xee', b'\xb8', b'\x14', b'\xde', b'\x5e', b'\x0b', b'\xdb', b'\xe0', b'\x32',
|
|
b'\x3a', b'\x0a', b'\x49', b'\x06', b'\x24', b'\x5c', b'\xc2', b'\xd3', b'\xac',
|
|
b'\x62', b'\x91', b'\x95', b'\xe4', b'\x79', b'\xe7', b'\xc8', b'\x37', b'\x6d',
|
|
b'\x8d', b'\xd5', b'\x4e', b'\xa9', b'\x6c', b'\x56', b'\xf4', b'\xea', b'\x65',
|
|
b'\x7a', b'\xae', b'\x08', b'\xba', b'\x78', b'\x25', b'\x2e', b'\x1c', b'\xa6',
|
|
b'\xb4', b'\xc6', b'\xe8', b'\xdd', b'\x74', b'\x1f', b'\x4b', b'\xbd', b'\x8b',
|
|
b'\x8a', b'\x70', b'\x3e', b'\xb5', b'\x66', b'\x48', b'\x03', b'\xf6', b'\x0e',
|
|
b'\x61', b'\x35', b'\x57', b'\xb9', b'\x86', b'\xc1', b'\x1d', b'\x9e', b'\xe1',
|
|
b'\xf8', b'\x98', b'\x11', b'\x69', b'\xd9', b'\x8e', b'\x94', b'\x9b', b'\x1e',
|
|
b'\x87', b'\xe9', b'\xce', b'\x55', b'\x28', b'\xdf', b'\x8c', b'\xa1', b'\x89',
|
|
b'\x0d', b'\xbf', b'\xe6', b'\x42', b'\x68', b'\x41', b'\x99', b'\x2d', b'\x0f',
|
|
b'\xb0', b'\x54', b'\xbb', b'\x16',
|
|
]
|
|
|
|
///|
|
|
let aes_inv_sbox : FixedArray[Byte] = [
|
|
b'\x52', b'\x09', b'\x6a', b'\xd5', b'\x30', b'\x36', b'\xa5', b'\x38', b'\xbf',
|
|
b'\x40', b'\xa3', b'\x9e', b'\x81', b'\xf3', b'\xd7', b'\xfb', b'\x7c', b'\xe3',
|
|
b'\x39', b'\x82', b'\x9b', b'\x2f', b'\xff', b'\x87', b'\x34', b'\x8e', b'\x43',
|
|
b'\x44', b'\xc4', b'\xde', b'\xe9', b'\xcb', b'\x54', b'\x7b', b'\x94', b'\x32',
|
|
b'\xa6', b'\xc2', b'\x23', b'\x3d', b'\xee', b'\x4c', b'\x95', b'\x0b', b'\x42',
|
|
b'\xfa', b'\xc3', b'\x4e', b'\x08', b'\x2e', b'\xa1', b'\x66', b'\x28', b'\xd9',
|
|
b'\x24', b'\xb2', b'\x76', b'\x5b', b'\xa2', b'\x49', b'\x6d', b'\x8b', b'\xd1',
|
|
b'\x25', b'\x72', b'\xf8', b'\xf6', b'\x64', b'\x86', b'\x68', b'\x98', b'\x16',
|
|
b'\xd4', b'\xa4', b'\x5c', b'\xcc', b'\x5d', b'\x65', b'\xb6', b'\x92', b'\x6c',
|
|
b'\x70', b'\x48', b'\x50', b'\xfd', b'\xed', b'\xb9', b'\xda', b'\x5e', b'\x15',
|
|
b'\x46', b'\x57', b'\xa7', b'\x8d', b'\x9d', b'\x84', b'\x90', b'\xd8', b'\xab',
|
|
b'\x00', b'\x8c', b'\xbc', b'\xd3', b'\x0a', b'\xf7', b'\xe4', b'\x58', b'\x05',
|
|
b'\xb8', b'\xb3', b'\x45', b'\x06', b'\xd0', b'\x2c', b'\x1e', b'\x8f', b'\xca',
|
|
b'\x3f', b'\x0f', b'\x02', b'\xc1', b'\xaf', b'\xbd', b'\x03', b'\x01', b'\x13',
|
|
b'\x8a', b'\x6b', b'\x3a', b'\x91', b'\x11', b'\x41', b'\x4f', b'\x67', b'\xdc',
|
|
b'\xea', b'\x97', b'\xf2', b'\xcf', b'\xce', b'\xf0', b'\xb4', b'\xe6', b'\x73',
|
|
b'\x96', b'\xac', b'\x74', b'\x22', b'\xe7', b'\xad', b'\x35', b'\x85', b'\xe2',
|
|
b'\xf9', b'\x37', b'\xe8', b'\x1c', b'\x75', b'\xdf', b'\x6e', b'\x47', b'\xf1',
|
|
b'\x1a', b'\x71', b'\x1d', b'\x29', b'\xc5', b'\x89', b'\x6f', b'\xb7', b'\x62',
|
|
b'\x0e', b'\xaa', b'\x18', b'\xbe', b'\x1b', b'\xfc', b'\x56', b'\x3e', b'\x4b',
|
|
b'\xc6', b'\xd2', b'\x79', b'\x20', b'\x9a', b'\xdb', b'\xc0', b'\xfe', b'\x78',
|
|
b'\xcd', b'\x5a', b'\xf4', b'\x1f', b'\xdd', b'\xa8', b'\x33', b'\x88', b'\x07',
|
|
b'\xc7', b'\x31', b'\xb1', b'\x12', b'\x10', b'\x59', b'\x27', b'\x80', b'\xec',
|
|
b'\x5f', b'\x60', b'\x51', b'\x7f', b'\xa9', b'\x19', b'\xb5', b'\x4a', b'\x0d',
|
|
b'\x2d', b'\xe5', b'\x7a', b'\x9f', b'\x93', b'\xc9', b'\x9c', b'\xef', b'\xa0',
|
|
b'\xe0', b'\x3b', b'\x4d', b'\xae', b'\x2a', b'\xf5', b'\xb0', b'\xc8', b'\xeb',
|
|
b'\xbb', b'\x3c', b'\x83', b'\x53', b'\x99', b'\x61', b'\x17', b'\x2b', b'\x04',
|
|
b'\x7e', b'\xba', b'\x77', b'\xd6', b'\x26', b'\xe1', b'\x69', b'\x14', b'\x63',
|
|
b'\x55', b'\x21', b'\x0c', b'\x7d',
|
|
]
|
|
|
|
///|
|
|
let aes_rcon : FixedArray[UInt32] = [
|
|
0x00000000U, 0x01000000U, 0x02000000U, 0x04000000U, 0x08000000U, 0x10000000U, 0x20000000U,
|
|
0x40000000U,
|
|
]
|
|
|
|
///|
|
|
fn aes_sub_word(w : UInt32) -> UInt32 {
|
|
let b0 = aes_sbox[((w >> 24) & 0xFFU).reinterpret_as_int()].to_uint()
|
|
let b1 = aes_sbox[((w >> 16) & 0xFFU).reinterpret_as_int()].to_uint()
|
|
let b2 = aes_sbox[((w >> 8) & 0xFFU).reinterpret_as_int()].to_uint()
|
|
let b3 = aes_sbox[(w & 0xFFU).reinterpret_as_int()].to_uint()
|
|
(b0 << 24) | (b1 << 16) | (b2 << 8) | b3
|
|
}
|
|
|
|
///|
|
|
fn aes_rot_word(w : UInt32) -> UInt32 {
|
|
(w << 8) | (w >> 24)
|
|
}
|
|
|
|
///|
|
|
/// Expands a 32-byte AES-256 key into 60 32-bit round key words (240 bytes).
|
|
fn aes256_expand_key(key : Bytes) -> FixedArray[Byte] {
|
|
let w = FixedArray::make(60, 0U)
|
|
for i = 0; i < 8; i = i + 1 {
|
|
w[i] = (key[4 * i].to_uint() << 24) |
|
|
(key[4 * i + 1].to_uint() << 16) |
|
|
(key[4 * i + 2].to_uint() << 8) |
|
|
key[4 * i + 3].to_uint()
|
|
}
|
|
for i = 8; i < 60; i = i + 1 {
|
|
let mut temp = w[i - 1]
|
|
if i % 8 == 0 {
|
|
temp = aes_sub_word(aes_rot_word(temp)) ^ aes_rcon[i / 8]
|
|
} else if i % 8 == 4 {
|
|
temp = aes_sub_word(temp)
|
|
}
|
|
w[i] = w[i - 8] ^ temp
|
|
}
|
|
let rk = FixedArray::make(240, b'\x00')
|
|
for i = 0; i < 60; i = i + 1 {
|
|
rk[4 * i] = ((w[i] >> 24) & 0xFFU).to_byte()
|
|
rk[4 * i + 1] = ((w[i] >> 16) & 0xFFU).to_byte()
|
|
rk[4 * i + 2] = ((w[i] >> 8) & 0xFFU).to_byte()
|
|
rk[4 * i + 3] = (w[i] & 0xFFU).to_byte()
|
|
}
|
|
rk
|
|
}
|
|
|
|
///|
|
|
fn aes_xtime(a : Int32) -> Int32 {
|
|
let hi = a & 0x80
|
|
let shifted = (a << 1) & 0xFF
|
|
if hi != 0 {
|
|
shifted ^ 0x1b
|
|
} else {
|
|
shifted
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn aes_gf_mul(a : Int32, b : Int32) -> Int32 {
|
|
let mut x = a & 0xFF
|
|
let mut y = b & 0xFF
|
|
let mut p = 0
|
|
for _i = 0; _i < 8; _i = _i + 1 {
|
|
if (y & 1) != 0 {
|
|
p = p ^ x
|
|
}
|
|
x = aes_xtime(x)
|
|
y = y >> 1
|
|
}
|
|
p
|
|
}
|
|
|
|
///|
|
|
fn aes_add_round_key(
|
|
state : FixedArray[Byte],
|
|
rk : FixedArray[Byte],
|
|
round : Int32,
|
|
) -> Unit {
|
|
let off = round * 16
|
|
for i = 0; i < 16; i = i + 1 {
|
|
state[i] = (state[i].to_int() ^ rk[off + i].to_int()).to_byte()
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn aes_sub_bytes(state : FixedArray[Byte]) -> Unit {
|
|
for i = 0; i < 16; i = i + 1 {
|
|
state[i] = aes_sbox[state[i].to_int()]
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn aes_inv_sub_bytes(state : FixedArray[Byte]) -> Unit {
|
|
for i = 0; i < 16; i = i + 1 {
|
|
state[i] = aes_inv_sbox[state[i].to_int()]
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn aes_shift_rows(s : FixedArray[Byte]) -> Unit {
|
|
// Row 1: left shift by 1 (indices 1, 5, 9, 13)
|
|
let t1 = s[1]
|
|
s[1] = s[5]
|
|
s[5] = s[9]
|
|
s[9] = s[13]
|
|
s[13] = t1
|
|
// Row 2: left shift by 2 (indices 2, 6, 10, 14)
|
|
let t2 = s[2]
|
|
let t6 = s[6]
|
|
s[2] = s[10]
|
|
s[6] = s[14]
|
|
s[10] = t2
|
|
s[14] = t6
|
|
// Row 3: left shift by 3 (indices 3, 7, 11, 15)
|
|
let t15 = s[15]
|
|
s[15] = s[11]
|
|
s[11] = s[7]
|
|
s[7] = s[3]
|
|
s[3] = t15
|
|
}
|
|
|
|
///|
|
|
fn aes_inv_shift_rows(s : FixedArray[Byte]) -> Unit {
|
|
// Row 1: right shift by 1 (indices 1, 5, 9, 13)
|
|
let t13 = s[13]
|
|
s[13] = s[9]
|
|
s[9] = s[5]
|
|
s[5] = s[1]
|
|
s[1] = t13
|
|
// Row 2: right shift by 2 (indices 2, 6, 10, 14)
|
|
let t2 = s[2]
|
|
let t6 = s[6]
|
|
s[2] = s[10]
|
|
s[6] = s[14]
|
|
s[10] = t2
|
|
s[14] = t6
|
|
// Row 3: right shift by 3 (indices 3, 7, 11, 15)
|
|
let t3 = s[3]
|
|
s[3] = s[7]
|
|
s[7] = s[11]
|
|
s[11] = s[15]
|
|
s[15] = t3
|
|
}
|
|
|
|
///|
|
|
fn aes_mix_columns(s : FixedArray[Byte]) -> Unit {
|
|
for c = 0; c < 4; c = c + 1 {
|
|
let i = c * 4
|
|
let a0 = s[i].to_int()
|
|
let a1 = s[i + 1].to_int()
|
|
let a2 = s[i + 2].to_int()
|
|
let a3 = s[i + 3].to_int()
|
|
let t = a0 ^ a1 ^ a2 ^ a3
|
|
s[i] = (a0 ^ t ^ aes_xtime(a0 ^ a1)).to_byte()
|
|
s[i + 1] = (a1 ^ t ^ aes_xtime(a1 ^ a2)).to_byte()
|
|
s[i + 2] = (a2 ^ t ^ aes_xtime(a2 ^ a3)).to_byte()
|
|
s[i + 3] = (a3 ^ t ^ aes_xtime(a3 ^ a0)).to_byte()
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn aes_inv_mix_columns(s : FixedArray[Byte]) -> Unit {
|
|
for c = 0; c < 4; c = c + 1 {
|
|
let i = c * 4
|
|
let a0 = s[i].to_int()
|
|
let a1 = s[i + 1].to_int()
|
|
let a2 = s[i + 2].to_int()
|
|
let a3 = s[i + 3].to_int()
|
|
s[i] = (aes_gf_mul(a0, 14) ^
|
|
aes_gf_mul(a1, 11) ^
|
|
aes_gf_mul(a2, 13) ^
|
|
aes_gf_mul(a3, 9)).to_byte()
|
|
s[i + 1] = (aes_gf_mul(a0, 9) ^
|
|
aes_gf_mul(a1, 14) ^
|
|
aes_gf_mul(a2, 11) ^
|
|
aes_gf_mul(a3, 13)).to_byte()
|
|
s[i + 2] = (aes_gf_mul(a0, 13) ^
|
|
aes_gf_mul(a1, 9) ^
|
|
aes_gf_mul(a2, 14) ^
|
|
aes_gf_mul(a3, 11)).to_byte()
|
|
s[i + 3] = (aes_gf_mul(a0, 11) ^
|
|
aes_gf_mul(a1, 13) ^
|
|
aes_gf_mul(a2, 9) ^
|
|
aes_gf_mul(a3, 14)).to_byte()
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn aes256_encrypt_block_with_rk(
|
|
rk : FixedArray[Byte],
|
|
state : FixedArray[Byte],
|
|
) -> Unit {
|
|
aes_add_round_key(state, rk, 0)
|
|
for round = 1; round < 14; round = round + 1 {
|
|
aes_sub_bytes(state)
|
|
aes_shift_rows(state)
|
|
aes_mix_columns(state)
|
|
aes_add_round_key(state, rk, round)
|
|
}
|
|
aes_sub_bytes(state)
|
|
aes_shift_rows(state)
|
|
aes_add_round_key(state, rk, 14)
|
|
}
|
|
|
|
///|
|
|
fn aes256_decrypt_block_with_rk(
|
|
rk : FixedArray[Byte],
|
|
state : FixedArray[Byte],
|
|
) -> Unit {
|
|
aes_add_round_key(state, rk, 14)
|
|
for round = 13; round >= 1; round = round - 1 {
|
|
aes_inv_shift_rows(state)
|
|
aes_inv_sub_bytes(state)
|
|
aes_add_round_key(state, rk, round)
|
|
aes_inv_mix_columns(state)
|
|
}
|
|
aes_inv_shift_rows(state)
|
|
aes_inv_sub_bytes(state)
|
|
aes_add_round_key(state, rk, 0)
|
|
}
|
|
|
|
///|
|
|
/// Encrypts a single 16-byte block with AES-256 (ECB).
|
|
pub fn aes256_encrypt_block(
|
|
key : Bytes,
|
|
block : Bytes,
|
|
) -> Bytes raise CryptoError {
|
|
if key.length() != 32 {
|
|
raise InvalidKeyLength("AES-256 key must be 32 bytes, got \{key.length()}")
|
|
}
|
|
if block.length() != 16 {
|
|
raise InvalidLength("AES block must be 16 bytes, got \{block.length()}")
|
|
}
|
|
let rk = aes256_expand_key(key)
|
|
let state = FixedArray::make(16, b'\x00')
|
|
for i = 0; i < 16; i = i + 1 {
|
|
state[i] = block[i]
|
|
}
|
|
aes256_encrypt_block_with_rk(rk, state)
|
|
Bytes::from_array(state)
|
|
}
|
|
|
|
///|
|
|
/// Decrypts a single 16-byte block with AES-256 (ECB).
|
|
pub fn aes256_decrypt_block(
|
|
key : Bytes,
|
|
block : Bytes,
|
|
) -> Bytes raise CryptoError {
|
|
if key.length() != 32 {
|
|
raise InvalidKeyLength("AES-256 key must be 32 bytes, got \{key.length()}")
|
|
}
|
|
if block.length() != 16 {
|
|
raise InvalidLength("AES block must be 16 bytes, got \{block.length()}")
|
|
}
|
|
let rk = aes256_expand_key(key)
|
|
let state = FixedArray::make(16, b'\x00')
|
|
for i = 0; i < 16; i = i + 1 {
|
|
state[i] = block[i]
|
|
}
|
|
aes256_decrypt_block_with_rk(rk, state)
|
|
Bytes::from_array(state)
|
|
}
|
|
|
|
///|
|
|
/// Encrypts `data` (length must be a multiple of 16 bytes) using AES-256-CBC (no padding).
|
|
pub fn aes256_cbc_encrypt(
|
|
key : Bytes,
|
|
iv : Bytes,
|
|
data : Bytes,
|
|
) -> Bytes raise CryptoError {
|
|
if key.length() != 32 {
|
|
raise InvalidKeyLength("AES-256 key must be 32 bytes, got \{key.length()}")
|
|
}
|
|
if iv.length() != 16 {
|
|
raise InvalidNonceLength("AES-CBC IV must be 16 bytes, got \{iv.length()}")
|
|
}
|
|
if data.length() % 16 != 0 {
|
|
raise InvalidLength(
|
|
"AES-CBC input must be a multiple of 16 bytes, got \{data.length()}",
|
|
)
|
|
}
|
|
let rk = aes256_expand_key(key)
|
|
let out = FixedArray::make(data.length(), b'\x00')
|
|
let prev = FixedArray::make(16, b'\x00')
|
|
for i = 0; i < 16; i = i + 1 {
|
|
prev[i] = iv[i]
|
|
}
|
|
let state = FixedArray::make(16, b'\x00')
|
|
let mut off = 0
|
|
while off < data.length() {
|
|
for i = 0; i < 16; i = i + 1 {
|
|
state[i] = (data[off + i].to_int() ^ prev[i].to_int()).to_byte()
|
|
}
|
|
aes256_encrypt_block_with_rk(rk, state)
|
|
for i = 0; i < 16; i = i + 1 {
|
|
out[off + i] = state[i]
|
|
prev[i] = state[i]
|
|
}
|
|
off += 16
|
|
}
|
|
Bytes::from_array(out)
|
|
}
|
|
|
|
///|
|
|
/// Decrypts `data` (length must be a multiple of 16 bytes) using AES-256-CBC (no padding).
|
|
pub fn aes256_cbc_decrypt(
|
|
key : Bytes,
|
|
iv : Bytes,
|
|
data : Bytes,
|
|
) -> Bytes raise CryptoError {
|
|
if key.length() != 32 {
|
|
raise InvalidKeyLength("AES-256 key must be 32 bytes, got \{key.length()}")
|
|
}
|
|
if iv.length() != 16 {
|
|
raise InvalidNonceLength("AES-CBC IV must be 16 bytes, got \{iv.length()}")
|
|
}
|
|
if data.length() % 16 != 0 {
|
|
raise InvalidLength(
|
|
"AES-CBC input must be a multiple of 16 bytes, got \{data.length()}",
|
|
)
|
|
}
|
|
let rk = aes256_expand_key(key)
|
|
let out = FixedArray::make(data.length(), b'\x00')
|
|
let prev = FixedArray::make(16, b'\x00')
|
|
for i = 0; i < 16; i = i + 1 {
|
|
prev[i] = iv[i]
|
|
}
|
|
let state = FixedArray::make(16, b'\x00')
|
|
let mut off = 0
|
|
while off < data.length() {
|
|
for i = 0; i < 16; i = i + 1 {
|
|
state[i] = data[off + i]
|
|
}
|
|
aes256_decrypt_block_with_rk(rk, state)
|
|
for i = 0; i < 16; i = i + 1 {
|
|
out[off + i] = (state[i].to_int() ^ prev[i].to_int()).to_byte()
|
|
prev[i] = data[off + i]
|
|
}
|
|
off += 16
|
|
}
|
|
Bytes::from_array(out)
|
|
}
|