2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
/*
|
|
|
|
Package hmac implements the Keyed-Hash Message Authentication Code (HMAC) as
|
|
|
|
defined in U.S. Federal Information Processing Standards Publication 198.
|
|
|
|
An HMAC is a cryptographic hash that uses a key to sign a message.
|
|
|
|
The receiver verifies the hash by recomputing it using the same key.
|
|
|
|
|
|
|
|
Receivers should be careful to use Equal to compare MACs in order to avoid
|
|
|
|
timing side-channels:
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
// CheckMAC reports whether messageMAC is a valid HMAC tag for message.
|
2012-11-21 08:03:38 +01:00
|
|
|
func CheckMAC(message, messageMAC, key []byte) bool {
|
|
|
|
mac := hmac.New(sha256.New, key)
|
|
|
|
mac.Write(message)
|
|
|
|
expectedMAC := mac.Sum(nil)
|
|
|
|
return hmac.Equal(messageMAC, expectedMAC)
|
|
|
|
}
|
|
|
|
*/
|
2010-12-03 05:34:57 +01:00
|
|
|
package hmac
|
|
|
|
|
|
|
|
import (
|
2012-11-21 08:03:38 +01:00
|
|
|
"crypto/subtle"
|
2010-12-03 05:34:57 +01:00
|
|
|
"hash"
|
|
|
|
)
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
// FIPS 198-1:
|
|
|
|
// http://csrc.nist.gov/publications/fips/fips198-1/FIPS-198-1_final.pdf
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
// key is zero padded to the block size of the hash function
|
|
|
|
// ipad = 0x36 byte repeated for key length
|
|
|
|
// opad = 0x5c byte repeated for key length
|
2010-12-03 05:34:57 +01:00
|
|
|
// hmac = H([key ^ opad] H([key ^ ipad] text))
|
|
|
|
|
|
|
|
type hmac struct {
|
|
|
|
size int
|
2012-01-25 22:54:22 +01:00
|
|
|
blocksize int
|
2016-07-22 20:15:38 +02:00
|
|
|
opad, ipad []byte
|
2010-12-03 05:34:57 +01:00
|
|
|
outer, inner hash.Hash
|
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
func (h *hmac) Sum(in []byte) []byte {
|
2011-12-14 16:41:54 +01:00
|
|
|
origLen := len(in)
|
|
|
|
in = h.inner.Sum(in)
|
2010-12-03 05:34:57 +01:00
|
|
|
h.outer.Reset()
|
2016-07-22 20:15:38 +02:00
|
|
|
h.outer.Write(h.opad)
|
|
|
|
h.outer.Write(in[origLen:])
|
2011-12-14 16:41:54 +01:00
|
|
|
return h.outer.Sum(in[:origLen])
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (h *hmac) Write(p []byte) (n int, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return h.inner.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hmac) Size() int { return h.size }
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func (h *hmac) BlockSize() int { return h.blocksize }
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
func (h *hmac) Reset() {
|
|
|
|
h.inner.Reset()
|
2016-07-22 20:15:38 +02:00
|
|
|
h.inner.Write(h.ipad)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
// New returns a new HMAC hash using the given hash.Hash type and key.
|
2010-12-03 05:34:57 +01:00
|
|
|
func New(h func() hash.Hash, key []byte) hash.Hash {
|
|
|
|
hm := new(hmac)
|
|
|
|
hm.outer = h()
|
|
|
|
hm.inner = h()
|
|
|
|
hm.size = hm.inner.Size()
|
2012-01-25 22:54:22 +01:00
|
|
|
hm.blocksize = hm.inner.BlockSize()
|
2016-07-22 20:15:38 +02:00
|
|
|
hm.ipad = make([]byte, hm.blocksize)
|
|
|
|
hm.opad = make([]byte, hm.blocksize)
|
2012-01-25 22:54:22 +01:00
|
|
|
if len(key) > hm.blocksize {
|
2010-12-03 05:34:57 +01:00
|
|
|
// If key is too big, hash it.
|
|
|
|
hm.outer.Write(key)
|
2011-12-13 20:16:27 +01:00
|
|
|
key = hm.outer.Sum(nil)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
copy(hm.ipad, key)
|
|
|
|
copy(hm.opad, key)
|
|
|
|
for i := range hm.ipad {
|
|
|
|
hm.ipad[i] ^= 0x36
|
|
|
|
}
|
|
|
|
for i := range hm.opad {
|
|
|
|
hm.opad[i] ^= 0x5c
|
|
|
|
}
|
|
|
|
hm.inner.Write(hm.ipad)
|
2010-12-03 05:34:57 +01:00
|
|
|
return hm
|
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
|
|
|
|
// Equal compares two MACs for equality without leaking timing information.
|
|
|
|
func Equal(mac1, mac2 []byte) bool {
|
|
|
|
// We don't have to be constant time if the lengths of the MACs are
|
|
|
|
// different as that suggests that a completely different hash function
|
|
|
|
// was used.
|
2017-01-14 01:05:42 +01:00
|
|
|
return subtle.ConstantTimeCompare(mac1, mac2) == 1
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|