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

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



// Tests for Cmac128 — NIST SP 800-38B vectors.
// Key: 2b7e151628aed2a6abf7158809cf4f3c
@Test
component AesCmac128Test
{
    cmac: Cmac128

    new()
    {
        this.cmac = new Cmac128()
    }

    // -----------------------------------------------------------------------
    // NIST SP 800-38B, AES-128, zero-length message.
    // Input:  (empty)
    // Padded: 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    // Tag:    bb1d6929e95937287fa37d129b756746
    // -----------------------------------------------------------------------
    @Test
    fn TestNistEmptyMessage()
    {
        var key: byte[16] = [
            0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
            0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]

        var padded: byte[16] = [
            0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]

        this.cmac.SetKey(key)
        this.cmac.Reset()
        this.cmac.FinalizePaddedBlock(padded)

        var tag: byte[16] = this.cmac.GetTag()
        assert tag[0]  == 0xbb
        assert tag[1]  == 0x1d
        assert tag[2]  == 0x69
        assert tag[3]  == 0x29
        assert tag[4]  == 0xe9
        assert tag[5]  == 0x59
        assert tag[6]  == 0x37
        assert tag[7]  == 0x28
        assert tag[8]  == 0x7f
        assert tag[9]  == 0xa3
        assert tag[10] == 0x7d
        assert tag[11] == 0x12
        assert tag[12] == 0x9b
        assert tag[13] == 0x75
        assert tag[14] == 0x67
        assert tag[15] == 0x46
    }

    // -----------------------------------------------------------------------
    // NIST SP 800-38B, AES-128, one-block message.
    // Message: 6bc1bee22e409f96e93d7e117393172a
    // Tag:     070a16b46b4d4144f79bdd9dd04a287c
    // -----------------------------------------------------------------------
    @Test
    fn TestNistOneBlock()
    {
        var key: byte[16] = [
            0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
            0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]

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

        this.cmac.SetKey(key)
        this.cmac.Reset()
        this.cmac.FinalizeBlock(msg)

        var tag: byte[16] = this.cmac.GetTag()
        assert tag[0]  == 0x07
        assert tag[1]  == 0x0a
        assert tag[2]  == 0x16
        assert tag[3]  == 0xb4
        assert tag[4]  == 0x6b
        assert tag[5]  == 0x4d
        assert tag[6]  == 0x41
        assert tag[7]  == 0x44
        assert tag[8]  == 0xf7
        assert tag[9]  == 0x9b
        assert tag[10] == 0xdd
        assert tag[11] == 0x9d
        assert tag[12] == 0xd0
        assert tag[13] == 0x4a
        assert tag[14] == 0x28
        assert tag[15] == 0x7c
    }

    // -----------------------------------------------------------------------
    // Determinism: same key + message → identical tags on two consecutive runs.
    // -----------------------------------------------------------------------
    @Test
    fn TestDeterminism()
    {
        var key: byte[16] = [
            0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
            0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]

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

        this.cmac.SetKey(key)

        // First run.
        this.cmac.Reset()
        this.cmac.FinalizeBlock(msg)
        var tag1: byte[16] = this.cmac.GetTag()
        var t0:  byte = tag1[0]
        var t1:  byte = tag1[1]
        var t2:  byte = tag1[2]
        var t3:  byte = tag1[3]
        var t4:  byte = tag1[4]
        var t5:  byte = tag1[5]
        var t6:  byte = tag1[6]
        var t7:  byte = tag1[7]
        var t8:  byte = tag1[8]
        var t9:  byte = tag1[9]
        var t10: byte = tag1[10]
        var t11: byte = tag1[11]
        var t12: byte = tag1[12]
        var t13: byte = tag1[13]
        var t14: byte = tag1[14]
        var t15: byte = tag1[15]

        // Second run.
        this.cmac.Reset()
        this.cmac.FinalizeBlock(msg)
        var tag2: byte[16] = this.cmac.GetTag()

        assert tag2[0]  == t0
        assert tag2[1]  == t1
        assert tag2[2]  == t2
        assert tag2[3]  == t3
        assert tag2[4]  == t4
        assert tag2[5]  == t5
        assert tag2[6]  == t6
        assert tag2[7]  == t7
        assert tag2[8]  == t8
        assert tag2[9]  == t9
        assert tag2[10] == t10
        assert tag2[11] == t11
        assert tag2[12] == t12
        assert tag2[13] == t13
        assert tag2[14] == t14
        assert tag2[15] == t15
    }
}