]> Cypherpunks repositories - keks.git/commitdiff
Use Go 1.24's new crypto/* libraries
authorSergey Matveev <stargrave@stargrave.org>
Wed, 12 Feb 2025 11:31:06 +0000 (14:31 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 12 Feb 2025 11:31:06 +0000 (14:31 +0300)
12 files changed:
go/be/be.go
go/cmd/pp/main.go
go/cmd/textdump-tester/main.go
go/encode.go
go/go.mod
go/pki/cmd/enctool/main.go
go/pki/go.mod
go/pki/hash/algo.go
go/pki/hash/merkle/hash.go
go/pki/hash/shake.go
go/pki/sntrup4591761-x25519/go.mod
spec/format/cer.texi

index ad79db7a7a0160925f69f8e51084867fa3e2deebeca707e631fa48b613467cd9..ba21c57f87a91a6c3547c9d945c5c33ce1093c4c34f0a750a26cd661c2a5df47 100644 (file)
@@ -62,7 +62,7 @@ func Get(buf []byte) (v uint64) {
                        (uint64(buf[6]) << 8) |
                        uint64(buf[7])
        default:
-               for i := 0; i < len(buf); i++ {
+               for i := range len(buf) {
                        v |= uint64(buf[i]) << ((len(buf) - i - 1) * 8)
                }
        }
@@ -116,7 +116,7 @@ func Put(buf []byte, v uint64) {
                buf[1] = byte((v & 0x00FF000000000000) >> 48)
                buf[0] = byte((v & 0xFF00000000000000) >> 56)
        default:
-               for i := 0; i < len(buf); i++ {
+               for i := range len(buf) {
                        buf[i] = byte((v & (0xFF << ((len(buf) - i - 1) * 8)) >> ((len(buf) - i - 1) * 8)) & 0xFF)
                }
        }
index 48fc6c1161b549ccc23c36507f4733bb78986751ee8e247a69fe1959feeff1e2..532781b1699c74c58156383f98004ddbd19baa7b439ab44cbde9ea008bfacf77 100644 (file)
@@ -57,7 +57,7 @@ func printbin(s []byte) {
 }
 
 func printer(iter *keks.Iterator, count int, inList, inMap bool) {
-       for i := 0; i < count; i++ {
+       for i := range count {
                if !iter.Next() {
                        panic("unexpected")
                }
index cab6ad996c40c6d3ed9426249b15e257759a9afc4db4ac789e865c5608f8e927..77f84f8760791b4a7faba946763db73c0413ec99fe3d6be1e559b741156501e4 100644 (file)
@@ -245,7 +245,7 @@ func checker(v any) {
                if len(our) != their {
                        log.Fatalln("MAP len differs:", our, their)
                }
-               for i := 0; i < their; i++ {
+               for range their {
                        fields = getFields()
                        k := string(mustDecodeHex(fields[1]))
                        item, ok := our[k]
index aca17b6e8e8a355f13ba52430ec7b03164d18e3b9e24b42ef6076982d459add2..5fd2324af95d7aa3e8589154956a58895dedf153b7e08d4323eace25cbbc53b7 100644 (file)
@@ -132,7 +132,7 @@ func Encode(w io.Writer, v any, opts *EncodeOpts) (written int64, err error) {
                                }
                        }
                } else {
-                       for i := 0; i < vv.Len(); i++ {
+                       for i := range vv.Len() {
                                n64, err = Encode(w, vv.Index(i).Interface(), opts)
                                written += n64
                                if err != nil {
index aab08133f50eb50accfd6fa99c3dbca6052caa8210960a292f0cf9cab8701a00..540eec2ba9f1dd3aeadecbf4ade38db5d4cfcb86cda5d86c1d880be564f66a32 100644 (file)
--- a/go/go.mod
+++ b/go/go.mod
@@ -1,15 +1,13 @@
 module go.cypherpunks.su/keks
 
-go 1.22
+go 1.24
 
 require go.cypherpunks.su/tai64n/v4 v4.1.0
 
 require (
        github.com/google/uuid v1.6.0
        github.com/mitchellh/mapstructure v1.5.0
+       golang.org/x/term v0.27.0
 )
 
-require (
-       golang.org/x/sys v0.28.0 // indirect
-       golang.org/x/term v0.27.0 // indirect
-)
+require golang.org/x/sys v0.28.0 // indirect
index ca1ec8fad9484dd81fd84cdcb120a977a96f048d35d1ed45766c5b3b0a0257a4..5533ac0997f2add2c283a6855d2837e1071379acad41990b312ffb56c793ee2d 100644 (file)
@@ -18,6 +18,7 @@ package main
 import (
        "bytes"
        "crypto/ecdh"
+       "crypto/hkdf"
        "crypto/rand"
        "errors"
        "flag"
@@ -34,12 +35,11 @@ import (
        "go.cypherpunks.su/balloon/v3"
        "golang.org/x/crypto/blake2b"
        "golang.org/x/crypto/chacha20poly1305"
-       "golang.org/x/crypto/hkdf"
-       "golang.org/x/crypto/sha3"
        "golang.org/x/term"
 
        "go.cypherpunks.su/keks"
        "go.cypherpunks.su/keks/pki"
+       pkihash "go.cypherpunks.su/keks/pki/hash"
        "go.cypherpunks.su/keks/pki/utils"
        "go.cypherpunks.su/keks/types"
 )
@@ -91,10 +91,6 @@ func blake2b256() hash.Hash {
        return h
 }
 
-func shake256() hash.Hash {
-       return sha3.NewShake256()
-}
-
 func readPasswd(prompt string) (passwd []byte) {
        tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
        if err != nil {
@@ -215,11 +211,15 @@ func main() {
                                }
                                passwd := readPasswd("Passphrase:")
                                {
-                                       kek := hkdf.Extract(blake2b256, balloon.H(blake2b256,
+                                       var kek []byte
+                                       kek, err = hkdf.Extract(blake2b256, balloon.H(blake2b256,
                                                passwd,
                                                append(encrypted.Bind[:], *kem.Salt...),
                                                int(kem.Cost.S), int(kem.Cost.T), int(kem.Cost.P),
                                        ), []byte(BalloonHKDFSalt))
+                                       if err != nil {
+                                               log.Fatal(err)
+                                       }
                                        var cekp []byte
                                        cekp, err = kemChaPolyOpen(kek, kem.CEK, chacha20poly1305.KeySize)
                                        if err != nil {
@@ -282,8 +282,12 @@ func main() {
                                                        *kem.Encap, pub,
                                                        keySNTRUP[:], keyX25519,
                                                }, []byte{})
-                                               kek := hkdf.Extract(blake2b256,
+                                               var kek []byte
+                                               kek, err = hkdf.Extract(blake2b256,
                                                        ikm, []byte(SNTRUP4591761X25519Salt))
+                                               if err != nil {
+                                                       log.Fatal(err)
+                                               }
                                                var cekp []byte
                                                cekp, err = kemChaPolyOpen(kek, kem.CEK, chacha20poly1305.KeySize)
                                                if err != nil {
@@ -357,8 +361,12 @@ func main() {
                                                        *kem.Encap, pub,
                                                        keyMcEliece, keyX25519,
                                                }, []byte{})
-                                               kek := hkdf.Extract(shake256,
+                                               var kek []byte
+                                               kek, err = hkdf.Extract(pkihash.NewSHAKE256,
                                                        ikm, []byte(ClassicMcEliece6960119X25519Salt))
+                                               if err != nil {
+                                                       log.Fatal(err)
+                                               }
                                                var cekp []byte
                                                cekp, err = kemChaPolyOpen(
                                                        kek[:chacha20poly1305.KeySize],
@@ -429,11 +437,15 @@ func main() {
                                },
                        }
                        {
-                               kek := hkdf.Extract(blake2b256, balloon.H(blake2b256,
+                               var kek []byte
+                               kek, err = hkdf.Extract(blake2b256, balloon.H(blake2b256,
                                        passwd,
                                        append(binding[:], salt...),
                                        *balloonS, *balloonT, *balloonP,
                                ), []byte(BalloonHKDFSalt))
+                               if err != nil {
+                                       log.Fatal(err)
+                               }
                                kem.CEK, err = kemChaPolySeal(kek, cek)
                                if err != nil {
                                        log.Fatal(err)
@@ -485,8 +497,12 @@ func main() {
                                                encap, pub.V,
                                                keySNTRUP[:], keyX25519,
                                        }, []byte{})
-                                       kek := hkdf.Extract(blake2b256,
+                                       var kek []byte
+                                       kek, err = hkdf.Extract(blake2b256,
                                                ikm, []byte(SNTRUP4591761X25519Salt))
+                                       if err != nil {
+                                               log.Fatal(err)
+                                       }
                                        kem.CEK, err = kemChaPolySeal(kek, cek)
                                        if err != nil {
                                                log.Fatal(err)
@@ -540,8 +556,12 @@ func main() {
                                                encap, pub.V,
                                                keyMcEliece[:], keyX25519,
                                        }, []byte{})
-                                       kek := hkdf.Extract(shake256,
+                                       var kek []byte
+                                       kek, err = hkdf.Extract(pkihash.NewSHAKE256,
                                                ikm, []byte(ClassicMcEliece6960119X25519Salt))
+                                       if err != nil {
+                                               log.Fatal(err)
+                                       }
                                        kem.CEK, err = kemChaPolySeal(kek[:chacha20poly1305.KeySize], cek)
                                        if err != nil {
                                                log.Fatal(err)
index cb57a064dc78fdca3d6e9616c08f933e3f9a878abb921fb2ad6d2538d1cb53ee..a6e87651472d3f194c8b968d322e83d49c37d8c61140419d1e025243852ed3c6 100644 (file)
@@ -1,6 +1,6 @@
 module go.cypherpunks.su/keks/pki
 
-go 1.22
+go 1.24
 
 require (
        github.com/google/uuid v1.6.0
index b2e4cbab618d8deb65fafa8249020f8aaba0f91399ddbf222f0ebdb343743de3..68438390107766c5b4adc407e4e2b79887e7f2785ef88bd78f31591329f96f3a 100644 (file)
@@ -22,7 +22,6 @@ import (
        "go.cypherpunks.su/gogost/v6/gost34112012256"
        "go.cypherpunks.su/gogost/v6/gost34112012512"
        "golang.org/x/crypto/blake2b"
-       "golang.org/x/crypto/sha3"
 
        ed25519blake2b "go.cypherpunks.su/keks/pki/ed25519-blake2b"
        "go.cypherpunks.su/keks/pki/gost"
@@ -73,9 +72,9 @@ func ByName(name string) hash.Hash {
                }
                return h
        case SHAKE128:
-               return sha3.NewShake128()
+               return NewSHAKE128()
        case SHAKE256:
-               return sha3.NewShake256()
+               return NewSHAKE256()
        case SHAKE128Merkle:
                return NewSHAKE128MerkleHasher(
                        merkle.DefaultChunkLen, runtime.NumCPU())
index ea4eaa22142e73690e5e1774b44ff8e25a3bbaf3d6611c7f17424a822d5a8349..d38dd491df43d2205ce7e371863c2c100777dd6d46f4b6781f0ad5969f0a2078 100644 (file)
@@ -85,7 +85,7 @@ func NewHasher(
                chunkLen:  chunkLen,
        }
        hashSize := h.Size()
-       for i := 0; i < 2*maxDepth; i++ {
+       for i := range 2 * maxDepth {
                h.hashes[i] = make([]byte, hashSize)
                h.frees[i] = true
        }
@@ -106,7 +106,7 @@ func (h *Hasher) prepare() {
 func (h *Hasher) Reset() {
        h.pw.Close()
        <-h.finished
-       for i := 0; i < 2*maxDepth; i++ {
+       for i := range 2 * maxDepth {
                h.frees[i] = true
        }
        h.prepare()
@@ -126,7 +126,7 @@ func (h *Hasher) get(l int) []byte {
 
 func (h *Hasher) fold() {
        var err error
-       for l := 0; l < maxDepth; l++ {
+       for l := range maxDepth {
                if h.frees[l*2+0] || h.frees[l*2+1] {
                        continue
                }
index ea9c6fc05070897231207f43271c51dfe25f200fcfdecce325261e465c4ce240..84e05f5ff6acbadc34e5624baa07756aa65bfab36e6fa99e71de7547494349a5 100644 (file)
 package hash
 
 import (
+       "crypto/sha3"
        "hash"
+       "io"
 
        "go.cypherpunks.su/keks/pki/hash/merkle"
-       "golang.org/x/crypto/sha3"
 )
 
+type SHAKE struct {
+       xof *sha3.SHAKE
+       l   int
+}
+
+func (h SHAKE) Size() int {
+       return h.l
+}
+
+func (h SHAKE) BlockSize() int {
+       return h.xof.BlockSize()
+}
+
+func (h SHAKE) Reset() {
+       h.xof.Reset()
+}
+
+func (h SHAKE) Write(p []byte) (int, error) {
+       return h.xof.Write(p)
+}
+
+func (h SHAKE) Sum(b []byte) []byte {
+       buf := make([]byte, h.l)
+       if _, err := io.ReadFull(h.xof, buf); err != nil {
+               panic(err)
+       }
+       return append(b, buf...)
+}
+
+func NewSHAKE128() hash.Hash {
+       return SHAKE{xof: sha3.NewSHAKE128(), l: 32}
+}
+
+func NewSHAKE256() hash.Hash {
+       return SHAKE{xof: sha3.NewSHAKE256(), l: 32}
+}
+
+func NewCSHAKE128(s []byte) hash.Hash {
+       return SHAKE{xof: sha3.NewCSHAKE128(nil, s), l: 32}
+}
+
+func NewCSHAKE256(s []byte) hash.Hash {
+       return SHAKE{xof: sha3.NewCSHAKE256(nil, s), l: 32}
+}
+
 func NewSHAKE128MerkleHasher(chunkLen, workers int) hash.Hash {
-       leafHash := sha3.NewCShake128(nil, []byte(merkle.Leaf))
-       nodeHash := sha3.NewCShake128(nil, []byte(merkle.Node))
        return merkle.NewHasher(
-               func() hash.Hash { return leafHash.Clone() },
-               func() hash.Hash { return nodeHash.Clone() },
-               func(hash.Hash) hash.Hash { return leafHash.Clone() },
-               func(hash.Hash) hash.Hash { return nodeHash.Clone() },
+               func() hash.Hash { return NewCSHAKE128([]byte(merkle.Leaf)) },
+               func() hash.Hash { return NewCSHAKE128([]byte(merkle.Node)) },
+               func(h hash.Hash) hash.Hash {
+                       h.Reset()
+                       return h
+               },
+               func(h hash.Hash) hash.Hash {
+                       h.Reset()
+                       return h
+               },
                chunkLen,
                workers,
        )
 }
 
 func NewSHAKE256MerkleHasher(chunkLen, workers int) hash.Hash {
-       leafHash := sha3.NewCShake256(nil, []byte(merkle.Leaf))
-       nodeHash := sha3.NewCShake256(nil, []byte(merkle.Node))
        return merkle.NewHasher(
-               func() hash.Hash { return leafHash.Clone() },
-               func() hash.Hash { return nodeHash.Clone() },
-               func(hash.Hash) hash.Hash { return leafHash.Clone() },
-               func(hash.Hash) hash.Hash { return nodeHash.Clone() },
+               func() hash.Hash { return NewCSHAKE256([]byte(merkle.Leaf)) },
+               func() hash.Hash { return NewCSHAKE256([]byte(merkle.Node)) },
+               func(h hash.Hash) hash.Hash {
+                       h.Reset()
+                       return h
+               },
+               func(h hash.Hash) hash.Hash {
+                       h.Reset()
+                       return h
+               },
                chunkLen,
                workers,
        )
index 57d8e0c25e16d5b6f3dee3c4bfd84cd339336c62c24826a37624546e6b4bd8b9..6e563ce61b53474fb4048e411d798a38f8878ceb0b7a64aa1214bf1362c4713f 100644 (file)
@@ -1,5 +1,5 @@
 module go.cypherpunks.su/keks/pki/sntrup4591761-x25519
 
-go 1.22
+go 1.24
 
 require github.com/companyzero/sntrup4591761 v0.0.0-20220309191932-9e0f3af2f07a
index 8bbde027ef77ac6f873da04465b7742340837fa34cd65ed88f866ae41e593dd9..9061b61950590f23b94ee4901aee0a477b7fb93ab4a1762d265758d1b1e64f2e 100644 (file)
@@ -129,14 +129,14 @@ Public key's identifier and @code{cid} should be calculated
 using BLAKE2b hash with 128 or 256 bit output length specified.
 
 @node cer-mceliece6960119-x25519
-@subsection cer with Classic McEliece 6960119+x25519
+@subsection cer with Classic McEliece 6960-119+x25519
 
-Certificate with combined Classic McEliece 6960119 and Curve25519
+Certificate with combined Classic McEliece 6960-119 and Curve25519
 public keys is used for KEM purposes, so should have "kem" key usage set.
 
 Its algorithm identifier is @code{mceliece6960119-x25519}. Its public key
-value is a concatenation of 1047319-byte mceliece6960119 public key and
-32-byte Curve25519 one.
+value is a concatenation of 1047319-byte @code{mceliece6960119} public key
+and 32-byte Curve25519 one.
 
 Public key's identifier and @code{cid} should be calculated
 using either SHAKE128 or SHAKE256 hash.