2022-03-10 09:44:48 +00:00
|
|
|
package hmac
|
|
|
|
|
2024-01-18 18:54:54 +00:00
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
2022-03-10 09:44:48 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"hash"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2024-01-18 18:54:54 +00:00
|
|
|
// setZeroes sets all bytes from b to zeroes.
|
|
|
|
//
|
|
|
|
// See https://github.com/golang/go/issues/5373
|
|
|
|
func setZeroes(b []byte) {
|
|
|
|
for i := range b {
|
|
|
|
b[i] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
func (h *hmac) resetTo(key []byte) {
|
|
|
|
h.outer.Reset()
|
|
|
|
h.inner.Reset()
|
2024-01-18 18:54:54 +00:00
|
|
|
setZeroes(h.ipad)
|
|
|
|
setZeroes(h.opad)
|
|
|
|
if len(key) > h.blocksize {
|
2022-03-10 09:44:48 +00:00
|
|
|
// If key is too big, hash it.
|
2024-01-18 18:54:54 +00:00
|
|
|
h.outer.Write(key)
|
2022-03-10 09:44:48 +00:00
|
|
|
key = h.outer.Sum(nil)
|
|
|
|
}
|
|
|
|
copy(h.ipad, key)
|
|
|
|
copy(h.opad, key)
|
|
|
|
for i := range h.ipad {
|
|
|
|
h.ipad[i] ^= 0x36
|
|
|
|
}
|
|
|
|
for i := range h.opad {
|
|
|
|
h.opad[i] ^= 0x5c
|
|
|
|
}
|
2024-01-18 18:54:54 +00:00
|
|
|
h.inner.Write(h.ipad)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 18:54:54 +00:00
|
|
|
var hmacSHA1Pool = &sync.Pool{
|
2022-03-10 09:44:48 +00:00
|
|
|
New: func() interface{} {
|
|
|
|
h := New(sha1.New, make([]byte, sha1.BlockSize))
|
|
|
|
return h
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// AcquireSHA1 returns new HMAC from pool.
|
|
|
|
func AcquireSHA1(key []byte) hash.Hash {
|
2024-01-18 18:54:54 +00:00
|
|
|
h := hmacSHA1Pool.Get().(*hmac)
|
2022-03-10 09:44:48 +00:00
|
|
|
assertHMACSize(h, sha1.Size, sha1.BlockSize)
|
|
|
|
h.resetTo(key)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutSHA1 puts h to pool.
|
|
|
|
func PutSHA1(h hash.Hash) {
|
2024-01-18 18:54:54 +00:00
|
|
|
hm := h.(*hmac)
|
2022-03-10 09:44:48 +00:00
|
|
|
assertHMACSize(hm, sha1.Size, sha1.BlockSize)
|
|
|
|
hmacSHA1Pool.Put(hm)
|
|
|
|
}
|
|
|
|
|
2024-01-18 18:54:54 +00:00
|
|
|
var hmacSHA256Pool = &sync.Pool{
|
2022-03-10 09:44:48 +00:00
|
|
|
New: func() interface{} {
|
|
|
|
h := New(sha256.New, make([]byte, sha256.BlockSize))
|
|
|
|
return h
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// AcquireSHA256 returns new HMAC from SHA256 pool.
|
|
|
|
func AcquireSHA256(key []byte) hash.Hash {
|
2024-01-18 18:54:54 +00:00
|
|
|
h := hmacSHA256Pool.Get().(*hmac)
|
2022-03-10 09:44:48 +00:00
|
|
|
assertHMACSize(h, sha256.Size, sha256.BlockSize)
|
|
|
|
h.resetTo(key)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutSHA256 puts h to SHA256 pool.
|
|
|
|
func PutSHA256(h hash.Hash) {
|
2024-01-18 18:54:54 +00:00
|
|
|
hm := h.(*hmac)
|
2022-03-10 09:44:48 +00:00
|
|
|
assertHMACSize(hm, sha256.Size, sha256.BlockSize)
|
|
|
|
hmacSHA256Pool.Put(hm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertHMACSize panics if h.size != size or h.blocksize != blocksize.
|
|
|
|
//
|
|
|
|
// Put and Acquire functions are internal functions to project, so
|
|
|
|
// checking it via such assert is optimal.
|
2024-01-18 18:54:54 +00:00
|
|
|
func assertHMACSize(h *hmac, size, blocksize int) {
|
|
|
|
if h.size != size || h.blocksize != blocksize {
|
|
|
|
panic("BUG: hmac size invalid") // nolint
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
}
|