Livt packages
Livt.Crypto
Version 1.0.5
Fixed-size cryptographic components for Livt, including AES, ChaCha20, SHA, MACs, and key derivation.
Published files (69)
tests/aes/AesDecryptBlock256Test.lvt
namespace Livt.Crypto.Tests.Aes
using Livt.Crypto.Aes
@Test
component AesDecryptBlock256Test
{
aes: DecryptBlock256
enc: EncryptBlock256
new()
{
this.aes = new DecryptBlock256()
this.enc = new EncryptBlock256()
}
// NIST FIPS 197 Appendix C.3 AES-256 decryption vector.
// Key: 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
// Ciphertext: 8ea2b7ca516745bfeafc49904b496089
// Plaintext: 00112233445566778899aabbccddeeff
// Verify: echo "8ea2b7ca516745bfeafc49904b496089" | xxd -r -p | openssl enc -d -aes-256-ecb -K 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f -nopad 2>/dev/null | xxd -p
@Test
fn TestNistFips197()
{
var key: byte[32] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f]
var ciphertext: byte[16] = [
0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89]
this.aes.SetKey(key)
this.aes.LoadBlock(ciphertext)
this.aes.DecryptBlock()
var pt: byte[16] = this.aes.GetBlock()
assert pt[0] == 0x00
assert pt[1] == 0x11
assert pt[2] == 0x22
assert pt[3] == 0x33
assert pt[4] == 0x44
assert pt[5] == 0x55
assert pt[6] == 0x66
assert pt[7] == 0x77
assert pt[8] == 0x88
assert pt[9] == 0x99
assert pt[10] == 0xaa
assert pt[11] == 0xbb
assert pt[12] == 0xcc
assert pt[13] == 0xdd
assert pt[14] == 0xee
assert pt[15] == 0xff
}
// NIST SP 800-38A Appendix F.1.5 AES-256 ECB — block 1 decryption.
// Key: 603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4
// Ciphertext: f3eed1bdb5d2a03c064b5a7e3db181f8
// Plaintext: 6bc1bee22e409f96e93d7e117393172a
// Verify: echo "f3eed1bdb5d2a03c064b5a7e3db181f8" | xxd -r -p | openssl enc -d -aes-256-ecb -K 603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4 -nopad 2>/dev/null | xxd -p
@Test
fn TestNistSp80038ABlock1()
{
var key: byte[32] = [
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4]
var ciphertext: byte[16] = [
0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8]
this.aes.SetKey(key)
this.aes.LoadBlock(ciphertext)
this.aes.DecryptBlock()
var pt: byte[16] = this.aes.GetBlock()
assert pt[0] == 0x6b
assert pt[1] == 0xc1
assert pt[2] == 0xbe
assert pt[3] == 0xe2
assert pt[4] == 0x2e
assert pt[5] == 0x40
assert pt[6] == 0x9f
assert pt[7] == 0x96
assert pt[8] == 0xe9
assert pt[9] == 0x3d
assert pt[10] == 0x7e
assert pt[11] == 0x11
assert pt[12] == 0x73
assert pt[13] == 0x93
assert pt[14] == 0x17
assert pt[15] == 0x2a
}
// All-zero 256-bit key: decrypt dc95c078a2408989ad48a21492842087 → all zeros.
// Verify: echo "dc95c078a2408989ad48a21492842087" | xxd -r -p | openssl enc -d -aes-256-ecb -K 0000000000000000000000000000000000000000000000000000000000000000 -nopad 2>/dev/null | xxd -p
@Test
fn TestAllZeroKeyAndCiphertext()
{
var key: byte[32] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
var ciphertext: byte[16] = [
0xdc, 0x95, 0xc0, 0x78, 0xa2, 0x40, 0x89, 0x89,
0xad, 0x48, 0xa2, 0x14, 0x92, 0x84, 0x20, 0x87]
this.aes.SetKey(key)
this.aes.LoadBlock(ciphertext)
this.aes.DecryptBlock()
var pt: byte[16] = this.aes.GetBlock()
assert pt[0] == 0x00
assert pt[1] == 0x00
assert pt[2] == 0x00
assert pt[3] == 0x00
assert pt[4] == 0x00
assert pt[5] == 0x00
assert pt[6] == 0x00
assert pt[7] == 0x00
assert pt[8] == 0x00
assert pt[9] == 0x00
assert pt[10] == 0x00
assert pt[11] == 0x00
assert pt[12] == 0x00
assert pt[13] == 0x00
assert pt[14] == 0x00
assert pt[15] == 0x00
}
// User behaviour: encrypt a block then decrypt the result — must recover the
// original plaintext. Uses the NIST FIPS 197 Appendix C.3 key.
// This validates that AES-256 encrypt and decrypt are true inverses.
@Test
fn TestEncryptDecryptRoundtrip()
{
var key: byte[32] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f]
var plaintext: byte[16] = [
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]
// Encrypt.
this.enc.SetKey(key)
this.enc.LoadBlock(plaintext)
this.enc.EncryptBlock()
var ciphertext: byte[16] = this.enc.GetBlock()
// Decrypt the ciphertext — must recover the original plaintext.
this.aes.SetKey(key)
this.aes.LoadBlock(ciphertext)
this.aes.DecryptBlock()
var recovered: byte[16] = this.aes.GetBlock()
assert recovered[0] == 0x00
assert recovered[1] == 0x11
assert recovered[2] == 0x22
assert recovered[3] == 0x33
assert recovered[4] == 0x44
assert recovered[5] == 0x55
assert recovered[6] == 0x66
assert recovered[7] == 0x77
assert recovered[8] == 0x88
assert recovered[9] == 0x99
assert recovered[10] == 0xaa
assert recovered[11] == 0xbb
assert recovered[12] == 0xcc
assert recovered[13] == 0xdd
assert recovered[14] == 0xee
assert recovered[15] == 0xff
}
}