Skip to content

Commit c3d56ac

Browse files
committed
feat: add api for setting kyber privkey
1 parent c4ca1d0 commit c3d56ac

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

example/chat/dial/main.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package main
22

33
import (
44
"context"
5+
"crypto/rand"
56
"crypto/x509"
7+
"encoding/hex"
68
"encoding/pem"
79
"errors"
810
"flag"
@@ -20,7 +22,7 @@ import (
2022
func main() {
2123
var remoteAddr = flag.String("raddr", "127.0.0.1:6666", "remote address")
2224
var localAddr = flag.String("laddr", "127.0.0.1:6667", "remote address")
23-
// var pubkey = flag.String("secret", "0b63baad7f2f4bb5b547c53adc0fbb179852910607935e6f4b5639fd989b1156", "shared secret")
25+
var pubkey = flag.String("pubkey", "0b63baad7f2f4bb5b547c53adc0fbb179852910607935e6f4b5639fd989b1156", "pubkey")
2426
// var covert = flag.String("covert", "1.2.3.4:5678", "covert address")
2527
flag.Parse()
2628

@@ -44,11 +46,22 @@ func main() {
4446

4547
// quicSpec.ClientHelloSpec = &chSpec
4648

47-
kyberClient := kyber.NewClient()
48-
kyberServer := kyber.NewServer()
49+
clientPrivKey := [32]byte{}
50+
if _, err := rand.Read(clientPrivKey[:]); err != nil {
51+
panic(err)
52+
}
53+
kyberClient := kyber.Client{Host: kyber.NewHost(clientPrivKey)}
54+
55+
pub, err := hex.DecodeString(*pubkey)
56+
util.Check(err)
57+
58+
pub32 := [32]byte{}
59+
if n := copy(pub32[:], pub); n != 32 {
60+
panic("key len != 32")
61+
}
4962

5063
clientData := []byte("hello world")
51-
kyberClient.ComputeSharedKey(kyberServer.GetPublicKey())
64+
kyberClient.ComputeSharedKey(pub32)
5265
x25519kyber768Parrot := kyberClient.GenKyber(clientData)
5366

5467
tp := quic.UTransport{
@@ -83,7 +96,7 @@ func main() {
8396
},
8497
&tls.SupportedCurvesExtension{
8598
Curves: []tls.CurveID{
86-
tls.X25519Kyber768Draft00,
99+
tls.X25519MLKEM768,
87100
tls.CurveX25519,
88101
tls.CurveSECP256R1,
89102
tls.CurveSECP384R1,
@@ -112,7 +125,7 @@ func main() {
112125
&tls.KeyShareExtension{
113126
KeyShares: []tls.KeyShare{
114127
{
115-
Group: tls.X25519Kyber768Draft00,
128+
Group: tls.X25519MLKEM768,
116129
Data: x25519kyber768Parrot,
117130
},
118131
{

example/chat/listen/main.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/pion/dtls/v3/examples/util"
1414
quic "github.com/refraction-networking/uquic"
15+
"github.com/refraction-networking/uquic/internal/kyber"
1516
"github.com/refraction-networking/uquic/qlog"
1617
tls "github.com/refraction-networking/utls"
1718
)
@@ -43,7 +44,10 @@ func main() {
4344
priv, err := hex.DecodeString(station_privkey)
4445
util.Check(err)
4546

46-
fmt.Printf("%v\n", priv)
47+
priv32 := [32]byte{}
48+
if n := copy(priv32[:], priv); n != 32 {
49+
panic("key len != 32")
50+
}
4751

4852
pconn, err := net.ListenUDP("udp", addr)
4953
util.Check(err)
@@ -56,10 +60,18 @@ func main() {
5660
Tracer: qlog.NewTracer(f),
5761
}
5862

63+
kyberServer := kyber.Server{Host: kyber.NewHost(priv32)}
64+
fmt.Printf("kyber server pub key: %x\n", kyberServer.GetPublicKey())
65+
5966
listener, err := tp.ListenEarly(&tls.Config{
6067
Certificates: []tls.Certificate{certificate},
6168
NextProtos: []string{"h3"},
6269
CurvePreferences: []tls.CurveID{tls.X25519},
70+
GetOscur0KeyShare: func(key *tls.KeyShare) error {
71+
data := kyberServer.DecodeKyber(key.Data)
72+
fmt.Printf("Data: %s\n", data)
73+
return nil
74+
},
6375
}, &quic.Config{Tracer: qlog.DefaultConnectionTracer})
6476
util.Check(err)
6577

internal/kyber/main.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ type Host struct {
2020
}
2121

2222
// NewHost initializes a Host with an X25519 private key
23-
func NewHost() *Host {
24-
host := &Host{}
25-
_, err := rand.Read(host.privateKey[:])
26-
if err != nil {
27-
panic(err)
23+
func NewHost(privKey [32]byte) *Host {
24+
host := &Host{
25+
privateKey: privKey,
2826
}
2927
curve25519.ScalarBaseMult(&host.publicKey, &host.privateKey)
3028
return host
@@ -53,11 +51,6 @@ type Client struct {
5351
*Host
5452
}
5553

56-
// NewClient initializes a Client
57-
func NewClient() *Client {
58-
return &Client{NewHost()}
59-
}
60-
6154
// GenKyber generates a Kyber-style payload
6255
func (c *Client) GenKyber(data []byte) []byte {
6356
if len(data) > 1121 {
@@ -106,11 +99,6 @@ type Server struct {
10699
*Host
107100
}
108101

109-
// NewServer initializes a Server
110-
func NewServer() *Server {
111-
return &Server{NewHost()}
112-
}
113-
114102
// DecodeKyber decodes the Kyber-style payload and extracts the original data
115103
func (s *Server) DecodeKyber(parrot []byte) []byte {
116104
var clientPublicKey [32]byte
@@ -239,8 +227,16 @@ func Decode(a []byte, w int) []int {
239227

240228
func main() {
241229
for i := 0; i < 10000; i++ {
242-
client := NewClient()
243-
server := NewServer()
230+
privKey := [32]byte{}
231+
if _, err := rand.Read(privKey[:]); err != nil {
232+
panic(err)
233+
}
234+
clientPrivKey := [32]byte{}
235+
if _, err := rand.Read(clientPrivKey[:]); err != nil {
236+
panic(err)
237+
}
238+
server := &Server{Host: NewHost(privKey)}
239+
client := &Client{Host: NewHost(clientPrivKey)}
244240

245241
clientData := []byte("hello world")
246242
client.ComputeSharedKey(server.GetPublicKey())

0 commit comments

Comments
 (0)