forked from FiloSottile/torchwood
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcosignature.go
More file actions
153 lines (134 loc) · 4.23 KB
/
Copy pathcosignature.go
File metadata and controls
153 lines (134 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package torchwood
import (
"crypto"
"crypto/ed25519"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"math"
"strings"
"time"
"unicode"
"unicode/utf8"
"golang.org/x/crypto/cryptobyte"
"golang.org/x/mod/sumdb/note"
)
const algCosignatureV1 = 4
// NewCosignatureSigner constructs a new [CosignatureSigner] from an Ed25519
// private key.
func NewCosignatureSigner(name string, key crypto.Signer) (*CosignatureSigner, error) {
if !isValidName(name) {
return nil, errors.New("invalid name")
}
k, ok := key.Public().(ed25519.PublicKey)
if !ok {
return nil, errors.New("key type is not Ed25519")
}
s := &CosignatureSigner{}
s.v.name = name
s.v.hash = keyHash(name, append([]byte{algCosignatureV1}, k...))
s.v.key = k
s.sign = func(msg []byte) ([]byte, error) {
t := uint64(time.Now().Unix())
m, err := formatCosignatureV1(t, msg)
if err != nil {
return nil, err
}
s, err := key.Sign(nil, m, crypto.Hash(0))
if err != nil {
return nil, err
}
// The signature itself is encoded as timestamp || signature.
sig := make([]byte, 0, 8+ed25519.SignatureSize)
sig = binary.BigEndian.AppendUint64(sig, t)
sig = append(sig, s...)
return sig, nil
}
s.v.verify = func(msg, sig []byte) bool {
if len(sig) != 8+ed25519.SignatureSize {
return false
}
t := binary.BigEndian.Uint64(sig)
sig = sig[8:]
m, err := formatCosignatureV1(t, msg)
if err != nil {
return false
}
return ed25519.Verify(k, m, sig)
}
return s, nil
}
func formatCosignatureV1(t uint64, msg []byte) ([]byte, error) {
// The signed message is in the following format
//
// cosignature/v1
// time TTTTTTTTTT
// [checkpoint]
//
// where TTTTTTTTTT is the current UNIX timestamp.
c, err := ParseCheckpoint(string(msg))
if err != nil {
return nil, fmt.Errorf("message being signed is not a valid checkpoint: %w", err)
}
if string(msg) != c.String() {
return nil, errors.New("message being signed does not match parsed checkpoint")
}
return []byte(fmt.Sprintf("cosignature/v1\ntime %d\n%s", t, msg)), nil
}
// CosignatureSigner is a [note.Signer] that produces timestamped
// cosignatures according to c2sp.org/tlog-cosignature.
type CosignatureSigner struct {
v CosignatureVerifier
sign func([]byte) ([]byte, error)
}
func (s *CosignatureSigner) Name() string { return s.v.Name() }
func (s *CosignatureSigner) KeyHash() uint32 { return s.v.KeyHash() }
func (s *CosignatureSigner) Sign(msg []byte) ([]byte, error) { return s.sign(msg) }
func (s *CosignatureSigner) Verifier() *CosignatureVerifier { return &s.v }
var _ note.Signer = &CosignatureSigner{}
// CosignatureVerifier is a [note.Verifier] that verifies cosignatures
// according to c2sp.org/tlog-cosignature.
type CosignatureVerifier struct {
verifier
key ed25519.PublicKey
}
var _ note.Verifier = &CosignatureVerifier{}
// String returns the vkey encoding of the verifier, according to
// c2sp.org/signed-note.
func (v *CosignatureVerifier) String() string {
return fmt.Sprintf("%s+%08x+%s", v.name, v.hash, base64.StdEncoding.EncodeToString(
append([]byte{algCosignatureV1}, v.key...)))
}
// isValidName reports whether name is valid.
// It must be non-empty and not have any Unicode spaces or pluses.
func isValidName(name string) bool {
return name != "" && utf8.ValidString(name) && strings.IndexFunc(name, unicode.IsSpace) < 0 && !strings.Contains(name, "+")
}
func keyHash(name string, key []byte) uint32 {
h := sha256.New()
h.Write([]byte(name))
h.Write([]byte("\n"))
h.Write(key)
sum := h.Sum(nil)
return binary.BigEndian.Uint32(sum)
}
// CosignatureTimestamp returns the timestamp of the cosignature, which is the
// time at which the witness signed the checkpoint.
func CosignatureTimestamp(sig note.Signature) (int64, error) {
sigBytes, err := base64.StdEncoding.DecodeString(sig.Base64)
if err != nil {
return 0, err
}
var timestamp uint64
s := cryptobyte.String(sigBytes)
if !s.Skip(4 /* key hash */) || !s.ReadUint64(×tamp) ||
timestamp > math.MaxInt64 {
return 0, errors.New("malformed cosignature")
}
return int64(timestamp), nil
}