-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecaf_curve.go
207 lines (163 loc) · 4.79 KB
/
decaf_curve.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
package ed448
import (
"errors"
"io"
"golang.org/x/crypto/sha3"
)
func decafPseudoRandomFunction(sym []byte) []byte {
hash := sha3.NewShake256()
hash.Write(sym[:])
hash.Write([]byte("decaf_448_derive_private_key"))
var out [64]byte
hash.Read(out[:])
return out[:]
}
func (c *decafCurveT) decafDerivePrivateKey(sym [symKeyBytes]byte) (*privateKey, error) {
k := &privateKey{}
copy(k.symKey(), sym[:])
skb := decafPseudoRandomFunction(sym[:])
secretKey := &scalar{}
barrettDeserializeAndReduce(secretKey[:], skb, &curvePrimeOrder)
err := secretKey.encode(k.secretKey())
if err != nil {
return nil, err
}
publicKey := precomputedScalarMul(secretKey)
publicKey.decafEncode(k.publicKey())
return k, nil
}
func (c *decafCurveT) decafGenerateKeys(r io.Reader) (k *privateKey, err error) {
symKey, err := generateSymmetricKey(r)
if err != nil {
return
}
return c.decafDerivePrivateKey(symKey)
}
func (c *decafCurveT) decafComputeSecret(myPriv *privateKey, yourPub [fieldBytes]byte) ([]byte, word) {
var delta, less uint16
var ser [fieldBytes]byte
var sk scalar
invalid := "decaf_448_ss_invalid"
priv := myPriv.secretKey()
pub := myPriv.publicKey()
sym := myPriv.symKey()
_ = barrettDeserializeReturnMask(sk[:], priv, &curvePrimeOrder)
// Lexsort keys. Less will be -1 if mine is less, and 0 otherwise.
for i := uintZero; i < fieldBytes; i++ {
delta = uint16(pub[i])
delta -= uint16(yourPub[i])
// Case:
// = -> delta = 0 -> hi delta-1 = -1, hi delta = 0
// > -> delta > 0 -> hi delta-1 = 0, hi delta = 0
// < -> delta < 0 -> hi delta-1 = (doesnt matter), hi delta = -1
less &= delta - 1
less |= delta
}
less >>= 8
// update the lesser
for j := uintZero; j < fieldBytes; j++ {
ser[j] = uint8(uint16(pub[j])&less) | uint8(uint16(yourPub[j])&^less) // check
}
hash := sha3.NewShake256()
hash.Write(ser[:])
// update the greater
for k := uintZero; k < fieldBytes; k++ {
ser[k] = uint8(uint16(pub[k])&^less | uint16(yourPub[k])&less)
}
hash.Write(ser[:])
ser, ok := directPointScalarMul(yourPub, &sk, decafFalse)
// If invalid, replace
for l := uintZero; l < fieldBytes; l++ {
ser[l] &= uint8(ok)
if l < wordBits {
ser[l] |= sym[l] & ^uint8(ok)
} else if l-wordBits < uint(len(invalid)) {
ser[l] |= invalid[l-wordBits] & ^uint8(ok)
}
}
hash.Write(ser[:])
var shared [fieldBytes]byte
hash.Read(shared[:])
//TODO: should we wipe ser bytes?
return shared[:], ok
}
func decafDeriveNonce(msg []byte, symKey []byte) *scalar {
h := sha3.NewShake256()
h.Write(msg)
h.Write(symKey)
h.Write([]byte("decaf_448_sign_shake"))
var out [64]byte
h.Read(out[:])
dst := &scalar{}
barrettDeserializeAndReduce(dst[:], out[:], &curvePrimeOrder)
return dst
}
func decafDeriveChallenge(pubKey []byte, tmpSignature [fieldBytes]byte, msg []byte) *scalar {
h := sha3.NewShake256()
h.Write(msg)
h.Write(pubKey)
h.Write(tmpSignature[:])
var out [64]byte
h.Read(out[:])
dst := &scalar{}
barrettDeserializeAndReduce(dst[:], out[:], &curvePrimeOrder)
return dst
}
func (c *decafCurveT) decafDeriveTemporarySignature(nonce *scalar) (dst [fieldBytes]byte) {
point := precomputedScalarMul(nonce)
point.decafEncode(dst[:])
return
}
func (c *decafCurveT) decafSign(msg []byte, k *privateKey) (sig [signatureBytes]byte, err error) {
secretKeyWords := &scalar{}
//TODO: should secret words be destroyed?
if ok := barrettDeserialize(secretKeyWords[:], k.secretKey(), &curvePrimeOrder); !ok {
err = errors.New("Corrupted private key")
return
}
nonce := decafDeriveNonce(msg, k.symKey())
tmpSignature := c.decafDeriveTemporarySignature(nonce)
challenge := decafDeriveChallenge(k.publicKey(), tmpSignature, msg)
challenge.mul(challenge, secretKeyWords)
nonce.sub(nonce, challenge)
copy(sig[:fieldBytes], tmpSignature[:])
nonce.encode(sig[fieldBytes:])
//TODO: should nonce and challenge be destroyed?
return
}
func (c *decafCurveT) decafVerify(signature [signatureBytes]byte, msg []byte, k *publicKey) (bool, error) {
serPubkey := serialized(*k)
tmpSig := [fieldBytes]byte{}
copy(tmpSig[:], signature[:])
challenge := decafDeriveChallenge(serPubkey[:], tmpSig, msg)
point := &twExtendedPoint{
x: &bigNumber{},
y: &bigNumber{},
z: &bigNumber{},
t: &bigNumber{},
}
pkPoint := &twExtendedPoint{
x: &bigNumber{},
y: &bigNumber{},
z: &bigNumber{},
t: &bigNumber{},
}
ret, err := decafDecodeOld(point, tmpSig, true)
if err != nil {
return false, err
}
ret1, err := decafDecodeOld(pkPoint, serPubkey, false)
if err != nil {
return false, err
}
// TODO: hacky. FIX ME.
ret &= ret1
response := &scalar{}
ret &= response.decode(signature[56:])
pkPoint = decafDoubleNonSecretScalarMul(pkPoint, response, challenge)
ret &= pkPoint.equals(point)
if ret != word(lmask) {
return false, errors.New("unable to verify given signature")
}
return true, nil
}