-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathciphersuite.go
306 lines (250 loc) · 8.37 KB
/
ciphersuite.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Package ciphersuite defines the interfaces that Onet needs to setup
// a secure channel between the conodes. It is built around a cipher suite
// interface that provides the cryptographic primitives.
//
// The package also provides a cipher suite implementation that is using the
// Ed25519 signature scheme.
//
// As a server could use multiple cipher suites, the package implements a
// cipher registry that takes an implementation of a cipher suite and
// registered using the name of the suite.
//
// Public keys and signatures may need to be transmitted over the network and
// interfaces cannot be used as is. That is why every the different elements
// can be packed as CipherData. The registry provides functions to unpack
// them as the structure is self-contained.
package ciphersuite
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"golang.org/x/xerrors"
)
// encodedNameLengthSize defines the size in bytes of the name length
// when marshaling cipher data.
const encodedNameLengthSize = 32 / 8
// Name is the type that can differentiate multiple ciphers.
type Name = string
// Nameable binds a structure to a cipher.
type Nameable interface {
Name() Name
}
// CipherData is a self-contained message type that can be used
// over the network in the contrary of the interfaces.
type CipherData struct {
Data []byte
CipherName Name
}
// Name returns the name of the cipher suite compatible with the data
// contained in the raw structure.
func (d *CipherData) Name() Name {
return d.CipherName
}
func (d *CipherData) String() string {
buf := append([]byte(d.Name()), d.Data...)
return hex.EncodeToString(buf)
}
// Equal verifies if both self and other are deeply equal.
func (d *CipherData) Equal(other *CipherData) bool {
return d.Name() == other.Name() && bytes.Equal(d.Data, other.Data)
}
// Clone returns a clone of the cipher data.
func (d *CipherData) Clone() *CipherData {
data := make([]byte, len(d.Data))
copy(data, d.Data)
return &CipherData{
CipherName: d.Name(),
Data: data,
}
}
// WriteTo implements the io.WriteTo interface so that the cipher
// data can be written into any standard writer (e.g. hash).
func (d *CipherData) WriteTo(w io.Writer) (n int64, err error) {
var size int
size, err = w.Write([]byte(d.Name()))
n += int64(size)
if err != nil {
return n, xerrors.Errorf("writing name: %v", err)
}
size, err = w.Write(d.Data)
n += int64(size)
if err != nil {
return n, xerrors.Errorf("writing data: %v", err)
}
return n, nil
}
// MarshalText implements the encoding interface TextMarshaler so that
// it can be serialized in format such as TOML.
func (d *CipherData) MarshalText() ([]byte, error) {
name := []byte(d.Name())
size := make([]byte, encodedNameLengthSize)
binary.LittleEndian.PutUint32(size, uint32(len(name)))
// Buffer starts with the size of the cipher suite name, then the name
// and finally the data.
data := append(append(size, name...), d.Data...)
buf := make([]byte, hex.EncodedLen(len(data)))
hex.Encode(buf, data)
return buf, nil
}
// UnmarshalText implements the encoding interface TextUnmarshaler so that
// format such as TOML can deserialize the data.
func (d *CipherData) UnmarshalText(text []byte) error {
buf := make([]byte, hex.DecodedLen(len(text)))
_, err := hex.Decode(buf, text)
if err != nil {
return xerrors.Errorf("decoding hex: %v", err)
}
if len(buf) < encodedNameLengthSize {
return xerrors.Errorf("data is too small")
}
size := int(binary.LittleEndian.Uint32(buf[:encodedNameLengthSize]))
if len(buf) < encodedNameLengthSize+size {
return xerrors.Errorf("data is too small")
}
d.CipherName = string(buf[encodedNameLengthSize : encodedNameLengthSize+size])
d.Data = buf[encodedNameLengthSize+size:]
return nil
}
// RawPublicKey is a raw data structure of a public key implementation.
type RawPublicKey struct {
*CipherData
}
// NewRawPublicKey returns an instance of a public key.
func NewRawPublicKey(name Name, data []byte) *RawPublicKey {
return &RawPublicKey{
CipherData: &CipherData{
Data: data,
CipherName: name,
},
}
}
// Raw returns the raw data of a public key. It is implemented to allow
// a raw public key to be compatible with the interface.
func (raw *RawPublicKey) Raw() *RawPublicKey {
return raw
}
// Equal returns true when the two data structure contains the same public
// key.
func (raw *RawPublicKey) Equal(other PublicKey) bool {
data := other.Raw()
return data.CipherData.Equal(raw.CipherData)
}
// Clone returns a clone of the raw public key.
func (raw *RawPublicKey) Clone() *RawPublicKey {
return &RawPublicKey{CipherData: raw.CipherData.Clone()}
}
// UnmarshalText converts the raw public key back from a text marshaling.
func (raw *RawPublicKey) UnmarshalText(text []byte) error {
raw.CipherData = &CipherData{}
err := raw.CipherData.UnmarshalText(text)
if err != nil {
return xerrors.Errorf("unmarshaling cipher data: %v", err)
}
return nil
}
// RawSecretKey is a raw data structure of a secret key implementation.
type RawSecretKey struct {
*CipherData
}
// NewRawSecretKey returns an instance of a raw secret key.
func NewRawSecretKey(name Name, data []byte) *RawSecretKey {
return &RawSecretKey{
CipherData: &CipherData{
CipherName: name,
Data: data,
},
}
}
// Raw returns the raw data of a secret key. It is implemented to allow
// a raw secret key to be compatible with the interface.
func (raw *RawSecretKey) Raw() *RawSecretKey {
return raw
}
// Clone makes a clone of the secret key.
func (raw *RawSecretKey) Clone() *RawSecretKey {
return &RawSecretKey{CipherData: raw.CipherData.Clone()}
}
// UnmarshalText converts the raw secret key back from a text marshaling.
func (raw *RawSecretKey) UnmarshalText(text []byte) error {
raw.CipherData = &CipherData{}
err := raw.CipherData.UnmarshalText(text)
if err != nil {
return xerrors.Errorf("unmarshaling cipher data: %v", err)
}
return nil
}
// RawSignature is a raw data structure of a signature implementation.
type RawSignature struct {
*CipherData
}
// NewRawSignature returns an instance of a raw signature.
func NewRawSignature(name Name, data []byte) *RawSignature {
return &RawSignature{
CipherData: &CipherData{
CipherName: name,
Data: data,
},
}
}
// Raw returns the raw data of a signature. It is implemented to allow
// a raw signature to be compatible with the interface.
func (raw *RawSignature) Raw() *RawSignature {
return raw
}
// Clone returns a clone of a raw signature.
func (raw *RawSignature) Clone() *RawSignature {
return &RawSignature{CipherData: raw.CipherData.Clone()}
}
// UnmarshalText converts the raw signature back from a text marshaling.
func (raw *RawSignature) UnmarshalText(text []byte) error {
raw.CipherData = &CipherData{}
err := raw.CipherData.UnmarshalText(text)
if err != nil {
return xerrors.Errorf("unmarshaling cipher data: %v", err)
}
return nil
}
// PublicKey represents one of the two sides of an asymmetric key pair
// which can be safely shared publicly.
type PublicKey interface {
Nameable
fmt.Stringer
Raw() *RawPublicKey
Equal(other PublicKey) bool
}
// SecretKey represents one of the two sides of an asymmetric key pair
// which must remain private.
type SecretKey interface {
Nameable
fmt.Stringer
Raw() *RawSecretKey
}
// Signature represents a signature produced using a secret key and
// that can be verified with the associated public key.
type Signature interface {
Nameable
fmt.Stringer
Raw() *RawSignature
}
// CipherSuite provides the primitive needed to create and verify
// signatures using an asymmetric key pair.
type CipherSuite interface {
Nameable
// PublicKey must return an implementation of a public key.
PublicKey(raw *RawPublicKey) (PublicKey, error)
// SecretKey must return an implementation of a secret key.
SecretKey(raw *RawSecretKey) (SecretKey, error)
// Signature must return an implementation of a signature.
Signature(raw *RawSignature) (Signature, error)
// GenerateKeyPair must return a random secret key and its associated public key.
GenerateKeyPair(reader io.Reader) (PublicKey, SecretKey, error)
// Sign must produce a signature that can be validated by the
// associated public key of the secret key.
Sign(sk SecretKey, msg []byte) (Signature, error)
// Verify must return nil when the signature is valid for the
// message and the public key. Otherwise it should return the
// reason of the invalidity.
Verify(pk PublicKey, signature Signature, msg []byte) error
}