Skip to content

Commit

Permalink
test: optimise key generation for tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Vasek <[email protected]>
  • Loading branch information
matejvasek committed Oct 2, 2023
1 parent 62ae60d commit 0e089aa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pkg/ssh/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"encoding/binary"
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -107,11 +107,11 @@ func prepareSSHServer(t *testing.T, authorizedKeys ...any) (sshServer *SSHServer
authorizedKeys: authorizedKeys,
}

rsaKey, err := rsa.GenerateKey(rand.Reader, 4096)
rsaKey, err := rsa.GenerateKey(rand.New(rand.NewSource(time.Now().UnixNano())), 2048)
if err != nil {
t.Fatal(err)
}
ecdsaKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
ecdsaKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.New(rand.NewSource(time.Now().UnixNano())))
if err != nil {
t.Fatal(err)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/ssh/ssh_dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -49,7 +49,7 @@ type testParams struct {

func TestCreateDialer(t *testing.T) {

clientPrivKeyRSA, clientPrivKeyECDSA := generateKeys(t)
clientPrivKeyRSA, clientPrivKeyECDSA := generateClientKeys(t)

withoutSSHAgent(t)
withCleanHome(t)
Expand Down Expand Up @@ -504,7 +504,7 @@ func marshallKey(t *testing.T, key any, destPath, passphrase string) {
}

if passphrase != "" {
blk, err = x509.EncryptPEMBlock(rand.Reader, blk.Type, blk.Bytes, []byte(passphrase), x509.PEMCipherAES256)
blk, err = x509.EncryptPEMBlock(rand.New(rand.NewSource(time.Now().UnixNano())), blk.Type, blk.Bytes, []byte(passphrase), x509.PEMCipherAES256)

Check failure on line 507 in pkg/ssh/ssh_dialer_test.go

View workflow job for this annotation

GitHub Actions / Check Source (1.20.2, ubuntu-latest)

SA1019: x509.EncryptPEMBlock has been deprecated since Go 1.16 because it shouldn't be used: Legacy PEM encryption as specified in RFC 1423 is insecure by design. Since it does not authenticate the ciphertext, it is vulnerable to padding oracle attacks that can let an attacker recover the plaintext. (staticcheck)

Check failure on line 507 in pkg/ssh/ssh_dialer_test.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

SA1019: x509.EncryptPEMBlock has been deprecated since Go 1.16 because it shouldn't be used: Legacy PEM encryption as specified in RFC 1423 is insecure by design. Since it does not authenticate the ciphertext, it is vulnerable to padding oracle attacks that can let an attacker recover the plaintext. (staticcheck)
th.AssertNil(t, err)
}

Expand Down Expand Up @@ -847,7 +847,7 @@ func (a signerAgent) Sign(key ssh.PublicKey, data []byte) (*ssh.Signature, error
}
if signer.PublicKey().Type() == key.Type() &&
bytes.Equal(signer.PublicKey().Marshal(), key.Marshal()) {
return signer.Sign(rand.Reader, data)
return signer.Sign(rand.New(rand.NewSource(time.Now().UnixNano())), data)
}
}
return nil, errors.New("key not found")
Expand Down Expand Up @@ -993,15 +993,15 @@ func withRemoteDockerHost(host string, sshServer *SSHServer) setUpEnvFn {
}
}

func generateKeys(t *testing.T) (privKeyRSA *rsa.PrivateKey, privKeyECDSA *ecdsa.PrivateKey) {
func generateClientKeys(t *testing.T) (privKeyRSA *rsa.PrivateKey, privKeyECDSA *ecdsa.PrivateKey) {
var err error

privKeyRSA, err = rsa.GenerateKey(rand.Reader, 4096)
privKeyRSA, err = rsa.GenerateKey(rand.New(rand.NewSource(time.Now().UnixNano())), 2048)
if err != nil {
t.Fatal(err)
}

privKeyECDSA, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
privKeyECDSA, err = ecdsa.GenerateKey(elliptic.P384(), rand.New(rand.NewSource(time.Now().UnixNano())))
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 0e089aa

Please sign in to comment.