Skip to content

Commit 60f6d2f

Browse files
FiloSottilegopherbot
authored andcommitted
crypto/internal/fips140/entropy: support SHA-384 sizes for ACVP tests
Change-Id: I6a6a6964decc662d753ee3eec357570bd3c95e2d Reviewed-on: https://go-review.googlesource.com/c/go/+/710056 Auto-Submit: Filippo Valsorda <filippo@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Roland Shoemaker <roland@golang.org> Reviewed-by: Nooras Saba‎ <noorassaba@google.com> Reviewed-by: Daniel McCarney <daniel@binaryparadox.net>
1 parent 6fd8e88 commit 60f6d2f

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

src/crypto/internal/fips140/entropy/sha384.go

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ import "math/bits"
88

99
// This file includes a SHA-384 implementation to insulate the entropy source
1010
// from any changes in the FIPS 140-3 module's crypto/internal/fips140/sha512
11-
// package. We only support 1024-byte inputs.
11+
// package. We support 1024-byte inputs for the entropy source, and arbitrary
12+
// length inputs for ACVP testing.
13+
14+
var initState = [8]uint64{
15+
0xcbbb9d5dc1059ed8,
16+
0x629a292a367cd507,
17+
0x9159015a3070dd17,
18+
0x152fecd8f70e5939,
19+
0x67332667ffc00b31,
20+
0x8eb44a8768581511,
21+
0xdb0c2e0d64f98fa7,
22+
0x47b5481dbefa4fa4,
23+
}
1224

1325
func SHA384(p *[1024]byte) [48]byte {
14-
h := [8]uint64{
15-
0xcbbb9d5dc1059ed8,
16-
0x629a292a367cd507,
17-
0x9159015a3070dd17,
18-
0x152fecd8f70e5939,
19-
0x67332667ffc00b31,
20-
0x8eb44a8768581511,
21-
0xdb0c2e0d64f98fa7,
22-
0x47b5481dbefa4fa4,
23-
}
26+
h := initState
2427

2528
sha384Block(&h, (*[128]byte)(p[0:128]))
2629
sha384Block(&h, (*[128]byte)(p[128:256]))
@@ -36,6 +39,38 @@ func SHA384(p *[1024]byte) [48]byte {
3639
bePutUint64(padlen[112+8:], 1024*8)
3740
sha384Block(&h, &padlen)
3841

42+
return digestBytes(&h)
43+
}
44+
45+
func TestingOnlySHA384(p []byte) [48]byte {
46+
if len(p) == 1024 {
47+
return SHA384((*[1024]byte)(p))
48+
}
49+
50+
h := initState
51+
bitLen := uint64(len(p)) * 8
52+
53+
// Process full 128-byte blocks.
54+
for len(p) >= 128 {
55+
sha384Block(&h, (*[128]byte)(p[:128]))
56+
p = p[128:]
57+
}
58+
59+
// Process final block and padding.
60+
var finalBlock [128]byte
61+
copy(finalBlock[:], p)
62+
finalBlock[len(p)] = 0x80
63+
if len(p) >= 112 {
64+
sha384Block(&h, &finalBlock)
65+
finalBlock = [128]byte{}
66+
}
67+
bePutUint64(finalBlock[112+8:], bitLen)
68+
sha384Block(&h, &finalBlock)
69+
70+
return digestBytes(&h)
71+
}
72+
73+
func digestBytes(h *[8]uint64) [48]byte {
3974
var digest [48]byte
4075
bePutUint64(digest[0:], h[0])
4176
bePutUint64(digest[8:], h[1])

src/crypto/internal/fips140test/entropy_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"crypto/internal/cryptotest"
1212
"crypto/internal/fips140/drbg"
1313
"crypto/internal/fips140/entropy"
14+
"crypto/rand"
1415
"crypto/sha256"
1516
"crypto/sha512"
1617
"encoding/hex"
@@ -159,6 +160,16 @@ func TestEntropySHA384(t *testing.T) {
159160
if got != want {
160161
t.Errorf("SHA384() = %x, want %x", got, want)
161162
}
163+
164+
for l := range 1024*3 + 1 {
165+
input := make([]byte, l)
166+
rand.Read(input)
167+
want := sha512.Sum384(input)
168+
got := entropy.TestingOnlySHA384(input)
169+
if got != want {
170+
t.Errorf("TestingOnlySHA384(%d bytes) = %x, want %x", l, got, want)
171+
}
172+
}
162173
}
163174

164175
func TestEntropyRepetitionCountTest(t *testing.T) {
@@ -230,7 +241,7 @@ func TestEntropyUnchanged(t *testing.T) {
230241
// entropy source through the Entropy Source Validation program,
231242
// independently of the FIPS 140-3 module. It must not change even across
232243
// FIPS 140-3 module versions, in order to reuse the ESV certificate.
233-
exp := "35976eb8a11678c79777da07aaab5511d4325701f837777df205f6e7b20c6821"
244+
exp := "1b68d4c091ef66c6006602e4ed3ac10f8a82ad193708ec99d63b145e3baa3e6c"
234245
if got := hex.EncodeToString(h.Sum(nil)); got != exp {
235246
t.Errorf("hash of crypto/internal/fips140/entropy = %s, want %s", got, exp)
236247
}

0 commit comments

Comments
 (0)