Skip to content

Commit acc84b8

Browse files
committed
LMS: Replace the private key's WeakHashMap node cache, which held every node below the persisted top on keys nothing retained, with a fixed array of the top 63 nodes plus the retained authentication path and ancestor chain of the last one-time key signed with, advanced under the key's lock, ported from bc-csharp
1 parent e37e7da commit acc84b8

3 files changed

Lines changed: 314 additions & 77 deletions

File tree

‎core/src/main/java/org/bouncycastle/crypto/params/LMSPrivateKeyParameters.java‎

Lines changed: 145 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import java.io.DataInputStream;
66
import java.io.IOException;
77
import java.io.InputStream;
8-
import java.util.Map;
9-
import java.util.WeakHashMap;
108

119
import javax.security.auth.Destroyable;
1210

@@ -17,32 +15,70 @@
1715
import org.bouncycastle.crypto.signers.lms.LMSEngine;
1816
import org.bouncycastle.util.Arrays;
1917
import org.bouncycastle.util.Exceptions;
18+
import org.bouncycastle.util.Integers;
2019
import org.bouncycastle.util.Objects;
2120
import org.bouncycastle.util.io.Streams;
2221

2322
public class LMSPrivateKeyParameters
2423
extends LMSKeyParameters
2524
implements LMSContextBasedSigner, Destroyable
2625
{
27-
private static CacheKey T1 = new CacheKey(1);
28-
private static CacheKey[] internedKeys = new CacheKey[64];
29-
30-
static
31-
{
32-
internedKeys[1] = T1;
33-
for (int i = 2; i < internedKeys.length; i++)
34-
{
35-
internedKeys[i] = new CacheKey(i);
36-
}
37-
}
26+
/**
27+
* The number of tree nodes eligible for the cache (nodes 1 .. CACHE_TOP_LIMIT - 1: the top six levels
28+
* of the tree), in memory and in the persisted trailer alike. It defines the interchange format's
29+
* cache-count limit, shared with bc-csharp.
30+
*/
31+
private static final int CACHE_TOP_LIMIT = 64;
3832

3933
private final byte[] I;
4034
private final LMSigParameters parameters;
4135
private final LMOtsParameters otsParameters;
4236
private final int maxQ;
4337
private final byte[] masterSecret;
44-
private final Map<CacheKey, byte[]> tCache;
38+
39+
//
40+
// Two tiers of Merkle tree nodes are kept, neither of them secret: every node is published in some
41+
// signature or recomputed by every verifier.
42+
//
43+
// tCache holds nodes 1 .. maxCacheR - 1 (at most 63, about 2 KB), computed on demand and kept for the
44+
// life of the key. It is the tier the encoding persists, so a decoded key resumes with it. Caching
45+
// deeper nodes buys nothing that lasts: each leaf is consumed once by its parent, and an unbounded
46+
// cache of every node reaches 2 GB at h = 25. (The WeakHashMap this replaced keyed the deeper nodes
47+
// on objects nothing retained, so they were collectable on insertion and cost their allocation for
48+
// a hit rate that depended on when the collector next ran.)
49+
//
50+
// retained holds the authentication path of the last one-time key signed with, together with the
51+
// chain of its ancestors, and is advanced under the key's lock as q is allocated
52+
// (advanceRetainedPath). Consecutive signatures share most of their path, so a signature costs about
53+
// (h - 5) / 2 + 1 leaf derivations amortised, in place of the 2^(h - 5) it takes to rebuild the path
54+
// below the cached top every time. The worst case (crossing into the other half of the tree) is
55+
// still that rebuild; only a scheduled traversal (BDS) would smooth it.
56+
//
57+
// The arrays of both tiers are handed out by reference to contexts and signatures and must never be
58+
// modified or wiped.
59+
//
60+
private final byte[][] tCache;
4561
private final int maxCacheR;
62+
private RetainedPath retained;
63+
64+
/**
65+
* The authentication path of one-time key q with the ancestors of its leaf, indexed by level from
66+
* the leaf (0) up to just below the root (h - 1). Immutable: a key replaces it wholesale under its
67+
* lock, and a shard or repositioned key inherits the parent's current instance by reference.
68+
*/
69+
private static final class RetainedPath
70+
{
71+
final int q;
72+
final byte[][] path; // path[i] is the sibling of anc[i]
73+
final byte[][] anc; // anc[0] is the leaf node of q itself
74+
75+
RetainedPath(int q, byte[][] path, byte[][] anc)
76+
{
77+
this.q = q;
78+
this.path = path;
79+
this.anc = anc;
80+
}
81+
}
4682

4783
private int q;
4884

@@ -86,8 +122,8 @@ public LMSPrivateKeyParameters(LMSigParameters lmsParameter, LMOtsParameters ots
86122
this.I = Arrays.clone(I);
87123
this.maxQ = maxQ;
88124
this.masterSecret = Arrays.clone(masterSecret);
89-
this.maxCacheR = 1 << (parameters.getH() + 1);
90-
this.tCache = new WeakHashMap<CacheKey, byte[]>();
125+
this.maxCacheR = Math.min(CACHE_TOP_LIMIT, 1 << (parameters.getH() + 1));
126+
this.tCache = new byte[maxCacheR][];
91127
}
92128

93129
/**
@@ -106,15 +142,18 @@ protected LMSPrivateKeyParameters(LMSigParameters lmsParameter, LMOtsParameters
106142
this.I = new byte[0];
107143
this.maxQ = maxQ;
108144
this.masterSecret = new byte[0];
109-
this.maxCacheR = 1 << (lmsParameter.getH() + 1);
110-
this.tCache = new WeakHashMap<CacheKey, byte[]>();
145+
this.maxCacheR = Math.min(CACHE_TOP_LIMIT, 1 << (lmsParameter.getH() + 1));
146+
this.tCache = new byte[maxCacheR][];
111147
}
112148

113149
private LMSPrivateKeyParameters(LMSPrivateKeyParameters parent, int q, int maxQ)
114150
{
115-
this(parent, q, maxQ, 1 << parent.parameters.getH());
151+
this(parent, q, maxQ, Math.min(CACHE_TOP_LIMIT, 1 << parent.parameters.getH()));
116152
}
117153

154+
// Called under the parent's lock (extractKeyShard, repositionTo), which is what makes reading its
155+
// retained path safe. I, masterSecret and tCache are shared by reference with the parent; the retained
156+
// path too, but it is immutable and holds no secrets.
118157
private LMSPrivateKeyParameters(LMSPrivateKeyParameters parent, int q, int maxQ, int maxCacheR)
119158
{
120159
super(true);
@@ -126,6 +165,7 @@ private LMSPrivateKeyParameters(LMSPrivateKeyParameters parent, int q, int maxQ,
126165
this.masterSecret = parent.masterSecret;
127166
this.maxCacheR = maxCacheR;
128167
this.tCache = parent.tCache;
168+
this.retained = parent.retained;
129169
this.publicKey = parent.publicKey;
130170
}
131171

@@ -329,7 +369,7 @@ private static void readTreeCache(DataInputStream dIn, LMSPrivateKeyParameters k
329369
throws IOException
330370
{
331371
int cacheCount = dIn.readInt();
332-
if (cacheCount < 0 || cacheCount >= internedKeys.length)
372+
if (cacheCount < 0 || cacheCount >= CACHE_TOP_LIMIT)
333373
{
334374
throw new IOException("tree cache node count out of range: " + cacheCount);
335375
}
@@ -438,10 +478,12 @@ public LMSContext generateLMSContext()
438478
// Step 2
439479
int h = lmsParameter.getH();
440480
int q;
481+
byte[][] path;
441482

442483
//
443484
// The index is claimed before the context is handed out, so a one-time key is never issued
444-
// twice even if the caller then abandons the context.
485+
// twice even if the caller then abandons the context. The path is built under the same lock
486+
// so that the retained path advances in step with the index.
445487
//
446488
synchronized (this)
447489
{
@@ -452,18 +494,7 @@ public LMSContext generateLMSContext()
452494
throw new ExhaustedPrivateKeyException("ots private key exhausted");
453495
}
454496
q = this.q++;
455-
}
456-
457-
int i = 0;
458-
int r = (1 << h) + q;
459-
byte[][] path = new byte[h][];
460-
461-
while (i < h)
462-
{
463-
int tmp = (r / (1 << i)) ^ 1;
464-
465-
path[i] = this.findT(tmp);
466-
i++;
497+
path = advanceRetainedPath(h, q);
467498
}
468499

469500
return LMSEngine.generateSignContext(lmsParameter, otsParameters, I, q, masterSecret, path);
@@ -590,38 +621,98 @@ public LMSPublicKeyParameters getPublicKey()
590621
{
591622
if (publicKey == null)
592623
{
593-
publicKey = new LMSPublicKeyParameters(parameters, otsParameters, this.findT(T1), I);
624+
publicKey = new LMSPublicKeyParameters(parameters, otsParameters, this.findT(1), I);
594625
}
595626
return publicKey;
596627
}
597628
}
598629

599630
byte[] findT(int r)
600631
{
601-
if (r < maxCacheR)
632+
if (r >= maxCacheR)
602633
{
603-
return findT(r < internedKeys.length ? internedKeys[r] : new CacheKey(r));
634+
return calcT(r);
604635
}
605636

606-
return calcT(r);
637+
// The lock is held across the (recursive) computation, so a node is computed once and the
638+
// array it is published in is safely visible to every thread that reads the slot.
639+
synchronized (tCache)
640+
{
641+
byte[] t = tCache[r];
642+
643+
if (t == null)
644+
{
645+
t = calcT(r);
646+
tCache[r] = t;
647+
}
648+
649+
return t;
650+
}
607651
}
608652

609-
private byte[] findT(CacheKey key)
653+
/**
654+
* Build the authentication path of one-time key q, reusing whatever it shares with the path of the
655+
* last one-time key signed with, and retain the result in its place. Called under the key's lock.
656+
*/
657+
private byte[][] advanceRetainedPath(int h, int q)
610658
{
611-
synchronized (tCache)
659+
int r = (1 << h) + q;
660+
661+
byte[][] path = new byte[h][];
662+
byte[][] anc = new byte[h][];
663+
664+
// Levels below 'fresh' need computing; levels from 'fresh' up are shared with the retained path.
665+
int fresh = h;
666+
667+
RetainedPath old = retained;
668+
if (old != null)
612669
{
613-
byte[] t = tCache.get(key);
670+
// The paths of q and old.q agree above the highest bit in which the two differ. At that level
671+
// the roles swap: the old ancestor (root of the subtree just left) becomes the new sibling,
672+
// and the old sibling (root of the subtree now entered) becomes the new ancestor.
673+
int b = Integers.bitLength(q ^ old.q);
674+
if (b == 0)
675+
{
676+
return old.path;
677+
}
614678

615-
if (t != null)
679+
for (int i = b; i < h; ++i)
616680
{
617-
return t;
681+
path[i] = old.path[i];
682+
anc[i] = old.anc[i];
618683
}
619684

620-
t = calcT(key.index);
621-
tCache.put(key, t);
685+
path[b - 1] = old.anc[b - 1];
686+
anc[b - 1] = old.path[b - 1];
687+
fresh = b - 1;
688+
}
622689

623-
return t;
690+
// Below the divergence everything lies inside the subtree just entered: the siblings are subtrees
691+
// that findT computes (caching only those within the pinned top), and the ancestors fold up from
692+
// the new leaf.
693+
for (int i = 0; i < fresh; ++i)
694+
{
695+
path[i] = findT((r >> i) ^ 1);
696+
}
697+
698+
if (fresh > 0)
699+
{
700+
anc[0] = findT(r);
701+
702+
Digest tDigest = LMSEngine.createDigest(parameters);
703+
704+
for (int i = 1; i < fresh; ++i)
705+
{
706+
byte[] child = anc[i - 1], sibling = path[i - 1];
707+
708+
anc[i] = ((r >> (i - 1)) & 1) == 0
709+
? LMSEngine.computeNode(tDigest, I, r >> i, child, sibling)
710+
: LMSEngine.computeNode(tDigest, I, r >> i, sibling, child);
711+
}
624712
}
713+
714+
retained = new RetainedPath(q, path, anc);
715+
return path;
625716
}
626717

627718
private byte[] calcT(int r)
@@ -655,21 +746,20 @@ private byte[] calcT(int r)
655746

656747
/**
657748
* Populate the node cache with the top-of-tree nodes recovered from the optional trailing
658-
* cache in a version 0 encoding. Entries are keyed on the interned cache keys so they are not
659-
* evicted, matching the state a freshly generated key reaches after its public key has been
660-
* derived.
749+
* cache in a version 0 encoding, matching the state a freshly generated key reaches after its
750+
* public key has been derived.
661751
*
662752
* @param cachedT nodes indexed by tree node number; index 0 is unused, entries 1..n are cached.
663753
*/
664754
void primeTreeCache(byte[][] cachedT)
665755
{
666756
synchronized (tCache)
667757
{
668-
for (int r = 1; r < cachedT.length; r++)
758+
for (int r = 1; r < cachedT.length && r < tCache.length; r++)
669759
{
670760
if (cachedT[r] != null)
671761
{
672-
tCache.put(internedKeys[r], cachedT[r]);
762+
tCache[r] = cachedT[r];
673763
}
674764
}
675765
}
@@ -684,7 +774,7 @@ byte[] peekRootT()
684774
{
685775
synchronized (tCache)
686776
{
687-
return (byte[])tCache.get(T1);
777+
return tCache[1];
688778
}
689779
}
690780

@@ -698,7 +788,7 @@ boolean isTreeCachePrimed()
698788
{
699789
synchronized (tCache)
700790
{
701-
return tCache.get(T1) != null;
791+
return tCache[1] != null;
702792
}
703793
}
704794

@@ -781,7 +871,9 @@ public byte[] getEncoded()
781871
// the master secret and never looks at the trailing bytes.
782872
//
783873

784-
int cacheTop = Math.min(internedKeys.length, maxCacheR);
874+
// The whole of the in-memory cache is eligible, so a decoded key resumes with the cache it was
875+
// encoded with; findT computes any node not yet there.
876+
int cacheTop = maxCacheR;
785877

786878
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
787879

@@ -803,28 +895,4 @@ public byte[] getEncoded()
803895
return bOut.toByteArray();
804896
}
805897

806-
private static class CacheKey
807-
{
808-
private final int index;
809-
810-
CacheKey(int index)
811-
{
812-
this.index = index;
813-
}
814-
815-
public int hashCode()
816-
{
817-
return index;
818-
}
819-
820-
public boolean equals(Object o)
821-
{
822-
if (o instanceof CacheKey)
823-
{
824-
return ((CacheKey)o).index == this.index;
825-
}
826-
827-
return false;
828-
}
829-
}
830898
}

0 commit comments

Comments
 (0)