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

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



@Test
component AesCbc192Test
{
    cbc: Cbc192

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

    // -----------------------------------------------------------------------
    // NIST SP 800-38A Appendix F.2.3 / F.2.4, AES-192 CBC
    // Key:   8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b
    // IV:    000102030405060708090a0b0c0d0e0f
    // PT[1]: 6bc1bee22e409f96e93d7e117393172a  CT[1]: 4f021db243bc633d7178183a9fa071e8
    // -----------------------------------------------------------------------

    // NIST SP 800-38A F.2.3 CBC-AES192 Encrypt — Block 1.
    @Test
    fn TestNistSp80038AEncryptBlock1()
    {
        var key: byte[24] = [
            0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
            0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
            0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b]

        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]  == 0x4f
        assert ct[1]  == 0x02
        assert ct[2]  == 0x1d
        assert ct[3]  == 0xb2
        assert ct[4]  == 0x43
        assert ct[5]  == 0xbc
        assert ct[6]  == 0x63
        assert ct[7]  == 0x3d
        assert ct[8]  == 0x71
        assert ct[9]  == 0x78
        assert ct[10] == 0x18
        assert ct[11] == 0x3a
        assert ct[12] == 0x9f
        assert ct[13] == 0xa0
        assert ct[14] == 0x71
        assert ct[15] == 0xe8
    }

    // NIST SP 800-38A F.2.4 CBC-AES192 Decrypt — Block 1.
    @Test
    fn TestNistSp80038ADecryptBlock1()
    {
        var key: byte[24] = [
            0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
            0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
            0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b]

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

        var ct: byte[16] = [
            0x4f, 0x02, 0x1d, 0xb2, 0x43, 0xbc, 0x63, 0x3d,
            0x71, 0x78, 0x18, 0x3a, 0x9f, 0xa0, 0x71, 0xe8]

        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[24] = [
            0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
            0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
            0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b]

        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
    }
}