Source file src/crypto/internal/fips140test/cmac_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fipstest
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/internal/fips140/aes"
    10  	"crypto/internal/fips140/aes/gcm"
    11  	"testing"
    12  )
    13  
    14  func TestCMAC(t *testing.T) {
    15  	// https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/AES_CMAC.pdf
    16  	key := "2B7E1516 28AED2A6 ABF71588 09CF4F3C"
    17  	tests := []struct {
    18  		in, out string
    19  	}{
    20  		{
    21  			"",
    22  			"BB1D6929 E9593728 7FA37D12 9B756746",
    23  		},
    24  		{
    25  			"6BC1BEE2 2E409F96 E93D7E11 7393172A",
    26  			"070A16B4 6B4D4144 F79BDD9D D04A287C",
    27  		},
    28  		{
    29  			"6BC1BEE2 2E409F96 E93D7E11 7393172A AE2D8A57",
    30  			"7D85449E A6EA19C8 23A7BF78 837DFADE",
    31  		},
    32  	}
    33  
    34  	b, err := aes.New(decodeHex(t, key))
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	c := gcm.NewCMAC(b)
    39  	for i, test := range tests {
    40  		in := decodeHex(t, test.in)
    41  		out := decodeHex(t, test.out)
    42  		got := c.MAC(in)
    43  		if !bytes.Equal(got[:], out) {
    44  			t.Errorf("test %d: got %x, want %x", i, got, out)
    45  		}
    46  	}
    47  }
    48  

View as plain text