-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoldilocks.go
224 lines (169 loc) · 4.19 KB
/
goldilocks.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
package ed448
import (
"fmt"
"io"
"golang.org/x/crypto/sha3"
)
type PublicKey [57]byte
type PrivateKey [57]byte
func PointByPrivate(p PrivateKey) Point {
digest := [57]byte{}
sha3.ShakeSum256(digest[:], p[:])
clamp(digest[:])
r := NewScalar(digest[:])
r.Halve(r)
r.Halve(r)
h := PrecomputedScalarMul(r)
return h
}
func PointBySecret(p PrivateKey) Point {
digest := [57]byte{}
copy(digest[:], p[:])
clamp(digest[:])
r := NewScalar(digest[:])
r.Halve(r)
r.Halve(r)
h := PrecomputedScalarMul(r)
Zero := PrivateKey{0}
copy(digest[:], Zero[:])
return h
}
// SLICES TO KEYS
func BytesToPublicKey(key []byte) (pk PublicKey) {
if len(key) != len(PublicKey{}) {
return PublicKey{}
}
copy(pk[:], key)
return
}
func BytesToPrivateKey(key []byte) (pk PrivateKey) {
if len(key) != len(PrivateKey{}) {
return PrivateKey{}
}
copy(pk[:], key)
return
}
// DIFFIE-HELLMAN SHARED SECRET
func EdPrivateKeyToX448(edKey PrivateKey) [56]byte {
x := [56]byte{}
sha3.ShakeSum256(x[:], edKey[:])
return x
}
func EdPublicKeyToX448(edKey PublicKey) [56]byte {
return fromEdDSATox448(edKey[:])
}
func Ed448DeriveSecret(pubkey PublicKey, privkey PrivateKey) [56]byte {
var xpriv = [56]byte{0}
if privkey[57-1]&0x80 == 0x00 {
xpriv = EdPrivateKeyToX448(privkey)
} else {
copy(xpriv[:], privkey[0:56])
}
xpub := EdPublicKeyToX448(pubkey)
a, b := x448ScalarMul(xpub[:], xpriv[:])
if !b {
panic("Diffie-Hellman: result must not be zero")
}
return a
}
// PRIVATE, SECRET AND PUBLIC
func PrivateToSecret(pk PrivateKey) PrivateKey {
var sk PrivateKey
sha3.ShakeSum256(sk[:], pk[:])
sk[56] |= 0x80
return sk
}
func SecretToPublic(sk PrivateKey) PublicKey {
var s1 PrivateKey
copy(s1[:], sk[:])
clamp(s1[:])
r := NewScalar(s1[:])
r.Halve(r)
r.Halve(r)
h := PrecomputedScalarMul(r)
var pub PublicKey
p := h.EdDSAEncode()
copy(pub[:], p[:])
Zero := PrivateKey{0}
copy(s1[:], Zero[:])
return pub
}
func PrivateToPublic(privkey PrivateKey) PublicKey {
var pub PublicKey
p := PointByPrivate(privkey).EdDSAEncode()
copy(pub[:], p[:])
return pub
}
func Ed448DerivePublicKey(privkey PrivateKey) PublicKey {
if privkey[57-1]&0x80 == 0x00 {
return PrivateToPublic(privkey)
} else {
return SecretToPublic(privkey)
}
}
// CREATE SIGNATURE
func SignWithPrivate(privkey PrivateKey, message []byte) [114]byte {
return DSASign(privkey, PointByPrivate(privkey), message)
}
func SignSecretAndNonce(secret, n PrivateKey, msg []byte) [114]byte {
var s1 PrivateKey
copy(s1[:], secret[:])
clamp(s1[:])
pub := PointBySecret(secret)
sec := NewScalar(s1[:])
seed := n[:]
nonce := make([]byte, 114)
hashWithDom(nonce, append(seed, msg...))
nonceScalar := NewScalar(nonce[:])
nonceScalar2 := NewScalar()
nonceScalar2.Halve(nonceScalar)
nonceScalar2.Halve(nonceScalar2)
noncePoint := PrecomputedScalarMul(nonceScalar2).EdDSAEncode()
challenge := make([]byte, 114)
hashWithDom(challenge, append(append(noncePoint, pub.EdDSAEncode()...), msg...))
challengeScalar := NewScalar(challenge)
challengeScalar.Mul(challengeScalar, sec)
challengeScalar.Add(challengeScalar, nonceScalar)
var sig [114]byte
copy(sig[:], noncePoint)
copy(sig[57:], challengeScalar.Encode())
Zero := PrivateKey{0}
copy(s1[:], Zero[:])
return sig
}
func Ed448Sign(privkey PrivateKey, message []byte) [114]byte {
if privkey[57-1]&0x80 == 0x00 {
return SignWithPrivate(privkey, message)
} else {
var sk PrivateKey
copy(sk[:], privkey[:])
clamp(sk[:])
sig := SignSecretAndNonce(sk, sk, message)
// Erasing temporary values of private keys
var sZero = PrivateKey{0}
copy(sk[:], sZero[:])
return sig
}
}
// VERIFY SIGNATURE
func Ed448Verify(pubkey PublicKey, signature, message []byte) bool {
p := NewPoint([16]uint32{}, [16]uint32{}, [16]uint32{}, [16]uint32{})
if !p.EdDSADecode(pubkey[:]) {
return false
}
var sig [114]byte
copy(sig[:], signature[:])
return DSAVerify(sig, p, message)
}
// GENERATE PRIVATE KEY
func Ed448GenerateKey(reader io.Reader) (PrivateKey, error) {
key := new(PrivateKey)
n, err := io.ReadFull(reader, key[:])
if err != nil {
return PrivateKey{}, err
} else if n != 57 {
return PrivateKey{}, fmt.Errorf("not 57 random bytes")
}
key[56] &= 0x7f
return *key, nil
}