As part of #65269 (and for #69536), I propose we migrate golang.org/x/crypto/sha3 to the standard library, with the following API.
package sha3
func Sum224(data []byte) [28]byte
func Sum256(data []byte) [32]byte
func Sum384(data []byte) [48]byte
func Sum512(data []byte) [64]byte
func SumSHAKE128(data []byte, length int) []byte
func SumSHAKE256(data []byte, length int) []byte
type SHA3 struct{}
func New224() *SHA3
func New256() *SHA3
func New384() *SHA3
func New512() *SHA3
func (*SHA3) Write(p []byte) (n int, err error)
func (*SHA3) Sum(b []byte) []byte
func (*SHA3) Reset()
func (*SHA3) Size() int
func (*SHA3) BlockSize() int
func (*SHA3) MarshalBinary() ([]byte, error)
func (*SHA3) AppendBinary([]byte) ([]byte, error)
func (*SHA3) UnmarshalBinary(data []byte) error
type SHAKE struct {}
func (*SHAKE) Write(p []byte) (n int, err error)
func (*SHAKE) Read(p []byte) (n int, err error)
func (*SHAKE) Reset()
func (*SHAKE) BlockSize() int
func (*SHAKE) MarshalBinary() ([]byte, error)
func (*SHAKE) AppendBinary([]byte) ([]byte, error)
func (*SHAKE) UnmarshalBinary(data []byte) error
func NewCSHAKE128(N, S []byte) *SHAKE
func NewCSHAKE256(N, S []byte) *SHAKE
func NewSHAKE128() *SHAKE
func NewSHAKE256() *SHAKE
/cc @golang/security @cpu
Changes from golang.org/x/crypto/sha3
- Return concrete types
*SHA3 and *SHAKE instead of hash.Hash and ShakeHash
- This lets us add new methods in the future, but note that it makes it harder to use New functions with crypto/hmac, because they don't have type
func() hash.Hash without wrapping. Feels worth it to me, and maybe we can make a v2 one day with generics, but if anyone has ideas to smooth this over let me know.
- Expose MarshalBinary, AppendBinary, and UnmarshalBinary
- Dropped Sum and Size from SHAKE
- These were added recently by @mdempsky without a proposal in CL 526937, I see the value in making SHAKE usable as a hash, especially since the actual hashes NIST specified are so needlessly slow, but I found it confusing that SHAKE-128's Size is 256 bits, which doesn't come from any specs. We can add these back in a separate proposal.
- Replaced
ShakeSum/n/(hash, data []byte) with SumSHAKE/n/(data []byte, length int) []byte
- I found the old one confusing with its two
[]byte arguments. The new one matches more closely Sum/n/ and can be made zero-allocations in most cases by outlining a fixed-size allocation of a reasonable length. We can also make the default recommendation "Just use SHAKE128 with a length of 32" and this would be the easiest to use API for that.
- Dropped
Clone() ShakeHash
- Dropped NewLegacyKeccak256 and NewLegacyKeccak512
- We can use a linkname for now to make x/crypto/sha3 a full wrapper of crypto/sha3, and can add them back to the public API if they are still necessary.
- Renamed
Shake to SHAKE to match standard spelling
For reference, here is the current x/crypto/sha3 API.
func New224() hash.Hash
func New256() hash.Hash
func New384() hash.Hash
func New512() hash.Hash
func NewLegacyKeccak256() hash.Hash
func NewLegacyKeccak512() hash.Hash
func ShakeSum128(hash, data []byte)
func ShakeSum256(hash, data []byte)
func Sum224(data []byte) (digest [28]byte)
func Sum256(data []byte) (digest [32]byte)
func Sum384(data []byte) (digest [48]byte)
func Sum512(data []byte) (digest [64]byte)
type ShakeHash interface {
hash.Hash
io.Reader
Clone() ShakeHash
}
func NewCShake128(N, S []byte) ShakeHash
func NewCShake256(N, S []byte) ShakeHash
func NewShake128() ShakeHash
func NewShake256() ShakeHash
As part of #65269 (and for #69536), I propose we migrate golang.org/x/crypto/sha3 to the standard library, with the following API.
/cc @golang/security @cpu
Changes from golang.org/x/crypto/sha3
*SHA3and*SHAKEinstead ofhash.HashandShakeHashfunc() hash.Hashwithout wrapping. Feels worth it to me, and maybe we can make a v2 one day with generics, but if anyone has ideas to smooth this over let me know.ShakeSum/n/(hash, data []byte)withSumSHAKE/n/(data []byte, length int) []byte[]bytearguments. The new one matches more closelySum/n/and can be made zero-allocations in most cases by outlining a fixed-size allocation of a reasonable length. We can also make the default recommendation "Just use SHAKE128 with a length of 32" and this would be the easiest to use API for that.Clone() ShakeHashShaketoSHAKEto match standard spellingFor reference, here is the current x/crypto/sha3 API.