/
saltpack.go
239 lines (189 loc) · 5.84 KB
/
saltpack.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"bytes"
"fmt"
"github.com/keybase/client/go/kbcrypto"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/saltpack"
"golang.org/x/crypto/ed25519"
"golang.org/x/crypto/nacl/box"
)
// Wrap types from naclwrap.go in saltpack interfaces.
type naclBoxPublicKey NaclDHKeyPublic
var _ saltpack.BoxPublicKey = naclBoxPublicKey{}
func (b naclBoxPublicKey) ToKID() []byte {
return b[:]
}
func (b naclBoxPublicKey) ToRawBoxKeyPointer() *saltpack.RawBoxKey {
return (*saltpack.RawBoxKey)(&b)
}
func (b naclBoxPublicKey) CreateEphemeralKey() (saltpack.BoxSecretKey, error) {
kp, err := GenerateNaclDHKeyPair()
if err != nil {
return nil, err
}
return naclBoxSecretKey(kp), nil
}
func (b naclBoxPublicKey) HideIdentity() bool {
return false
}
type hiddenNaclBoxPublicKey NaclDHKeyPublic
func (b hiddenNaclBoxPublicKey) ToKID() []byte {
return b[:]
}
func (b hiddenNaclBoxPublicKey) ToRawBoxKeyPointer() *saltpack.RawBoxKey {
return (*saltpack.RawBoxKey)(&b)
}
func (b hiddenNaclBoxPublicKey) CreateEphemeralKey() (saltpack.BoxSecretKey, error) {
kp, err := GenerateNaclDHKeyPair()
if err != nil {
return nil, err
}
return naclBoxSecretKey(kp), nil
}
func (b hiddenNaclBoxPublicKey) HideIdentity() bool {
return true
}
type naclBoxPrecomputedSharedKey [32]byte
var _ saltpack.BoxPrecomputedSharedKey = naclBoxPrecomputedSharedKey{}
func (k naclBoxPrecomputedSharedKey) Unbox(nonce saltpack.Nonce, msg []byte) (
[]byte, error) {
ret, ok := box.OpenAfterPrecomputation(
[]byte{}, msg, (*[24]byte)(&nonce), (*[32]byte)(&k))
if !ok {
return nil, DecryptionError{}
}
return ret, nil
}
func (k naclBoxPrecomputedSharedKey) Box(nonce saltpack.Nonce, msg []byte) []byte {
ret := box.SealAfterPrecomputation([]byte{}, msg, (*[24]byte)(&nonce), (*[32]byte)(&k))
return ret
}
type naclBoxSecretKey NaclDHKeyPair
var _ saltpack.BoxSecretKey = naclBoxSecretKey{}
func (n naclBoxSecretKey) Box(
receiver saltpack.BoxPublicKey, nonce saltpack.Nonce, msg []byte) []byte {
ret := box.Seal([]byte{}, msg, (*[24]byte)(&nonce),
(*[32]byte)(receiver.ToRawBoxKeyPointer()),
(*[32]byte)(n.Private))
return ret
}
func (n naclBoxSecretKey) Unbox(
sender saltpack.BoxPublicKey, nonce saltpack.Nonce, msg []byte) (
[]byte, error) {
ret, ok := box.Open([]byte{}, msg, (*[24]byte)(&nonce),
(*[32]byte)(sender.ToRawBoxKeyPointer()),
(*[32]byte)(n.Private))
if !ok {
return nil, DecryptionError{}
}
return ret, nil
}
func (n naclBoxSecretKey) GetPublicKey() saltpack.BoxPublicKey {
return naclBoxPublicKey(n.Public)
}
func (n naclBoxSecretKey) Precompute(
sender saltpack.BoxPublicKey) saltpack.BoxPrecomputedSharedKey {
var res naclBoxPrecomputedSharedKey
box.Precompute((*[32]byte)(&res),
(*[32]byte)(sender.ToRawBoxKeyPointer()),
(*[32]byte)(n.Private))
return res
}
// A secret key also functions as a keyring with a single key.
type naclKeyring naclBoxSecretKey
var _ saltpack.Keyring = naclKeyring{}
func (n naclKeyring) LookupBoxSecretKey(
kids [][]byte) (int, saltpack.BoxSecretKey) {
sk := (naclBoxSecretKey)(n)
pkKid := sk.GetPublicKey().ToKID()
for i, kid := range kids {
if bytes.Equal(pkKid, kid) {
return i, sk
}
}
return -1, nil
}
func (n naclKeyring) LookupBoxPublicKey(kid []byte) saltpack.BoxPublicKey {
var pk naclBoxPublicKey
if len(kid) != len(pk) {
return nil
}
copy(pk[:], kid)
return pk
}
func (n naclKeyring) GetAllBoxSecretKeys() []saltpack.BoxSecretKey {
return []saltpack.BoxSecretKey{naclBoxSecretKey(n)}
}
func (n naclKeyring) ImportBoxEphemeralKey(kid []byte) saltpack.BoxPublicKey {
return n.LookupBoxPublicKey(kid)
}
func (n naclKeyring) CreateEphemeralKey() (saltpack.BoxSecretKey, error) {
kp, err := GenerateNaclDHKeyPair()
if err != nil {
return nil, err
}
return naclBoxSecretKey(kp), nil
}
func (n naclKeyring) LookupSigningPublicKey(kid []byte) saltpack.SigningPublicKey {
if len(kid) != ed25519.PublicKeySize {
return nil
}
keyBytes := [ed25519.PublicKeySize]byte{}
copy(keyBytes[:], kid)
return saltSignerPublic{kbcrypto.NaclSigningKeyPublic(keyBytes)}
}
// An empty keyring just for generating ephemeral keys.
type emptyKeyring struct{}
var _ saltpack.Keyring = emptyKeyring{}
func (e emptyKeyring) LookupBoxSecretKey(kids [][]byte) (int, saltpack.BoxSecretKey) {
panic("unimplemented")
}
func (e emptyKeyring) LookupBoxPublicKey(kid []byte) saltpack.BoxPublicKey {
panic("unimplemented")
}
func (e emptyKeyring) GetAllBoxSecretKeys() []saltpack.BoxSecretKey {
panic("unimplemented")
}
func (e emptyKeyring) ImportBoxEphemeralKey(kid []byte) saltpack.BoxPublicKey {
panic("unimplemented")
}
func (e emptyKeyring) CreateEphemeralKey() (saltpack.BoxSecretKey, error) {
kp, err := GenerateNaclDHKeyPair()
if err != nil {
return nil, err
}
return naclBoxSecretKey(kp), nil
}
func BoxPublicKeyToKeybaseKID(k saltpack.BoxPublicKey) (ret keybase1.KID) {
if k == nil {
return ret
}
p := k.ToKID()
return keybase1.KIDFromRawKey(p, byte(kbcrypto.KIDNaclDH))
}
func checkSaltpackBrand(b string) error {
// Everything is awesome!
return nil
}
func SaltpackVersionFromArg(saltpackVersionArg int) (saltpack.Version, error) {
if saltpackVersionArg == 0 {
return CurrentSaltpackVersion(), nil
}
// The arg specifies the major version we want, and the minor version is
// assumed to be the latest available. Make a map to accomplish that.
majorVersionMap := map[int]saltpack.Version{}
for _, v := range saltpack.KnownVersions() {
latestPair, found := majorVersionMap[v.Major]
if !found || (v.Minor > latestPair.Minor) {
majorVersionMap[v.Major] = v
}
}
ret, found := majorVersionMap[saltpackVersionArg]
if !found {
return saltpack.Version{}, fmt.Errorf("failed to find saltpack major version %d", saltpackVersionArg)
}
return ret, nil
}