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/mac/HmacSha256Test.lvt

namespace Livt.Crypto.Tests.Mac
using Livt.Crypto.Mac



// Tests for HmacSha256 (RFC 2104 / RFC 4231 test vectors).
@Test component HmacSha256Test
{
    hmac: HmacSha256

    new()
    {
        this.hmac = new HmacSha256()
    }

    // RFC 4231 Test Case 1 — key = 20 x 0x0b, data = "Hi There" (8 bytes).
    //
    // Inner SHA-256 input: k_ipad (64 B) || "Hi There" (8 B) = 72 B total.
    // k_ipad[64] = 0x3d*20 || 0x36*44
    //
    // Inner block 2 (64 B) = "Hi There" || 0x80 || 0x00*47 || be64(576)
    //   where 72*8 = 576 = 0x240
    //
    // Expected HMAC-SHA-256:
    //   b0 34 4c 61  d8 db 38 53  5c a8 af ce  af 0b f1 2b
    //   88 1d c2 00  c9 83 3d a7  26 e9 37 6c  2e 32 cf f7
    @Test fn TestRfc4231Tc1()
    {
        var key: byte[32] = [
            0x0b, 0x0b, 0x0b, 0x0b,  0x0b, 0x0b, 0x0b, 0x0b,
            0x0b, 0x0b, 0x0b, 0x0b,  0x0b, 0x0b, 0x0b, 0x0b,
            0x0b, 0x0b, 0x0b, 0x0b,  0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00]
        this.hmac.SetKey(key)
        this.hmac.Init()

        var msg2: byte[64] = [
            0x48, 0x69, 0x20, 0x54,  0x68, 0x65, 0x72, 0x65,
            0x80, 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, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x02, 0x40]
        this.hmac.ProcessBlock(msg2)

        var result: byte[32] = this.hmac.GetHmac()
        assert result[0]  == 0xb0; assert result[1]  == 0x34; assert result[2]  == 0x4c; assert result[3]  == 0x61
        assert result[4]  == 0xd8; assert result[5]  == 0xdb; assert result[6]  == 0x38; assert result[7]  == 0x53
        assert result[8]  == 0x5c; assert result[9]  == 0xa8; assert result[10] == 0xaf; assert result[11] == 0xce
        assert result[12] == 0xaf; assert result[13] == 0x0b; assert result[14] == 0xf1; assert result[15] == 0x2b
        assert result[16] == 0x88; assert result[17] == 0x1d; assert result[18] == 0xc2; assert result[19] == 0x00
        assert result[20] == 0xc9; assert result[21] == 0x83; assert result[22] == 0x3d; assert result[23] == 0xa7
        assert result[24] == 0x26; assert result[25] == 0xe9; assert result[26] == 0x37; assert result[27] == 0x6c
        assert result[28] == 0x2e; assert result[29] == 0x32; assert result[30] == 0xcf; assert result[31] == 0xf7
    }

    // RFC 4231 Test Case 2 — key = "Jefe" (4 bytes),
    //   data = "what do ya want for nothing?" (28 bytes).
    //
    // Inner SHA-256 input: k_ipad (64 B) || data (28 B) = 92 B total.
    // k_ipad bytes: 0x4a^0x36=0x7c, 0x65^0x36=0x53, 0x66^0x36=0x50, 0x65^0x36=0x53, 0x36*60
    //
    // Inner block 2 (64 B) = data || 0x80 || 0x00*27 || be64(736)
    //   where 92*8 = 736 = 0x2e0
    //
    // Expected HMAC-SHA-256:
    //   5b dc c1 46  bf 60 75 4e  6a 04 24 26  08 95 75 c7
    //   5a 00 3f 08  9d 27 39 83  9d ec 58 b9  64 ec 38 43
    @Test fn TestRfc4231Tc2()
    {
        var key: byte[32] = [
            0x4a, 0x65, 0x66, 0x65,  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]
        this.hmac.SetKey(key)
        this.hmac.Init()

        // "what do ya want for nothing?" = 28 bytes, then SHA-256 pad to 92 bytes.
        var msg2: byte[64] = [
            0x77, 0x68, 0x61, 0x74,  0x20, 0x64, 0x6f, 0x20,
            0x79, 0x61, 0x20, 0x77,  0x61, 0x6e, 0x74, 0x20,
            0x66, 0x6f, 0x72, 0x20,  0x6e, 0x6f, 0x74, 0x68,
            0x69, 0x6e, 0x67, 0x3f,  0x80, 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, 0x00, 0x02, 0xe0]
        this.hmac.ProcessBlock(msg2)

        var result: byte[32] = this.hmac.GetHmac()
        assert result[0]  == 0x5b; assert result[1]  == 0xdc; assert result[2]  == 0xc1; assert result[3]  == 0x46
        assert result[4]  == 0xbf; assert result[5]  == 0x60; assert result[6]  == 0x75; assert result[7]  == 0x4e
        assert result[8]  == 0x6a; assert result[9]  == 0x04; assert result[10] == 0x24; assert result[11] == 0x26
        assert result[12] == 0x08; assert result[13] == 0x95; assert result[14] == 0x75; assert result[15] == 0xc7
        assert result[16] == 0x5a; assert result[17] == 0x00; assert result[18] == 0x3f; assert result[19] == 0x08
        assert result[20] == 0x9d; assert result[21] == 0x27; assert result[22] == 0x39; assert result[23] == 0x83
        assert result[24] == 0x9d; assert result[25] == 0xec; assert result[26] == 0x58; assert result[27] == 0xb9
        assert result[28] == 0x64; assert result[29] == 0xec; assert result[30] == 0x38; assert result[31] == 0x43
    }
}