1// Copyright 2020 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14// 15//////////////////////////////////////////////////////////////////////////////// 16 17// Package subtle provides subtle implementations of the MAC primitive. 18package subtle 19 20import ( 21 "crypto/hmac" 22 "errors" 23 "fmt" 24 "hash" 25 26 "github.com/google/tink/go/subtle" 27) 28 29const ( 30 // Minimum key size in bytes. 31 minKeySizeInBytes = uint32(16) 32 33 // Minimum tag size in bytes. This provides minimum 80-bit security strength. 34 minTagSizeInBytes = uint32(10) 35) 36 37var errHMACInvalidInput = errors.New("HMAC: invalid input") 38 39// HMAC implementation of interface tink.MAC 40type HMAC struct { 41 HashFunc func() hash.Hash 42 Key []byte 43 TagSize uint32 44} 45 46// NewHMAC creates a new instance of HMAC with the specified key and tag size. 47func NewHMAC(hashAlg string, key []byte, tagSize uint32) (*HMAC, error) { 48 keySize := uint32(len(key)) 49 if err := ValidateHMACParams(hashAlg, keySize, tagSize); err != nil { 50 return nil, fmt.Errorf("hmac: %s", err) 51 } 52 hashFunc := subtle.GetHashFunc(hashAlg) 53 if hashFunc == nil { 54 return nil, fmt.Errorf("hmac: invalid hash algorithm") 55 } 56 return &HMAC{ 57 HashFunc: hashFunc, 58 Key: key, 59 TagSize: tagSize, 60 }, nil 61} 62 63// ValidateHMACParams validates parameters of HMAC constructor. 64func ValidateHMACParams(hash string, keySize uint32, tagSize uint32) error { 65 // validate tag size 66 digestSize, err := subtle.GetHashDigestSize(hash) 67 if err != nil { 68 return err 69 } 70 if tagSize > digestSize { 71 return fmt.Errorf("tag size too big") 72 } 73 if tagSize < minTagSizeInBytes { 74 return fmt.Errorf("tag size too small") 75 } 76 // validate key size 77 if keySize < minKeySizeInBytes { 78 return fmt.Errorf("key too short") 79 } 80 return nil 81} 82 83// ComputeMAC computes message authentication code (MAC) for the given data. 84func (h *HMAC) ComputeMAC(data []byte) ([]byte, error) { 85 mac := hmac.New(h.HashFunc, h.Key) 86 if _, err := mac.Write(data); err != nil { 87 return nil, err 88 } 89 tag := mac.Sum(nil) 90 return tag[:h.TagSize], nil 91} 92 93// VerifyMAC verifies whether the given MAC is a correct message authentication 94// code (MAC) the given data. 95func (h *HMAC) VerifyMAC(mac []byte, data []byte) error { 96 expectedMAC, err := h.ComputeMAC(data) 97 if err != nil { 98 return err 99 } 100 if hmac.Equal(expectedMAC, mac) { 101 return nil 102 } 103 return errors.New("HMAC: invalid MAC") 104} 105