diff options
-rw-r--r-- | crypto/src/pqc/crypto/lms/HSS.cs | 28 | ||||
-rw-r--r-- | crypto/src/pqc/crypto/lms/LMSKeyParameters.cs | 2 | ||||
-rw-r--r-- | crypto/src/pqc/crypto/lms/LMSPrivateKeyParameters.cs | 33 | ||||
-rw-r--r-- | crypto/src/pqc/crypto/lms/LMSPublicKeyParameters.cs | 2 |
4 files changed, 28 insertions, 37 deletions
diff --git a/crypto/src/pqc/crypto/lms/HSS.cs b/crypto/src/pqc/crypto/lms/HSS.cs index 9c21198e4..317ee89f5 100644 --- a/crypto/src/pqc/crypto/lms/HSS.cs +++ b/crypto/src/pqc/crypto/lms/HSS.cs @@ -39,17 +39,19 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms 0, I, 1 << lms.LMSigParameters.H, - rootSeed); + rootSeed, + isPlaceholder: false); } else { - keys[t] = new PlaceholderLMSPrivateKey( + keys[t] = new LmsPrivateKeyParameters( lms.LMSigParameters, lms.LMOtsParameters, -1, zero, 1 << lms.LMSigParameters.H, - zero); + zero, + isPlaceholder: true); } hssKeyMaxIndex <<= lms.LMSigParameters.H; } @@ -196,25 +198,5 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } return Lms.VerifySignature(key, sigList[Nspk], message); } - - private class PlaceholderLMSPrivateKey - : LmsPrivateKeyParameters - { - internal PlaceholderLMSPrivateKey(LMSigParameters lmsParameter, LMOtsParameters otsParameters, int q, - byte[] I, int maxQ, byte[] masterSecret) - : base(lmsParameter, otsParameters, q, I, maxQ, masterSecret) - { - } - - internal override LMOtsPrivateKey GetNextOtsPrivateKey() - { - throw new Exception("placeholder only"); - } - - public override LmsPublicKeyParameters GetPublicKey() - { - throw new Exception("placeholder only"); - } - } } } diff --git a/crypto/src/pqc/crypto/lms/LMSKeyParameters.cs b/crypto/src/pqc/crypto/lms/LMSKeyParameters.cs index b35ba36c4..aaddfb823 100644 --- a/crypto/src/pqc/crypto/lms/LMSKeyParameters.cs +++ b/crypto/src/pqc/crypto/lms/LMSKeyParameters.cs @@ -6,7 +6,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms public abstract class LmsKeyParameters : AsymmetricKeyParameter, IEncodable { - protected LmsKeyParameters(bool isPrivateKey) + internal LmsKeyParameters(bool isPrivateKey) : base(isPrivateKey) { } diff --git a/crypto/src/pqc/crypto/lms/LMSPrivateKeyParameters.cs b/crypto/src/pqc/crypto/lms/LMSPrivateKeyParameters.cs index 25ca81938..dcdeb7ab5 100644 --- a/crypto/src/pqc/crypto/lms/LMSPrivateKeyParameters.cs +++ b/crypto/src/pqc/crypto/lms/LMSPrivateKeyParameters.cs @@ -9,7 +9,7 @@ using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class LmsPrivateKeyParameters + public sealed class LmsPrivateKeyParameters : LmsKeyParameters, ILmsContextBasedSigner { private static CacheKey T1 = new CacheKey(1); @@ -34,6 +34,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms private IDigest tDigest; private int q; + private readonly bool m_isPlaceholder; // // These are not final because they can be generated. @@ -41,9 +42,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // private LmsPublicKeyParameters publicKey; - public LmsPrivateKeyParameters(LMSigParameters lmsParameter, LMOtsParameters otsParameters, int q, byte[] I, int maxQ, byte[] masterSecret) + : this(lmsParameter, otsParameters, q, I, maxQ, masterSecret, false) + { + } + + internal LmsPrivateKeyParameters(LMSigParameters lmsParameter, LMOtsParameters otsParameters, int q, byte[] I, + int maxQ, byte[] masterSecret, bool isPlaceholder) : base(true) { this.parameters = lmsParameter; @@ -55,6 +61,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms this.maxCacheR = 1 << (parameters.H + 1); this.tCache = new Dictionary<CacheKey, byte[]>(); this.tDigest = DigestUtilities.GetDigest(lmsParameter.DigestOid); + this.m_isPlaceholder = isPlaceholder; } private LmsPrivateKeyParameters(LmsPrivateKeyParameters parent, int q, int maxQ) @@ -203,8 +210,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } } - internal virtual LMOtsPrivateKey GetNextOtsPrivateKey() + internal LMOtsPrivateKey GetNextOtsPrivateKey() { + if (m_isPlaceholder) + throw new Exception("placeholder only"); + lock (this) { if (q >= maxQ) @@ -264,8 +274,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return maxQ - q; } - public virtual LmsPublicKeyParameters GetPublicKey() + public LmsPublicKeyParameters GetPublicKey() { + if (m_isPlaceholder) + throw new Exception("placeholder only"); + lock (this) { if (publicKey == null) @@ -276,7 +289,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } } - byte[] FindT(int r) + internal byte[] FindT(int r) { if (r < maxCacheR) { @@ -290,14 +303,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { lock (tCache) { - byte[] t; - if (!tCache.TryGetValue(key, out t)) - { - t = CalcT(key.index); - tCache[key] = t; - } + if (tCache.TryGetValue(key, out byte[] t)) + return t; - return t; + return tCache[key] = CalcT(key.index); } } diff --git a/crypto/src/pqc/crypto/lms/LMSPublicKeyParameters.cs b/crypto/src/pqc/crypto/lms/LMSPublicKeyParameters.cs index fa12b47c3..f8d0970af 100644 --- a/crypto/src/pqc/crypto/lms/LMSPublicKeyParameters.cs +++ b/crypto/src/pqc/crypto/lms/LMSPublicKeyParameters.cs @@ -6,7 +6,7 @@ using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class LmsPublicKeyParameters + public sealed class LmsPublicKeyParameters : LmsKeyParameters, ILmsContextBasedVerifier { private LMSigParameters parameterSet; |