| |
| |
| |
|
|
| |
| |
| package sha3 |
|
|
| import ( |
| "crypto" |
| "crypto/internal/fips140/sha3" |
| "hash" |
| _ "unsafe" |
| ) |
|
|
| func init() { |
| crypto.RegisterHash(crypto.SHA3_224, func() hash.Hash { return New224() }) |
| crypto.RegisterHash(crypto.SHA3_256, func() hash.Hash { return New256() }) |
| crypto.RegisterHash(crypto.SHA3_384, func() hash.Hash { return New384() }) |
| crypto.RegisterHash(crypto.SHA3_512, func() hash.Hash { return New512() }) |
| } |
|
|
| |
| func Sum224(data []byte) [28]byte { |
| var out [28]byte |
| h := sha3.New224() |
| h.Write(data) |
| h.Sum(out[:0]) |
| return out |
| } |
|
|
| |
| func Sum256(data []byte) [32]byte { |
| var out [32]byte |
| h := sha3.New256() |
| h.Write(data) |
| h.Sum(out[:0]) |
| return out |
| } |
|
|
| |
| func Sum384(data []byte) [48]byte { |
| var out [48]byte |
| h := sha3.New384() |
| h.Write(data) |
| h.Sum(out[:0]) |
| return out |
| } |
|
|
| |
| func Sum512(data []byte) [64]byte { |
| var out [64]byte |
| h := sha3.New512() |
| h.Write(data) |
| h.Sum(out[:0]) |
| return out |
| } |
|
|
| |
| |
| func SumSHAKE128(data []byte, length int) []byte { |
| |
| out := make([]byte, 32) |
| return sumSHAKE128(out, data, length) |
| } |
|
|
| func sumSHAKE128(out, data []byte, length int) []byte { |
| if len(out) < length { |
| out = make([]byte, length) |
| } else { |
| out = out[:length] |
| } |
| h := sha3.NewShake128() |
| h.Write(data) |
| h.Read(out) |
| return out |
| } |
|
|
| |
| |
| func SumSHAKE256(data []byte, length int) []byte { |
| |
| out := make([]byte, 64) |
| return sumSHAKE256(out, data, length) |
| } |
|
|
| func sumSHAKE256(out, data []byte, length int) []byte { |
| if len(out) < length { |
| out = make([]byte, length) |
| } else { |
| out = out[:length] |
| } |
| h := sha3.NewShake256() |
| h.Write(data) |
| h.Read(out) |
| return out |
| } |
|
|
| |
| |
| type SHA3 struct { |
| s sha3.Digest |
| } |
|
|
| |
| func fips140hash_sha3Unwrap(sha3 *SHA3) *sha3.Digest { |
| return &sha3.s |
| } |
|
|
| |
| func New224() *SHA3 { |
| return &SHA3{*sha3.New224()} |
| } |
|
|
| |
| func New256() *SHA3 { |
| return &SHA3{*sha3.New256()} |
| } |
|
|
| |
| func New384() *SHA3 { |
| return &SHA3{*sha3.New384()} |
| } |
|
|
| |
| func New512() *SHA3 { |
| return &SHA3{*sha3.New512()} |
| } |
|
|
| func (s *SHA3) init() { |
| if s.s.Size() == 0 { |
| *s = *New256() |
| } |
| } |
|
|
| |
| func (s *SHA3) Write(p []byte) (n int, err error) { |
| s.init() |
| return s.s.Write(p) |
| } |
|
|
| |
| func (s *SHA3) Sum(b []byte) []byte { |
| s.init() |
| return s.s.Sum(b) |
| } |
|
|
| |
| func (s *SHA3) Reset() { |
| s.init() |
| s.s.Reset() |
| } |
|
|
| |
| func (s *SHA3) Size() int { |
| s.init() |
| return s.s.Size() |
| } |
|
|
| |
| func (s *SHA3) BlockSize() int { |
| s.init() |
| return s.s.BlockSize() |
| } |
|
|
| |
| func (s *SHA3) MarshalBinary() ([]byte, error) { |
| s.init() |
| return s.s.MarshalBinary() |
| } |
|
|
| |
| func (s *SHA3) AppendBinary(p []byte) ([]byte, error) { |
| s.init() |
| return s.s.AppendBinary(p) |
| } |
|
|
| |
| func (s *SHA3) UnmarshalBinary(data []byte) error { |
| s.init() |
| return s.s.UnmarshalBinary(data) |
| } |
|
|
| |
| func (d *SHA3) Clone() (hash.Cloner, error) { |
| r := *d |
| return &r, nil |
| } |
|
|
| |
| |
| type SHAKE struct { |
| s sha3.SHAKE |
| } |
|
|
| func (s *SHAKE) init() { |
| if s.s.Size() == 0 { |
| *s = *NewSHAKE256() |
| } |
| } |
|
|
| |
| func NewSHAKE128() *SHAKE { |
| return &SHAKE{*sha3.NewShake128()} |
| } |
|
|
| |
| func NewSHAKE256() *SHAKE { |
| return &SHAKE{*sha3.NewShake256()} |
| } |
|
|
| |
| |
| |
| |
| |
| func NewCSHAKE128(N, S []byte) *SHAKE { |
| return &SHAKE{*sha3.NewCShake128(N, S)} |
| } |
|
|
| |
| |
| |
| |
| |
| func NewCSHAKE256(N, S []byte) *SHAKE { |
| return &SHAKE{*sha3.NewCShake256(N, S)} |
| } |
|
|
| |
| |
| |
| func (s *SHAKE) Write(p []byte) (n int, err error) { |
| s.init() |
| return s.s.Write(p) |
| } |
|
|
| |
| |
| |
| func (s *SHAKE) Read(p []byte) (n int, err error) { |
| s.init() |
| return s.s.Read(p) |
| } |
|
|
| |
| func (s *SHAKE) Reset() { |
| s.init() |
| s.s.Reset() |
| } |
|
|
| |
| func (s *SHAKE) BlockSize() int { |
| s.init() |
| return s.s.BlockSize() |
| } |
|
|
| |
| func (s *SHAKE) MarshalBinary() ([]byte, error) { |
| s.init() |
| return s.s.MarshalBinary() |
| } |
|
|
| |
| func (s *SHAKE) AppendBinary(p []byte) ([]byte, error) { |
| s.init() |
| return s.s.AppendBinary(p) |
| } |
|
|
| |
| func (s *SHAKE) UnmarshalBinary(data []byte) error { |
| s.init() |
| return s.s.UnmarshalBinary(data) |
| } |
|
|