hash.mx raw

   1  // Copyright 2024 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  package fips140hash
   6  
   7  import (
   8  	fsha3 "crypto/internal/fips140/sha3"
   9  	"crypto/sha3"
  10  	"hash"
  11  	_ "unsafe"
  12  )
  13  
  14  //go:linkname sha3Unwrap
  15  func sha3Unwrap(*sha3.SHA3) *fsha3.Digest
  16  
  17  // Unwrap returns h, or a crypto/internal/fips140 inner implementation of h.
  18  //
  19  // The return value can be type asserted to one of
  20  // [crypto/internal/fips140/sha256.Digest],
  21  // [crypto/internal/fips140/sha512.Digest], or
  22  // [crypto/internal/fips140/sha3.Digest] if it is a FIPS 140-3 approved hash.
  23  func Unwrap(h hash.Hash) hash.Hash {
  24  	if sha3, ok := h.(*sha3.SHA3); ok {
  25  		return sha3Unwrap(sha3)
  26  	}
  27  	return h
  28  }
  29  
  30  // UnwrapNew returns a function that calls newHash and applies [Unwrap] to the
  31  // return value.
  32  func UnwrapNew[Hash hash.Hash](newHash func() Hash) func() hash.Hash {
  33  	return func() hash.Hash { return Unwrap(newHash()) }
  34  }
  35