From ba0c89d567fcb599e71933ad9888d469bcf3a1ba Mon Sep 17 00:00:00 2001 From: eyedeekay Date: Sun, 9 Mar 2025 14:02:10 -0400 Subject: [PATCH] fix tests that failed because of inconsistent usage of crypto API --- .../key_certificate/key_certificate_test.go | 2 +- lib/common/keys_and_cert/keys_and_cert.go | 2 +- lib/common/router_info/router_info2_test.go | 5 ++-- lib/crypto/ed25519.go | 26 ++++++++++++++----- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/common/key_certificate/key_certificate_test.go b/lib/common/key_certificate/key_certificate_test.go index 5db40ca..6f08f65 100644 --- a/lib/common/key_certificate/key_certificate_test.go +++ b/lib/common/key_certificate/key_certificate_test.go @@ -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) } diff --git a/lib/common/keys_and_cert/keys_and_cert.go b/lib/common/keys_and_cert/keys_and_cert.go index 0a9856b..465fe2c 100644 --- a/lib/common/keys_and_cert/keys_and_cert.go +++ b/lib/common/keys_and_cert/keys_and_cert.go @@ -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") diff --git a/lib/common/router_info/router_info2_test.go b/lib/common/router_info/router_info2_test.go index 3978bf9..ed50581 100644 --- a/lib/common/router_info/router_info2_test.go +++ b/lib/common/router_info/router_info2_test.go @@ -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) } diff --git a/lib/crypto/ed25519.go b/lib/crypto/ed25519.go index dffccde..cee54fc 100644 --- a/lib/crypto/ed25519.go +++ b/lib/crypto/ed25519.go @@ -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 }