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/aead/Aes128GcmTest.lvt

namespace Livt.Crypto.Tests.Aead
using Livt.Crypto.Aead



// AES-128 GCM test suite.
// Reference vectors computed from NIST SP 800-38D / NIST CAVS using
// key  = feffe9928665731c6d6a8f9467308308
// iv   = cafebabefacedbaddecaf888
// pt   = d9313225f88406e5a55909c5aff5269a
@Test
component Aes128GcmTest
{
    gcm: Aes128Gcm

    new()
    {
        this.gcm = new Aes128Gcm()
    }

    // -----------------------------------------------------------------------
    // Encrypt one block — no AAD.
    // CT  = 42831ec2217774244b7221b784d0d49c
    // Tag = 57926dde92a5c01ee854dc9b33ebc856
    // -----------------------------------------------------------------------
    @Test
    fn TestEncryptNoAAD()
    {
        var key: byte[16] = [
            0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
            0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08]

        var nonce: byte[12] = [
            0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
            0xde, 0xca, 0xf8, 0x88]

        var pt: byte[16] = [
            0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
            0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a]

        this.gcm.SetKey(key)
        this.gcm.SetNonce(nonce)
        this.gcm.LoadBlock(pt)
        this.gcm.EncryptBlock()
        this.gcm.FinalizeTag(0, 16)

        var ct: byte[16] = this.gcm.GetBlock()
        assert ct[0]  == 0x42
        assert ct[1]  == 0x83
        assert ct[2]  == 0x1e
        assert ct[3]  == 0xc2
        assert ct[4]  == 0x21
        assert ct[5]  == 0x77
        assert ct[6]  == 0x74
        assert ct[7]  == 0x24
        assert ct[8]  == 0x4b
        assert ct[9]  == 0x72
        assert ct[10] == 0x21
        assert ct[11] == 0xb7
        assert ct[12] == 0x84
        assert ct[13] == 0xd0
        assert ct[14] == 0xd4
        assert ct[15] == 0x9c

        var tag: byte[16] = this.gcm.GetTag()
        assert tag[0]  == 0x57
        assert tag[1]  == 0x92
        assert tag[2]  == 0x6d
        assert tag[3]  == 0xde
        assert tag[4]  == 0x92
        assert tag[5]  == 0xa5
        assert tag[6]  == 0xc0
        assert tag[7]  == 0x1e
        assert tag[8]  == 0xe8
        assert tag[9]  == 0x54
        assert tag[10] == 0xdc
        assert tag[11] == 0x9b
        assert tag[12] == 0x33
        assert tag[13] == 0xeb
        assert tag[14] == 0xc8
        assert tag[15] == 0x56

        var ok: int = this.gcm.VerifyTag(tag)
        assert ok == 1
        tag[0] = tag[0] ^ 0x01
        var bad: int = this.gcm.VerifyTag(tag)
        assert bad == 0
    }

    // -----------------------------------------------------------------------
    // Decrypt: applying encrypt twice should recover the original plaintext.
    // -----------------------------------------------------------------------
    @Test
    fn TestDecryptRoundtrip()
    {
        var key: byte[16] = [
            0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
            0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08]

        var nonce: byte[12] = [
            0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
            0xde, 0xca, 0xf8, 0x88]

        var pt: byte[16] = [
            0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
            0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a]

        // Encrypt.
        this.gcm.SetKey(key)
        this.gcm.SetNonce(nonce)
        this.gcm.LoadBlock(pt)
        this.gcm.EncryptBlock()
        var ct: byte[16] = this.gcm.GetBlock()

        // Decrypt: reset nonce, feed ciphertext to DecryptBlock.
        this.gcm.SetNonce(nonce)
        this.gcm.LoadBlock(ct)
        this.gcm.DecryptBlock()
        var recovered: byte[16] = this.gcm.GetBlock()

        assert recovered[0]  == pt[0]
        assert recovered[1]  == pt[1]
        assert recovered[2]  == pt[2]
        assert recovered[3]  == pt[3]
        assert recovered[4]  == pt[4]
        assert recovered[5]  == pt[5]
        assert recovered[6]  == pt[6]
        assert recovered[7]  == pt[7]
        assert recovered[8]  == pt[8]
        assert recovered[9]  == pt[9]
        assert recovered[10] == pt[10]
        assert recovered[11] == pt[11]
        assert recovered[12] == pt[12]
        assert recovered[13] == pt[13]
        assert recovered[14] == pt[14]
        assert recovered[15] == pt[15]
    }
}