Skip to content

Commit 312e7e9

Browse files
FiloSottilegopherbot
authored andcommitted
crypto/internal/fips/sha3: restructure as an internal package
Main changes are - return concrete *Digest and *SHAKE instead of interfaces - make tests external (sha3_test) so they will be easy to move to the public package - drop most of the developer guidance docs (to be updated and reintroduced in the public package) - consolidate the _noasm.go files (matching the single _s390x.go) - move TestAllocations from build tags to testenv - temporarily disable s390x code, to refactor in a following CL For #69536 Change-Id: Ie5fd3e2b589b9eb835b9e3174b7a79c2ac728ab1 Reviewed-on: https://go-review.googlesource.com/c/go/+/617357 Reviewed-by: Roland Shoemaker <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Carlos Amedee <[email protected]> Auto-Submit: Filippo Valsorda <[email protected]> Reviewed-by: Daniel McCarney <[email protected]>
1 parent 7557a24 commit 312e7e9

File tree

12 files changed

+264
-392
lines changed

12 files changed

+264
-392
lines changed

src/crypto/internal/fips/hmac/hmac.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package hmac
1010
import (
1111
"crypto/internal/fips"
1212
"crypto/internal/fips/sha256"
13+
"crypto/internal/fips/sha3"
1314
"crypto/internal/fips/sha512"
1415
)
1516

@@ -158,8 +159,7 @@ func setServiceIndicator(h fips.Hash, key []byte) {
158159
}
159160

160161
switch h.(type) {
161-
case *sha256.Digest, *sha512.Digest:
162-
// TODO(fips): SHA-3
162+
case *sha256.Digest, *sha512.Digest, *sha3.Digest:
163163
default:
164164
return
165165
}

src/crypto/internal/fips/sha3/allocations_test.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/crypto/internal/fips/sha3/doc.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/crypto/internal/fips/sha3/hashes.go

Lines changed: 24 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,23 @@
44

55
package sha3
66

7-
// This file provides functions for creating instances of the SHA-3
8-
// and SHAKE hash functions, as well as utility functions for hashing
9-
// bytes.
10-
11-
import "crypto/internal/fips"
12-
13-
// New224 creates a new SHA3-224 hash.
14-
// Its generic security strength is 224 bits against preimage attacks,
15-
// and 112 bits against collision attacks.
16-
func New224() fips.Hash {
7+
// New224 returns a new Digest computing the SHA3-224 hash.
8+
func New224() *Digest {
179
return new224()
1810
}
1911

20-
// New256 creates a new SHA3-256 hash.
21-
// Its generic security strength is 256 bits against preimage attacks,
22-
// and 128 bits against collision attacks.
23-
func New256() fips.Hash {
12+
// New256 returns a new Digest computing the SHA3-256 hash.
13+
func New256() *Digest {
2414
return new256()
2515
}
2616

27-
// New384 creates a new SHA3-384 hash.
28-
// Its generic security strength is 384 bits against preimage attacks,
29-
// and 192 bits against collision attacks.
30-
func New384() fips.Hash {
17+
// New384 returns a new Digest computing the SHA3-384 hash.
18+
func New384() *Digest {
3119
return new384()
3220
}
3321

34-
// New512 creates a new SHA3-512 hash.
35-
// Its generic security strength is 512 bits against preimage attacks,
36-
// and 256 bits against collision attacks.
37-
func New512() fips.Hash {
22+
// New512 returns a new Digest computing the SHA3-512 hash.
23+
func New512() *Digest {
3824
return new512()
3925
}
4026

@@ -60,66 +46,30 @@ const (
6046
rateK1024 = (1600 - 1024) / 8
6147
)
6248

63-
func new224Generic() *state {
64-
return &state{rate: rateK448, outputLen: 28, dsbyte: dsbyteSHA3}
65-
}
66-
67-
func new256Generic() *state {
68-
return &state{rate: rateK512, outputLen: 32, dsbyte: dsbyteSHA3}
69-
}
70-
71-
func new384Generic() *state {
72-
return &state{rate: rateK768, outputLen: 48, dsbyte: dsbyteSHA3}
49+
func new224Generic() *Digest {
50+
return &Digest{rate: rateK448, outputLen: 28, dsbyte: dsbyteSHA3}
7351
}
7452

75-
func new512Generic() *state {
76-
return &state{rate: rateK1024, outputLen: 64, dsbyte: dsbyteSHA3}
77-
}
78-
79-
// NewLegacyKeccak256 creates a new Keccak-256 hash.
80-
//
81-
// Only use this function if you require compatibility with an existing cryptosystem
82-
// that uses non-standard padding. All other users should use New256 instead.
83-
func NewLegacyKeccak256() fips.Hash {
84-
return &state{rate: rateK512, outputLen: 32, dsbyte: dsbyteKeccak}
85-
}
86-
87-
// NewLegacyKeccak512 creates a new Keccak-512 hash.
88-
//
89-
// Only use this function if you require compatibility with an existing cryptosystem
90-
// that uses non-standard padding. All other users should use New512 instead.
91-
func NewLegacyKeccak512() fips.Hash {
92-
return &state{rate: rateK1024, outputLen: 64, dsbyte: dsbyteKeccak}
53+
func new256Generic() *Digest {
54+
return &Digest{rate: rateK512, outputLen: 32, dsbyte: dsbyteSHA3}
9355
}
9456

95-
// Sum224 returns the SHA3-224 digest of the data.
96-
func Sum224(data []byte) (digest [28]byte) {
97-
h := New224()
98-
h.Write(data)
99-
h.Sum(digest[:0])
100-
return
57+
func new384Generic() *Digest {
58+
return &Digest{rate: rateK768, outputLen: 48, dsbyte: dsbyteSHA3}
10159
}
10260

103-
// Sum256 returns the SHA3-256 digest of the data.
104-
func Sum256(data []byte) (digest [32]byte) {
105-
h := New256()
106-
h.Write(data)
107-
h.Sum(digest[:0])
108-
return
61+
func new512Generic() *Digest {
62+
return &Digest{rate: rateK1024, outputLen: 64, dsbyte: dsbyteSHA3}
10963
}
11064

111-
// Sum384 returns the SHA3-384 digest of the data.
112-
func Sum384(data []byte) (digest [48]byte) {
113-
h := New384()
114-
h.Write(data)
115-
h.Sum(digest[:0])
116-
return
65+
// NewLegacyKeccak256 returns a new Digest computing the legacy, non-standard
66+
// Keccak-256 hash.
67+
func NewLegacyKeccak256() *Digest {
68+
return &Digest{rate: rateK512, outputLen: 32, dsbyte: dsbyteKeccak}
11769
}
11870

119-
// Sum512 returns the SHA3-512 digest of the data.
120-
func Sum512(data []byte) (digest [64]byte) {
121-
h := New512()
122-
h.Write(data)
123-
h.Sum(digest[:0])
124-
return
71+
// NewLegacyKeccak512 returns a new Digest computing the legacy, non-standard
72+
// Keccak-512 hash.
73+
func NewLegacyKeccak512() *Digest {
74+
return &Digest{rate: rateK1024, outputLen: 64, dsbyte: dsbyteKeccak}
12575
}

src/crypto/internal/fips/sha3/hashes_noasm.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)