-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey.go
184 lines (149 loc) · 3.34 KB
/
key.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
package delphi
import (
"bytes"
"crypto/ecdh"
"crypto/ed25519"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"slices"
)
const subKeySize = 32
// a subKey is either: a public encryption, public signing, private encryption, or private signing key
type subKey [subKeySize]byte
// a subKey is zero if all it's bytes are zero
func (s subKey) IsZero() bool {
for _, b := range s {
if b != 0 {
return false
}
}
return true
}
func (s subKey) Bytes() []byte {
return s[:]
}
// a Key is two (specifically one encryption and one signing) subKeys
type Key [2]subKey
func (k Key) MarshalText() ([]byte, error) {
return []byte(k.ToHex()), nil
}
// a Key is zero if all it's subKeys are zero
func (k Key) IsZero() bool {
return k[0].IsZero() && k[1].IsZero()
}
func (k Key) From(b []byte) Key {
var enc subKey
var sig subKey
copy(enc[:], b[:subKeySize])
copy(sig[:], b[subKeySize:])
var j Key
j[0] = enc
j[1] = sig
return j
}
// a KeyPair is two [Key]s. One public, one private
type KeyPair [2]Key
// a KeyPair is zero if all it's keys are zero
func (kp KeyPair) IsZero() bool {
return kp[0].IsZero() && kp[1].IsZero()
}
func (k Key) Bytes() []byte {
b := make([]byte, 2*subKeySize)
copy(b[:subKeySize], k[0][:])
copy(b[subKeySize:], k[1][:])
return b
}
func (k Key) ToInt64() int64 {
var num int64
buf := bytes.NewReader(k.Bytes())
err := binary.Read(buf, binary.BigEndian, &num)
if err != nil {
// Handle the error appropriately
}
return num
}
func (k Key) Equal(j Key) bool {
for i := range 2 {
jslice := j[i][:]
kslice := k[i][:]
same := slices.Equal(jslice, kslice)
if !same {
return false
}
// if !slices.Equal(k[i][:], j[i][:]) {
// return false
// }
}
return true
}
func (k Key) Signing() subKey {
return k[1]
}
func (k Key) Encryption() subKey {
return k[0]
}
func (k KeyPair) Bytes() []byte {
b := make([]byte, 4*subKeySize)
copy(b[:2*subKeySize], k[0].Bytes()) // public
copy(b[2*subKeySize:], k[1].Bytes()) // private
return b
}
func (k Key) ToHex() string {
return hex.EncodeToString(k.Bytes())
}
func KeyFromHex(str string) Key {
bin, err := hex.DecodeString(str)
if err != nil {
return Key{}
}
return KeyFromBytes(bin)
}
func KeyFromBytes(b []byte) Key {
gotSize := len(b)
wantSize := subKeySize * 2
if gotSize != wantSize {
panic(fmt.Sprintf("wrong length for key. Wanted %d but got %d", wantSize, gotSize))
}
k := Key{}
copy(k[0][:], b[:subKeySize])
copy(k[1][:], b[subKeySize:])
return k
}
func NewSubKey(randy io.Reader) subKey {
sk := subKey{}
randy.Read(sk[:])
return sk
}
func NewKey(randy io.Reader) Key {
return Key{NewSubKey(randy), NewSubKey(randy)}
}
// NewKeyPair generates valid ed25519 and X25519 keys
func NewKeyPair(randy io.Reader) KeyPair {
/**
* Layout:
* 1st 32 bytes: public encrpytion key
* 2nd 32 bytes: public signing key
* 3rd 32 bytes: private encryption key
* 4th 32 bytes: private signing key
**/
var kp KeyPair
// encryption keys
ed := ecdh.X25519()
encryptionPriv, err := ed.GenerateKey(randy)
if err != nil {
panic(err)
}
encryptionPub := encryptionPriv.PublicKey()
kp[0][0] = subKey(encryptionPub.Bytes())
kp[1][0] = subKey(encryptionPriv.Bytes())
// signing keys
signPub, signPriv, err := ed25519.GenerateKey(randy)
if err != nil {
panic(err)
}
kp[0][1] = subKey(signPub)
kp[1][1] = subKey(signPriv[:subKeySize])
return kp
}