]> Cypherpunks repositories - keks.git/commitdiff
Simplify cm/hashed
authorSergey Matveev <stargrave@stargrave.org>
Thu, 5 Jun 2025 13:01:19 +0000 (16:01 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 5 Jun 2025 13:01:19 +0000 (16:01 +0300)
13 files changed:
go/cm/cmd/cmhshtool/main.go
go/cm/cmd/cmsigtool/main.go
go/cm/hash/.gitignore [new file with mode: 0644]
go/cm/hash/hash.go [deleted file]
go/cm/hash/prehash.go [new file with mode: 0644]
go/cm/sign/signed.go
go/cm/utils/mk-bin
spec/cm/hashed/index
spec/cm/signed/index
tcl/schemas/hashed.tcl
tcl/schemas/prehash.tcl [new file with mode: 0644]
tcl/schemas/signed-prehash.tcl [deleted file]
tcl/schemas/signed.tcl

index e4ddc5bc767947923c62a76191fe0b91e9811048e601a3474c76c5d0ea240f68..d8119edda3f20d982938ee0069bcbac2946360758cc2d57279c072225e2b3c53 100644 (file)
@@ -75,10 +75,12 @@ func main() {
        if err != nil {
                log.Fatal(err)
        }
-       s := cmhash.Hashed{
-               Algo: []string{*algo},
-               Typ:  "data",
-               Hash: [][]byte{hasher.Sum(nil)},
+       _, err = keks.Encode(os.Stdout, cmhash.Magic, nil)
+       if err != nil {
+               log.Fatal(err)
+       }
+       _, err = keks.Encode(os.Stdout, map[string][]byte{*algo: hasher.Sum(nil)}, nil)
+       if err != nil {
+               log.Fatal(err)
        }
-       keks.Encode(os.Stdout, s, nil)
 }
index 81a48b892e327e7bccab85c570452ae9e188e35f4b5b5fde212efcf817f58603..af99ab004b7e88be677a030e16102059499a570895ff6b211a2e4868dd1b7037 100644 (file)
@@ -104,17 +104,17 @@ func main() {
                if err != nil {
                        log.Fatal(err)
                }
-               var prehash sign.Prehash
-               err = schema.Check("prehash", sign.SignedSchemas, v)
+               var prehash cmhash.Prehash
+               err = schema.Check("prehash", cmhash.PrehashSchemas, v)
                if err == nil {
                        err = decoder.UnmarshalStruct(&prehash)
                }
                var signed sign.Signed
                hashers := make(map[string]hash.Hash)
-               if err == nil && prehash.T == mode.PrehashT {
-                       dsts := make([]io.Writer, 0, len(prehash.Sigs)+1)
+               if err == nil && prehash.T == cmhash.PrehashT {
+                       dsts := make([]io.Writer, 0, len(prehash.Algos)+1)
                        dsts = append(dsts, os.Stdout)
-                       for algo := range prehash.Sigs {
+                       for algo := range prehash.Algos {
                                hasher := cmhash.ByName(algo)
                                if hasher == nil {
                                        log.Fatalln("prehash: unsupported algorithm:", algo)
@@ -245,9 +245,9 @@ func main() {
                                log.Fatal(err)
                        }
                } else {
-                       if _, err = keks.Encode(os.Stdout, sign.Prehash{
-                               T:    mode.PrehashT,
-                               Sigs: map[string]*struct{}{signer.Algo(): nil},
+                       if _, err = keks.Encode(os.Stdout, cmhash.Prehash{
+                               T:     cmhash.PrehashT,
+                               Algos: map[string]*struct{}{signer.Algo(): nil},
                        }, nil); err != nil {
                                log.Fatal(err)
                        }
diff --git a/go/cm/hash/.gitignore b/go/cm/hash/.gitignore
new file mode 100644 (file)
index 0000000..43d54db
--- /dev/null
@@ -0,0 +1 @@
+/prehash.schema.keks
diff --git a/go/cm/hash/hash.go b/go/cm/hash/hash.go
deleted file mode 100644 (file)
index 2491f4b..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-package hash
-
-type Hashed struct {
-       Algo []string `keks:"a"`
-       Typ  string   `keks:"t"`
-       Hash [][]byte `keks:"hash"`
-}
diff --git a/go/cm/hash/prehash.go b/go/cm/hash/prehash.go
new file mode 100644 (file)
index 0000000..bf52813
--- /dev/null
@@ -0,0 +1,48 @@
+// GoKEKS/CM -- KEKS-encoded cryptographic messages
+// Copyright (C) 2024-2025 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+package hash
+
+import (
+       _ "embed"
+
+       "go.cypherpunks.su/keks"
+       "go.cypherpunks.su/keks/schema"
+)
+
+const PrehashT = "prehash"
+
+type Prehash struct {
+       T     string               `keks:"t"`
+       Algos map[string]*struct{} `keks:"algos"`
+}
+
+//go:embed prehash.schema.keks
+var PrehashSchemasRaw []byte
+
+var PrehashSchemas map[string][][]any
+
+func init() {
+       var magic keks.Magic
+       magic, PrehashSchemasRaw = keks.StripMagic(PrehashSchemasRaw)
+       if magic != schema.Magic {
+               panic("wrong magic in prehash.schema.keks")
+       }
+       if err := keks.NewDecoderFromBytes(
+               PrehashSchemasRaw, nil,
+       ).DecodeStruct(&PrehashSchemas); err != nil {
+               panic(err)
+       }
+}
index d5c03fbb6399bab1768fdd5bd6d28ae684b071384135af6ca7c6d75aa994dc8a..1b7e41ebcb47eb2f2eb83ab298400514c9413f4cd94aa2421d3329da4a7f0df0 100644 (file)
@@ -32,11 +32,6 @@ import (
 
 const SignedMagic = keks.Magic("cm/signed")
 
-type Prehash struct {
-       Sigs map[string]*struct{} `keks:"sigs"`
-       T    string               `keks:"t"`
-}
-
 type Load struct {
        V *any   `keks:"v,omitempty"`
        T string `keks:"t"`
index ee594388588bc4b4f8cda132846950cfdeb89bdf87723e62b9a7f1c6cc9d2e27..027a42294b0ad444ae22491fbcc98daf4524303887bdad9de576b920ad60ed99 100755 (executable)
@@ -8,7 +8,11 @@
 GO_LDFLAGS="${GO_LDFLAGS:--s}"
 root="$(dirname "$(realpath -- "$0")")"
 cd "$root/.."
-redo-ifchange sign/signed.schema.keks sign/pub.schema.keks enc/encrypted.schema.keks
+redo-ifchange \
+    enc/encrypted.schema.keks \
+    hash/prehash.schema.keks \
+    sign/pub.schema.keks \
+    sign/signed.schema.keks
 mkdir -p bin
 for cmd in enc hsh key sig ; do
     cmd=cm${cmd}tool
index fe01c5d9de41f9e11af6f883ae20650452203901f5c43e6b4fb34b320e63f69e..17db7aae8b62fdd45f633577145b6a9c1e54c07f16654a5b23678daf75a9fc1d 100644 (file)
@@ -6,6 +6,11 @@ Stored in a file, it should begin with "cm/hashed" [encoding/MAGIC].
 
 <<    [schemas/hashed.tcl]\r
 
-"/a" tells what algorithms will be used to hash the data.
-"/t" tells the type of the data inside.
-"/hash" contains the hash values for all corresponding "/a" algorithms.
+It is just a single map of algorithm identifiers with hashes.
+
+Hashed data is provided any way you wish. Consider using "prehash"
+structure similarly as [cm/signed/] does:
+
+<<    [schemas/prehash.tcl]\r
+
+    prehash || BLOB(data) || cm/hashed
index 48d67820985aec5a5cc42db087afdcfddb80f68b2f5a77baeefd383215083157..03a13a55dbe282b3ae1093caeacf6c23e7299cec711fa15d764a7af8ce746641 100644 (file)
@@ -30,10 +30,11 @@ following approach:
 
     prehash || BLOB(detached-data) || cm/signed
 
-<<    [schemas/signed-prehash.tcl]\r
+<<    [schemas/prehash.tcl]\r
 
 With "prehash" you initialise your hashers used during signing process
 and feed BLOB's contents (not the encoded BLOB itself!) into the them.
+prehash'es /algos must contain /sigs/*/sign/a identifiers:
 
 "/sigs/*/tbs/when" is optional signing time.
 
index 1a10da88e7d678a42f5a1c69900e54e3f0648eafbb04757736240a077192aa77..7504f19b21d36c6481926414f9adaeec7d8a465725f0f82af413d5936fa1efe0 100644 (file)
@@ -1,8 +1 @@
-ai {{field . {str} >0}}
-
-hashed {
-    {field a {list} {of ai} >0}
-    {field t {str} >0}
-    {field v {bin blob} optional}
-    {field hash {list} {of bin} >0}
-}
+hashed {{field . {map} {of bin} >0}}
diff --git a/tcl/schemas/prehash.tcl b/tcl/schemas/prehash.tcl
new file mode 100644 (file)
index 0000000..312b92c
--- /dev/null
@@ -0,0 +1,4 @@
+prehash {
+    {field t {str} =prehash}
+    {field algos {set} >0} {# set of hash algorithm identifiers}
+}
diff --git a/tcl/schemas/signed-prehash.tcl b/tcl/schemas/signed-prehash.tcl
deleted file mode 100644 (file)
index 5170544..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-prehash {
-    {field t {str} =prehash}
-    {field sigs {set} >0} {# set of /sigs/*/sign/a}
-}
index 8748d86e38f04687a7e213fc36c139cf36581e12d29fc7963bc51a2c63abfe5d..1d9160a8ec8911713ba74fd915b516bda0dce5a97bb5fbf270f56021f3f710c2 100644 (file)
@@ -27,5 +27,3 @@ tbs {
     {# recipient's fingerprints}
     {field encrypted-to {list} {of fpr} >0 optional}
 }
-
-schema-include signed-prehash.tcl