Skip to content

Commit ff338aa

Browse files
committed
chore: replace rand.Reader with nil in keygen and signing calls
1 parent 57214b4 commit ff338aa

75 files changed

Lines changed: 3000 additions & 2967 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ca/ca_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ func TestNoteSignError(t *testing.T) {
935935

936936
func TestGenerateSKID(t *testing.T) {
937937
t.Parallel()
938-
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
938+
key, err := ecdsa.GenerateKey(elliptic.P256(), nil)
939939
test.AssertNotError(t, err, "Error generating key")
940940

941941
sha256skid, err := generateSKID(key.Public())
@@ -949,25 +949,25 @@ func TestVerifyTBSCertIsDeterministic(t *testing.T) {
949949
t.Parallel()
950950

951951
// Create first keypair and cert
952-
testKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
952+
testKey, err := ecdsa.GenerateKey(elliptic.P256(), nil)
953953
test.AssertNotError(t, err, "unable to generate ECDSA private key")
954954
template := &x509.Certificate{
955955
NotAfter: time.Now().Add(1 * time.Hour),
956956
DNSNames: []string{"example.com"},
957957
SerialNumber: big.NewInt(1),
958958
}
959-
certDer1, err := x509.CreateCertificate(rand.Reader, template, template, &testKey.PublicKey, testKey)
959+
certDer1, err := x509.CreateCertificate(nil, template, template, &testKey.PublicKey, testKey)
960960
test.AssertNotError(t, err, "unable to create certificate")
961961

962962
// Create second keypair and cert
963-
testKey2, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
963+
testKey2, err := ecdsa.GenerateKey(elliptic.P256(), nil)
964964
test.AssertNotError(t, err, "unable to generate ECDSA private key")
965965
template2 := &x509.Certificate{
966966
NotAfter: time.Now().Add(2 * time.Hour),
967967
DNSNames: []string{"example.net"},
968968
SerialNumber: big.NewInt(2),
969969
}
970-
certDer2, err := x509.CreateCertificate(rand.Reader, template2, template2, &testKey2.PublicKey, testKey2)
970+
certDer2, err := x509.CreateCertificate(nil, template2, template2, &testKey2.PublicKey, testKey2)
971971
test.AssertNotError(t, err, "unable to create certificate")
972972

973973
testCases := []struct {

ca/testdata/testcsr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func main() {
16-
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
16+
priv, err := ecdsa.GenerateKey(elliptic.P256(), nil)
1717
if err != nil {
1818
log.Fatalf("Failed to parse private key: %s", err)
1919
}
@@ -29,7 +29,7 @@ func main() {
2929
"Capitalizedletters.COM",
3030
},
3131
}
32-
csr, err := x509.CreateCertificateRequest(rand.Reader, req, priv)
32+
csr, err := x509.CreateCertificateRequest(nil, req, priv)
3333
if err != nil {
3434
log.Fatalf("unable to create CSR: %s", err)
3535
}

cmd/admin/cert_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestSerialsFromPrivateKey(t *testing.T) {
105105
fc := clock.NewFake()
106106
fc.Set(time.Now())
107107

108-
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
108+
privKey, err := ecdsa.GenerateKey(elliptic.P256(), nil)
109109
test.AssertNotError(t, err, "creating test private key")
110110
keyBytes, err := x509.MarshalPKCS8PrivateKey(privKey)
111111
test.AssertNotError(t, err, "marshalling test private key bytes")

cmd/admin/key_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ import (
3232

3333
func TestSPKIHashesFromPrivateKeys(t *testing.T) {
3434

35-
ecdsaKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
35+
ecdsaKey, err := ecdsa.GenerateKey(elliptic.P256(), nil)
3636
test.AssertNotError(t, err, "Generating ECDSA key")
3737
pkcs8ecdsa, err := x509.MarshalPKCS8PrivateKey(ecdsaKey)
3838
test.AssertNotError(t, err, "Marshalling PKCS8 private key")
3939

40-
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
40+
rsaKey, err := rsa.GenerateKey(nil, 2048)
4141
test.AssertNotError(t, err, "Generating RSA key")
4242
pkcs8rsa, err := x509.MarshalPKCS8PrivateKey(rsaKey)
4343
test.AssertNotError(t, err, "Marshalling PKCS8 private key")
@@ -166,7 +166,7 @@ func TestBlockSPKIHash(t *testing.T) {
166166
log := blog.NewMock()
167167
msa := mockSARecordingBlocks{}
168168

169-
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
169+
privKey, err := ecdsa.GenerateKey(elliptic.P256(), nil)
170170
test.AssertNotError(t, err, "creating test private key")
171171
keyHash, err := core.KeyDigest(privKey.Public())
172172
test.AssertNotError(t, err, "computing test SPKI hash")

cmd/ceremony/cert_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ func TestGenerateCSR(t *testing.T) {
549549
Country: "country",
550550
}
551551

552-
signer, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
552+
signer, err := ecdsa.GenerateKey(elliptic.P256(), nil)
553553
test.AssertNotError(t, err, "failed to generate test key")
554554

555555
csrBytes, err := generateCSR(profile, &wrappedSigner{signer})

cmd/ceremony/crl_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ func TestGenerateCRLTimeBounds(t *testing.T) {
4848
// we need to wrap it as we pass a purposefully broken io.Reader to Sign in order
4949
// to verify that go isn't using it as a source of randomness (we expect this
5050
// randomness to come from the HSM). If we directly call Sign on the crypto.Signer
51-
// it would fail, so we wrap it so that we can use a shim rand.Reader in the Sign
51+
// it would fail, so we wrap it so that we can use a shim nil in the Sign
5252
// call.
5353
type wrappedSigner struct{ k crypto.Signer }
5454

5555
func (p wrappedSigner) Sign(_ io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
56-
return p.k.Sign(rand.Reader, digest, opts)
56+
return p.k.Sign(nil, digest, opts)
5757
}
5858

5959
func (p wrappedSigner) Public() crypto.PublicKey {
6060
return p.k.Public()
6161
}
6262

6363
func TestGenerateCRLLints(t *testing.T) {
64-
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
64+
k, err := ecdsa.GenerateKey(elliptic.P256(), nil)
6565
test.AssertNotError(t, err, "failed to generate test key")
6666

6767
cert := &x509.Certificate{
@@ -74,7 +74,7 @@ func TestGenerateCRLLints(t *testing.T) {
7474
SubjectKeyId: []byte{1, 2, 3},
7575
}
7676

77-
certBytes, err := x509.CreateCertificate(rand.Reader, cert, cert, k.Public(), k)
77+
certBytes, err := x509.CreateCertificate(nil, cert, cert, k.Public(), k)
7878
test.AssertNotError(t, err, "failed to generate test cert")
7979
cert, err = x509.ParseCertificate(certBytes)
8080
test.AssertNotError(t, err, "failed to parse test cert")
@@ -102,7 +102,7 @@ func TestGenerateCRLLints(t *testing.T) {
102102
}
103103

104104
func TestGenerateCRL(t *testing.T) {
105-
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
105+
k, err := ecdsa.GenerateKey(elliptic.P256(), nil)
106106
test.AssertNotError(t, err, "failed to generate test key")
107107

108108
template := &x509.Certificate{
@@ -116,7 +116,7 @@ func TestGenerateCRL(t *testing.T) {
116116
SubjectKeyId: []byte{1, 2, 3},
117117
}
118118

119-
certBytes, err := x509.CreateCertificate(rand.Reader, template, template, k.Public(), k)
119+
certBytes, err := x509.CreateCertificate(nil, template, template, k.Public(), k)
120120
test.AssertNotError(t, err, "failed to generate test cert")
121121
cert, err := x509.ParseCertificate(certBytes)
122122
test.AssertNotError(t, err, "failed to parse test cert")

cmd/ceremony/ecdsa_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestECGenerate(t *testing.T) {
4040
ctx.GenerateRandomFunc = func(pkcs11.SessionHandle, int) ([]byte, error) {
4141
return []byte{1, 2, 3}, nil
4242
}
43-
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
43+
priv, err := ecdsa.GenerateKey(elliptic.P256(), nil)
4444
test.AssertNotError(t, err, "Failed to generate a ECDSA test key")
4545

4646
// Test ecGenerate fails with unknown curve
@@ -92,7 +92,7 @@ func TestECGenerate(t *testing.T) {
9292
}
9393

9494
func ecPKCS11Sign(priv *ecdsa.PrivateKey, msg []byte) ([]byte, error) {
95-
r, s, err := ecdsa.Sign(rand.Reader, priv, msg[:])
95+
r, s, err := ecdsa.Sign(nil, priv, msg[:])
9696
if err != nil {
9797
return nil, err
9898
}

cmd/ceremony/key_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestGenerateKeyRSA(t *testing.T) {
4646
tmp := t.TempDir()
4747

4848
ctx := setupCtx()
49-
rsaPriv, err := rsa.GenerateKey(rand.Reader, 1024)
49+
rsaPriv, err := rsa.GenerateKey(nil, 1024)
5050
test.AssertNotError(t, err, "Failed to generate a test RSA key")
5151
ctx.GetAttributeValueFunc = func(pkcs11.SessionHandle, pkcs11.ObjectHandle, []*pkcs11.Attribute) ([]*pkcs11.Attribute, error) {
5252
return []*pkcs11.Attribute{
@@ -56,7 +56,7 @@ func TestGenerateKeyRSA(t *testing.T) {
5656
}
5757
ctx.SignFunc = func(_ pkcs11.SessionHandle, msg []byte) ([]byte, error) {
5858
// Chop of the hash identifier and feed back into rsa.SignPKCS1v15
59-
return rsa.SignPKCS1v15(rand.Reader, rsaPriv, crypto.SHA256, msg[19:])
59+
return rsa.SignPKCS1v15(nil, rsaPriv, crypto.SHA256, msg[19:])
6060
}
6161
s := &pkcs11helpers.Session{Module: &ctx, Session: 0}
6262
keyPath := path.Join(tmp, "test-rsa-key.pem")
@@ -74,7 +74,7 @@ func TestGenerateKeyRSA(t *testing.T) {
7474
}
7575

7676
func setECGenerateFuncs(ctx *pkcs11helpers.MockCtx) {
77-
ecPriv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
77+
ecPriv, err := ecdsa.GenerateKey(elliptic.P256(), nil)
7878
if err != nil {
7979
panic(err)
8080
}

cmd/ceremony/main_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
func TestLoadPubKey(t *testing.T) {
2424
tmp := t.TempDir()
25-
key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
25+
key, _ := ecdsa.GenerateKey(elliptic.P256(), nil)
2626

2727
_, _, err := loadPubKey(path.Join(tmp, "does", "not", "exist"))
2828
test.AssertError(t, err, "should fail on non-existent file")
@@ -1295,14 +1295,14 @@ func TestPostIssuanceLinting(t *testing.T) {
12951295
err := postIssuanceLinting(nil, nil)
12961296
test.AssertError(t, err, "should have failed because no certificate was provided")
12971297

1298-
testKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
1298+
testKey, err := ecdsa.GenerateKey(elliptic.P256(), nil)
12991299
test.AssertNotError(t, err, "unable to generate ECDSA private key")
13001300
template := &x509.Certificate{
13011301
NotAfter: clk.Now().Add(1 * time.Hour),
13021302
DNSNames: []string{"example.com"},
13031303
SerialNumber: big.NewInt(1),
13041304
}
1305-
certDer, err := x509.CreateCertificate(rand.Reader, template, template, &testKey.PublicKey, testKey)
1305+
certDer, err := x509.CreateCertificate(nil, template, template, &testKey.PublicKey, testKey)
13061306
test.AssertNotError(t, err, "unable to create certificate")
13071307
parsedCert, err := x509.ParseCertificate(certDer)
13081308
test.AssertNotError(t, err, "unable to parse DER bytes")

cmd/ceremony/rsa_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestRSAGenerate(t *testing.T) {
4444
return []byte{1, 2, 3}, nil
4545
}
4646

47-
priv, err := rsa.GenerateKey(rand.Reader, 1024)
47+
priv, err := rsa.GenerateKey(nil, 1024)
4848
test.AssertNotError(t, err, "Failed to generate a RSA test key")
4949

5050
// Test rsaGenerate fails when GenerateKeyPair fails
@@ -86,7 +86,7 @@ func TestRSAGenerate(t *testing.T) {
8686
}
8787
ctx.SignFunc = func(_ pkcs11.SessionHandle, msg []byte) ([]byte, error) {
8888
// Chop of the hash identifier and feed back into rsa.SignPKCS1v15
89-
return rsa.SignPKCS1v15(rand.Reader, priv, crypto.SHA256, msg[19:])
89+
return rsa.SignPKCS1v15(nil, priv, crypto.SHA256, msg[19:])
9090
}
9191
_, _, err = rsaGenerate(s, "", 1024)
9292
test.AssertNotError(t, err, "rsaGenerate didn't succeed when everything worked as expected")

0 commit comments

Comments
 (0)