blob: 5ca3c6c1bfa2adbb024742d297cb12b4570fd3e3 [file] [log] [blame]
Holger Hildebrandtfa074992020-03-27 15:42:06 +00001// Copyright (c) 2016 Andreas Auernhammer. All rights reserved.
2// Use of this source code is governed by a license that can be
3// found in the LICENSE file.
4
5// Package aes implements the CMAC MAC with the AES.
6// AES-CMAC is specified in RFC 4493 and RFC 4494.
7package aes // import "github.com/aead/cmac/aes"
8
9import (
10 aesCipher "crypto/aes"
11 "hash"
12
13 "github.com/aead/cmac"
14)
15
16// Sum computes the AES-CMAC checksum with the given tagsize of msg using the cipher.Block.
17func Sum(msg, key []byte, tagsize int) ([]byte, error) {
18 c, err := aesCipher.NewCipher(key)
19 if err != nil {
20 return nil, err
21 }
22 return cmac.Sum(msg, c, tagsize)
23}
24
25// Verify computes the AES-CMAC checksum with the given tagsize of msg and compares
26// it with the given mac. This functions returns true if and only if the given mac
27// is equal to the computed one.
28func Verify(mac, msg, key []byte, tagsize int) bool {
29 c, err := aesCipher.NewCipher(key)
30 if err != nil {
31 return false
32 }
33 return cmac.Verify(mac, msg, c, tagsize)
34}
35
36// New returns a hash.Hash computing the AES-CMAC checksum.
37func New(key []byte) (hash.Hash, error) {
38 return NewWithTagSize(key, aesCipher.BlockSize)
39}
40
41// NewWithTagSize returns a hash.Hash computing the AES-CMAC checksum with the
42// given tag size. The tag size must between the 1 and the cipher's block size.
43func NewWithTagSize(key []byte, tagsize int) (hash.Hash, error) {
44 c, err := aesCipher.NewCipher(key)
45 if err != nil {
46 return nil, err
47 }
48 return cmac.NewWithTagSize(c, tagsize)
49}