Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module go.step.sm/crypto

go 1.25.8
go 1.26

retract [v0.77.3, v0.77.7] // unintentional releases tagged from non-master branch

Expand Down
6 changes: 3 additions & 3 deletions internal/bcrypt_pbkdf/bcrypt_pbkdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Key(password, salt []byte, rounds, keyLen int) ([]byte, error) {
h.Reset()
h.Write(tmp[:])
bcryptHash(tmp[:], shapass[:], h.Sum(shasalt[:0]))
for j := 0; j < len(out); j++ {
for j := range len(out) {
out[j] ^= tmp[j]
}
}
Expand All @@ -81,13 +81,13 @@ func bcryptHash(out, shapass, shasalt []byte) {
if err != nil {
panic(err)
}
for i := 0; i < 64; i++ {
for range 64 {
blowfish.ExpandKey(shasalt, c)
blowfish.ExpandKey(shapass, c)
}
copy(out, magic)
for i := 0; i < 32; i += 8 {
for j := 0; j < 64; j++ {
for range 64 {
c.Encrypt(out[i:i+8], out[i:i+8])
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/bcrypt_pbkdf/bcrypt_pbkdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestBcryptHash(t *testing.T) {
}
var pass, salt [64]byte
var result [32]byte
for i := 0; i < 64; i++ {
for i := range 64 {
pass[i] = byte(i)
salt[i] = byte(i + 64)
}
Expand Down
2 changes: 1 addition & 1 deletion jose/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func Decrypt(data []byte, opts ...Option) ([]byte, error) {
// Try with a given password prompter.
if ctx.passwordPrompter != nil || PromptPassword != nil {
var pass []byte
for i := 0; i < MaxDecryptTries; i++ {
for range MaxDecryptTries {
switch {
case ctx.passwordPrompter != nil:
if pass, err = ctx.passwordPrompter(ctx.passwordPrompt); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion jose/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func TestDecrypt(t *testing.T) {
encryptedData := []byte(s)

// Create wrong encrypted data
m := make(map[string]interface{})
m := make(map[string]any)
if err := json.Unmarshal([]byte(jwe.FullSerialize()), &m); err != nil {
t.Fatal(err)
}
Expand Down
42 changes: 8 additions & 34 deletions jose/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
"crypto/x509"
"encoding/json"
"encoding/pem"
"io"
"math/big"
"os"
"reflect"
"testing"
"testing/cryptotest"

jose "github.com/go-jose/go-jose/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -91,7 +90,7 @@ func TestGenerateJWK(t *testing.T) {
size int
expectedAlg string
expectedSize int
expectedType interface{}
expectedType any
ok bool
}{
{"EC", "", "", "", "", 0, "ES256", 256, &ecdsa.PrivateKey{}, true},
Expand Down Expand Up @@ -224,7 +223,7 @@ func TestKeyUsageForCert(t *testing.T) {

func TestGenerateJWKFromPEM(t *testing.T) {
t.Parallel()
mustKey := func(filename string) interface{} {
mustKey := func(filename string) any {
key, err := pemutil.Read(filename)
require.NoError(t, err)
return key
Expand Down Expand Up @@ -391,30 +390,8 @@ func tempFile(t *testing.T) (*os.File, func()) {
}
}

type mockReader struct{}

func (mockReader) Read(buf []byte) (int, error) {
for i := range buf {
buf[i] = byte(i % 256)
}
return len(buf), nil
}

type eofReader struct{}

func (eofReader) Read(buf []byte) (int, error) {
return 0, io.EOF
}

func TestGenerateDefaultKeyPair(t *testing.T) {
rr := rand.Reader
t.Cleanup(func() {
rand.Reader = rr
jose.RandReader = rr
})

rand.Reader = mockReader{}
jose.RandReader = mockReader{}
cryptotest.SetGlobalRandom(t, 0)
jwk := mustGenerateJWK(t, "EC", "P-256", "ES256", "sig", "", 0)
jwe := mustEncryptJWK(t, jwk, []byte("planned password"))

Expand All @@ -424,9 +401,9 @@ func TestGenerateDefaultKeyPair(t *testing.T) {
}
jwkPub := jwk.Public()

cryptotest.SetGlobalRandom(t, 0)
type args struct {
passphrase []byte
randReader io.Reader
}
tests := []struct {
name string
Expand All @@ -436,15 +413,12 @@ func TestGenerateDefaultKeyPair(t *testing.T) {
want1Decrypted *JSONWebKey
wantErr bool
}{
{"ok", args{[]byte("planned password"), mockReader{}}, &jwkPub, jwe, jwk, false},
{"failEmptyPassword", args{[]byte(""), rr}, nil, nil, nil, true},
{"failNilPassword", args{nil, rr}, nil, nil, nil, true},
{"failEOF", args{[]byte("planned password"), eofReader{}}, nil, nil, nil, true},
{"ok", args{[]byte("planned password")}, &jwkPub, jwe, jwk, false},
{"failEmptyPassword", args{[]byte("")}, nil, nil, nil, true},
{"failNilPassword", args{nil}, nil, nil, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rand.Reader = tt.args.randReader
jose.RandReader = tt.args.randReader
got, got1, err := GenerateDefaultKeyPair(tt.args.passphrase)
if (err != nil) != tt.wantErr {
t.Errorf("GenerateDefaultKeyPair() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
4 changes: 2 additions & 2 deletions jose/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func ParseKeySet(b []byte, opts ...Option) (*JSONWebKey, error) {
}
}

func decodeCerts(l []interface{}) ([]*x509.Certificate, error) {
func decodeCerts(l []any) ([]*x509.Certificate, error) {
certs := make([]*x509.Certificate, len(l))
for i, j := range l {
certStr, ok := j.(string)
Expand Down Expand Up @@ -229,7 +229,7 @@ func GetX5cInsecureHeader(jwt *JSONWebToken) ([]*x509.Certificate, error) {
if !ok {
return nil, errors.New("ssh check-host token missing x5cInsecure header")
}
interfaces, ok := x5cVal.([]interface{})
interfaces, ok := x5cVal.([]any)
if !ok {
return nil, errors.Errorf("ssh check-host token x5cInsecure header has wrong type; expected []string, but got %T", x5cVal)
}
Expand Down
6 changes: 3 additions & 3 deletions jose/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestReadKeyPasswordFile(t *testing.T) {

func TestParseKey(t *testing.T) {
t.Parallel()
marshal := func(i interface{}) []byte {
marshal := func(i any) []byte {
b, err := json.Marshal(i)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -706,7 +706,7 @@ func TestParseKeySet(t *testing.T) {
}

func Test_guessKeyType(t *testing.T) {
marshal := func(i interface{}) []byte {
marshal := func(i any) []byte {
b, err := json.Marshal(i)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -754,7 +754,7 @@ func Test_guessKeyType(t *testing.T) {
}

func Test_guessSignatureAlgorithm(t *testing.T) {
must := func(args ...interface{}) crypto.PrivateKey {
must := func(args ...any) crypto.PrivateKey {
last := len(args) - 1
if err := args[last]; err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion jose/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func NewOpaqueSigner(signer crypto.Signer) OpaqueSigner {

// Verify validates the token payload with the given public key and deserializes
// the token into the destination.
func Verify(token *JSONWebToken, publicKey interface{}, dest ...interface{}) error {
func Verify(token *JSONWebToken, publicKey any, dest ...any) error {
if k, ok := publicKey.(x25519.PublicKey); ok {
publicKey = X25519Verifier(k)
}
Expand Down
2 changes: 1 addition & 1 deletion jose/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestTrimPrefix(t *testing.T) {
}

func TestSignVerify(t *testing.T) {
must := func(args ...interface{}) crypto.Signer {
must := func(args ...any) crypto.Signer {
last := len(args) - 1
if err := args[last]; err != nil {
t.Fatal(err)
Expand Down
8 changes: 4 additions & 4 deletions jose/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// ValidateSSHPOP validates the given SSH certificate and key for use in an
// sshpop header.
func ValidateSSHPOP(certFile string, key interface{}) (string, error) {
func ValidateSSHPOP(certFile string, key any) (string, error) {
if certFile == "" {
return "", errors.New("ssh certfile cannot be empty")
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func validateKeyPair(pub crypto.PublicKey, priv crypto.PrivateKey) error {
}
}

func validateX5(certs []*x509.Certificate, key interface{}) error {
func validateX5(certs []*x509.Certificate, key any) error {
if len(certs) == 0 {
return errors.New("certs cannot be empty")
}
Expand All @@ -78,7 +78,7 @@ func validateX5(certs []*x509.Certificate, key interface{}) error {

// ValidateX5C validates the given certificate chain and key for use as a token
// signer and x5t header.
func ValidateX5C(certs []*x509.Certificate, key interface{}) ([]string, error) {
func ValidateX5C(certs []*x509.Certificate, key any) ([]string, error) {
if err := validateX5(certs, key); err != nil {
return nil, errors.Wrap(err, "ValidateX5C")
}
Expand All @@ -91,7 +91,7 @@ func ValidateX5C(certs []*x509.Certificate, key interface{}) ([]string, error) {

// ValidateX5T validates the given certificate and key for use as a token signer
// and x5t header.
func ValidateX5T(certs []*x509.Certificate, key interface{}) (string, error) {
func ValidateX5T(certs []*x509.Certificate, key any) (string, error) {
if err := validateX5(certs, key); err != nil {
return "", errors.Wrap(err, "ValidateX5T")
}
Expand Down
8 changes: 4 additions & 4 deletions jose/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestValidateSSHPOP(t *testing.T) {

type args struct {
certFile string
key interface{}
key any
}
tests := []struct {
name string
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestValidateSSHPOP(t *testing.T) {
func Test_validateX5(t *testing.T) {
type test struct {
certs []*x509.Certificate
key interface{}
key any
err error
}
tests := map[string]func() test{
Expand Down Expand Up @@ -145,7 +145,7 @@ func Test_validateX5(t *testing.T) {
func TestValidateX5T(t *testing.T) {
type test struct {
certs []*x509.Certificate
key interface{}
key any
fp string
err error
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func TestValidateX5T(t *testing.T) {
func TestValidateX5C(t *testing.T) {
type test struct {
certs []*x509.Certificate
key interface{}
key any
err error
}
tests := map[string]func() test{
Expand Down
Loading