fix tests that failed because of inconsistent usage of crypto API

This commit is contained in:
eyedeekay
2025-03-09 14:02:10 -04:00
parent 598ee1eb70
commit ba0c89d567
4 changed files with 24 additions and 11 deletions

View File

@ -44,7 +44,7 @@ func TestPublicKeyTypeWithInvalidData(t *testing.T) {
// Test with invalid short data
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x02})
assert.NotNil(err)
assert.Contains(err.Error(), "key certificate data too short", "Expected error for invalid data")
assert.Contains(err.Error(), "certificate parsing warning: certificate data is shorter than specified by length", "Expected error for invalid data")
assert.Nil(key_cert)
}

View File

@ -139,7 +139,7 @@ func (keys_and_cert *KeysAndCert) Certificate() (cert Certificate) {
// ReadKeysAndCert creates a new *KeysAndCert from []byte using ReadKeysAndCert.
// Returns a pointer to KeysAndCert unlike ReadKeysAndCert.
func ReadKeysAndCert(data []byte) (keys_and_cert KeysAndCert, remainder []byte, err error) {
func ReadKeysAndCert(data []byte) (keys_and_cert *KeysAndCert, remainder []byte, err error) {
log.WithFields(logrus.Fields{
"input_length": len(data),
}).Debug("Reading KeysAndCert from data")

View File

@ -22,11 +22,12 @@ import (
func TestCreateRouterInfo(t *testing.T) {
// Generate signing key pair (Ed25519)
var ed25519_privkey crypto.Ed25519PrivateKey
_, err := (&ed25519_privkey).Generate()
ed25519_signingprivkey, err := ed25519_privkey.Generate()
if err != nil {
t.Fatalf("Failed to generate Ed25519 private key: %v\n", err)
}
ed25519_pubkey_raw, err := ed25519_privkey.Public()
ed25519_pubkey_raw, err := ed25519_signingprivkey.Public()
if err != nil {
t.Fatalf("Failed to derive Ed25519 public key: %v\n", err)
}

View File

@ -273,26 +273,38 @@ func (k Ed25519PrivateKey) Len() int {
}
func (k Ed25519PrivateKey) Generate() (SigningPrivateKey, error) {
// Generate a new Ed25519 key pair
_, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, err
return nil, oops.Errorf("failed to generate ed25519 key: %v", err)
}
// Assign the generated private key to the receiver
k = Ed25519PrivateKey(priv)
return k, nil
// Copy the full private key (includes public key)
newKey := make(Ed25519PrivateKey, ed25519.PrivateKeySize)
copy(newKey, priv)
return newKey, nil
}
func (k Ed25519PrivateKey) Public() (SigningPublicKey, error) {
fmt.Printf("Ed25519PrivateKey.Public(): len(k) = %d\n", len(k))
if len(k) != ed25519.PrivateKeySize {
return nil, oops.Errorf("invalid ed25519 private key size: expected %d, got %d", ed25519.PrivateKeySize, len(k))
return nil, oops.Errorf("invalid ed25519 private key size: expected %d, got %d",
ed25519.PrivateKeySize, len(k))
}
pubKey := k[32:]
// Extract public key portion (last 32 bytes)
pubKey := ed25519.PrivateKey(k).Public().(ed25519.PublicKey)
fmt.Printf("Ed25519PrivateKey.Public(): extracted pubKey length: %d\n", len(pubKey))
return Ed25519PublicKey(pubKey), nil
}
func CreateEd25519PrivateKeyFromBytes(data []byte) (Ed25519PrivateKey, error) {
if len(data) != ed25519.PrivateKeySize {
return nil, oops.Errorf("invalid ed25519 private key size: expected %d, got %d",
ed25519.PrivateKeySize, len(data))
}
privKey := make(Ed25519PrivateKey, ed25519.PrivateKeySize)
copy(privKey, data)
return privKey, nil
}
type Ed25519Signer struct {
k []byte
}