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/AesCbc128Test.lvt

namespace Livt.Crypto.Tests.Aes
using Livt.Crypto.Aes



@Test
component AesCbc128Test
{
    cbc: Cbc128

    new()
    {
        this.cbc = new Cbc128()
    }

    // -----------------------------------------------------------------------
    // NIST SP 800-38A Appendix F.2.1 / F.2.2, AES-128 CBC
    // Key:   2b7e151628aed2a6abf7158809cf4f3c
    // IV:    000102030405060708090a0b0c0d0e0f
    // PT[1]: 6bc1bee22e409f96e93d7e117393172a  CT[1]: 7649abac8119b246cee98e9b12e9197d
    // -----------------------------------------------------------------------

    // NIST SP 800-38A F.2.1 CBC-AES128 Encrypt — Block 1.
    @Test
    fn TestNistSp80038AEncryptBlock1()
    {
        var key: byte[16] = [
            0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
            0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]

        var iv: byte[16] = [
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]

        var pt: byte[16] = [
            0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
            0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]

        this.cbc.SetKey(key)
        this.cbc.SetIV(iv)
        this.cbc.LoadBlock(pt)
        this.cbc.EncryptBlock()

        var ct: byte[16] = this.cbc.GetBlock()
        assert ct[0]  == 0x76
        assert ct[1]  == 0x49
        assert ct[2]  == 0xab
        assert ct[3]  == 0xac
        assert ct[4]  == 0x81
        assert ct[5]  == 0x19
        assert ct[6]  == 0xb2
        assert ct[7]  == 0x46
        assert ct[8]  == 0xce
        assert ct[9]  == 0xe9
        assert ct[10] == 0x8e
        assert ct[11] == 0x9b
        assert ct[12] == 0x12
        assert ct[13] == 0xe9
        assert ct[14] == 0x19
        assert ct[15] == 0x7d
    }

    // NIST SP 800-38A F.2.2 CBC-AES128 Decrypt — Block 1.
    @Test
    fn TestNistSp80038ADecryptBlock1()
    {
        var key: byte[16] = [
            0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
            0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]

        var iv: byte[16] = [
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]

        var ct: byte[16] = [
            0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
            0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d]

        this.cbc.SetKey(key)
        this.cbc.SetIV(iv)
        this.cbc.LoadBlock(ct)
        this.cbc.DecryptBlock()

        var pt: byte[16] = this.cbc.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
    }

    // Encrypt then decrypt — must recover the original plaintext.
    @Test
    fn TestEncryptDecryptRoundtrip()
    {
        var key: byte[16] = [
            0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
            0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]

        var iv: byte[16] = [
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f]

        var pt: byte[16] = [
            0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
            0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a]

        this.cbc.SetKey(key)
        this.cbc.SetIV(iv)
        this.cbc.LoadBlock(pt)
        this.cbc.EncryptBlock()
        var ct: byte[16] = this.cbc.GetBlock()

        this.cbc.SetIV(iv)
        this.cbc.LoadBlock(ct)
        this.cbc.DecryptBlock()

        var recovered: byte[16] = this.cbc.GetBlock()
        assert recovered[0]  == 0x6b
        assert recovered[1]  == 0xc1
        assert recovered[2]  == 0xbe
        assert recovered[3]  == 0xe2
        assert recovered[4]  == 0x2e
        assert recovered[5]  == 0x40
        assert recovered[6]  == 0x9f
        assert recovered[7]  == 0x96
        assert recovered[8]  == 0xe9
        assert recovered[9]  == 0x3d
        assert recovered[10] == 0x7e
        assert recovered[11] == 0x11
        assert recovered[12] == 0x73
        assert recovered[13] == 0x93
        assert recovered[14] == 0x17
        assert recovered[15] == 0x2a
    }
}