/
xmss.go
314 lines (284 loc) · 7.51 KB
/
xmss.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) 2018 Aidos Developer
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package xmss
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"github.com/vmihailenco/msgpack"
)
//PrivKey is a private key of XMSS.
type PrivKey struct {
msgPRF *prf //SK_PRF in draft, used to get hash of index(randomness r in draft) when signing by XMSS.
wotsPRF *prf //S in draft, used to generate private key elements of WOTS.
pubPRF *prf //SEED in draft , used to make public keys of WOTS.
root []byte
}
type privkey struct {
MsgSeed []byte
WotsSeed []byte
PubSeed []byte
Root []byte
}
func (x *PrivKey) exports() *privkey {
return &privkey{
MsgSeed: x.msgPRF.seed,
WotsSeed: x.wotsPRF.seed,
PubSeed: x.pubPRF.seed,
Root: x.root,
}
}
func (x *PrivKey) imports(s *privkey) {
x.msgPRF = newPRF(s.MsgSeed)
x.wotsPRF = newPRF(s.WotsSeed)
x.pubPRF = newPRF(s.PubSeed)
x.root = s.Root
}
//MarshalJSON marshals PrivKey into valid JSON.
func (x *PrivKey) MarshalJSON() ([]byte, error) {
return json.Marshal(x.exports())
}
//UnmarshalJSON unmarshals JSON to PrivKey.
func (x *PrivKey) UnmarshalJSON(b []byte) error {
var s privkey
err := json.Unmarshal(b, &s)
if err == nil {
x.imports(&s)
}
return err
}
//EncodeMsgpack marshals PrivKey into valid msgpack.
func (x *PrivKey) EncodeMsgpack(enc *msgpack.Encoder) error {
return enc.Encode(x.exports())
}
//DecodeMsgpack unmarshals msgpack to PrivKey.
func (x *PrivKey) DecodeMsgpack(dec *msgpack.Decoder) error {
var s privkey
if err := dec.Decode(&s); err != nil {
return err
}
x.imports(&s)
return nil
}
func (x *PrivKey) newWotsPrivKey(addrs addr, priv wotsPrivKey) {
s := make([]byte, 32)
x.wotsPRF.sum(addrs, s)
p := newPRF(s)
for i := range priv {
p.sumInt(uint32(i), priv[i])
}
}
//PublicKey for xmss
type PublicKey struct {
Height byte
Root []byte
Seed []byte
}
//Serialize returns serialized bytes of XMSS PublicKey.
func (p *PublicKey) Serialize() []byte {
key := make([]byte, 1+n+n)
key[0] = p.Height
copy(key[1:], p.Root)
copy(key[1+n:], p.Seed)
return key
}
//DeserializePK deserialized bytes to XMSS PublicKey.
func DeserializePK(key []byte) (*PublicKey, error) {
if len(key) != 65 {
return nil, errors.New("invalid bytes length")
}
return &PublicKey{
Height: key[0],
Root: key[1:33],
Seed: key[33:65],
}, nil
}
func randHash(left, right []byte, p *prf, addrs addr, out []byte) {
addrs.set(adrKM, 0)
key := make([]byte, 32)
p.sum(addrs, key)
addrs.set(adrKM, 1)
bm0 := make([]byte, 32)
p.sum(addrs, bm0)
addrs.set(adrKM, 2)
bm1 := make([]byte, 32)
p.sum(addrs, bm1)
lxor := make([]byte, 32)
xorWords(lxor, left, bm0)
rxor := make([]byte, 32)
xorWords(rxor, right, bm1)
hashH(key, lxor, rxor, out)
}
func (pk wotsPubKey) ltree(p *prf, addrs addr) []byte {
var height uint32
addrs.set(adrHeight, 0)
var l uint32
for l = wlen; l > 1; l = (l >> 1) + (l & 0x1) {
var i uint32
for i = 0; i < l>>1; i++ {
addrs.set(adrIndex, i)
randHash(pk[2*i], pk[2*i+1], p, addrs, pk[i])
}
if l&0x1 == 1 {
copy(pk[l>>1], pk[l-1])
}
height++
addrs.set(adrHeight, height)
}
return pk[0]
}
type xmssSigBody struct {
sig wotsSig
auth [][]byte
}
type xmssSig struct {
idx uint32
r []byte
*xmssSigBody
}
func (x *xmssSig) bytes() []byte {
sigSize := 4 + n + wlen*n + len(x.auth)*n
sig := make([]byte, sigSize)
binary.BigEndian.PutUint32(sig, x.idx)
copy(sig[4:], x.r)
sigBody := x.xmssSigBody.bytes()
copy(sig[4+n:], sigBody)
return sig
}
func (x *xmssSigBody) bytes() []byte {
sigSize := wlen*n + len(x.auth)*n
sig := make([]byte, sigSize)
for i, s := range x.sig {
copy(sig[i*n:], s)
}
for i, s := range x.auth {
copy(sig[wlen*n+i*n:], s)
}
return sig
}
func bytes2sig(b []byte, h byte) (*xmssSig, error) {
height := (len(b) - (4 + n + wlen*n)) >> 5
if height != int(h) {
return nil, errors.New("invalid length of bytes")
}
body := bytes2sigBody(b[4+n:], height)
sig := &xmssSig{
idx: binary.BigEndian.Uint32(b),
r: b[4 : 4+n],
xmssSigBody: body,
}
return sig, nil
}
func bytes2sigBody(b []byte, height int) *xmssSigBody {
body := &xmssSigBody{
sig: make([][]byte, wlen),
auth: make([][]byte, height),
}
for i := 0; i < wlen; i++ {
body.sig[i] = b[i*n : (i+1)*n]
}
for i := 0; i < height; i++ {
body.auth[i] = b[n*wlen+n*i : n*wlen+n*(i+1)]
}
return body
}
//Sign signs by XMSS with MerkleTree.
func (m *Merkle) Sign(msg []byte) []byte {
index := make([]byte, 32)
binary.BigEndian.PutUint32(index[28:], m.Leaf)
r := make([]byte, 32*3)
m.priv.msgPRF.sum(index, r)
copy(r[32:], m.priv.root)
copy(r[64:], index)
hmsg := hashMsg(r, msg)
sigBody := m.sign(hmsg)
sig := &xmssSig{
idx: m.Leaf,
r: r[:32],
xmssSigBody: sigBody,
}
result := sig.bytes()
m.Traverse() //never relocate the line to above
return result
}
func (m *Merkle) sign(hmsg []byte) *xmssSigBody {
wsk := make(wotsPrivKey, wlen)
for i := range wsk {
wsk[i] = make([]byte, 32)
}
addrs := make(addr, 32)
addrs.set(adrLayer, m.layer)
addrs.setTree(m.tree)
addrs.set(adrOTS, m.Leaf)
m.priv.newWotsPrivKey(addrs, wsk)
sig := wsk.sign(hmsg, m.priv.pubPRF, addrs)
return &xmssSigBody{
sig: sig,
auth: m.auth,
}
}
//IndexFromSig returns index of merkle from the signature bsig.
func IndexFromSig(bsig []byte) (uint32, error) {
if len(bsig) < 4 {
return 0, errors.New("invalid signature length")
}
return binary.BigEndian.Uint32(bsig), nil
}
//Verify verifies msg by XMSS.
func Verify(bsig, msg, bpk []byte) bool {
pk, err := DeserializePK(bpk)
if err != nil {
return false
}
sig, err := bytes2sig(bsig, pk.Height)
if err != nil {
return false
}
prf := newPRF(pk.Seed)
r := make([]byte, 32*3)
copy(r, sig.r)
copy(r[32:], pk.Root)
binary.BigEndian.PutUint32(r[64+28:], sig.idx)
hmsg := hashMsg(r, msg)
root := rootFromSig(sig.idx, hmsg, sig.xmssSigBody, prf, 0, 0)
return bytes.Equal(root, pk.Root)
}
func rootFromSig(idx uint32, hmsg []byte, body *xmssSigBody, prf *prf, layer uint32, tree uint64) []byte {
addrs := make(addr, 32)
addrs.set(adrLayer, layer)
addrs.setTree(tree)
addrs.set(adrOTS, idx)
pkOTS := body.sig.pubkey(hmsg, prf, addrs)
addrs.set(adrType, 1)
addrs.set(adrLtree, idx)
node0 := pkOTS.ltree(prf, addrs)
addrs.set(adrType, 2)
addrs.set(adrLtree, 0)
var k uint32
for k = 0; k < uint32(len(body.auth)); k++ {
addrs.set(adrHeight, k)
addrs.set(adrIndex, idx>>1)
if idx&0x1 == 0 {
randHash(node0, body.auth[k], prf, addrs, node0)
} else {
randHash(body.auth[k], node0, prf, addrs, node0)
}
idx >>= 1
}
return node0
}