Repository navigation
Expand file tree
/
Copy pathxmss.go
More file actions
72 lines (63 loc) · 2.01 KB
/
Copy pathxmss.go
File metadata and controls
72 lines (63 loc) · 2.01 KB
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
package slhdsa
// XMSS (eXtended Merkle Signature Scheme) implementation
// as specified in FIPS 205.
// xmssNode computes a node in the XMSS tree.
// FIPS 205 Algorithm 9: xmss_node
func (c *hashCtx) xmssNode(out, tmpBuf []byte, i, z uint32, addr address) {
if z == 0 {
// Leaf node: compute WOTS+ public key
addr.setTypeAndClear(adrsWOTSHash)
addr.setKeyPairAddress(i)
c.wotsPkGen(out, tmpBuf, addr)
} else {
// Internal node: hash children
var lnode, rnode [maxN]byte
c.xmssNode(lnode[:], tmpBuf, 2*i, z-1, addr)
c.xmssNode(rnode[:], tmpBuf, 2*i+1, z-1, addr)
addr.setTypeAndClear(adrsTree)
addr.setTreeHeight(z)
addr.setTreeIndex(i)
c.h.h(c, addr, lnode[:], rnode[:], out)
}
}
// xmssSign generates an XMSS signature.
// FIPS 205 Algorithm 10: xmss_sign
func (c *hashCtx) xmssSign(msg, tmpBuf []byte, leafIdx uint32, addr address, sig []byte) {
// Build authentication path
authStart := c.params.n * c.params.len
authPath := sig[authStart:]
idx := leafIdx
for j := range c.params.hPrime {
c.xmssNode(authPath, tmpBuf, idx^1, j, addr)
authPath = authPath[c.params.n:]
idx >>= 1
}
// Generate WOTS+ signature
addr.setTypeAndClear(adrsWOTSHash)
addr.setKeyPairAddress(leafIdx)
c.wotsSign(msg, addr, sig)
}
// xmssPkFromSig computes an XMSS public key from a signature.
// FIPS 205 Algorithm 11: xmss_pkFromSig
func (c *hashCtx) xmssPkFromSig(leafIdx uint32, sig, msg, tmpBuf []byte, addr address, out []byte) {
// Compute WOTS+ public key from signature
addr.setTypeAndClear(adrsWOTSHash)
addr.setKeyPairAddress(leafIdx)
c.wotsPkFromSig(sig, msg, tmpBuf, addr, out)
// Compute root from authentication path
addr.setTypeAndClear(adrsTree)
sig = sig[c.params.len*c.params.n:] // Skip to auth path
for k := range c.params.hPrime {
addr.setTreeHeight(k + 1)
if leafIdx&1 == 0 {
leafIdx >>= 1
addr.setTreeIndex(leafIdx)
c.h.h(c, addr, out, sig, out)
} else {
leafIdx = (leafIdx - 1) >> 1
addr.setTreeIndex(leafIdx)
c.h.h(c, addr, sig, out, out)
}
sig = sig[c.params.n:]
}
}