54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package protocol
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"encoding/hex"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestOMAC1_RFC4493(t *testing.T) {
|
|
// RFC 4493 Test Vectors for AES-128-CMAC
|
|
keyHex := "2b7e151628aed2a6abf7158809cf4f3c"
|
|
key, _ := hex.DecodeString(keyHex)
|
|
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Example 1: Empty Message
|
|
msg1 := []byte{}
|
|
want1Hex := "bb1d6929e95937287fa37d129b756746"
|
|
want1, _ := hex.DecodeString(want1Hex)
|
|
|
|
got1 := omac1(block, msg1)
|
|
if !reflect.DeepEqual(got1, want1) {
|
|
t.Errorf("OMAC1 Empty Message Mismatch.\nGot: %x\nWant: %x", got1, want1)
|
|
}
|
|
|
|
// Example 2: 16 bytes
|
|
msg2Hex := "6bc1bee22e409f96e93d7e117393172a"
|
|
msg2, _ := hex.DecodeString(msg2Hex)
|
|
want2Hex := "070a16b46b4d4144f79bdd9dd04a287c"
|
|
want2, _ := hex.DecodeString(want2Hex)
|
|
|
|
got2 := omac1(block, msg2)
|
|
if !reflect.DeepEqual(got2, want2) {
|
|
t.Errorf("OMAC1 16-byte Message Mismatch.\nGot: %x\nWant: %x", got2, want2)
|
|
}
|
|
|
|
// Example 3: 40 bytes (not multiple of 16)
|
|
msg3Hex := "6bc1bee22e409f96e93d7e117393172a" +
|
|
"ae2d8a571e03ac9c9eb76fac45af8e51" +
|
|
"30c81c46a35ce411"
|
|
msg3, _ := hex.DecodeString(msg3Hex)
|
|
want3Hex := "dfa66747de9ae63030ca32611497c827"
|
|
want3, _ := hex.DecodeString(want3Hex)
|
|
|
|
got3 := omac1(block, msg3)
|
|
if !reflect.DeepEqual(got3, want3) {
|
|
t.Errorf("OMAC1 40-byte Message Mismatch.\nGot: %x\nWant: %x", got3, want3)
|
|
}
|
|
}
|