| |
| |
| |
|
|
| |
| |
| |
| package hmac |
|
|
| import ( |
| "crypto/internal/fips140" |
| "crypto/internal/fips140/sha256" |
| "crypto/internal/fips140/sha3" |
| "crypto/internal/fips140/sha512" |
| "errors" |
| "hash" |
| ) |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| type marshalable interface { |
| MarshalBinary() ([]byte, error) |
| UnmarshalBinary([]byte) error |
| } |
|
|
| type HMAC struct { |
| |
| opad, ipad []byte |
| outer, inner hash.Hash |
|
|
| |
| |
| |
| marshaled bool |
|
|
| |
| forHKDF bool |
| keyLen int |
| } |
|
|
| func (h *HMAC) Sum(in []byte) []byte { |
| |
| |
| |
| if h.keyLen < 112/8 && !h.forHKDF { |
| fips140.RecordNonApproved() |
| } |
| switch h.inner.(type) { |
| case *sha256.Digest, *sha512.Digest, *sha3.Digest: |
| default: |
| fips140.RecordNonApproved() |
| } |
|
|
| origLen := len(in) |
| in = h.inner.Sum(in) |
|
|
| if h.marshaled { |
| if err := h.outer.(marshalable).UnmarshalBinary(h.opad); err != nil { |
| panic(err) |
| } |
| } else { |
| h.outer.Reset() |
| h.outer.Write(h.opad) |
| } |
| h.outer.Write(in[origLen:]) |
| return h.outer.Sum(in[:origLen]) |
| } |
|
|
| func (h *HMAC) Write(p []byte) (n int, err error) { |
| return h.inner.Write(p) |
| } |
|
|
| func (h *HMAC) Size() int { return h.outer.Size() } |
| func (h *HMAC) BlockSize() int { return h.inner.BlockSize() } |
|
|
| func (h *HMAC) Reset() { |
| if h.marshaled { |
| if err := h.inner.(marshalable).UnmarshalBinary(h.ipad); err != nil { |
| panic(err) |
| } |
| return |
| } |
|
|
| h.inner.Reset() |
| h.inner.Write(h.ipad) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| marshalableInner, innerOK := h.inner.(marshalable) |
| if !innerOK { |
| return |
| } |
| marshalableOuter, outerOK := h.outer.(marshalable) |
| if !outerOK { |
| return |
| } |
|
|
| imarshal, err := marshalableInner.MarshalBinary() |
| if err != nil { |
| return |
| } |
|
|
| h.outer.Reset() |
| h.outer.Write(h.opad) |
| omarshal, err := marshalableOuter.MarshalBinary() |
| if err != nil { |
| return |
| } |
|
|
| |
| h.ipad = imarshal |
| h.opad = omarshal |
| h.marshaled = true |
| } |
|
|
| type errCloneUnsupported struct{} |
|
|
| func (e errCloneUnsupported) Error() string { |
| return "crypto/hmac: hash does not support hash.Cloner" |
| } |
|
|
| func (e errCloneUnsupported) Unwrap() error { |
| return errors.ErrUnsupported |
| } |
|
|
| |
| |
| func (h *HMAC) Clone() (hash.Cloner, error) { |
| r := *h |
| ic, ok := h.inner.(hash.Cloner) |
| if !ok { |
| return nil, errCloneUnsupported{} |
| } |
| oc, ok := h.outer.(hash.Cloner) |
| if !ok { |
| return nil, errCloneUnsupported{} |
| } |
| var err error |
| r.inner, err = ic.Clone() |
| if err != nil { |
| return nil, errCloneUnsupported{} |
| } |
| r.outer, err = oc.Clone() |
| if err != nil { |
| return nil, errCloneUnsupported{} |
| } |
| return &r, nil |
| } |
|
|
| |
| func New[H hash.Hash](h func() H, key []byte) *HMAC { |
| hm := &HMAC{keyLen: len(key)} |
| hm.outer = h() |
| hm.inner = h() |
| unique := true |
| func() { |
| defer func() { |
| |
| _ = recover() |
| }() |
| if hm.outer == hm.inner { |
| unique = false |
| } |
| }() |
| if !unique { |
| panic("crypto/hmac: hash generation function does not produce unique values") |
| } |
| blocksize := hm.inner.BlockSize() |
| hm.ipad = make([]byte, blocksize) |
| hm.opad = make([]byte, blocksize) |
| if len(key) > blocksize { |
| |
| hm.outer.Write(key) |
| key = hm.outer.Sum(nil) |
| } |
| 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) |
|
|
| return hm |
| } |
|
|
| |
| func MarkAsUsedInKDF(h *HMAC) { |
| h.forHKDF = true |
| } |
|
|