Skip to content

Commit 9383619

Browse files
committed
demo: Update to Go 1.27, switch to standard library ML-DSA
1 parent 9e4045c commit 9383619

4 files changed

Lines changed: 6 additions & 52 deletions

File tree

demo/cosign.go

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package main
22

33
import (
4-
"bytes"
54
"crypto"
65
"crypto/ecdsa"
76
"crypto/ed25519"
87
"crypto/elliptic"
8+
"crypto/mldsa"
99
"crypto/rand"
1010
"crypto/sha256"
1111
_ "crypto/sha512"
1212
"crypto/x509"
1313
"fmt"
1414
"io"
15-
"slices"
1615

17-
"filippo.io/mldsa"
1816
"golang.org/x/crypto/cryptobyte"
1917
)
2018

@@ -28,47 +26,6 @@ func tlogOrigin(id TrustAnchorID) string {
2826
return fmt.Sprintf("oid/1.3.6.1.4.1.%s", id)
2927
}
3028

31-
// When ML-DSA is added to the Go standard library, these wrappers can be
32-
// removed.
33-
34-
var (
35-
mldsa44PKCS8Prefix = []byte{0x30, 0x34, 0x02, 0x01, 0x00, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x11, 0x04, 0x22, 0x80, 0x20}
36-
mldsa65PKCS8Prefix = []byte{0x30, 0x34, 0x02, 0x01, 0x00, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x12, 0x04, 0x22, 0x80, 0x20}
37-
mldsa87PKCS8Prefix = []byte{0x30, 0x34, 0x02, 0x01, 0x00, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x13, 0x04, 0x22, 0x80, 0x20}
38-
39-
mldsa44SPKIPrefix = []byte{0x30, 0x82, 0x05, 0x32, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x11, 0x03, 0x82, 0x05, 0x21, 0x00}
40-
mldsa65SPKIPrefix = []byte{0x30, 0x82, 0x07, 0xb2, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x12, 0x03, 0x82, 0x07, 0xa1, 0x00}
41-
mldsa87SPKIPrefix = []byte{0x30, 0x82, 0x0a, 0x32, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x13, 0x03, 0x82, 0x0a, 0x21, 0x00}
42-
)
43-
44-
func parsePKCS8PrivateKey(der []byte) (key any, err error) {
45-
if seed, ok := bytes.CutPrefix(der, mldsa44PKCS8Prefix); ok && len(seed) == mldsa.PrivateKeySize {
46-
return mldsa.NewPrivateKey(mldsa.MLDSA44(), seed)
47-
}
48-
if seed, ok := bytes.CutPrefix(der, mldsa65PKCS8Prefix); ok && len(seed) == mldsa.PrivateKeySize {
49-
return mldsa.NewPrivateKey(mldsa.MLDSA65(), seed)
50-
}
51-
if seed, ok := bytes.CutPrefix(der, mldsa87PKCS8Prefix); ok && len(seed) == mldsa.PrivateKeySize {
52-
return mldsa.NewPrivateKey(mldsa.MLDSA87(), seed)
53-
}
54-
return x509.ParsePKCS8PrivateKey(der)
55-
}
56-
57-
func marshalPKIXPublicKey(pub any) ([]byte, error) {
58-
if ml, ok := pub.(*mldsa.PublicKey); ok {
59-
switch ml.Parameters() {
60-
case mldsa.MLDSA44():
61-
return append(slices.Clip(mldsa44SPKIPrefix), ml.Bytes()...), nil
62-
case mldsa.MLDSA65():
63-
return append(slices.Clip(mldsa65SPKIPrefix), ml.Bytes()...), nil
64-
case mldsa.MLDSA87():
65-
return append(slices.Clip(mldsa87SPKIPrefix), ml.Bytes()...), nil
66-
}
67-
panic("unknown ML-DSA parameters")
68-
}
69-
return x509.MarshalPKIXPublicKey(pub)
70-
}
71-
7229
type Cosigner struct {
7330
Version DraftVersion
7431
ID TrustAnchorID
@@ -79,7 +36,7 @@ type Cosigner struct {
7936
}
8037

8138
func NewCosignerFromConfig(version DraftVersion, config *CosignerConfig) (*Cosigner, error) {
82-
priv, err := parsePKCS8PrivateKey(config.PrivateKey)
39+
priv, err := x509.ParsePKCS8PrivateKey(config.PrivateKey)
8340
if err != nil {
8441
return nil, err
8542
}
@@ -116,7 +73,7 @@ func NewCosignerFromConfig(version DraftVersion, config *CosignerConfig) (*Cosig
11673
signer = ed
11774
opts = crypto.Hash(0)
11875
case SignatureAlgorithmMLDSA44, SignatureAlgorithmMLDSA65, SignatureAlgorithmMLDSA87:
119-
var params *mldsa.Parameters
76+
var params mldsa.Parameters
12077
switch config.SignatureAlgorithm {
12178
case SignatureAlgorithmMLDSA44:
12279
params = mldsa.MLDSA44()

demo/encode.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"cmp"
66
"crypto/sha256"
7+
"crypto/x509"
78
"crypto/x509/pkix"
89
"encoding/asn1"
910
"errors"
@@ -431,7 +432,7 @@ func CreateCertificate(config *CAConfig, issuanceLog *MerkleTree, cosigners []*C
431432

432433
func CreateCACertificate(config *CAConfig, cosigner *Cosigner) ([]byte, error) {
433434
pub := cosigner.Signer.Public()
434-
spki, err := marshalPKIXPublicKey(pub)
435+
spki, err := x509.MarshalPKIXPublicKey(pub)
435436
if err != nil {
436437
return nil, err
437438
}

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module github.com/ietf-plants-wg/merkle-tree-certs
22

3-
go 1.25.5
3+
go 1.27
44

55
require golang.org/x/crypto v0.54.0
6-
7-
require filippo.io/mldsa v0.0.0-20260215214346-43d0283efc3e

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
filippo.io/mldsa v0.0.0-20260215214346-43d0283efc3e h1:VsUbObBMxXlc23Eb9VeeJYE4jvTs87qa5RqSN2U5FJU=
2-
filippo.io/mldsa v0.0.0-20260215214346-43d0283efc3e/go.mod h1:32qQ5yj3R24Eu03iWFWchdC3OB653wPvoepWejkefbY=
31
golang.org/x/crypto v0.54.0 h1:YLIA59K4fiNzHzjnZt2tUJQjQtUWfWbeHBqKtk3eScw=
42
golang.org/x/crypto v0.54.0/go.mod h1:KWL8ny2AZdGR2cWmzeHrp2azQPGogOv+HeQaVEXC2dk=

0 commit comments

Comments
 (0)