// crypto/ecdh: X25519 was a NEW curve object per call, so every exchange failed. // // PrivateKey.ECDH and PublicKey.ECDH compare the key's curve against the // caller's curve object, and X25519() returned `&x25519Curve{}` - a fresh // pointer each call. Two keys built through two X25519() calls therefore never // matched, and ECDH answered "private key and public key curves do not match" // for every pair, including the RFC 7748 vector. crypto/tls key exchange goes // through exactly that shape. The curve is a package singleton now, built in // the package init beside the NIST curves. // // Also covers crypto/ecdh's x25519ScalarMult masking buffer, which is a slice // now: the RFC 7748 section 6.1 shared secret is the check that the clamp and // the ladder still work. package main import ( "crypto/ecdh" "os" ) func hex(b []byte) (out []byte) { const d = "0123456789abcdef" out = []byte{:len(b) * 2} for i := 0; i < len(b); i++ { out[i*2] = d[b[i]>>4] out[i*2+1] = d[b[i]&0xf] } return out } func main() { // Termination: a batch program must still be stoppable, so the entry // handles the channel the runtime closes on SIGINT/SIGTERM. select { case <-shutdown: return default: } alicePriv := []byte{0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16, 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0, 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a} bobPriv := []byte{0x5d, 0xab, 0x08, 0x7e, 0x62, 0x4a, 0x8a, 0x4b, 0x79, 0xe1, 0x7f, 0x8b, 0x83, 0x80, 0x0e, 0xe6, 0x6f, 0x3b, 0xb1, 0x29, 0x26, 0x18, 0xb6, 0xfd, 0x1c, 0x2f, 0x8b, 0x27, 0xff, 0x88, 0xe0, 0xeb} bobPub := []byte{0xde, 0x9e, 0xdb, 0x7d, 0x7b, 0x7d, 0xc1, 0xb4, 0xd3, 0x5b, 0x61, 0xc2, 0xec, 0xe4, 0x35, 0x37, 0x3f, 0x83, 0x43, 0xc8, 0x5b, 0x78, 0x67, 0x4d, 0xad, 0xfc, 0x7e, 0x14, 0x6f, 0x88, 0x2b, 0x4f} // RFC 7748 section 6.1: Alice's private key with Bob's public key. priv, err := ecdh.X25519().NewPrivateKey(alicePriv) if err != nil { println("FAIL: NewPrivateKey: " | err.Error()) os.Exit(1) } pub, perr := ecdh.X25519().NewPublicKey(bobPub) if perr != nil { println("FAIL: NewPublicKey: " | perr.Error()) os.Exit(1) } shared, serr := priv.ECDH(pub) if serr != nil { println("FAIL: ECDH: " | serr.Error()) os.Exit(1) } want := "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742" if got := string(hex(shared)); got != want { println("FAIL: shared = " | got | " want " | want) os.Exit(1) } // Both sides of a fresh exchange must agree. The two curves come from two // separate X25519() calls, which is what used to fail. apriv, aerr := ecdh.X25519().NewPrivateKey(alicePriv) if aerr != nil { println("FAIL: alice key: " | aerr.Error()) os.Exit(1) } bpriv, berr := ecdh.X25519().NewPrivateKey(bobPriv) if berr != nil { println("FAIL: bob key: " | berr.Error()) os.Exit(1) } apub := apriv.PublicKey() bpub := bpriv.PublicKey() a2b, ea := apriv.ECDH(bpub) if ea != nil { println("FAIL: alice ECDH: " | ea.Error()) os.Exit(1) } b2a, eb := bpriv.ECDH(apub) if eb != nil { println("FAIL: bob ECDH: " | eb.Error()) os.Exit(1) } if string(hex(a2b)) != string(hex(b2a)) { println("FAIL: the two shared secrets differ") os.Exit(1) } // The other half of the contract: a caller may compare the key's curve with // the package's, which is why the curve is still one object. The exchange // above does NOT depend on that - it compares curve kinds - so this check // pins the caller-facing identity while the kind comparison carries the // logic. if apriv.Curve() != ecdh.X25519() || apub.Curve() != ecdh.X25519() { println("FAIL: a key's curve is not the curve the package hands out") os.Exit(1) } println("ok") }