aboutsummaryrefslogtreecommitdiff
path: root/dh.go
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-03-10 11:58:37 +0100
committerOnur Gündüz <ogunduz@gmail.com>2026-03-10 11:58:37 +0100
commit03b95111f19a93ccee00e8e8e0afaab8112f5484 (patch)
treec0bfd7c5c768b441c901645a5a9a83c533eaf56f /dh.go
parentbcb40853f819cdbea99ec5d0ab2ee7594efd7690 (diff)
downloadnoise-03b95111f19a93ccee00e8e8e0afaab8112f5484.tar.gz
noise-03b95111f19a93ccee00e8e8e0afaab8112f5484.zip
Init package
Diffstat (limited to 'dh.go')
-rw-r--r--dh.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/dh.go b/dh.go
new file mode 100644
index 0000000..b2180b8
--- /dev/null
+++ b/dh.go
@@ -0,0 +1,87 @@
+package noisehandshake
+
+import (
+ "crypto/rand"
+ "fmt"
+ "io"
+
+ "golang.org/x/crypto/curve25519"
+)
+
+const (
+ // DiffieHellmanLength is the Diffie-Hellman output length.
+ DiffieHellmanLength = 32
+ // PublicKeyLength is the public key length.
+ PublicKeyLength = 32
+ // SecretKeyLength is the secret key length.
+ SecretKeyLength = 32
+ // DiffieHellmanAlgorithm is the DH algorithm name.
+ DiffieHellmanAlgorithm = "25519"
+)
+
+// KeyPair is a Curve25519 key pair.
+type KeyPair struct {
+ PublicKey []byte
+ SecretKey []byte
+}
+
+// Curve represents the Curve25519 DH operations.
+type Curve struct{}
+
+// DefaultCurve is the default Curve25519 implementation.
+var DefaultCurve = &Curve{}
+
+// GenerateKeyPair creates a new Curve25519 key pair.
+// If privKey is provided, it derives the public key from it.
+// For deterministic key generation, pass a 32-byte seed as privKey.
+func (c *Curve) GenerateKeyPair(privKey []byte) (*KeyPair, error) {
+ secretKey := make([]byte, SecretKeyLength)
+
+ if privKey != nil {
+ if len(privKey) != SecretKeyLength {
+ return nil, fmt.Errorf("noise: invalid private key length %d, expected %d", len(privKey), SecretKeyLength)
+ }
+ copy(secretKey, privKey)
+ } else {
+ // Generate random secret key
+ if _, err := io.ReadFull(rand.Reader, secretKey); err != nil {
+ return nil, fmt.Errorf("noise: failed to generate random key: %w", err)
+ }
+ }
+
+ // Calculate public key: publicKey = basePoint * secretKey
+ // curve25519.X25519 handles the clamping internally
+ publicKey, err := curve25519.X25519(secretKey, curve25519.Basepoint)
+ if err != nil {
+ return nil, fmt.Errorf("noise: X25519 operation failed: %w", err)
+ }
+
+ return &KeyPair{
+ PublicKey: publicKey,
+ SecretKey: secretKey,
+ }, nil
+}
+
+// DH performs Diffie-Hellman calculation.
+// It takes the remote party's public key and the local party's key pair,
+// and returns the shared secret.
+func (c *Curve) DH(publicKey []byte, localKey *KeyPair) ([]byte, error) {
+ if len(publicKey) != PublicKeyLength {
+ return nil, fmt.Errorf("noise: invalid public key length %d, expected %d", len(publicKey), PublicKeyLength)
+ }
+ if len(localKey.SecretKey) != SecretKeyLength {
+ return nil, fmt.Errorf("noise: invalid secret key length %d, expected %d", len(localKey.SecretKey), SecretKeyLength)
+ }
+
+ // Perform scalar multiplication: output = secretKey * publicKey
+ sharedSecret, err := curve25519.X25519(localKey.SecretKey, publicKey)
+ if err != nil {
+ return nil, fmt.Errorf("noise: DH calculation failed: %w", err)
+ }
+
+ return sharedSecret, nil
+}
+
+func (c *Curve) ALG() string {
+ return DiffieHellmanAlgorithm
+}