-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher_suite.go
250 lines (208 loc) · 4.75 KB
/
cipher_suite.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
package mdoc
import (
"crypto/ecdh"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"errors"
"github.com/fxamacker/cbor/v2"
"github.com/veraison/go-cose"
"io"
)
var (
ErrUnsupportedCurve = errors.New("unsupported curve")
)
const (
CipherSuiteVersion = 1
)
type Curve int
const (
CurveP256 Curve = iota
CurveP384
CurveP521
CurveX25519
CurveX448
CurveEd25519
CurveEd448
CurveBrainpoolP256r1
CurveBrainpoolP320r1
CurveBrainpoolP384r1
CurveBrainpoolP512r1
)
var (
SupportedCurves = [...]Curve{
CurveP256,
CurveP384,
CurveP521,
CurveX25519,
CurveEd25519,
}
)
type SDeviceKeyMode int
const (
SDeviceKeyModeSign SDeviceKeyMode = iota
SDeviceKeyModeMAC
)
func NewSDeviceKey(rand io.Reader, curve Curve, mode SDeviceKeyMode) (DeviceKeyPrivate, error) {
if mode == SDeviceKeyModeSign {
var c elliptic.Curve
switch curve {
case CurveP256:
c = elliptic.P256()
case CurveP384:
c = elliptic.P384()
case CurveP521:
c = elliptic.P521()
case CurveEd25519:
_, key, err := ed25519.GenerateKey(rand)
if err != nil {
return nil, err
}
return (*deviceKeyPrivateEd25519)(&key), nil
default:
return nil, ErrUnsupportedCurve
}
key, err := ecdsa.GenerateKey(c, rand)
if err != nil {
return nil, err
}
return (*deviceKeyPrivateECDSA)(key), nil
}
if mode == SDeviceKeyModeMAC {
var c ecdh.Curve
switch curve {
case CurveP256:
c = ecdh.P256()
case CurveP384:
c = ecdh.P384()
case CurveP521:
c = ecdh.P521()
case CurveX25519:
c = ecdh.X25519()
default:
return nil, ErrUnsupportedCurve
}
key, err := c.GenerateKey(rand)
if err != nil {
return nil, err
}
return (*deviceKeyPrivateECDH)(key), nil
}
return nil, ErrUnsupportedCurve
}
func NewEDeviceKey(rand io.Reader, curve Curve) (DeviceKeyPrivate, error) {
return NewSDeviceKey(rand, curve, SDeviceKeyModeMAC)
}
type DeviceKey cose.Key
func (dk *DeviceKey) MarshalCBOR() ([]byte, error) {
return cbor.Marshal((*cose.Key)(dk))
}
func (dk *DeviceKey) UnmarshalCBOR(data []byte) error {
return cbor.Unmarshal(data, (*cose.Key)(dk))
}
func (dk *DeviceKey) publicKeyECDH() (*ecdh.PublicKey, error) {
if dk.Type != cose.KeyTypeEC2 {
return nil, ErrUnsupportedAlgorithm
}
crv, x, y, _ := (*cose.Key)(dk).EC2()
curve, err := coseCurveToECDH(crv)
if err != nil {
return nil, err
}
lenX := len(x)
point := make([]byte, 1+lenX+len(y))
point[0] = 0x04
copy(point[1:], x)
copy(point[1+lenX:], y)
return curve.NewPublicKey(point)
}
type DeviceKeyPrivate interface {
DeviceKey() (*DeviceKey, error)
}
type deviceKeyPrivateECDH ecdh.PrivateKey
func (dk *deviceKeyPrivateECDH) DeviceKey() (*DeviceKey, error) {
publicKey := (*ecdh.PrivateKey)(dk).PublicKey()
curve, err := ecdhCurveToCOSE(publicKey.Curve())
if err != nil {
return nil, err
}
bytes := publicKey.Bytes()
startY := len(bytes)/2 + 1
x := bytes[1:startY]
y := bytes[startY:]
return &DeviceKey{
Type: cose.KeyTypeEC2,
Params: map[any]any{
cose.KeyLabelEC2Curve: curve,
cose.KeyLabelEC2X: x,
cose.KeyLabelEC2Y: y,
},
}, nil
}
type deviceKeyPrivateECDSA ecdsa.PrivateKey
func (dk *deviceKeyPrivateECDSA) DeviceKey() (*DeviceKey, error) {
key := (*ecdsa.PrivateKey)(dk).PublicKey
curve, err := ellipticCurveToCose(key.Curve)
if err != nil {
return nil, err
}
return &DeviceKey{
Type: cose.KeyTypeEC2,
Params: map[any]any{
cose.KeyLabelEC2Curve: curve,
cose.KeyLabelEC2X: key.X.Bytes(),
cose.KeyLabelEC2Y: key.Y.Bytes(),
},
}, nil
}
type deviceKeyPrivateEd25519 ed25519.PrivateKey
func (dk *deviceKeyPrivateEd25519) DeviceKey() (*DeviceKey, error) {
key := (*ed25519.PrivateKey)(dk).Public().(ed25519.PublicKey)
return &DeviceKey{
Type: cose.KeyTypeOKP,
Params: map[any]any{
cose.KeyLabelOKPCurve: cose.CurveEd25519,
cose.KeyLabelOKPX: key,
},
}, nil
}
func coseCurveToECDH(curve cose.Curve) (ecdh.Curve, error) {
switch curve {
case cose.CurveP256:
return ecdh.P256(), nil
case cose.CurveP384:
return ecdh.P384(), nil
case cose.CurveP521:
return ecdh.P521(), nil
case cose.CurveX25519:
return ecdh.X25519(), nil
default:
return nil, ErrUnsupportedCurve
}
}
func ecdhCurveToCOSE(curve ecdh.Curve) (cose.Curve, error) {
switch curve {
case ecdh.P256():
return cose.CurveP256, nil
case ecdh.P384():
return cose.CurveP384, nil
case ecdh.P521():
return cose.CurveP521, nil
case ecdh.X25519():
return cose.CurveX25519, nil
default:
return cose.CurveReserved, ErrUnsupportedCurve
}
}
func ellipticCurveToCose(curve elliptic.Curve) (cose.Curve, error) {
switch curve {
case elliptic.P256():
return cose.CurveP256, nil
case elliptic.P384():
return cose.CurveP384, nil
case elliptic.P521():
return cose.CurveP521, nil
default:
return cose.CurveReserved, ErrUnsupportedCurve
}
}