x25519_ecdh.mx raw

   1  // crypto/ecdh: X25519 was a NEW curve object per call, so every exchange failed.
   2  //
   3  // PrivateKey.ECDH and PublicKey.ECDH compare the key's curve against the
   4  // caller's curve object, and X25519() returned `&x25519Curve{}` - a fresh
   5  // pointer each call. Two keys built through two X25519() calls therefore never
   6  // matched, and ECDH answered "private key and public key curves do not match"
   7  // for every pair, including the RFC 7748 vector. crypto/tls key exchange goes
   8  // through exactly that shape. The curve is a package singleton now, built in
   9  // the package init beside the NIST curves.
  10  //
  11  // Also covers crypto/ecdh's x25519ScalarMult masking buffer, which is a slice
  12  // now: the RFC 7748 section 6.1 shared secret is the check that the clamp and
  13  // the ladder still work.
  14  package main
  15  
  16  import (
  17  	"crypto/ecdh"
  18  	"os"
  19  )
  20  
  21  func hex(b []byte) (out []byte) {
  22  	const d = "0123456789abcdef"
  23  	out = []byte{:len(b) * 2}
  24  	for i := 0; i < len(b); i++ {
  25  		out[i*2] = d[b[i]>>4]
  26  		out[i*2+1] = d[b[i]&0xf]
  27  	}
  28  	return out
  29  }
  30  
  31  func main() {
  32  	// Termination: a batch program must still be stoppable, so the entry
  33  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  34  	select {
  35  	case <-shutdown:
  36  		return
  37  	default:
  38  	}
  39  
  40  	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}
  41  	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}
  42  	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}
  43  
  44  	// RFC 7748 section 6.1: Alice's private key with Bob's public key.
  45  	priv, err := ecdh.X25519().NewPrivateKey(alicePriv)
  46  	if err != nil {
  47  		println("FAIL: NewPrivateKey: " | err.Error())
  48  		os.Exit(1)
  49  	}
  50  	pub, perr := ecdh.X25519().NewPublicKey(bobPub)
  51  	if perr != nil {
  52  		println("FAIL: NewPublicKey: " | perr.Error())
  53  		os.Exit(1)
  54  	}
  55  	shared, serr := priv.ECDH(pub)
  56  	if serr != nil {
  57  		println("FAIL: ECDH: " | serr.Error())
  58  		os.Exit(1)
  59  	}
  60  	want := "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"
  61  	if got := string(hex(shared)); got != want {
  62  		println("FAIL: shared = " | got | " want " | want)
  63  		os.Exit(1)
  64  	}
  65  
  66  	// Both sides of a fresh exchange must agree. The two curves come from two
  67  	// separate X25519() calls, which is what used to fail.
  68  	apriv, aerr := ecdh.X25519().NewPrivateKey(alicePriv)
  69  	if aerr != nil {
  70  		println("FAIL: alice key: " | aerr.Error())
  71  		os.Exit(1)
  72  	}
  73  	bpriv, berr := ecdh.X25519().NewPrivateKey(bobPriv)
  74  	if berr != nil {
  75  		println("FAIL: bob key: " | berr.Error())
  76  		os.Exit(1)
  77  	}
  78  	apub := apriv.PublicKey()
  79  	bpub := bpriv.PublicKey()
  80  	a2b, ea := apriv.ECDH(bpub)
  81  	if ea != nil {
  82  		println("FAIL: alice ECDH: " | ea.Error())
  83  		os.Exit(1)
  84  	}
  85  	b2a, eb := bpriv.ECDH(apub)
  86  	if eb != nil {
  87  		println("FAIL: bob ECDH: " | eb.Error())
  88  		os.Exit(1)
  89  	}
  90  	if string(hex(a2b)) != string(hex(b2a)) {
  91  		println("FAIL: the two shared secrets differ")
  92  		os.Exit(1)
  93  	}
  94  
  95  	// The other half of the contract: a caller may compare the key's curve with
  96  	// the package's, which is why the curve is still one object. The exchange
  97  	// above does NOT depend on that - it compares curve kinds - so this check
  98  	// pins the caller-facing identity while the kind comparison carries the
  99  	// logic.
 100  	if apriv.Curve() != ecdh.X25519() || apub.Curve() != ecdh.X25519() {
 101  		println("FAIL: a key's curve is not the curve the package hands out")
 102  		os.Exit(1)
 103  	}
 104  	println("ok")
 105  }
 106