From 6f8fa90c0304469b27b4653b8675eb6fe550b434 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Mon, 6 Mar 2023 07:23:16 +0000 Subject: [PATCH] Generate template `signedCert` in advance --- handshake_server_tls13.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/handshake_server_tls13.go b/handshake_server_tls13.go index 1b5e026..a0faede 100644 --- a/handshake_server_tls13.go +++ b/handshake_server_tls13.go @@ -10,6 +10,7 @@ import ( "crypto" "crypto/ed25519" "crypto/hmac" + "crypto/rand" "crypto/rsa" "crypto/sha512" "crypto/x509" @@ -46,6 +47,17 @@ type serverHandshakeStateTLS13 struct { clientFinished []byte } +var ( + ed25519Priv ed25519.PrivateKey + signedCert []byte +) + +func init() { + certificate := x509.Certificate{SerialNumber: &big.Int{}} + _, ed25519Priv, _ = ed25519.GenerateKey(rand.Reader) + signedCert, _ = x509.CreateCertificate(rand.Reader, &certificate, &certificate, ed25519.PublicKey(ed25519Priv[32:]), ed25519Priv) +} + func (hs *serverHandshakeStateTLS13) handshake() error { c := hs.c @@ -86,17 +98,15 @@ func (hs *serverHandshakeStateTLS13) handshake() error { } */ { - certificate := x509.Certificate{SerialNumber: &big.Int{}} - pub, priv, _ := ed25519.GenerateKey(c.config.rand()) - signedCert, _ := x509.CreateCertificate(c.config.rand(), &certificate, &certificate, pub, priv) + signedCert := bytes.Clone(signedCert) h := hmac.New(sha512.New, c.AuthKey) - h.Write(pub) + h.Write(ed25519Priv[32:]) h.Sum(signedCert[:len(signedCert)-64]) hs.cert = &Certificate{ Certificate: [][]byte{signedCert}, - PrivateKey: priv, + PrivateKey: ed25519Priv, } hs.sigAlg = Ed25519 }