diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-22 02:17:26 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-22 02:17:26 +0700 |
commit | c9f432da291298401537c8cfb1314134f21d66ac (patch) | |
tree | c11169c8bfa929f13826eca3625fda914f9313c4 | |
parent | Remove extraneous class (diff) | |
download | BouncyCastle.NET-ed25519-c9f432da291298401537c8cfb1314134f21d66ac.tar.xz |
Refactoring in Pqc.Crypto.Lms
- add missing factory entries
35 files changed, 619 insertions, 593 deletions
diff --git a/crypto/src/pqc/crypto/lms/HSS.cs b/crypto/src/pqc/crypto/lms/HSS.cs index 77c5fdab0..9c21198e4 100644 --- a/crypto/src/pqc/crypto/lms/HSS.cs +++ b/crypto/src/pqc/crypto/lms/HSS.cs @@ -3,15 +3,15 @@ using System.Collections.Generic; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class HSS + public static class Hss { - public static HSSPrivateKeyParameters GenerateHssKeyPair(HSSKeyGenerationParameters parameters) + public static HssPrivateKeyParameters GenerateHssKeyPair(HssKeyGenerationParameters parameters) { // // LmsPrivateKey can derive and hold the public key so we just use an array of those. // - LMSPrivateKeyParameters[] keys = new LMSPrivateKeyParameters[parameters.Depth]; - LMSSignature[] sig = new LMSSignature[parameters.Depth - 1]; + LmsPrivateKeyParameters[] keys = new LmsPrivateKeyParameters[parameters.Depth]; + LmsSignature[] sig = new LmsSignature[parameters.Depth - 1]; byte[] rootSeed = new byte[32]; parameters.Random.NextBytes(rootSeed); @@ -33,7 +33,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms var lms = parameters.GetLmsParameters(t); if (t == 0) { - keys[t] = new LMSPrivateKeyParameters( + keys[t] = new LmsPrivateKeyParameters( lms.LMSigParameters, lms.LMOtsParameters, 0, @@ -61,10 +61,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms hssKeyMaxIndex = long.MaxValue; } - return new HSSPrivateKeyParameters( + return new HssPrivateKeyParameters( parameters.Depth, - new List<LMSPrivateKeyParameters>(keys), - new List<LMSSignature>(sig), + new List<LmsPrivateKeyParameters>(keys), + new List<LmsSignature>(sig), 0, hssKeyMaxIndex); } @@ -77,7 +77,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms * * @param keyPair */ - public static void IncrementIndex(HSSPrivateKeyParameters keyPair) + public static void IncrementIndex(HssPrivateKeyParameters keyPair) { lock (keyPair) { @@ -87,7 +87,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } } - public static void RangeTestKeys(HSSPrivateKeyParameters keyPair) + public static void RangeTestKeys(HssPrivateKeyParameters keyPair) { lock (keyPair) { @@ -117,10 +117,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } - public static HSSSignature GenerateSignature(HSSPrivateKeyParameters keyPair, byte[] message) + public static HssSignature GenerateSignature(HssPrivateKeyParameters keyPair, byte[] message) { - LMSSignedPubKey[] signed_pub_key; - LMSPrivateKeyParameters nextKey; + LmsSignedPubKey[] signed_pub_key; + LmsPrivateKeyParameters nextKey; int L = keyPair.L; lock (keyPair) @@ -134,10 +134,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // Step 2. Stand in for sig[L-1] int i = 0; - signed_pub_key = new LMSSignedPubKey[L - 1]; + signed_pub_key = new LmsSignedPubKey[L - 1]; while (i < L - 1) { - signed_pub_key[i] = new LMSSignedPubKey(sig[i], keys[i + 1].GetPublicKey()); + signed_pub_key[i] = new LmsSignedPubKey(sig[i], keys[i + 1].GetPublicKey()); ++i; } @@ -147,26 +147,26 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms keyPair.IncIndex(); } - LMSContext context = nextKey.GenerateLmsContext().WithSignedPublicKeys(signed_pub_key); + LmsContext context = nextKey.GenerateLmsContext().WithSignedPublicKeys(signed_pub_key); context.BlockUpdate(message, 0, message.Length); return GenerateSignature(L, context); } - public static HSSSignature GenerateSignature(int L, LMSContext context) + public static HssSignature GenerateSignature(int L, LmsContext context) { - return new HSSSignature(L - 1, context.SignedPubKeys, LMS.GenerateSign(context)); + return new HssSignature(L - 1, context.SignedPubKeys, Lms.GenerateSign(context)); } - public static bool VerifySignature(HSSPublicKeyParameters publicKey, HSSSignature signature, byte[] message) + public static bool VerifySignature(HssPublicKeyParameters publicKey, HssSignature signature, byte[] message) { int Nspk = signature.GetlMinus1(); if (Nspk + 1 != publicKey.L) return false; - LMSSignature[] sigList = new LMSSignature[Nspk + 1]; - LMSPublicKeyParameters[] pubList = new LMSPublicKeyParameters[Nspk]; + LmsSignature[] sigList = new LmsSignature[Nspk + 1]; + LmsPublicKeyParameters[] pubList = new LmsPublicKeyParameters[Nspk]; for (int i = 0; i < Nspk; i++) { @@ -175,13 +175,13 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } sigList[Nspk] = signature.Signature; - LMSPublicKeyParameters key = publicKey.LmsPublicKey; + LmsPublicKeyParameters key = publicKey.LmsPublicKey; for (int i = 0; i < Nspk; i++) { - LMSSignature sig = sigList[i]; + LmsSignature sig = sigList[i]; byte[] msg = pubList[i].ToByteArray(); - if (!LMS.VerifySignature(key, sig, msg)) + if (!Lms.VerifySignature(key, sig, msg)) { return false; } @@ -194,11 +194,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms throw new Exception(ex.Message, ex); } } - return LMS.VerifySignature(key, sigList[Nspk], message); + return Lms.VerifySignature(key, sigList[Nspk], message); } private class PlaceholderLMSPrivateKey - : LMSPrivateKeyParameters + : LmsPrivateKeyParameters { internal PlaceholderLMSPrivateKey(LMSigParameters lmsParameter, LMOtsParameters otsParameters, int q, byte[] I, int maxQ, byte[] masterSecret) @@ -211,7 +211,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms throw new Exception("placeholder only"); } - public override LMSPublicKeyParameters GetPublicKey() + public override LmsPublicKeyParameters GetPublicKey() { throw new Exception("placeholder only"); } diff --git a/crypto/src/pqc/crypto/lms/HSSKeyGenerationParameters.cs b/crypto/src/pqc/crypto/lms/HSSKeyGenerationParameters.cs index 5baaa7aa2..61ea51368 100644 --- a/crypto/src/pqc/crypto/lms/HSSKeyGenerationParameters.cs +++ b/crypto/src/pqc/crypto/lms/HSSKeyGenerationParameters.cs @@ -5,10 +5,10 @@ using Org.BouncyCastle.Security; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public sealed class HSSKeyGenerationParameters + public sealed class HssKeyGenerationParameters : KeyGenerationParameters { - private static LMSParameters[] ValidateLmsParameters(LMSParameters[] lmsParameters) + private static LmsParameters[] ValidateLmsParameters(LmsParameters[] lmsParameters) { if (lmsParameters == null) throw new ArgumentNullException(nameof(lmsParameters)); @@ -17,7 +17,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return lmsParameters; } - private readonly LMSParameters[] m_lmsParameters; + private readonly LmsParameters[] m_lmsParameters; /** * Base constructor - parameters and a source of randomness. @@ -25,15 +25,15 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms * @param lmsParameters array of LMS parameters, one per level in the hierarchy (up to 8 levels). * @param random the random byte source. */ - public HSSKeyGenerationParameters(LMSParameters[] lmsParameters, SecureRandom random) - :base(random, LmsUtils.CalculateStrength(ValidateLmsParameters(lmsParameters)[0])) + public HssKeyGenerationParameters(LmsParameters[] lmsParameters, SecureRandom random) + :base(random, LmsUtilities.CalculateStrength(ValidateLmsParameters(lmsParameters)[0])) { m_lmsParameters = lmsParameters; } public int Depth => m_lmsParameters.Length; - public LMSParameters GetLmsParameters(int index) + public LmsParameters GetLmsParameters(int index) { if (index < 0 || index >= m_lmsParameters.Length) throw new ArgumentOutOfRangeException(nameof(index)); diff --git a/crypto/src/pqc/crypto/lms/HSSKeyPairGenerator.cs b/crypto/src/pqc/crypto/lms/HSSKeyPairGenerator.cs index 137275a9a..273e18904 100644 --- a/crypto/src/pqc/crypto/lms/HSSKeyPairGenerator.cs +++ b/crypto/src/pqc/crypto/lms/HSSKeyPairGenerator.cs @@ -2,19 +2,19 @@ using Org.BouncyCastle.Crypto; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public sealed class HSSKeyPairGenerator + public sealed class HssKeyPairGenerator : IAsymmetricCipherKeyPairGenerator { - private HSSKeyGenerationParameters m_parameters; + private HssKeyGenerationParameters m_parameters; public void Init(KeyGenerationParameters parameters) { - m_parameters = (HSSKeyGenerationParameters)parameters; + m_parameters = (HssKeyGenerationParameters)parameters; } public AsymmetricCipherKeyPair GenerateKeyPair() { - HSSPrivateKeyParameters privKey = HSS.GenerateHssKeyPair(m_parameters); + HssPrivateKeyParameters privKey = Hss.GenerateHssKeyPair(m_parameters); return new AsymmetricCipherKeyPair(privKey.GetPublicKey(), privKey); } diff --git a/crypto/src/pqc/crypto/lms/HSSPrivateKeyParameters.cs b/crypto/src/pqc/crypto/lms/HSSPrivateKeyParameters.cs index 535654fd8..447dbad27 100644 --- a/crypto/src/pqc/crypto/lms/HSSPrivateKeyParameters.cs +++ b/crypto/src/pqc/crypto/lms/HSSPrivateKeyParameters.cs @@ -8,25 +8,25 @@ using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class HSSPrivateKeyParameters - : LMSKeyParameters, ILMSContextBasedSigner + public class HssPrivateKeyParameters + : LmsKeyParameters, ILmsContextBasedSigner { private int l; private bool isShard; - private IList<LMSPrivateKeyParameters> keys; - private IList<LMSSignature> sig; + private IList<LmsPrivateKeyParameters> keys; + private IList<LmsSignature> sig; private long indexLimit; private long index = 0; - private HSSPublicKeyParameters publicKey; + private HssPublicKeyParameters publicKey; - public HSSPrivateKeyParameters(int l, IList<LMSPrivateKeyParameters> keys, IList<LMSSignature> sig, long index, + public HssPrivateKeyParameters(int l, IList<LmsPrivateKeyParameters> keys, IList<LmsSignature> sig, long index, long indexLimit) :base(true) { this.l = l; - this.keys = new List<LMSPrivateKeyParameters>(keys); - this.sig = new List<LMSSignature>(sig); + this.keys = new List<LmsPrivateKeyParameters>(keys); + this.sig = new List<LmsSignature>(sig); this.index = index; this.indexLimit = indexLimit; this.isShard = false; @@ -37,7 +37,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms ResetKeyToIndex(); } - private HSSPrivateKeyParameters(int l, IList<LMSPrivateKeyParameters> keys, IList<LMSSignature> sig, long index, + private HssPrivateKeyParameters(int l, IList<LmsPrivateKeyParameters> keys, IList<LmsSignature> sig, long index, long indexLimit, bool isShard) :base(true) { @@ -45,25 +45,25 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms this.l = l; // this.keys = new UnmodifiableListProxy(keys); // this.sig = new UnmodifiableListProxy(sig); - this.keys = new List<LMSPrivateKeyParameters>(keys); - this.sig = new List<LMSSignature>(sig); + this.keys = new List<LmsPrivateKeyParameters>(keys); + this.sig = new List<LmsSignature>(sig); this.index = index; this.indexLimit = indexLimit; this.isShard = isShard; } - public static HSSPrivateKeyParameters GetInstance(byte[] privEnc, byte[] pubEnc) + public static HssPrivateKeyParameters GetInstance(byte[] privEnc, byte[] pubEnc) { - HSSPrivateKeyParameters pKey = GetInstance(privEnc); + HssPrivateKeyParameters pKey = GetInstance(privEnc); - pKey.publicKey = HSSPublicKeyParameters.GetInstance(pubEnc); + pKey.publicKey = HssPublicKeyParameters.GetInstance(pubEnc); return pKey; } - public static HSSPrivateKeyParameters GetInstance(object src) + public static HssPrivateKeyParameters GetInstance(object src) { - if (src is HSSPrivateKeyParameters hssPrivateKeyParameters) + if (src is HssPrivateKeyParameters hssPrivateKeyParameters) { return hssPrivateKeyParameters; } @@ -81,20 +81,20 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms bool limited = binaryReader.ReadBoolean(); - var keys = new List<LMSPrivateKeyParameters>(); - var signatures = new List<LMSSignature>(); + var keys = new List<LmsPrivateKeyParameters>(); + var signatures = new List<LmsSignature>(); for (int t = 0; t < d; t++) { - keys.Add(LMSPrivateKeyParameters.GetInstance(src)); + keys.Add(LmsPrivateKeyParameters.GetInstance(src)); } for (int t = 0; t < d - 1; t++) { - signatures.Add(LMSSignature.GetInstance(src)); + signatures.Add(LmsSignature.GetInstance(src)); } - return new HSSPrivateKeyParameters(d, keys, signatures, index, maxIndex, limited); + return new HssPrivateKeyParameters(d, keys, signatures, index, maxIndex, limited); } else if (src is byte[] bytes) { @@ -128,19 +128,19 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return index; } - public LMSParameters[] GetLmsParameters() + public LmsParameters[] GetLmsParameters() { lock (this) { int len = keys.Count; - LMSParameters[] parms = new LMSParameters[len]; + LmsParameters[] parms = new LmsParameters[len]; for (int i = 0; i < len; i++) { - LMSPrivateKeyParameters lmsPrivateKey = keys[i]; + LmsPrivateKeyParameters lmsPrivateKey = keys[i]; - parms[i] = new LMSParameters(lmsPrivateKey.GetSigParameters(), lmsPrivateKey.GetOtsParameters()); + parms[i] = new LmsParameters(lmsPrivateKey.GetSigParameters(), lmsPrivateKey.GetOtsParameters()); } return parms; @@ -155,17 +155,17 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } } - private static HSSPrivateKeyParameters MakeCopy(HSSPrivateKeyParameters privateKeyParameters) + private static HssPrivateKeyParameters MakeCopy(HssPrivateKeyParameters privateKeyParameters) { return GetInstance(privateKeyParameters.GetEncoded()); } - protected void UpdateHierarchy(IList<LMSPrivateKeyParameters> newKeys, IList<LMSSignature> newSig) + protected void UpdateHierarchy(IList<LmsPrivateKeyParameters> newKeys, IList<LmsSignature> newSig) { lock (this) { - keys = new List<LMSPrivateKeyParameters>(newKeys); - sig = new List<LMSSignature>(newSig); + keys = new List<LmsPrivateKeyParameters>(newKeys); + sig = new List<LmsSignature>(newSig); } } @@ -181,7 +181,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return indexLimit - index; } - LMSPrivateKeyParameters GetRootKey() + LmsPrivateKeyParameters GetRootKey() { return keys[0]; } @@ -195,7 +195,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms * @param usageCount the number of usages the key should have. * @return a key based on the current key that can be used usageCount times. */ - public HSSPrivateKeyParameters ExtractKeyShard(int usageCount) + public HssPrivateKeyParameters ExtractKeyShard(int usageCount) { lock (this) { @@ -210,11 +210,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // index += usageCount; - var keys = new List<LMSPrivateKeyParameters>(this.GetKeys()); - var sig = new List<LMSSignature>(this.GetSig()); + var keys = new List<LmsPrivateKeyParameters>(this.GetKeys()); + var sig = new List<LmsSignature>(this.GetSig()); - HSSPrivateKeyParameters shard = MakeCopy( - new HSSPrivateKeyParameters(l, keys, sig, shardStartIndex, maxIndexForShard, true)); + HssPrivateKeyParameters shard = MakeCopy( + new HssPrivateKeyParameters(l, keys, sig, shardStartIndex, maxIndexForShard, true)); ResetKeyToIndex(); @@ -222,12 +222,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } } - public IList<LMSPrivateKeyParameters> GetKeys() + public IList<LmsPrivateKeyParameters> GetKeys() { lock (this) return keys; } - internal IList<LMSSignature> GetSig() + internal IList<LmsSignature> GetSig() { lock (this) return sig; } @@ -260,14 +260,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // LMSSignature[] sig = this.sig.toArray(new LMSSignature[this.sig.Count]);// new LMSSignature[originalKeys.Size() - 1]; // - LMSPrivateKeyParameters originalRootKey = this.GetRootKey(); + LmsPrivateKeyParameters originalRootKey = this.GetRootKey(); // // We need to replace the root key to a new q value. // if (keys[0].GetIndex() - 1 != qTreePath[0]) { - keys[0] = LMS.GenerateKeys( + keys[0] = Lms.GenerateKeys( originalRootKey.GetSigParameters(), originalRootKey.GetOtsParameters(), (int)qTreePath[0], originalRootKey.GetI(), originalRootKey.GetMasterSecret()); @@ -276,7 +276,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms for (int i = 1; i < qTreePath.Length; i++) { - LMSPrivateKeyParameters intermediateKey = keys[i - 1]; + LmsPrivateKeyParameters intermediateKey = keys[i - 1]; byte[] childI = new byte[16]; byte[] childSeed = new byte[32]; @@ -315,7 +315,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // // This means the parent has changed. // - keys[i] = LMS.GenerateKeys( + keys[i] = Lms.GenerateKeys( originalKeys[i].GetSigParameters(), originalKeys[i].GetOtsParameters(), (int)qTreePath[i], childI, childSeed); @@ -323,7 +323,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // // Ensure post increment occurs on parent and the new public key is signed. // - sig[i - 1] = LMS.GenerateSign((LMSPrivateKeyParameters)keys[i - 1], ((LMSPrivateKeyParameters)keys[i]).GetPublicKey().ToByteArray()); + sig[i - 1] = Lms.GenerateSign((LmsPrivateKeyParameters)keys[i - 1], ((LmsPrivateKeyParameters)keys[i]).GetPublicKey().ToByteArray()); changed = true; } else if (!lmsQMatch) @@ -332,7 +332,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // Q is different so we can generate a new private key but it will have the same public // key so we do not need to sign it again. // - keys[i] = LMS.GenerateKeys( + keys[i] = Lms.GenerateKeys( originalKeys[i].GetSigParameters(), originalKeys[i].GetOtsParameters(), (int)qTreePath[i], childI, childSeed); @@ -347,10 +347,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } } - public HSSPublicKeyParameters GetPublicKey() + public HssPublicKeyParameters GetPublicKey() { lock (this) - return new HSSPublicKeyParameters(l, GetRootKey().GetPublicKey()); + return new HssPublicKeyParameters(l, GetRootKey().GetPublicKey()); } internal void ReplaceConsumedKey(int d) @@ -364,21 +364,21 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms byte[] childI = new byte[16]; Array.Copy(postImage, 0, childI, 0, childI.Length); - var newKeys = new List<LMSPrivateKeyParameters>(keys); + var newKeys = new List<LmsPrivateKeyParameters>(keys); // // We need the parameters from the LMS key we are replacing. // - LMSPrivateKeyParameters oldPk = keys[d]; + LmsPrivateKeyParameters oldPk = keys[d]; - newKeys[d] = LMS.GenerateKeys(oldPk.GetSigParameters(), oldPk.GetOtsParameters(), 0, childI, childRootSeed); + newKeys[d] = Lms.GenerateKeys(oldPk.GetSigParameters(), oldPk.GetOtsParameters(), 0, childI, childRootSeed); - var newSig = new List<LMSSignature>(sig); + var newSig = new List<LmsSignature>(sig); - newSig[d - 1] = LMS.GenerateSign(newKeys[d - 1], newKeys[d].GetPublicKey().ToByteArray()); + newSig[d - 1] = Lms.GenerateSign(newKeys[d - 1], newKeys[d].GetPublicKey().ToByteArray()); - this.keys = new List<LMSPrivateKeyParameters>(newKeys); - this.sig = new List<LMSSignature>(newSig); + this.keys = new List<LmsPrivateKeyParameters>(newKeys); + this.sig = new List<LmsSignature>(newSig); } public override bool Equals(Object o) @@ -392,7 +392,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return false; } - HSSPrivateKeyParameters that = (HSSPrivateKeyParameters)o; + HssPrivateKeyParameters that = (HssPrivateKeyParameters)o; if (l != that.l) { @@ -445,12 +445,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms .U64Str(indexLimit) .Boolean(isShard); // Depth - foreach (LMSPrivateKeyParameters key in keys) + foreach (LmsPrivateKeyParameters key in keys) { composer.Bytes(key); } - foreach (LMSSignature s in sig) + foreach (LmsSignature s in sig) { composer.Bytes(s); } @@ -475,15 +475,15 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return MakeCopy(this); } - public LMSContext GenerateLmsContext() + public LmsContext GenerateLmsContext() { - LMSSignedPubKey[] signed_pub_key; - LMSPrivateKeyParameters nextKey; + LmsSignedPubKey[] signed_pub_key; + LmsPrivateKeyParameters nextKey; int L = this.L; lock (this) { - HSS.RangeTestKeys(this); + Hss.RangeTestKeys(this); var keys = this.GetKeys(); var sig = this.GetSig(); @@ -492,10 +492,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // Step 2. Stand in for sig[L-1] int i = 0; - signed_pub_key = new LMSSignedPubKey[L - 1]; + signed_pub_key = new LmsSignedPubKey[L - 1]; while (i < L - 1) { - signed_pub_key[i] = new LMSSignedPubKey(sig[i], keys[i + 1].GetPublicKey()); + signed_pub_key[i] = new LmsSignedPubKey(sig[i], keys[i + 1].GetPublicKey()); ++i; } @@ -508,11 +508,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return nextKey.GenerateLmsContext().WithSignedPublicKeys(signed_pub_key); } - public byte[] GenerateSignature(LMSContext context) + public byte[] GenerateSignature(LmsContext context) { try { - return HSS.GenerateSignature(L, context).GetEncoded(); + return Hss.GenerateSignature(L, context).GetEncoded(); } catch (IOException e) { diff --git a/crypto/src/pqc/crypto/lms/HSSPublicKeyParameters.cs b/crypto/src/pqc/crypto/lms/HSSPublicKeyParameters.cs index 788b00fbd..85b781228 100644 --- a/crypto/src/pqc/crypto/lms/HSSPublicKeyParameters.cs +++ b/crypto/src/pqc/crypto/lms/HSSPublicKeyParameters.cs @@ -5,22 +5,22 @@ using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public sealed class HSSPublicKeyParameters - : LMSKeyParameters, ILMSContextBasedVerifier + public sealed class HssPublicKeyParameters + : LmsKeyParameters, ILmsContextBasedVerifier { private readonly int m_l; - private readonly LMSPublicKeyParameters m_lmsPublicKey; + private readonly LmsPublicKeyParameters m_lmsPublicKey; - public HSSPublicKeyParameters(int l, LMSPublicKeyParameters lmsPublicKey) + public HssPublicKeyParameters(int l, LmsPublicKeyParameters lmsPublicKey) :base(false) { m_l = l; m_lmsPublicKey = lmsPublicKey; } - public static HSSPublicKeyParameters GetInstance(object src) + public static HssPublicKeyParameters GetInstance(object src) { - if (src is HSSPublicKeyParameters hssPublicKeyParameters) + if (src is HssPublicKeyParameters hssPublicKeyParameters) { return hssPublicKeyParameters; } @@ -28,8 +28,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { int L = BinaryReaders.ReadInt32BigEndian(binaryReader); - LMSPublicKeyParameters lmsPublicKey = LMSPublicKeyParameters.GetInstance(src); - return new HSSPublicKeyParameters(L, lmsPublicKey); + LmsPublicKeyParameters lmsPublicKey = LmsPublicKeyParameters.GetInstance(src); + return new HssPublicKeyParameters(L, lmsPublicKey); } else if (src is byte[] bytes) { @@ -54,7 +54,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms public int L => m_l; - public LMSPublicKeyParameters LmsPublicKey => m_lmsPublicKey; + public LmsPublicKeyParameters LmsPublicKey => m_lmsPublicKey; public override bool Equals(Object o) { @@ -63,7 +63,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms if (o == null || GetType() != o.GetType()) return false; - HSSPublicKeyParameters publicKey = (HSSPublicKeyParameters)o; + HssPublicKeyParameters publicKey = (HssPublicKeyParameters)o; return m_l == publicKey.m_l && m_lmsPublicKey.Equals(publicKey.m_lmsPublicKey); @@ -83,39 +83,39 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms .Build(); } - public LMSContext GenerateLmsContext(byte[] sigEnc) + public LmsContext GenerateLmsContext(byte[] sigEnc) { - HSSSignature signature; + HssSignature signature; try { - signature = HSSSignature.GetInstance(sigEnc, L); + signature = HssSignature.GetInstance(sigEnc, L); } catch (IOException e) { throw new Exception($"cannot parse signature: {e.Message}"); } - LMSSignedPubKey[] signedPubKeys = signature.GetSignedPubKeys(); - LMSPublicKeyParameters key = signedPubKeys[signedPubKeys.Length - 1].GetPublicKey(); + LmsSignedPubKey[] signedPubKeys = signature.GetSignedPubKeys(); + LmsPublicKeyParameters key = signedPubKeys[signedPubKeys.Length - 1].GetPublicKey(); return key.GenerateOtsContext(signature.Signature).WithSignedPublicKeys(signedPubKeys); } - public bool Verify(LMSContext context) + public bool Verify(LmsContext context) { - LMSSignedPubKey[] sigKeys = context.SignedPubKeys; + LmsSignedPubKey[] sigKeys = context.SignedPubKeys; if (sigKeys.Length != L - 1) return false; - LMSPublicKeyParameters key = LmsPublicKey; + LmsPublicKeyParameters key = LmsPublicKey; bool failed = false; for (int i = 0; i < sigKeys.Length; i++) { - LMSSignature sig = sigKeys[i].GetSignature(); + LmsSignature sig = sigKeys[i].GetSignature(); byte[] msg = sigKeys[i].GetPublicKey().ToByteArray(); - if (!LMS.VerifySignature(key, sig, msg)) + if (!Lms.VerifySignature(key, sig, msg)) { failed = true; } diff --git a/crypto/src/pqc/crypto/lms/HSSSignature.cs b/crypto/src/pqc/crypto/lms/HSSSignature.cs index bd4af1bb8..7c4599835 100644 --- a/crypto/src/pqc/crypto/lms/HSSSignature.cs +++ b/crypto/src/pqc/crypto/lms/HSSSignature.cs @@ -6,30 +6,29 @@ using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class HSSSignature + public sealed class HssSignature : IEncodable { private int lMinus1; - private LMSSignedPubKey[] signedPubKey; - private LMSSignature signature; + private LmsSignedPubKey[] signedPubKey; + private LmsSignature signature; - public HSSSignature(int lMinus1, LMSSignedPubKey[] signedPubKey, LMSSignature signature) + public HssSignature(int lMinus1, LmsSignedPubKey[] signedPubKey, LmsSignature signature) { this.lMinus1 = lMinus1; this.signedPubKey = signedPubKey; this.signature = signature; } - /** - * @param src byte[], InputStream or HSSSignature - * @param L The HSS depth, available from public key. - * @return An HSSSignature instance. - * @throws IOException - */ - public static HSSSignature GetInstance(object src, int L) + * @param src byte[], InputStream or HSSSignature + * @param L The HSS depth, available from public key. + * @return An HSSSignature instance. + * @throws IOException + */ + public static HssSignature GetInstance(object src, int L) { - if (src is HSSSignature hssSignature) + if (src is HssSignature hssSignature) { return hssSignature; } @@ -39,19 +38,19 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms if (lminus != L - 1) throw new Exception("nspk exceeded maxNspk"); - LMSSignedPubKey[] signedPubKeys = new LMSSignedPubKey[lminus]; + LmsSignedPubKey[] signedPubKeys = new LmsSignedPubKey[lminus]; if (lminus != 0) { for (int t = 0; t < signedPubKeys.Length; t++) { - signedPubKeys[t] = new LMSSignedPubKey(LMSSignature.GetInstance(src), - LMSPublicKeyParameters.GetInstance(src)); + signedPubKeys[t] = new LmsSignedPubKey(LmsSignature.GetInstance(src), + LmsPublicKeyParameters.GetInstance(src)); } } - LMSSignature sig = LMSSignature.GetInstance(src); + LmsSignature sig = LmsSignature.GetInstance(src); - return new HSSSignature(lminus, signedPubKeys, sig); + return new HssSignature(lminus, signedPubKeys, sig); } else if (src is byte[] bytes) { @@ -80,12 +79,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return lMinus1; } - public LMSSignedPubKey[] GetSignedPubKeys() + public LmsSignedPubKey[] GetSignedPubKeys() { return signedPubKey; } - public LMSSignature Signature => signature; + public LmsSignature Signature => signature; public override bool Equals(Object o) { @@ -99,7 +98,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return false; } - HSSSignature signature1 = (HSSSignature) o; + HssSignature signature1 = (HssSignature) o; if (lMinus1 != signature1.lMinus1) { @@ -139,7 +138,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms composer.U32Str(lMinus1); if (signedPubKey != null) { - foreach (LMSSignedPubKey sigPub in signedPubKey) + foreach (LmsSignedPubKey sigPub in signedPubKey) { composer.Bytes(sigPub); } diff --git a/crypto/src/pqc/crypto/lms/HSSSigner.cs b/crypto/src/pqc/crypto/lms/HSSSigner.cs index 4bbdff848..9ef7b57ae 100644 --- a/crypto/src/pqc/crypto/lms/HSSSigner.cs +++ b/crypto/src/pqc/crypto/lms/HSSSigner.cs @@ -5,21 +5,21 @@ using Org.BouncyCastle.Crypto; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class HSSSigner + public sealed class HssSigner : IMessageSigner { - private HSSPrivateKeyParameters privKey; - private HSSPublicKeyParameters pubKey; + private HssPrivateKeyParameters privKey; + private HssPublicKeyParameters pubKey; public void Init(bool forSigning, ICipherParameters param) { if (forSigning) { - this.privKey = (HSSPrivateKeyParameters) param; + this.privKey = (HssPrivateKeyParameters) param; } else { - this.pubKey = (HSSPublicKeyParameters) param; + this.pubKey = (HssPublicKeyParameters) param; } } @@ -27,7 +27,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { try { - return HSS.GenerateSignature(privKey, message).GetEncoded(); + return Hss.GenerateSignature(privKey, message).GetEncoded(); } catch (IOException e) { @@ -39,7 +39,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { try { - return HSS.VerifySignature(pubKey, HSSSignature.GetInstance(signature, pubKey.L), message); + return Hss.VerifySignature(pubKey, HssSignature.GetInstance(signature, pubKey.L), message); } catch (IOException e) { diff --git a/crypto/src/pqc/crypto/lms/ILMSContextBasedSigner.cs b/crypto/src/pqc/crypto/lms/ILMSContextBasedSigner.cs index 86144b8e8..94864b471 100644 --- a/crypto/src/pqc/crypto/lms/ILMSContextBasedSigner.cs +++ b/crypto/src/pqc/crypto/lms/ILMSContextBasedSigner.cs @@ -1,10 +1,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public interface ILMSContextBasedSigner + public interface ILmsContextBasedSigner { - LMSContext GenerateLmsContext(); + LmsContext GenerateLmsContext(); - byte[] GenerateSignature(LMSContext context); + byte[] GenerateSignature(LmsContext context); long GetUsagesRemaining(); } diff --git a/crypto/src/pqc/crypto/lms/ILMSContextBasedVerifier.cs b/crypto/src/pqc/crypto/lms/ILMSContextBasedVerifier.cs index 76538d052..ffba83ad2 100644 --- a/crypto/src/pqc/crypto/lms/ILMSContextBasedVerifier.cs +++ b/crypto/src/pqc/crypto/lms/ILMSContextBasedVerifier.cs @@ -1,9 +1,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public interface ILMSContextBasedVerifier + public interface ILmsContextBasedVerifier { - LMSContext GenerateLmsContext(byte[] signature); + LmsContext GenerateLmsContext(byte[] signature); - bool Verify(LMSContext context); + bool Verify(LmsContext context); } } \ No newline at end of file diff --git a/crypto/src/pqc/crypto/lms/LMOtsPrivateKey.cs b/crypto/src/pqc/crypto/lms/LMOtsPrivateKey.cs index 8482c1a53..e9df6fbfd 100644 --- a/crypto/src/pqc/crypto/lms/LMOtsPrivateKey.cs +++ b/crypto/src/pqc/crypto/lms/LMOtsPrivateKey.cs @@ -18,22 +18,22 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms m_masterSecret = masterSecret; } - public LMSContext GetSignatureContext(LMSigParameters sigParams, byte[][] path) + public LmsContext GetSignatureContext(LMSigParameters sigParams, byte[][] path) { - byte[] C = new byte[LM_OTS.SEED_LEN]; + byte[] C = new byte[LMOts.SEED_LEN]; SeedDerive derive = GetDerivationFunction(); - derive.J = LM_OTS.SEED_RANDOMISER_INDEX; // This value from reference impl. + derive.J = LMOts.SEED_RANDOMISER_INDEX; // This value from reference impl. derive.DeriveSeed(false, C, 0); IDigest ctx = DigestUtilities.GetDigest(m_parameters.DigestOid); - LmsUtils.ByteArray(m_I, ctx); - LmsUtils.U32Str(m_q, ctx); - LmsUtils.U16Str(LM_OTS.D_MESG, ctx); - LmsUtils.ByteArray(C, ctx); + LmsUtilities.ByteArray(m_I, ctx); + LmsUtilities.U32Str(m_q, ctx); + LmsUtilities.U16Str(LMOts.D_MESG, ctx); + LmsUtilities.ByteArray(C, ctx); - return new LMSContext(this, sigParams, ctx, C, path); + return new LmsContext(this, sigParams, ctx, C, path); } internal SeedDerive GetDerivationFunction() diff --git a/crypto/src/pqc/crypto/lms/LMOtsPublicKey.cs b/crypto/src/pqc/crypto/lms/LMOtsPublicKey.cs index 480cb3dbd..09e8b2951 100644 --- a/crypto/src/pqc/crypto/lms/LMOtsPublicKey.cs +++ b/crypto/src/pqc/crypto/lms/LMOtsPublicKey.cs @@ -103,28 +103,28 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms .Build(); } - internal LMSContext CreateOtsContext(LMOtsSignature signature) + internal LmsContext CreateOtsContext(LMOtsSignature signature) { IDigest ctx = DigestUtilities.GetDigest(m_parameters.DigestOid); - LmsUtils.ByteArray(m_I, ctx); - LmsUtils.U32Str(m_q, ctx); - LmsUtils.U16Str(LM_OTS.D_MESG, ctx); - LmsUtils.ByteArray(signature.C, ctx); + LmsUtilities.ByteArray(m_I, ctx); + LmsUtilities.U32Str(m_q, ctx); + LmsUtilities.U16Str(LMOts.D_MESG, ctx); + LmsUtilities.ByteArray(signature.C, ctx); - return new LMSContext(this, signature, ctx); + return new LmsContext(this, signature, ctx); } - internal LMSContext CreateOtsContext(LMSSignature signature) + internal LmsContext CreateOtsContext(LmsSignature signature) { IDigest ctx = DigestUtilities.GetDigest(m_parameters.DigestOid); - LmsUtils.ByteArray(m_I, ctx); - LmsUtils.U32Str(m_q, ctx); - LmsUtils.U16Str(LM_OTS.D_MESG, ctx); - LmsUtils.ByteArray(signature.OtsSignature.C, ctx); + LmsUtilities.ByteArray(m_I, ctx); + LmsUtilities.U32Str(m_q, ctx); + LmsUtilities.U16Str(LMOts.D_MESG, ctx); + LmsUtilities.ByteArray(signature.OtsSignature.C, ctx); - return new LMSContext(this, signature, ctx); + return new LmsContext(this, signature, ctx); } } } diff --git a/crypto/src/pqc/crypto/lms/LMS.cs b/crypto/src/pqc/crypto/lms/LMS.cs index c2401bd35..3c17b0a1e 100644 --- a/crypto/src/pqc/crypto/lms/LMS.cs +++ b/crypto/src/pqc/crypto/lms/LMS.cs @@ -5,12 +5,12 @@ using Org.BouncyCastle.Security; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public static class LMS + public static class Lms { internal static ushort D_LEAF = 0x8282; internal static ushort D_INTR = 0x8383; - public static LMSPrivateKeyParameters GenerateKeys(LMSigParameters parameterSet, + public static LmsPrivateKeyParameters GenerateKeys(LMSigParameters parameterSet, LMOtsParameters lmOtsParameters, int q, byte[] I, byte[] rootSeed) { // @@ -30,10 +30,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms int twoToH = 1 << parameterSet.H; - return new LMSPrivateKeyParameters(parameterSet, lmOtsParameters, q, I, twoToH, rootSeed); + return new LmsPrivateKeyParameters(parameterSet, lmOtsParameters, q, I, twoToH, rootSeed); } - public static LMSSignature GenerateSign(LMSPrivateKeyParameters privateKey, byte[] message) + public static LmsSignature GenerateSign(LmsPrivateKeyParameters privateKey, byte[] message) { // // Get T from the public key. @@ -42,14 +42,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // byte[][] T = new byte[privateKey.getMaxQ()][]; // Step 2 - LMSContext context = privateKey.GenerateLmsContext(); + LmsContext context = privateKey.GenerateLmsContext(); context.BlockUpdate(message, 0, message.Length); return GenerateSign(context); } - public static LMSSignature GenerateSign(LMSContext context) + public static LmsSignature GenerateSign(LmsContext context) { // // Get T from the public key. @@ -59,36 +59,36 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // Step 1. LMOtsSignature ots_signature = - LM_OTS.LMOtsGenerateSignature(context.PrivateKey, context.GetQ(), context.C); + LMOts.LMOtsGenerateSignature(context.PrivateKey, context.GetQ(), context.C); - return new LMSSignature(context.PrivateKey.Q, ots_signature, context.SigParams, context.Path); + return new LmsSignature(context.PrivateKey.Q, ots_signature, context.SigParams, context.Path); } - public static bool VerifySignature(LMSPublicKeyParameters publicKey, LMSSignature S, byte[] message) + public static bool VerifySignature(LmsPublicKeyParameters publicKey, LmsSignature S, byte[] message) { - LMSContext context = publicKey.GenerateOtsContext(S); + LmsContext context = publicKey.GenerateOtsContext(S); - LmsUtils.ByteArray(message, context); + LmsUtilities.ByteArray(message, context); return VerifySignature(publicKey, context); } - public static bool VerifySignature(LMSPublicKeyParameters publicKey, byte[] S, byte[] message) + public static bool VerifySignature(LmsPublicKeyParameters publicKey, byte[] S, byte[] message) { - LMSContext context = publicKey.GenerateLmsContext(S); + LmsContext context = publicKey.GenerateLmsContext(S); - LmsUtils.ByteArray(message, context); + LmsUtilities.ByteArray(message, context); return VerifySignature(publicKey, context); } - public static bool VerifySignature(LMSPublicKeyParameters publicKey, LMSContext context) + public static bool VerifySignature(LmsPublicKeyParameters publicKey, LmsContext context) { - LMSSignature S = (LMSSignature)context.Signature; + LmsSignature S = (LmsSignature)context.Signature; LMSigParameters lmsParameter = S.SigParameters; int h = lmsParameter.H; byte[][] path = S.Y; - byte[] Kc = LM_OTS.LMOtsValidateSignatureCalculate(context); + byte[] Kc = LMOts.LMOtsValidateSignatureCalculate(context); // Step 4 // node_num = 2^h + q int node_num = (1 << h) + S.Q; @@ -99,8 +99,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms byte[] tmp = new byte[H.GetDigestSize()]; H.BlockUpdate(I, 0, I.Length); - LmsUtils.U32Str(node_num, H); - LmsUtils.U16Str(D_LEAF, H); + LmsUtilities.U32Str(node_num, H); + LmsUtilities.U16Str(D_LEAF, H); H.BlockUpdate(Kc, 0, Kc.Length); H.DoFinal(tmp, 0); @@ -112,8 +112,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { // is odd H.BlockUpdate(I, 0, I.Length); - LmsUtils.U32Str(node_num / 2, H); - LmsUtils.U16Str(D_INTR, H); + LmsUtilities.U32Str(node_num / 2, H); + LmsUtilities.U16Str(D_INTR, H); H.BlockUpdate(path[i], 0, path[i].Length); H.BlockUpdate(tmp, 0, tmp.Length); H.DoFinal(tmp, 0); @@ -121,8 +121,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms else { H.BlockUpdate(I, 0, I.Length); - LmsUtils.U32Str(node_num / 2, H); - LmsUtils.U16Str(D_INTR, H); + LmsUtilities.U32Str(node_num / 2, H); + LmsUtilities.U16Str(D_INTR, H); H.BlockUpdate(tmp, 0, tmp.Length); H.BlockUpdate(path[i], 0, path[i].Length); H.DoFinal(tmp, 0); diff --git a/crypto/src/pqc/crypto/lms/LMSContext.cs b/crypto/src/pqc/crypto/lms/LMSContext.cs index 12975b637..6fcbd9413 100644 --- a/crypto/src/pqc/crypto/lms/LMSContext.cs +++ b/crypto/src/pqc/crypto/lms/LMSContext.cs @@ -4,7 +4,7 @@ using Org.BouncyCastle.Crypto; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public sealed class LMSContext + public sealed class LmsContext : IDigest { private readonly byte[] m_c; @@ -14,10 +14,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms private readonly LMOtsPublicKey m_publicKey; private readonly object m_signature; - private LMSSignedPubKey[] m_signedPubKeys; + private LmsSignedPubKey[] m_signedPubKeys; private volatile IDigest m_digest; - public LMSContext(LMOtsPrivateKey privateKey, LMSigParameters sigParams, IDigest digest, byte[] C, + public LmsContext(LMOtsPrivateKey privateKey, LMSigParameters sigParams, IDigest digest, byte[] C, byte[][] path) { m_privateKey = privateKey; @@ -29,7 +29,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms m_signature = null; } - public LMSContext(LMOtsPublicKey publicKey, object signature, IDigest digest) + public LmsContext(LMOtsPublicKey publicKey, object signature, IDigest digest) { m_publicKey = publicKey; m_signature = signature; @@ -44,7 +44,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms public byte[] GetQ() { - byte[] Q = new byte[LM_OTS.MAX_HASH + 2]; + byte[] Q = new byte[LMOts.MAX_HASH + 2]; m_digest.DoFinal(Q, 0); m_digest = null; return Q; @@ -60,9 +60,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms public object Signature => m_signature; - internal LMSSignedPubKey[] SignedPubKeys => m_signedPubKeys; + internal LmsSignedPubKey[] SignedPubKeys => m_signedPubKeys; - internal LMSContext WithSignedPublicKeys(LMSSignedPubKey[] signedPubKeys) + internal LmsContext WithSignedPublicKeys(LmsSignedPubKey[] signedPubKeys) { m_signedPubKeys = signedPubKeys; return this; diff --git a/crypto/src/pqc/crypto/lms/LMSException.cs b/crypto/src/pqc/crypto/lms/LMSException.cs index c9c286b7f..694a370ed 100644 --- a/crypto/src/pqc/crypto/lms/LMSException.cs +++ b/crypto/src/pqc/crypto/lms/LMSException.cs @@ -4,25 +4,25 @@ using System.Runtime.Serialization; namespace Org.BouncyCastle.Pqc.Crypto.Lms { [Serializable] - public class LMSException + public class LmsException : Exception { - public LMSException() + public LmsException() : base() { } - public LMSException(string message) + public LmsException(string message) : base(message) { } - public LMSException(string message, Exception innerException) + public LmsException(string message, Exception innerException) : base(message, innerException) { } - protected LMSException(SerializationInfo info, StreamingContext context) + protected LmsException(SerializationInfo info, StreamingContext context) : base(info, context) { } diff --git a/crypto/src/pqc/crypto/lms/LMSKeyGenerationParameters.cs b/crypto/src/pqc/crypto/lms/LMSKeyGenerationParameters.cs index dd932c17d..ed2cb8957 100644 --- a/crypto/src/pqc/crypto/lms/LMSKeyGenerationParameters.cs +++ b/crypto/src/pqc/crypto/lms/LMSKeyGenerationParameters.cs @@ -3,10 +3,10 @@ using Org.BouncyCastle.Security; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class LMSKeyGenerationParameters + public class LmsKeyGenerationParameters : KeyGenerationParameters { - private readonly LMSParameters m_lmsParameters; + private readonly LmsParameters m_lmsParameters; /** * Base constructor - parameters and a source of randomness. @@ -14,12 +14,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms * @param lmsParameters LMS parameter set to use. * @param random the random byte source. */ - public LMSKeyGenerationParameters(LMSParameters lmsParameters, SecureRandom random) - : base(random, LmsUtils.CalculateStrength(lmsParameters)) // TODO: need something for "strength" + public LmsKeyGenerationParameters(LmsParameters lmsParameters, SecureRandom random) + : base(random, LmsUtilities.CalculateStrength(lmsParameters)) // TODO: need something for "strength" { m_lmsParameters = lmsParameters; } - public LMSParameters LmsParameters => m_lmsParameters; + public LmsParameters LmsParameters => m_lmsParameters; } } diff --git a/crypto/src/pqc/crypto/lms/LMSKeyPairGenerator.cs b/crypto/src/pqc/crypto/lms/LMSKeyPairGenerator.cs index 9147c3fdf..e1afb00d9 100644 --- a/crypto/src/pqc/crypto/lms/LMSKeyPairGenerator.cs +++ b/crypto/src/pqc/crypto/lms/LMSKeyPairGenerator.cs @@ -3,14 +3,14 @@ using Org.BouncyCastle.Security; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public sealed class LMSKeyPairGenerator + public sealed class LmsKeyPairGenerator : IAsymmetricCipherKeyPairGenerator { - private LMSKeyGenerationParameters m_parameters; + private LmsKeyGenerationParameters m_parameters; public void Init(KeyGenerationParameters parameters) { - m_parameters = (LMSKeyGenerationParameters)parameters; + m_parameters = (LmsKeyGenerationParameters)parameters; } public AsymmetricCipherKeyPair GenerateKeyPair() @@ -23,7 +23,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms byte[] rootSecret = new byte[32]; source.NextBytes(rootSecret); - LMSPrivateKeyParameters privKey = LMS.GenerateKeys(m_parameters.LmsParameters.LMSigParameters, + LmsPrivateKeyParameters privKey = Lms.GenerateKeys(m_parameters.LmsParameters.LMSigParameters, m_parameters.LmsParameters.LMOtsParameters, 0, I, rootSecret); return new AsymmetricCipherKeyPair(privKey.GetPublicKey(), privKey); diff --git a/crypto/src/pqc/crypto/lms/LMSKeyParameters.cs b/crypto/src/pqc/crypto/lms/LMSKeyParameters.cs index e4b2df66d..b35ba36c4 100644 --- a/crypto/src/pqc/crypto/lms/LMSKeyParameters.cs +++ b/crypto/src/pqc/crypto/lms/LMSKeyParameters.cs @@ -3,10 +3,10 @@ using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public abstract class LMSKeyParameters + public abstract class LmsKeyParameters : AsymmetricKeyParameter, IEncodable { - protected LMSKeyParameters(bool isPrivateKey) + protected LmsKeyParameters(bool isPrivateKey) : base(isPrivateKey) { } diff --git a/crypto/src/pqc/crypto/lms/LMSParameters.cs b/crypto/src/pqc/crypto/lms/LMSParameters.cs index adffad2da..49ce2f71a 100644 --- a/crypto/src/pqc/crypto/lms/LMSParameters.cs +++ b/crypto/src/pqc/crypto/lms/LMSParameters.cs @@ -1,11 +1,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public sealed class LMSParameters + public sealed class LmsParameters { private readonly LMSigParameters m_lmSigParameters; private readonly LMOtsParameters m_lmOtsParameters; - public LMSParameters(LMSigParameters lmSigParameters, LMOtsParameters lmOtsParameters) + public LmsParameters(LMSigParameters lmSigParameters, LMOtsParameters lmOtsParameters) { m_lmSigParameters = lmSigParameters; m_lmOtsParameters = lmOtsParameters; diff --git a/crypto/src/pqc/crypto/lms/LMSPrivateKeyParameters.cs b/crypto/src/pqc/crypto/lms/LMSPrivateKeyParameters.cs index ff1a9f6fd..25ca81938 100644 --- a/crypto/src/pqc/crypto/lms/LMSPrivateKeyParameters.cs +++ b/crypto/src/pqc/crypto/lms/LMSPrivateKeyParameters.cs @@ -9,13 +9,13 @@ using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class LMSPrivateKeyParameters - : LMSKeyParameters, ILMSContextBasedSigner + public class LmsPrivateKeyParameters + : LmsKeyParameters, ILmsContextBasedSigner { private static CacheKey T1 = new CacheKey(1); private static CacheKey[] internedKeys = new CacheKey[129]; - static LMSPrivateKeyParameters() + static LmsPrivateKeyParameters() { internedKeys[1] = T1; for (int i = 2; i < internedKeys.Length; i++) @@ -39,10 +39,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms // These are not final because they can be generated. // They also do not need to be persisted. // - private LMSPublicKeyParameters publicKey; + private LmsPublicKeyParameters publicKey; - public LMSPrivateKeyParameters(LMSigParameters lmsParameter, LMOtsParameters otsParameters, int q, byte[] I, + public LmsPrivateKeyParameters(LMSigParameters lmsParameter, LMOtsParameters otsParameters, int q, byte[] I, int maxQ, byte[] masterSecret) : base(true) { @@ -57,7 +57,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms this.tDigest = DigestUtilities.GetDigest(lmsParameter.DigestOid); } - private LMSPrivateKeyParameters(LMSPrivateKeyParameters parent, int q, int maxQ) + private LmsPrivateKeyParameters(LmsPrivateKeyParameters parent, int q, int maxQ) : base(true) { this.parameters = parent.parameters; @@ -72,18 +72,18 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms this.publicKey = parent.publicKey; } - public static LMSPrivateKeyParameters GetInstance(byte[] privEnc, byte[] pubEnc) + public static LmsPrivateKeyParameters GetInstance(byte[] privEnc, byte[] pubEnc) { - LMSPrivateKeyParameters pKey = GetInstance(privEnc); + LmsPrivateKeyParameters pKey = GetInstance(privEnc); - pKey.publicKey = LMSPublicKeyParameters.GetInstance(pubEnc); + pKey.publicKey = LmsPublicKeyParameters.GetInstance(pubEnc); return pKey; } - public static LMSPrivateKeyParameters GetInstance(object src) + public static LmsPrivateKeyParameters GetInstance(object src) { - if (src is LMSPrivateKeyParameters lmsPrivateKeyParameters) + if (src is LmsPrivateKeyParameters lmsPrivateKeyParameters) { return lmsPrivateKeyParameters; } @@ -91,7 +91,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { int version = BinaryReaders.ReadInt32BigEndian(binaryReader); if (version != 0) - throw new Exception("expected version 0 lms private key"); + throw new Exception("unknown version for LMS private key"); int sigParamType = BinaryReaders.ReadInt32BigEndian(binaryReader); LMSigParameters sigParameter = LMSigParameters.GetParametersByID(sigParamType); @@ -111,7 +111,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms byte[] masterSecret = BinaryReaders.ReadBytesFully(binaryReader, l); - return new LMSPrivateKeyParameters(sigParameter, otsParameter, q, I, maxQ, masterSecret); + return new LmsPrivateKeyParameters(sigParameter, otsParameter, q, I, maxQ, masterSecret); } else if (src is byte[] bytes) { @@ -166,7 +166,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms q++; } - public LMSContext GenerateLmsContext() + public LmsContext GenerateLmsContext() { // Step 1. LMSigParameters lmsParameter = this.GetSigParameters(); @@ -191,11 +191,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return otsPk.GetSignatureContext(this.GetSigParameters(), path); } - public byte[] GenerateSignature(LMSContext context) + public byte[] GenerateSignature(LmsContext context) { try { - return LMS.GenerateSign(context).GetEncoded(); + return Lms.GenerateSign(context).GetEncoded(); } catch (IOException e) { @@ -225,14 +225,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms * @param usageCount the number of usages the key should have. * @return a key based on the current key that can be used usageCount times. */ - public LMSPrivateKeyParameters ExtractKeyShard(int usageCount) + public LmsPrivateKeyParameters ExtractKeyShard(int usageCount) { lock (this) { if (q + usageCount >= maxQ) throw new ArgumentException("usageCount exceeds usages remaining"); - LMSPrivateKeyParameters keyParameters = new LMSPrivateKeyParameters(this, q, q + usageCount); + LmsPrivateKeyParameters keyParameters = new LmsPrivateKeyParameters(this, q, q + usageCount); q += usageCount; return keyParameters; @@ -264,13 +264,13 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return maxQ - q; } - public virtual LMSPublicKeyParameters GetPublicKey() + public virtual LmsPublicKeyParameters GetPublicKey() { lock (this) { if (publicKey == null) { - publicKey = new LMSPublicKeyParameters(parameters, otsParameters, this.FindT(T1), I); + publicKey = new LmsPublicKeyParameters(parameters, otsParameters, this.FindT(T1), I); } return publicKey; } @@ -313,17 +313,17 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms if (r >= twoToh) { - LmsUtils.ByteArray(this.GetI(), tDigest); - LmsUtils.U32Str(r, tDigest); - LmsUtils.U16Str(LMS.D_LEAF, tDigest); + LmsUtilities.ByteArray(this.GetI(), tDigest); + LmsUtilities.U32Str(r, tDigest); + LmsUtilities.U16Str(Lms.D_LEAF, tDigest); // // These can be pre generated at the time of key generation and held within the private key. // However it will cost memory to have them stick around. // - byte[] K = LM_OTS.LmsOtsGeneratePublicKey(this.GetOtsParameters(), this.GetI(), (r - twoToh), + byte[] K = LMOts.LmsOtsGeneratePublicKey(this.GetOtsParameters(), this.GetI(), (r - twoToh), this.GetMasterSecret()); - LmsUtils.ByteArray(K, tDigest); + LmsUtilities.ByteArray(K, tDigest); T = new byte[tDigest.GetDigestSize()]; tDigest.DoFinal(T, 0); return T; @@ -332,11 +332,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms byte[] t2r = FindT(2 * r); byte[] t2rPlus1 = FindT((2 * r + 1)); - LmsUtils.ByteArray(this.GetI(), tDigest); - LmsUtils.U32Str(r, tDigest); - LmsUtils.U16Str(LMS.D_INTR, tDigest); - LmsUtils.ByteArray(t2r, tDigest); - LmsUtils.ByteArray(t2rPlus1, tDigest); + LmsUtilities.ByteArray(this.GetI(), tDigest); + LmsUtilities.U32Str(r, tDigest); + LmsUtilities.U16Str(Lms.D_INTR, tDigest); + LmsUtilities.ByteArray(t2r, tDigest); + LmsUtilities.ByteArray(t2rPlus1, tDigest); T = new byte[tDigest.GetDigestSize()]; tDigest.DoFinal(T, 0); @@ -354,7 +354,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return false; } - LMSPrivateKeyParameters that = (LMSPrivateKeyParameters)o; + LmsPrivateKeyParameters that = (LmsPrivateKeyParameters)o; if (q != that.q) { diff --git a/crypto/src/pqc/crypto/lms/LMSPublicKeyParameters.cs b/crypto/src/pqc/crypto/lms/LMSPublicKeyParameters.cs index 80cf15827..fa12b47c3 100644 --- a/crypto/src/pqc/crypto/lms/LMSPublicKeyParameters.cs +++ b/crypto/src/pqc/crypto/lms/LMSPublicKeyParameters.cs @@ -6,15 +6,15 @@ using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class LMSPublicKeyParameters - : LMSKeyParameters, ILMSContextBasedVerifier + public class LmsPublicKeyParameters + : LmsKeyParameters, ILmsContextBasedVerifier { private LMSigParameters parameterSet; private LMOtsParameters lmOtsType; private byte[] I; private byte[] T1; - public LMSPublicKeyParameters(LMSigParameters parameterSet, LMOtsParameters lmOtsType, byte[] T1, byte[] I) + public LmsPublicKeyParameters(LMSigParameters parameterSet, LMOtsParameters lmOtsType, byte[] T1, byte[] I) : base(false) { this.parameterSet = parameterSet; @@ -23,9 +23,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms this.T1 = Arrays.Clone(T1); } - public static LMSPublicKeyParameters GetInstance(object src) + public static LmsPublicKeyParameters GetInstance(object src) { - if (src is LMSPublicKeyParameters lmsPublicKeyParameters) + if (src is LmsPublicKeyParameters lmsPublicKeyParameters) { return lmsPublicKeyParameters; } @@ -41,7 +41,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms byte[] T1 = BinaryReaders.ReadBytesFully(binaryReader, lmsParameter.M); - return new LMSPublicKeyParameters(lmsParameter, ostTypeCode, T1, I); + return new LmsPublicKeyParameters(lmsParameter, ostTypeCode, T1, I); } else if (src is byte[] bytes) { @@ -81,9 +81,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return lmOtsType; } - public LMSParameters GetLmsParameters() + public LmsParameters GetLmsParameters() { - return new LMSParameters(this.GetSigParameters(), this.GetOtsParameters()); + return new LmsParameters(this.GetSigParameters(), this.GetOtsParameters()); } public byte[] GetT1() @@ -117,7 +117,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return false; } - LMSPublicKeyParameters publicKey = (LMSPublicKeyParameters)o; + LmsPublicKeyParameters publicKey = (LmsPublicKeyParameters)o; if (!parameterSet.Equals(publicKey.parameterSet)) { @@ -153,11 +153,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms .Build(); } - public LMSContext GenerateLmsContext(byte[] signature) + public LmsContext GenerateLmsContext(byte[] signature) { try { - return GenerateOtsContext(LMSSignature.GetInstance(signature)); + return GenerateOtsContext(LmsSignature.GetInstance(signature)); } catch (IOException e) { @@ -165,7 +165,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms } } - internal LMSContext GenerateOtsContext(LMSSignature S) + internal LmsContext GenerateOtsContext(LmsSignature S) { int ots_typecode = GetOtsParameters().ID; if (S.OtsSignature.ParamType.ID != ots_typecode) @@ -178,9 +178,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms .CreateOtsContext(S); } - public bool Verify(LMSContext context) + public bool Verify(LmsContext context) { - return LMS.VerifySignature(this, context); + return Lms.VerifySignature(this, context); } } } diff --git a/crypto/src/pqc/crypto/lms/LMSSignature.cs b/crypto/src/pqc/crypto/lms/LMSSignature.cs index 3f95ec516..f5d355297 100644 --- a/crypto/src/pqc/crypto/lms/LMSSignature.cs +++ b/crypto/src/pqc/crypto/lms/LMSSignature.cs @@ -6,7 +6,7 @@ using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class LMSSignature + public class LmsSignature : IEncodable { private int q; @@ -14,7 +14,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms private LMSigParameters parameter; private byte[][] y; - public LMSSignature(int q, LMOtsSignature otsSignature, LMSigParameters parameter, byte[][] y) + public LmsSignature(int q, LMOtsSignature otsSignature, LMSigParameters parameter, byte[][] y) { this.q = q; this.otsSignature = otsSignature; @@ -22,9 +22,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms this.y = y; } - public static LMSSignature GetInstance(object src) + public static LmsSignature GetInstance(object src) { - if (src is LMSSignature lmsSignature) + if (src is LmsSignature lmsSignature) { return lmsSignature; } @@ -44,7 +44,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms binaryReader.Read(path[h], 0, path[h].Length); } - return new LMSSignature(q, otsSignature, type, path); + return new LmsSignature(q, otsSignature, type, path); } else if (src is byte[] bytes) { @@ -77,7 +77,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return false; } - LMSSignature that = (LMSSignature)o; + LmsSignature that = (LmsSignature)o; if (q != that.q) { diff --git a/crypto/src/pqc/crypto/lms/LMSSignedPubKey.cs b/crypto/src/pqc/crypto/lms/LMSSignedPubKey.cs index 024ee5f4c..5e07c9c93 100644 --- a/crypto/src/pqc/crypto/lms/LMSSignedPubKey.cs +++ b/crypto/src/pqc/crypto/lms/LMSSignedPubKey.cs @@ -4,25 +4,25 @@ using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class LMSSignedPubKey + public class LmsSignedPubKey : IEncodable { - private LMSSignature signature; - private LMSPublicKeyParameters publicKey; + private LmsSignature signature; + private LmsPublicKeyParameters publicKey; - public LMSSignedPubKey(LMSSignature signature, LMSPublicKeyParameters publicKey) + public LmsSignedPubKey(LmsSignature signature, LmsPublicKeyParameters publicKey) { this.signature = signature; this.publicKey = publicKey; } - public LMSSignature GetSignature() + public LmsSignature GetSignature() { return signature; } - public LMSPublicKeyParameters GetPublicKey() + public LmsPublicKeyParameters GetPublicKey() { return publicKey; } @@ -38,7 +38,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms return false; } - LMSSignedPubKey that = (LMSSignedPubKey)o; + LmsSignedPubKey that = (LmsSignedPubKey)o; if (signature != null ? !signature.Equals(that.signature) : that.signature != null) { diff --git a/crypto/src/pqc/crypto/lms/LMSSigner.cs b/crypto/src/pqc/crypto/lms/LMSSigner.cs index 05824307a..edbfd7330 100644 --- a/crypto/src/pqc/crypto/lms/LMSSigner.cs +++ b/crypto/src/pqc/crypto/lms/LMSSigner.cs @@ -5,21 +5,21 @@ using Org.BouncyCastle.Crypto; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public sealed class LMSSigner + public sealed class LmsSigner : IMessageSigner { - private LMSPrivateKeyParameters m_privateKey; - private LMSPublicKeyParameters m_publicKey; + private LmsPrivateKeyParameters m_privateKey; + private LmsPublicKeyParameters m_publicKey; public void Init(bool forSigning, ICipherParameters param) { if (forSigning) { - m_privateKey = (LMSPrivateKeyParameters)param; + m_privateKey = (LmsPrivateKeyParameters)param; } else { - m_publicKey = (LMSPublicKeyParameters)param; + m_publicKey = (LmsPublicKeyParameters)param; } } @@ -27,7 +27,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { try { - return LMS.GenerateSign(m_privateKey, message).GetEncoded(); + return Lms.GenerateSign(m_privateKey, message).GetEncoded(); } catch (IOException e) { @@ -39,7 +39,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms { try { - return LMS.VerifySignature(m_publicKey, LMSSignature.GetInstance(signature), message); + return Lms.VerifySignature(m_publicKey, LmsSignature.GetInstance(signature), message); } catch (IOException e) { diff --git a/crypto/src/pqc/crypto/lms/LM_OTS.cs b/crypto/src/pqc/crypto/lms/LM_OTS.cs index b9634c652..c3cd3da90 100644 --- a/crypto/src/pqc/crypto/lms/LM_OTS.cs +++ b/crypto/src/pqc/crypto/lms/LM_OTS.cs @@ -7,7 +7,7 @@ using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class LM_OTS + public static class LMOts { private static ushort D_PBLC = 0x8080; private static int ITER_K = 20; @@ -113,9 +113,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms if (!preHashed) { - LMSContext qCtx = privateKey.GetSignatureContext(sigParams, path); + LmsContext qCtx = privateKey.GetSignatureContext(sigParams, path); - LmsUtils.ByteArray(message, 0, message.Length, qCtx); + LmsUtilities.ByteArray(message, 0, message.Length, qCtx); C = qCtx.C; Q = qCtx.GetQ(); @@ -175,7 +175,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms bool prehashed) { if (!signature.ParamType.Equals(publicKey.Parameters)) // todo check - throw new LMSException("public key and signature ots types do not match"); + throw new LmsException("public key and signature ots types do not match"); return Arrays.AreEqual(LMOtsValidateSignatureCalculate(publicKey, signature, message), publicKey.K); } @@ -183,22 +183,22 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms public static byte[] LMOtsValidateSignatureCalculate(LMOtsPublicKey publicKey, LMOtsSignature signature, byte[] message) { - LMSContext ctx = publicKey.CreateOtsContext(signature); + LmsContext ctx = publicKey.CreateOtsContext(signature); - LmsUtils.ByteArray(message, ctx); + LmsUtilities.ByteArray(message, ctx); return LMOtsValidateSignatureCalculate(ctx); } - public static byte[] LMOtsValidateSignatureCalculate(LMSContext context) + public static byte[] LMOtsValidateSignatureCalculate(LmsContext context) { LMOtsPublicKey publicKey = context.PublicKey; LMOtsParameters parameter = publicKey.Parameters; object sig = context.Signature; LMOtsSignature signature; - if (sig is LMSSignature) + if (sig is LmsSignature) { - signature = ((LMSSignature)sig).OtsSignature; + signature = ((LmsSignature)sig).OtsSignature; } else { @@ -218,9 +218,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms int q = publicKey.Q; IDigest finalContext = DigestUtilities.GetDigest(parameter.DigestOid); - LmsUtils.ByteArray(I, finalContext); - LmsUtils.U32Str(q, finalContext); - LmsUtils.U16Str(D_PBLC, finalContext); + LmsUtilities.ByteArray(I, finalContext); + LmsUtilities.U32Str(q, finalContext); + LmsUtilities.U16Str(D_PBLC, finalContext); byte[] tmp = Composer.Compose() .Bytes(I) diff --git a/crypto/src/pqc/crypto/lms/LmsUtils.cs b/crypto/src/pqc/crypto/lms/LmsUtils.cs index 3b08b009d..da3f457d2 100644 --- a/crypto/src/pqc/crypto/lms/LmsUtils.cs +++ b/crypto/src/pqc/crypto/lms/LmsUtils.cs @@ -4,7 +4,7 @@ using Org.BouncyCastle.Crypto; namespace Org.BouncyCastle.Pqc.Crypto.Lms { - public class LmsUtils + public static class LmsUtilities { public static void U32Str(int n, IDigest d) { @@ -30,7 +30,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms digest.BlockUpdate(array, start, len); } - public static int CalculateStrength(LMSParameters lmsParameters) + public static int CalculateStrength(LmsParameters lmsParameters) { if (lmsParameters == null) throw new ArgumentNullException(nameof(lmsParameters)); diff --git a/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs b/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs index 1bcd10934..9d6975742 100644 --- a/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs +++ b/crypto/src/pqc/crypto/utils/PrivateKeyFactory.cs @@ -24,9 +24,8 @@ using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Pqc.Crypto.Utilities { - public class PrivateKeyFactory + public static class PrivateKeyFactory { - /// <summary> Create a private key parameter from a PKCS8 PrivateKeyInfo encoding.</summary> /// <param name="privateKeyInfoData"> the PrivateKeyInfo encoding</param> /// <returns> a suitable private key parameter</returns> @@ -45,7 +44,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities return CreateKey(PrivateKeyInfo.GetInstance(new Asn1InputStream(inStr).ReadObject())); } - /// <summary> Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.</summary> /// <param name="keyInfo"> the PrivateKeyInfo object containing the key material</param> /// <returns> a suitable private key parameter</returns> @@ -66,14 +64,13 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities { byte[] pubEnc = pubKey.GetOctets(); - return LMSPrivateKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length), + return LmsPrivateKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length), Arrays.CopyOfRange(pubEnc, 4, pubEnc.Length)); } - return LMSPrivateKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); + return LmsPrivateKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); } } - if (algOID.On(BCObjectIdentifiers.pqc_kem_mceliece)) { CmcePrivateKey cmceKey = CmcePrivateKey.GetInstance(keyInfo.ParsePrivateKey()); @@ -81,7 +78,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities return new CmcePrivateKeyParameters(spParams, cmceKey.Delta, cmceKey.C, cmceKey.G, cmceKey.Alpha, cmceKey.S); } - if (algOID.On(BCObjectIdentifiers.sphincsPlus)) { byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); diff --git a/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs index 3525b79d3..be2807b19 100644 --- a/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs +++ b/crypto/src/pqc/crypto/utils/PrivateKeyInfoFactory.cs @@ -20,13 +20,8 @@ using Org.BouncyCastle.Pqc.Crypto.Hqc; namespace Org.BouncyCastle.Pqc.Crypto.Utilities { - public class PrivateKeyInfoFactory + public static class PrivateKeyInfoFactory { - private PrivateKeyInfoFactory() - { - - } - /// <summary> Create a PrivateKeyInfo representation of a private key.</summary> /// <param name="privateKey"> the key to be encoded into the info object.</param> /// <returns> the appropriate PrivateKeyInfo</returns> @@ -43,22 +38,19 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities /// <exception cref="ArgumentException"> on an error encoding the key</exception> public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey, Asn1Set attributes) { - if (privateKey is LMSPrivateKeyParameters) + if (privateKey is LmsPrivateKeyParameters lmsPrivateKeyParameters) { - LMSPrivateKeyParameters parameters = (LMSPrivateKeyParameters)privateKey; - - byte[] encoding = Composer.Compose().U32Str(1).Bytes(parameters).Build(); - byte[] pubEncoding = Composer.Compose().U32Str(1).Bytes(parameters.GetPublicKey()).Build(); + byte[] encoding = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters).Build(); + byte[] pubEncoding = Composer.Compose().U32Str(1).Bytes(lmsPrivateKeyParameters.GetPublicKey()).Build(); AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes, pubEncoding); } - if (privateKey is HSSPrivateKeyParameters) + if (privateKey is HssPrivateKeyParameters hssPrivateKeyParameters) { - HSSPrivateKeyParameters parameters = (HSSPrivateKeyParameters)privateKey; - - byte[] encoding = Composer.Compose().U32Str(parameters.L).Bytes(parameters).Build(); - byte[] pubEncoding = Composer.Compose().U32Str(parameters.L).Bytes(parameters.GetPublicKey().LmsPublicKey).Build(); + int L = hssPrivateKeyParameters.L; + byte[] encoding = Composer.Compose().U32Str(L).Bytes(hssPrivateKeyParameters).Build(); + byte[] pubEncoding = Composer.Compose().U32Str(L).Bytes(hssPrivateKeyParameters.GetPublicKey().LmsPublicKey).Build(); AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(encoding), attributes, pubEncoding); diff --git a/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs b/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs index 1d753c637..08dbaf507 100644 --- a/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs +++ b/crypto/src/pqc/crypto/utils/PublicKeyFactory.cs @@ -4,6 +4,7 @@ using System.IO; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.BC; +using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Utilities; @@ -15,6 +16,7 @@ using Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium; using Org.BouncyCastle.Pqc.Crypto.Crystals.Kyber; using Org.BouncyCastle.Pqc.Crypto.Falcon; using Org.BouncyCastle.Pqc.Crypto.Hqc; +using Org.BouncyCastle.Pqc.Crypto.Lms; using Org.BouncyCastle.Pqc.Crypto.Picnic; using Org.BouncyCastle.Pqc.Crypto.Saber; using Org.BouncyCastle.Pqc.Crypto.Sike; @@ -23,86 +25,88 @@ using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Pqc.Crypto.Utilities { - public class PublicKeyFactory + public static class PublicKeyFactory { - private static Dictionary<DerObjectIdentifier, SubjectPublicKeyInfoConverter> converters = new Dictionary<DerObjectIdentifier, SubjectPublicKeyInfoConverter>(); - + private static Dictionary<DerObjectIdentifier, SubjectPublicKeyInfoConverter> Converters = + new Dictionary<DerObjectIdentifier, SubjectPublicKeyInfoConverter>(); static PublicKeyFactory() { - converters[BCObjectIdentifiers.sphincsPlus] = new SphincsPlusConverter(); - converters[BCObjectIdentifiers.sphincsPlus_shake_256] = new SphincsPlusConverter(); - converters[BCObjectIdentifiers.sphincsPlus_sha_256] = new SphincsPlusConverter(); - converters[BCObjectIdentifiers.sphincsPlus_sha_512] = new SphincsPlusConverter(); + Converters[PkcsObjectIdentifiers.IdAlgHssLmsHashsig] = new LmsConverter(); + + Converters[BCObjectIdentifiers.sphincsPlus] = new SphincsPlusConverter(); + Converters[BCObjectIdentifiers.sphincsPlus_shake_256] = new SphincsPlusConverter(); + Converters[BCObjectIdentifiers.sphincsPlus_sha_256] = new SphincsPlusConverter(); + Converters[BCObjectIdentifiers.sphincsPlus_sha_512] = new SphincsPlusConverter(); - converters[BCObjectIdentifiers.mceliece348864_r3] = new CmceConverter(); - converters[BCObjectIdentifiers.mceliece348864f_r3] = new CmceConverter(); - converters[BCObjectIdentifiers.mceliece460896_r3] = new CmceConverter(); - converters[BCObjectIdentifiers.mceliece460896f_r3] = new CmceConverter(); - converters[BCObjectIdentifiers.mceliece6688128_r3] = new CmceConverter(); - converters[BCObjectIdentifiers.mceliece6688128f_r3] = new CmceConverter(); - converters[BCObjectIdentifiers.mceliece6960119_r3] = new CmceConverter(); - converters[BCObjectIdentifiers.mceliece6960119f_r3] = new CmceConverter(); - converters[BCObjectIdentifiers.mceliece8192128_r3] = new CmceConverter(); - converters[BCObjectIdentifiers.mceliece8192128f_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece348864_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece348864f_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece460896_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece460896f_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece6688128_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece6688128f_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece6960119_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece6960119f_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece8192128_r3] = new CmceConverter(); + Converters[BCObjectIdentifiers.mceliece8192128f_r3] = new CmceConverter(); - converters[BCObjectIdentifiers.lightsaberkem128r3] = new SaberConverter(); - converters[BCObjectIdentifiers.saberkem128r3] = new SaberConverter(); - converters[BCObjectIdentifiers.firesaberkem128r3] = new SaberConverter(); - converters[BCObjectIdentifiers.lightsaberkem192r3] = new SaberConverter(); - converters[BCObjectIdentifiers.saberkem192r3] = new SaberConverter(); - converters[BCObjectIdentifiers.firesaberkem192r3] = new SaberConverter(); - converters[BCObjectIdentifiers.lightsaberkem256r3] = new SaberConverter(); - converters[BCObjectIdentifiers.saberkem256r3] = new SaberConverter(); - converters[BCObjectIdentifiers.firesaberkem256r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.lightsaberkem128r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.saberkem128r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.firesaberkem128r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.lightsaberkem192r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.saberkem192r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.firesaberkem192r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.lightsaberkem256r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.saberkem256r3] = new SaberConverter(); + Converters[BCObjectIdentifiers.firesaberkem256r3] = new SaberConverter(); - converters[BCObjectIdentifiers.picnic] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnicl1fs] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnicl1ur] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnicl3fs] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnicl3ur] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnicl5fs] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnicl5ur] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnic3l1] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnic3l3] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnic3l5] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnicl1full] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnicl3full] = new PicnicConverter(); - converters[BCObjectIdentifiers.picnicl5full] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnic] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl1fs] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl1ur] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl3fs] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl3ur] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl5fs] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl5ur] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnic3l1] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnic3l3] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnic3l5] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl1full] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl3full] = new PicnicConverter(); + Converters[BCObjectIdentifiers.picnicl5full] = new PicnicConverter(); - converters[BCObjectIdentifiers.sikep434] = new SikeConverter(); - converters[BCObjectIdentifiers.sikep503] = new SikeConverter(); - converters[BCObjectIdentifiers.sikep610] = new SikeConverter(); - converters[BCObjectIdentifiers.sikep751] = new SikeConverter(); - converters[BCObjectIdentifiers.sikep434_compressed] = new SikeConverter(); - converters[BCObjectIdentifiers.sikep503_compressed] = new SikeConverter(); - converters[BCObjectIdentifiers.sikep610_compressed] = new SikeConverter(); - converters[BCObjectIdentifiers.sikep751_compressed] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep434] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep503] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep610] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep751] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep434_compressed] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep503_compressed] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep610_compressed] = new SikeConverter(); + Converters[BCObjectIdentifiers.sikep751_compressed] = new SikeConverter(); - converters[BCObjectIdentifiers.dilithium2] = new DilithiumConverter(); - converters[BCObjectIdentifiers.dilithium3] = new DilithiumConverter(); - converters[BCObjectIdentifiers.dilithium5] = new DilithiumConverter(); - converters[BCObjectIdentifiers.dilithium2_aes] = new DilithiumConverter(); - converters[BCObjectIdentifiers.dilithium3_aes] = new DilithiumConverter(); - converters[BCObjectIdentifiers.dilithium5_aes] = new DilithiumConverter(); + Converters[BCObjectIdentifiers.dilithium2] = new DilithiumConverter(); + Converters[BCObjectIdentifiers.dilithium3] = new DilithiumConverter(); + Converters[BCObjectIdentifiers.dilithium5] = new DilithiumConverter(); + Converters[BCObjectIdentifiers.dilithium2_aes] = new DilithiumConverter(); + Converters[BCObjectIdentifiers.dilithium3_aes] = new DilithiumConverter(); + Converters[BCObjectIdentifiers.dilithium5_aes] = new DilithiumConverter(); - converters[BCObjectIdentifiers.falcon_512] = new FalconConverter(); - converters[BCObjectIdentifiers.falcon_1024] = new FalconConverter(); + Converters[BCObjectIdentifiers.falcon_512] = new FalconConverter(); + Converters[BCObjectIdentifiers.falcon_1024] = new FalconConverter(); - converters[BCObjectIdentifiers.kyber512] = new KyberConverter(); - converters[BCObjectIdentifiers.kyber512_aes] = new KyberConverter(); - converters[BCObjectIdentifiers.kyber768] = new KyberConverter(); - converters[BCObjectIdentifiers.kyber768_aes] = new KyberConverter(); - converters[BCObjectIdentifiers.kyber1024] = new KyberConverter(); - converters[BCObjectIdentifiers.kyber1024_aes] = new KyberConverter(); - - converters[BCObjectIdentifiers.bike128] = new BikeConverter(); - converters[BCObjectIdentifiers.bike192] = new BikeConverter(); - converters[BCObjectIdentifiers.bike256] = new BikeConverter(); - - converters[BCObjectIdentifiers.hqc128] = new HqcConverter(); - converters[BCObjectIdentifiers.hqc192] = new HqcConverter(); - converters[BCObjectIdentifiers.hqc256] = new HqcConverter(); + Converters[BCObjectIdentifiers.kyber512] = new KyberConverter(); + Converters[BCObjectIdentifiers.kyber512_aes] = new KyberConverter(); + Converters[BCObjectIdentifiers.kyber768] = new KyberConverter(); + Converters[BCObjectIdentifiers.kyber768_aes] = new KyberConverter(); + Converters[BCObjectIdentifiers.kyber1024] = new KyberConverter(); + Converters[BCObjectIdentifiers.kyber1024_aes] = new KyberConverter(); + + Converters[BCObjectIdentifiers.bike128] = new BikeConverter(); + Converters[BCObjectIdentifiers.bike192] = new BikeConverter(); + Converters[BCObjectIdentifiers.bike256] = new BikeConverter(); + + Converters[BCObjectIdentifiers.hqc128] = new HqcConverter(); + Converters[BCObjectIdentifiers.hqc192] = new HqcConverter(); + Converters[BCObjectIdentifiers.hqc256] = new HqcConverter(); } /// <summary> Create a public key from a SubjectPublicKeyInfo encoding</summary> @@ -140,7 +144,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities public static AsymmetricKeyParameter CreateKey(SubjectPublicKeyInfo keyInfo, object defaultParams) { AlgorithmIdentifier algId = keyInfo.AlgorithmID; - SubjectPublicKeyInfoConverter converter = (SubjectPublicKeyInfoConverter)converters[algId.Algorithm]; + SubjectPublicKeyInfoConverter converter = (SubjectPublicKeyInfoConverter)Converters[algId.Algorithm]; if (converter != null) { @@ -155,7 +159,30 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities { internal abstract AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams); } - + + private class LmsConverter + : SubjectPublicKeyInfoConverter + { + internal override AsymmetricKeyParameter GetPublicKeyParameters(SubjectPublicKeyInfo keyInfo, object defaultParams) + { + byte[] keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePublicKey()).GetOctets(); + + if (Pack.BE_To_UInt32(keyEnc, 0) == 1U) + { + return LmsPublicKeyParameters.GetInstance(Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length)); + } + else + { + // public key with extra tree height + if (keyEnc.Length == 64) + { + keyEnc = Arrays.CopyOfRange(keyEnc, 4, keyEnc.Length); + } + return HssPublicKeyParameters.GetInstance(keyEnc); + } + } + } + private class SphincsPlusConverter : SubjectPublicKeyInfoConverter { diff --git a/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs index 7ef63871f..e44a91a26 100644 --- a/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs +++ b/crypto/src/pqc/crypto/utils/SubjectPublicKeyInfoFactory.cs @@ -1,6 +1,7 @@ using System; using Org.BouncyCastle.Asn1; +using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Math; @@ -11,6 +12,7 @@ using Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium; using Org.BouncyCastle.Pqc.Crypto.Crystals.Kyber; using Org.BouncyCastle.Pqc.Crypto.Falcon; using Org.BouncyCastle.Pqc.Crypto.Hqc; +using Org.BouncyCastle.Pqc.Crypto.Lms; using Org.BouncyCastle.Pqc.Crypto.Picnic; using Org.BouncyCastle.Pqc.Crypto.Saber; using Org.BouncyCastle.Pqc.Crypto.Sike; @@ -23,26 +25,36 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities /// <summary> /// A factory to produce Public Key Info Objects. /// </summary> - public class SubjectPublicKeyInfoFactory + public static class SubjectPublicKeyInfoFactory { - private SubjectPublicKeyInfoFactory() - { - } - /// <summary> /// Create a Subject Public Key Info object for a given public key. /// </summary> /// <param name="publicKey">One of ElGammalPublicKeyParameters, DSAPublicKeyParameter, DHPublicKeyParameters, RsaKeyParameters or ECPublicKeyParameters</param> /// <returns>A subject public key info object.</returns> /// <exception cref="Exception">Throw exception if object provided is not one of the above.</exception> - public static SubjectPublicKeyInfo CreateSubjectPublicKeyInfo( - AsymmetricKeyParameter publicKey) + public static SubjectPublicKeyInfo CreateSubjectPublicKeyInfo(AsymmetricKeyParameter publicKey) { if (publicKey == null) throw new ArgumentNullException("publicKey"); if (publicKey.IsPrivate) throw new ArgumentException("Private key passed - public key expected.", "publicKey"); - + + if (publicKey is LmsPublicKeyParameters lmsPublicKeyParameters) + { + byte[] encoding = Composer.Compose().U32Str(1).Bytes(lmsPublicKeyParameters).Build(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); + return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); + } + if (publicKey is HssPublicKeyParameters hssPublicKeyParameters) + { + int L = hssPublicKeyParameters.L; + byte[] encoding = Composer.Compose().U32Str(L).Bytes(hssPublicKeyParameters.LmsPublicKey).Build(); + + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgHssLmsHashsig); + return new SubjectPublicKeyInfo(algorithmIdentifier, new DerOctetString(encoding)); + } if (publicKey is SphincsPlusPublicKeyParameters) { SphincsPlusPublicKeyParameters parameters = (SphincsPlusPublicKeyParameters)publicKey; diff --git a/crypto/test/src/pqc/crypto/lms/test/HssTests.cs b/crypto/test/src/pqc/crypto/lms/test/HssTests.cs index fcc3cb9c6..cbfbdbdd7 100644 --- a/crypto/test/src/pqc/crypto/lms/test/HssTests.cs +++ b/crypto/test/src/pqc/crypto/lms/test/HssTests.cs @@ -27,19 +27,19 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests FixedSecureRandom.Source[] source = { new FixedSecureRandom.Source(fixedSource) }; SecureRandom rand = new FixedSecureRandom(source); - HSSPrivateKeyParameters generatedPrivateKey = HSS.GenerateHssKeyPair( - new HSSKeyGenerationParameters(new LMSParameters[] + HssPrivateKeyParameters generatedPrivateKey = Hss.GenerateHssKeyPair( + new HssKeyGenerationParameters(new LmsParameters[] { - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), }, rand) ); - HSSSignature sigFromGeneratedPrivateKey = HSS.GenerateSignature(generatedPrivateKey, Hex.Decode("ABCDEF")); + HssSignature sigFromGeneratedPrivateKey = Hss.GenerateSignature(generatedPrivateKey, Hex.Decode("ABCDEF")); byte[] keyPairEnc = generatedPrivateKey.GetEncoded(); - HSSPrivateKeyParameters reconstructedPrivateKey = HSSPrivateKeyParameters.GetInstance(keyPairEnc); + HssPrivateKeyParameters reconstructedPrivateKey = HssPrivateKeyParameters.GetInstance(keyPairEnc); Assert.True(reconstructedPrivateKey.Equals(generatedPrivateKey)); reconstructedPrivateKey.GetPublicKey(); @@ -54,7 +54,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // // Check the reconstructed key can verify a signature. // - Assert.True(HSS.VerifySignature(reconstructedPrivateKey.GetPublicKey(), sigFromGeneratedPrivateKey, + Assert.True(Hss.VerifySignature(reconstructedPrivateKey.GetPublicKey(), sigFromGeneratedPrivateKey, Hex.Decode("ABCDEF"))); } @@ -67,10 +67,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests { var blocks = LoadVector("pqc.lms.testcase_1.txt"); - HSSPublicKeyParameters publicKey = HSSPublicKeyParameters.GetInstance(blocks[0]); + HssPublicKeyParameters publicKey = HssPublicKeyParameters.GetInstance(blocks[0]); byte[] message = blocks[1]; - HSSSignature signature = HSSSignature.GetInstance(blocks[2], publicKey.L); - Assert.True(HSS.VerifySignature(publicKey, signature, message), "Test Case 1 "); + HssSignature signature = HssSignature.GetInstance(blocks[2], publicKey.L); + Assert.True(Hss.VerifySignature(publicKey, signature, message), "Test Case 1 "); } /** @@ -82,16 +82,16 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests { var blocks = LoadVector("pqc.lms.testcase_2.txt"); - HSSPublicKeyParameters publicKey = HSSPublicKeyParameters.GetInstance(blocks[0]); + HssPublicKeyParameters publicKey = HssPublicKeyParameters.GetInstance(blocks[0]); byte[] message = blocks[1]; byte[] sig = blocks[2]; - HSSSignature signature = HSSSignature.GetInstance(sig, publicKey.L); - Assert.True(HSS.VerifySignature(publicKey, signature, message), "Test Case 2 Signature"); + HssSignature signature = HssSignature.GetInstance(sig, publicKey.L); + Assert.True(Hss.VerifySignature(publicKey, signature, message), "Test Case 2 Signature"); - LMSPublicKeyParameters lmsPub = LMSPublicKeyParameters.GetInstance(blocks[3]); - LMSSignature lmsSignature = LMSSignature.GetInstance(blocks[4]); + LmsPublicKeyParameters lmsPub = LmsPublicKeyParameters.GetInstance(blocks[3]); + LmsSignature lmsSignature = LmsSignature.GetInstance(blocks[4]); - Assert.True(LMS.VerifySignature(lmsPub, lmsSignature, message), "Test Case 2 Signature 2"); + Assert.True(Lms.VerifySignature(lmsPub, lmsSignature, message), "Test Case 2 Signature 2"); } private IList<byte[]> LoadVector(string vector) @@ -132,9 +132,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests { byte[] seed = Hex.Decode("558b8966c48ae9cb898b423c83443aae014a72f1b1ab5cc85cf1d892903b5439"); int level = 0; - LMSPrivateKeyParameters lmsPrivateKey = LMS.GenerateKeys(LMSigParameters.GetParametersByID(6), + LmsPrivateKeyParameters lmsPrivateKey = Lms.GenerateKeys(LMSigParameters.GetParametersByID(6), LMOtsParameters.GetParametersByID(3), level, Hex.Decode("d08fabd4a2091ff0a8cb4ed834e74534"), seed); - LMSPublicKeyParameters publicKey = lmsPrivateKey.GetPublicKey(); + LmsPublicKeyParameters publicKey = lmsPrivateKey.GetPublicKey(); Assert.True(Arrays.AreEqual(publicKey.GetT1(), Hex.Decode("32a58885cd9ba0431235466bff9651c6c92124404d45fa53cf161c28f1ad5a8e"))); Assert.True(Arrays.AreEqual(publicKey.GetI(), Hex.Decode("d08fabd4a2091ff0a8cb4ed834e74534"))); @@ -149,9 +149,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests { byte[] seed = Hex.Decode("a1c4696e2608035a886100d05cd99945eb3370731884a8235e2fb3d4d71f2547"); int level = 1; - LMSPrivateKeyParameters lmsPrivateKey = LMS.GenerateKeys(LMSigParameters.GetParametersByID(5), + LmsPrivateKeyParameters lmsPrivateKey = Lms.GenerateKeys(LMSigParameters.GetParametersByID(5), LMOtsParameters.GetParametersByID(4), level, Hex.Decode("215f83b7ccb9acbcd08db97b0d04dc2b"), seed); - LMSPublicKeyParameters publicKey = lmsPrivateKey.GetPublicKey(); + LmsPublicKeyParameters publicKey = lmsPrivateKey.GetPublicKey(); Assert.True(Arrays.AreEqual(publicKey.GetT1(), Hex.Decode("a1cd035833e0e90059603f26e07ad2aad152338e7a5e5984bcd5f7bb4eba40b7"))); Assert.True(Arrays.AreEqual(publicKey.GetI(), Hex.Decode("215f83b7ccb9acbcd08db97b0d04dc2b"))); @@ -174,11 +174,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests FixedSecureRandom.Source[] source = { new FixedSecureRandom.Source(fixedSource) }; SecureRandom rand = new FixedSecureRandom(source); - HSSPrivateKeyParameters keyPair = HSS.GenerateHssKeyPair( - new HSSKeyGenerationParameters(new LMSParameters[] + HssPrivateKeyParameters keyPair = Hss.GenerateHssKeyPair( + new HssKeyGenerationParameters(new LmsParameters[] { - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), }, rand)); // @@ -194,7 +194,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // Check that HSS public keys have value equality after deserialization. // Use external sourced pk for deserialization. // - Assert.True(keyPair.GetPublicKey().Equals(HSSPublicKeyParameters.GetInstance(Hex.Decode(expectedPk))), + Assert.True(keyPair.GetPublicKey().Equals(HssPublicKeyParameters.GetInstance(Hex.Decode(expectedPk))), "HSSPrivateKeyParameterss equal are deserialization"); // @@ -205,11 +205,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests FixedSecureRandom.Source[] source1 = { new FixedSecureRandom.Source(fixedSource) }; SecureRandom rand1 = new FixedSecureRandom(source1); - HSSPrivateKeyParameters regenKeyPair = HSS.GenerateHssKeyPair( - new HSSKeyGenerationParameters(new LMSParameters[] + HssPrivateKeyParameters regenKeyPair = Hss.GenerateHssKeyPair( + new HssKeyGenerationParameters(new LmsParameters[] { - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), }, rand1)); Assert.True( @@ -231,8 +231,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // // Deserialize them and see if they still equal. // - LMSPrivateKeyParameters pk1O = LMSPrivateKeyParameters.GetInstance(pk1); - LMSPrivateKeyParameters pk2O = LMSPrivateKeyParameters.GetInstance(pk2); + LmsPrivateKeyParameters pk1O = LmsPrivateKeyParameters.GetInstance(pk1); + LmsPrivateKeyParameters pk2O = LmsPrivateKeyParameters.GetInstance(pk2); Assert.True(pk1O.Equals(pk2O), "LmsPrivateKey still equal after deserialization"); @@ -249,11 +249,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // Use a real secure random this time. SecureRandom rand1 = new SecureRandom(); - HSSPrivateKeyParameters differentKey = HSS.GenerateHssKeyPair( - new HSSKeyGenerationParameters(new LMSParameters[] + HssPrivateKeyParameters differentKey = Hss.GenerateHssKeyPair( + new HssKeyGenerationParameters(new LmsParameters[] { - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), }, rand1) ); @@ -273,8 +273,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // // Deserialize them and see if they still equal. // - LMSPrivateKeyParameters pk1O = LMSPrivateKeyParameters.GetInstance(pk1); - LMSPrivateKeyParameters pk2O = LMSPrivateKeyParameters.GetInstance(pk2); + LmsPrivateKeyParameters pk1O = LmsPrivateKeyParameters.GetInstance(pk1); + LmsPrivateKeyParameters pk2O = LmsPrivateKeyParameters.GetInstance(pk2); Assert.False(pk1O.Equals(pk2O), "LmsPrivateKey not suddenly equal after deserialization"); } @@ -355,22 +355,22 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // // Deserialize pub key from reference impl. // - HSSPublicKeyParameters vectorSourcedPubKey = HSSPublicKeyParameters.GetInstance(hssPubEnc); - var lmsParams = new List<LMSParameters>(); + HssPublicKeyParameters vectorSourcedPubKey = HssPublicKeyParameters.GetInstance(hssPubEnc); + var lmsParams = new List<LmsParameters>(); for (int i = 0; i != lmsParameters.Count; i++) { - lmsParams.Add(new LMSParameters(lmsParameters[i], lmOtsParameters[i])); + lmsParams.Add(new LmsParameters(lmsParameters[i], lmOtsParameters[i])); } // // Using our fixed entropy source generate hss keypair // - LMSParameters[] lmsParamsArray = new LMSParameters[lmsParams.Count]; + LmsParameters[] lmsParamsArray = new LmsParameters[lmsParams.Count]; lmsParams.CopyTo(lmsParamsArray, 0); - HSSPrivateKeyParameters keyPair = HSS.GenerateHssKeyPair( - new HSSKeyGenerationParameters( + HssPrivateKeyParameters keyPair = Hss.GenerateHssKeyPair( + new HssKeyGenerationParameters( lmsParamsArray, fixRnd) ); @@ -378,7 +378,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // Public Key should match vector. // Encoded value equality. - HSSPublicKeyParameters generatedPubKey = keyPair.GetPublicKey(); + HssPublicKeyParameters generatedPubKey = keyPair.GetPublicKey(); Assert.True(Arrays.AreEqual(hssPubEnc, generatedPubKey.GetEncoded())); // Value equality. @@ -388,12 +388,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // // Generate a signature using the keypair we generated. // - HSSSignature sig = HSS.GenerateSignature(keyPair, message); + HssSignature sig = Hss.GenerateSignature(keyPair, message); - HSSSignature signatureFromVector = null; + HssSignature signatureFromVector = null; if (!Arrays.AreEqual(sig.GetEncoded(), encodedSigFromVector)) { - signatureFromVector = HSSSignature.GetInstance(encodedSigFromVector, d); + signatureFromVector = HssSignature.GetInstance(encodedSigFromVector, d); signatureFromVector.Equals(sig); } @@ -401,13 +401,13 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests Assert.True(Arrays.AreEqual(sig.GetEncoded(), encodedSigFromVector)); // Check we can verify our generated signature with the vectors sourced public key. - Assert.True(HSS.VerifySignature(vectorSourcedPubKey, sig, message)); + Assert.True(Hss.VerifySignature(vectorSourcedPubKey, sig, message)); // Deserialize the signature from the vector. - signatureFromVector = HSSSignature.GetInstance(encodedSigFromVector, d); + signatureFromVector = HssSignature.GetInstance(encodedSigFromVector, d); // Can we verify signature from vector with public key from vector. - Assert.True(HSS.VerifySignature(vectorSourcedPubKey, signatureFromVector, message)); + Assert.True(Hss.VerifySignature(vectorSourcedPubKey, signatureFromVector, message)); // // Check our generated signature and the one deserialized from the vector @@ -486,23 +486,23 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests FixedSecureRandom.Source[] source = { new FixedSecureRandom.Source(fixedESBuffer.ToArray()) }; FixedSecureRandom fixRnd = new FixedSecureRandom(source); fixedESBuffer.SetLength(0); - var lmsParams = new List<LMSParameters>(); + var lmsParams = new List<LmsParameters>(); for (int i = 0; i != lmsParameters.Count; i++) { - lmsParams.Add(new LMSParameters(lmsParameters[i], lmOtsParameters[i])); + lmsParams.Add(new LmsParameters(lmsParameters[i], lmOtsParameters[i])); } - LMSParameters[] lmsParamsArray = new LMSParameters[lmsParams.Count]; + LmsParameters[] lmsParamsArray = new LmsParameters[lmsParams.Count]; lmsParams.CopyTo(lmsParamsArray, 0); - HSSPrivateKeyParameters keyPair = HSS.GenerateHssKeyPair( - new HSSKeyGenerationParameters(lmsParamsArray, fixRnd) + HssPrivateKeyParameters keyPair = Hss.GenerateHssKeyPair( + new HssKeyGenerationParameters(lmsParamsArray, fixRnd) ); Assert.True(Arrays.AreEqual(hssPubEnc, keyPair.GetPublicKey().GetEncoded())); - HSSPublicKeyParameters pubKeyFromVector = HSSPublicKeyParameters.GetInstance(hssPubEnc); - HSSPublicKeyParameters pubKeyGenerated = null; + HssPublicKeyParameters pubKeyFromVector = HssPublicKeyParameters.GetInstance(hssPubEnc); + HssPublicKeyParameters pubKeyGenerated = null; Assert.AreEqual(1024, keyPair.GetUsagesRemaining()); @@ -512,11 +512,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // Split the space up with a shard. // - HSSPrivateKeyParameters shard1 = keyPair.ExtractKeyShard(500); + HssPrivateKeyParameters shard1 = keyPair.ExtractKeyShard(500); pubKeyGenerated = shard1.GetPublicKey(); - HSSPrivateKeyParameters pair = shard1; + HssPrivateKeyParameters pair = shard1; int c = 0; for (int i = 0; i < keyPair.IndexLimit; i++) @@ -525,7 +525,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests { try { - HSS.IncrementIndex(pair); + Hss.IncrementIndex(pair); Assert.Fail("shard should be exhausted."); } catch (Exception ex) @@ -541,17 +541,17 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests if (i % 5 == 0) { - HSSSignature sigCalculated = HSS.GenerateSignature(pair, message); + HssSignature sigCalculated = Hss.GenerateSignature(pair, message); Assert.True(Arrays.AreEqual(sigCalculated.GetEncoded(), sigVectors[c])); - Assert.True(HSS.VerifySignature(pubKeyFromVector, sigCalculated, message)); - Assert.True(HSS.VerifySignature(pubKeyGenerated, sigCalculated, message)); + Assert.True(Hss.VerifySignature(pubKeyFromVector, sigCalculated, message)); + Assert.True(Hss.VerifySignature(pubKeyGenerated, sigCalculated, message)); - HSSSignature sigFromVector = HSSSignature.GetInstance(sigVectors[c], + HssSignature sigFromVector = HssSignature.GetInstance(sigVectors[c], pubKeyFromVector.L); - Assert.True(HSS.VerifySignature(pubKeyFromVector, sigFromVector, message)); - Assert.True(HSS.VerifySignature(pubKeyGenerated, sigFromVector, message)); + Assert.True(Hss.VerifySignature(pubKeyFromVector, sigFromVector, message)); + Assert.True(Hss.VerifySignature(pubKeyGenerated, sigFromVector, message)); Assert.True(sigCalculated.Equals(sigFromVector)); @@ -561,7 +561,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests } else { - HSS.IncrementIndex(pair); + Hss.IncrementIndex(pair); } } } @@ -576,32 +576,32 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests [Test] public void TestRemaining() { - HSSPrivateKeyParameters keyPair = HSS.GenerateHssKeyPair( - new HSSKeyGenerationParameters(new LMSParameters[] + HssPrivateKeyParameters keyPair = Hss.GenerateHssKeyPair( + new HssKeyGenerationParameters(new LmsParameters[] { - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2) + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2) }, new SecureRandom()) ); - LMSPrivateKeyParameters lmsKey = keyPair.GetKeys()[keyPair.L - 1]; + LmsPrivateKeyParameters lmsKey = keyPair.GetKeys()[keyPair.L - 1]; // // There should be a max of 32768 signatures for this key. // Assert.True(1024 == keyPair.GetUsagesRemaining()); - HSS.IncrementIndex(keyPair); - HSS.IncrementIndex(keyPair); - HSS.IncrementIndex(keyPair); - HSS.IncrementIndex(keyPair); - HSS.IncrementIndex(keyPair); + Hss.IncrementIndex(keyPair); + Hss.IncrementIndex(keyPair); + Hss.IncrementIndex(keyPair); + Hss.IncrementIndex(keyPair); + Hss.IncrementIndex(keyPair); Assert.True(5 == keyPair.GetIndex()); // Next key is at index 5! Assert.True(1024 - 5 == keyPair.GetUsagesRemaining()); - HSSPrivateKeyParameters shard = keyPair.ExtractKeyShard(10); + HssPrivateKeyParameters shard = keyPair.ExtractKeyShard(10); Assert.True(15 == shard.IndexLimit); Assert.True(5 == shard.GetIndex()); @@ -614,29 +614,29 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // for (int t = 0; t < 17; t++) { - HSS.IncrementIndex(keyPair); + Hss.IncrementIndex(keyPair); } // We have used 32 keys. Assert.True(1024 - 32 == keyPair.GetUsagesRemaining()); - HSS.GenerateSignature(keyPair, Encoding.ASCII.GetBytes("Foo")); + Hss.GenerateSignature(keyPair, Encoding.ASCII.GetBytes("Foo")); // // This should trigger the generation of a new key. // - LMSPrivateKeyParameters potentialNewLMSKey = keyPair.GetKeys()[keyPair.L - 1]; + LmsPrivateKeyParameters potentialNewLMSKey = keyPair.GetKeys()[keyPair.L - 1]; Assert.False(potentialNewLMSKey.Equals(lmsKey)); } [Test] public void TestSharding() { - HSSPrivateKeyParameters keyPair = HSS.GenerateHssKeyPair( - new HSSKeyGenerationParameters(new LMSParameters[] + HssPrivateKeyParameters keyPair = Hss.GenerateHssKeyPair( + new HssKeyGenerationParameters(new LmsParameters[] { - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2) + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2) }, new SecureRandom()) ); @@ -644,13 +644,13 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests Assert.True(1024 == keyPair.IndexLimit); Assert.True(0 == keyPair.GetIndex()); Assert.False(keyPair.IsShard()); - HSS.IncrementIndex(keyPair); + Hss.IncrementIndex(keyPair); // // Take a shard that should cross boundaries // - HSSPrivateKeyParameters shard = keyPair.ExtractKeyShard(48); + HssPrivateKeyParameters shard = keyPair.ExtractKeyShard(48); Assert.True(shard.IsShard()); Assert.True(48 == shard.GetUsagesRemaining()); Assert.True(49 == shard.IndexLimit); @@ -662,22 +662,22 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests int t = 47; while (--t >= 0) { - HSS.IncrementIndex(shard); + Hss.IncrementIndex(shard); } - HSSSignature sig = HSS.GenerateSignature(shard, Encoding.ASCII.GetBytes("Cats")); + HssSignature sig = Hss.GenerateSignature(shard, Encoding.ASCII.GetBytes("Cats")); // // Test it validates and nothing has gone wrong with the public keys. // - Assert.True(HSS.VerifySignature(keyPair.GetPublicKey(), sig, Encoding.ASCII.GetBytes("Cats"))); - Assert.True(HSS.VerifySignature(shard.GetPublicKey(), sig, Encoding.ASCII.GetBytes("Cats"))); + Assert.True(Hss.VerifySignature(keyPair.GetPublicKey(), sig, Encoding.ASCII.GetBytes("Cats"))); + Assert.True(Hss.VerifySignature(shard.GetPublicKey(), sig, Encoding.ASCII.GetBytes("Cats"))); // Signing again should Assert.Fail. try { - HSS.GenerateSignature(shard, Encoding.ASCII.GetBytes("Cats")); + Hss.GenerateSignature(shard, Encoding.ASCII.GetBytes("Cats")); Assert.Fail(); } catch (Exception ex) @@ -686,7 +686,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests } // Should work without throwing. - HSS.GenerateSignature(keyPair, Encoding.ASCII.GetBytes("Cats")); + Hss.GenerateSignature(keyPair, Encoding.ASCII.GetBytes("Cats")); } /** @@ -721,15 +721,15 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests { HSSSecureRandom rand = new HSSSecureRandom(); - HSSPrivateKeyParameters keyPair = HSS.GenerateHssKeyPair( - new HSSKeyGenerationParameters(new LMSParameters[] + HssPrivateKeyParameters keyPair = Hss.GenerateHssKeyPair( + new HssKeyGenerationParameters(new LmsParameters[] { - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), - new LMSParameters(LMSigParameters.lms_sha256_n32_h10, LMOtsParameters.sha256_n32_w1), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w2), + new LmsParameters(LMSigParameters.lms_sha256_n32_h10, LMOtsParameters.sha256_n32_w1), }, rand) ); - HSSPublicKeyParameters pk = keyPair.GetPublicKey(); + HssPublicKeyParameters pk = keyPair.GetPublicKey(); int ctr = 0; @@ -753,7 +753,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // The test will take over an hour to complete. // Pack_Int32_To_BE(ctr, message, 0); - HSSSignature sig = HSS.GenerateSignature(keyPair, message); + HssSignature sig = Hss.GenerateSignature(keyPair, message); Assert.True(ctr % 1024 == sig.Signature.Q); @@ -781,7 +781,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests Assert.True(keyPair.GetKeys()[t].GetIndex() - 1 == qValues[t]); } - Assert.True(HSS.VerifySignature(pk, sig, message)); + Assert.True(Hss.VerifySignature(pk, sig, message)); Assert.True(sig.Signature.SigParameters.ID == LMSigParameters.lms_sha256_n32_h10.ID); { @@ -790,12 +790,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // byte[] rawSig = sig.GetEncoded(); rawSig[100] ^= 1; - HSSSignature parsedSig = HSSSignature.GetInstance(rawSig, pk.L); - Assert.False(HSS.VerifySignature(pk, parsedSig, message)); + HssSignature parsedSig = HssSignature.GetInstance(rawSig, pk.L); + Assert.False(Hss.VerifySignature(pk, parsedSig, message)); try { - HSSSignature.GetInstance(rawSig, 0); + HssSignature.GetInstance(rawSig, 0); Assert.Fail(); } catch (Exception ex) @@ -812,7 +812,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests byte[] newMsg = new byte[message.Length]; message.CopyTo(newMsg, 0); newMsg[1] ^= 1; - Assert.False(HSS.VerifySignature(pk, sig, newMsg)); + Assert.False(Hss.VerifySignature(pk, sig, newMsg)); } { @@ -821,14 +821,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // byte[] pkEnc = pk.GetEncoded(); pkEnc[35] ^= 1; - HSSPublicKeyParameters rebuiltPk = HSSPublicKeyParameters.GetInstance(pkEnc); - Assert.False(HSS.VerifySignature(rebuiltPk, sig, message)); + HssPublicKeyParameters rebuiltPk = HssPublicKeyParameters.GetInstance(pkEnc); + Assert.False(Hss.VerifySignature(rebuiltPk, sig, message)); } } else { // Skip some keys. - HSS.IncrementIndex(keyPair); + Hss.IncrementIndex(keyPair); } ctr++; diff --git a/crypto/test/src/pqc/crypto/lms/test/LmsKeyGenTests.cs b/crypto/test/src/pqc/crypto/lms/test/LmsKeyGenTests.cs index fb66db457..0aa937b6c 100644 --- a/crypto/test/src/pqc/crypto/lms/test/LmsKeyGenTests.cs +++ b/crypto/test/src/pqc/crypto/lms/test/LmsKeyGenTests.cs @@ -32,11 +32,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests int level = 1; // This is the second level, we use this because it signs the message. // Generate the private key. - LMSPrivateKeyParameters lmsPrivateKey = LMS.GenerateKeys(LMSigParameters.GetParametersByID(5), + LmsPrivateKeyParameters lmsPrivateKey = Lms.GenerateKeys(LMSigParameters.GetParametersByID(5), LMOtsParameters.GetParametersByID(4), level, I, seed); // This derives the public key. - LMSPublicKeyParameters publicKey = lmsPrivateKey.GetPublicKey(); + LmsPublicKeyParameters publicKey = lmsPrivateKey.GetPublicKey(); // From the vector. string pkEnc = "0000000500000004215f83b7ccb9acbcd08db97b0d04dc2ba1cd035833e0e90059603f26e07ad2aad152338e7a5e5984bcd5f7bb4eba40b7"; @@ -49,7 +49,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests // lmsPrivateKey.ExtractKeyShard(3); - LMSSignature signature = LMS.GenerateSign(lmsPrivateKey, msg); + LmsSignature signature = Lms.GenerateSign(lmsPrivateKey, msg); // The expected signature as encoded. string sigEnc = "00000004\n" + @@ -140,7 +140,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests Assert.True(Arrays.AreEqual(Hex.Decode(sigEnc), signature.GetEncoded())); // Sanity test - Assert.True(LMS.VerifySignature(publicKey, signature, msg)); + Assert.True(Lms.VerifySignature(publicKey, signature, msg)); } } } diff --git a/crypto/test/src/pqc/crypto/lms/test/LmsTests.cs b/crypto/test/src/pqc/crypto/lms/test/LmsTests.cs index a5bc26252..f7ab226ec 100644 --- a/crypto/test/src/pqc/crypto/lms/test/LmsTests.cs +++ b/crypto/test/src/pqc/crypto/lms/test/LmsTests.cs @@ -14,8 +14,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests public void TestCoefFunc() { byte[] S = Hex.Decode("1234"); - Assert.AreEqual(0, LM_OTS.Coef(S, 7, 1)); - Assert.AreEqual(1, LM_OTS.Coef(S, 0, 4)); + Assert.AreEqual(0, LMOts.Coef(S, 7, 1)); + Assert.AreEqual(1, LMOts.Coef(S, 0, 4)); } [Test] @@ -27,7 +27,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests byte[] I = Hex.Decode("d08fabd4a2091ff0a8cb4ed834e74534"); LMOtsPrivateKey privateKey = new LMOtsPrivateKey(parameter, I, 0, seed); - LMOtsPublicKey publicKey = LM_OTS.LmsOtsGeneratePublicKey(privateKey); + LMOtsPublicKey publicKey = LMOts.LmsOtsGeneratePublicKey(privateKey); byte[] ms = new byte[32]; for (int t = 0; t < ms.Length; t++) @@ -35,23 +35,23 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests ms[t] = (byte)t; } - LMSContext ctx = privateKey.GetSignatureContext(null, null); + LmsContext ctx = privateKey.GetSignatureContext(null, null); ctx.BlockUpdate(ms, 0, ms.Length); - LMOtsSignature sig = LM_OTS.LMOtsGenerateSignature(privateKey, ctx.GetQ(), ctx.C); - Assert.True(LM_OTS.LMOtsValidateSignature(publicKey, sig, ms, false)); + LMOtsSignature sig = LMOts.LMOtsGenerateSignature(privateKey, ctx.GetQ(), ctx.C); + Assert.True(LMOts.LMOtsValidateSignature(publicKey, sig, ms, false)); // Recreate signature { byte[] recreatedSignature = sig.GetEncoded(); - Assert.True(LM_OTS.LMOtsValidateSignature(publicKey, LMOtsSignature.GetInstance(recreatedSignature), ms, false)); + Assert.True(LMOts.LMOtsValidateSignature(publicKey, LMOtsSignature.GetInstance(recreatedSignature), ms, false)); } // Recreate public key. { byte[] recreatedPubKey = Arrays.Clone(publicKey.GetEncoded()); - Assert.True(LM_OTS.LMOtsValidateSignature(LMOtsPublicKey.GetInstance(recreatedPubKey), sig, ms, false)); + Assert.True(LMOts.LMOtsValidateSignature(LMOtsPublicKey.GetInstance(recreatedPubKey), sig, ms, false)); } // Vandalise signature @@ -59,14 +59,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests byte[] vandalisedSignature = sig.GetEncoded(); vandalisedSignature[256] ^= 1; // Single bit error - Assert.False(LM_OTS.LMOtsValidateSignature(publicKey, LMOtsSignature.GetInstance(vandalisedSignature), ms, false)); + Assert.False(LMOts.LMOtsValidateSignature(publicKey, LMOtsSignature.GetInstance(vandalisedSignature), ms, false)); } // Vandalise public key. { byte[] vandalisedPubKey = Arrays.Clone(publicKey.GetEncoded()); vandalisedPubKey[50] ^= 1; - Assert.False(LM_OTS.LMOtsValidateSignature(LMOtsPublicKey.GetInstance(vandalisedPubKey), sig, ms, false)); + Assert.False(LMOts.LMOtsValidateSignature(LMOtsPublicKey.GetInstance(vandalisedPubKey), sig, ms, false)); } // @@ -76,10 +76,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests { byte[] vandalisedPubKey = Arrays.Clone(publicKey.GetEncoded()); vandalisedPubKey[3] += 1; - LM_OTS.LMOtsValidateSignature(LMOtsPublicKey.GetInstance(vandalisedPubKey), sig, ms, false); + LMOts.LMOtsValidateSignature(LMOtsPublicKey.GetInstance(vandalisedPubKey), sig, ms, false); Assert.True(false, "Must fail as public key type not match signature type."); } - catch (LMSException ex) + catch (LmsException ex) { Assert.True(ex.Message.Contains("public key and signature ots types do not match")); } @@ -100,22 +100,22 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests byte[] seed = Hex.Decode("a1c4696e2608035a886100d05cd99945eb3370731884a8235e2fb3d4d71f2547"); int level = 1; - LMSPrivateKeyParameters lmsPrivateKey = LMS.GenerateKeys( + LmsPrivateKeyParameters lmsPrivateKey = Lms.GenerateKeys( LMSigParameters.GetParametersByID(5), LMOtsParameters.GetParametersByID(4), level, Hex.Decode("215f83b7ccb9acbcd08db97b0d04dc2b"), seed); - LMSPublicKeyParameters publicKey = lmsPrivateKey.GetPublicKey(); + LmsPublicKeyParameters publicKey = lmsPrivateKey.GetPublicKey(); lmsPrivateKey.ExtractKeyShard(3); - LMSSignature signature = LMS.GenerateSign(lmsPrivateKey, msg); - Assert.True(LMS.VerifySignature(publicKey, signature, msg)); + LmsSignature signature = Lms.GenerateSign(lmsPrivateKey, msg); + Assert.True(Lms.VerifySignature(publicKey, signature, msg)); // Serialize / Deserialize - Assert.True(LMS.VerifySignature( - LMSPublicKeyParameters.GetInstance(publicKey.GetEncoded()), - LMSSignature.GetInstance(signature.GetEncoded()), msg)); + Assert.True(Lms.VerifySignature( + LmsPublicKeyParameters.GetInstance(publicKey.GetEncoded()), + LmsSignature.GetInstance(signature.GetEncoded()), msg)); // // Vandalise signature. @@ -123,7 +123,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests { byte[] bustedSig = Arrays.Clone(signature.GetEncoded()); bustedSig[100] ^= 1; - Assert.False(LMS.VerifySignature(publicKey, LMSSignature.GetInstance(bustedSig), msg)); + Assert.False(Lms.VerifySignature(publicKey, LmsSignature.GetInstance(bustedSig), msg)); } // @@ -132,7 +132,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests { byte[] msg2 = Arrays.Clone(msg); msg2[10] ^= 1; - Assert.False(LMS.VerifySignature(publicKey, signature, msg2)); + Assert.False(Lms.VerifySignature(publicKey, signature, msg2)); } } @@ -145,7 +145,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests byte[] I = Hex.Decode("d08fabd4a2091ff0a8cb4ed834e74534"); LMOtsPrivateKey privateKey = new LMOtsPrivateKey(parameter, I, 0, seed); - LMOtsPublicKey publicKey = LM_OTS.LmsOtsGeneratePublicKey(privateKey); + LMOtsPublicKey publicKey = LMOts.LmsOtsGeneratePublicKey(privateKey); byte[] ms = new byte[32]; for (int t = 0; t < ms.Length; t++) @@ -153,12 +153,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests ms[t] = (byte)t; } - LMSContext ctx = privateKey.GetSignatureContext(null, null); + LmsContext ctx = privateKey.GetSignatureContext(null, null); ctx.BlockUpdate(ms, 0, ms.Length); - LMOtsSignature sig = LM_OTS.LMOtsGenerateSignature(privateKey, ctx.GetQ(), ctx.C); - Assert.True(LM_OTS.LMOtsValidateSignature(publicKey, sig, ms, false)); + LMOtsSignature sig = LMOts.LMOtsGenerateSignature(privateKey, ctx.GetQ(), ctx.C); + Assert.True(LMOts.LMOtsValidateSignature(publicKey, sig, ms, false)); try { diff --git a/crypto/test/src/pqc/crypto/lms/test/TypeTests.cs b/crypto/test/src/pqc/crypto/lms/test/TypeTests.cs index 948ce4747..10f21e9f3 100644 --- a/crypto/test/src/pqc/crypto/lms/test/TypeTests.cs +++ b/crypto/test/src/pqc/crypto/lms/test/TypeTests.cs @@ -15,27 +15,27 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests [Test] public void TestTypeForType() { - LMSSignature dummySig = new LMSSignature(0, null, null, null); + LmsSignature dummySig = new LmsSignature(0, null, null, null); { - var keys = new List<LMSPrivateKeyParameters>(); - keys.Add(new LMSPrivateKeyParameters(LMSigParameters.lms_sha256_n32_h5, null, 0, null, 0, new byte[32])); + var keys = new List<LmsPrivateKeyParameters>(); + keys.Add(new LmsPrivateKeyParameters(LMSigParameters.lms_sha256_n32_h5, null, 0, null, 0, new byte[32])); - var sig = new List<LMSSignature>(); + var sig = new List<LmsSignature>(); sig.Add(dummySig); - object o = new HSSPrivateKeyParameters(0, keys, sig, 1, 2); - Assert.True(o.Equals(HSSPrivateKeyParameters.GetInstance(o))); + object o = new HssPrivateKeyParameters(0, keys, sig, 1, 2); + Assert.True(o.Equals(HssPrivateKeyParameters.GetInstance(o))); } { - object o = new HSSPublicKeyParameters(0, null); - Assert.True(o.Equals(HSSPublicKeyParameters.GetInstance(o))); + object o = new HssPublicKeyParameters(0, null); + Assert.True(o.Equals(HssPublicKeyParameters.GetInstance(o))); } { - object o = new HSSSignature(0, null, null); - Assert.True(o.Equals(HSSSignature.GetInstance(o, 0))); + object o = new HssSignature(0, null, null); + Assert.True(o.Equals(HssSignature.GetInstance(o, 0))); } { @@ -49,18 +49,18 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms.Tests } { - object o = new LMSPrivateKeyParameters(LMSigParameters.lms_sha256_n32_h5, null, 0, null, 0, null); - Assert.True(o.Equals(LMSPrivateKeyParameters.GetInstance(o))); + object o = new LmsPrivateKeyParameters(LMSigParameters.lms_sha256_n32_h5, null, 0, null, 0, null); + Assert.True(o.Equals(LmsPrivateKeyParameters.GetInstance(o))); } { - object o = new LMSPublicKeyParameters(null, null, null, null); - Assert.True(o.Equals(LMSPublicKeyParameters.GetInstance(o))); + object o = new LmsPublicKeyParameters(null, null, null, null); + Assert.True(o.Equals(LmsPublicKeyParameters.GetInstance(o))); } { - object o = new LMSSignature(0, null, null, null); - Assert.True(o.Equals(LMSSignature.GetInstance(o))); + object o = new LmsSignature(0, null, null, null); + Assert.True(o.Equals(LmsSignature.GetInstance(o))); } } } diff --git a/crypto/test/src/pqc/crypto/test/HSSTest.cs b/crypto/test/src/pqc/crypto/test/HSSTest.cs index 678f1cbab..29b5e1f5a 100644 --- a/crypto/test/src/pqc/crypto/test/HSSTest.cs +++ b/crypto/test/src/pqc/crypto/test/HSSTest.cs @@ -16,17 +16,17 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests public void TestKeyGenAndSign() { byte[] msg = Strings.ToByteArray("Hello, world!"); - IAsymmetricCipherKeyPairGenerator kpGen = new HSSKeyPairGenerator(); + IAsymmetricCipherKeyPairGenerator kpGen = new HssKeyPairGenerator(); - kpGen.Init(new HSSKeyGenerationParameters( - new LMSParameters[]{ - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4) + kpGen.Init(new HssKeyGenerationParameters( + new LmsParameters[]{ + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4) }, new SecureRandom())); AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - HSSSigner signer = new HSSSigner(); + HssSigner signer = new HssSigner(); signer.Init(true, kp.Private); @@ -41,26 +41,26 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests public void TestKeyGenAndUsage() { byte[] msg = Strings.ToByteArray("Hello, world!"); - IAsymmetricCipherKeyPairGenerator kpGen = new HSSKeyPairGenerator(); + IAsymmetricCipherKeyPairGenerator kpGen = new HssKeyPairGenerator(); - kpGen.Init(new HSSKeyGenerationParameters( - new LMSParameters[]{ - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4) + kpGen.Init(new HssKeyGenerationParameters( + new LmsParameters[]{ + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4) }, new SecureRandom())); AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - HSSPrivateKeyParameters privKey = (HSSPrivateKeyParameters)kp.Private; + HssPrivateKeyParameters privKey = (HssPrivateKeyParameters)kp.Private; - HSSPublicKeyParameters pubKey = (HSSPublicKeyParameters)kp.Public; + HssPublicKeyParameters pubKey = (HssPublicKeyParameters)kp.Public; - LMSParameters lmsParam = pubKey.LmsPublicKey.GetLmsParameters(); + LmsParameters lmsParam = pubKey.LmsPublicKey.GetLmsParameters(); Assert.AreEqual(LMSigParameters.lms_sha256_n32_h5, lmsParam.LMSigParameters); Assert.AreEqual(LMOtsParameters.sha256_n32_w4, lmsParam.LMOtsParameters); - HSSSigner signer = new HSSSigner(); + HssSigner signer = new HssSigner(); signer.Init(true, privKey); @@ -82,21 +82,21 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests byte[] msg1 = Strings.ToByteArray("Hello, world!"); byte[] msg2 = Strings.ToByteArray("Now is the time"); - IAsymmetricCipherKeyPairGenerator kpGen = new HSSKeyPairGenerator(); + IAsymmetricCipherKeyPairGenerator kpGen = new HssKeyPairGenerator(); - kpGen.Init(new HSSKeyGenerationParameters( - new LMSParameters[]{ - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4) + kpGen.Init(new HssKeyGenerationParameters( + new LmsParameters[]{ + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4) }, new SecureRandom())); AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - HSSPrivateKeyParameters privKey = ((HSSPrivateKeyParameters)kp.Private).ExtractKeyShard(2); + HssPrivateKeyParameters privKey = ((HssPrivateKeyParameters)kp.Private).ExtractKeyShard(2); - Assert.AreEqual(2, ((HSSPrivateKeyParameters)kp.Private).GetIndex()); + Assert.AreEqual(2, ((HssPrivateKeyParameters)kp.Private).GetIndex()); - HSSSigner signer = new HSSSigner(); + HssSigner signer = new HssSigner(); Assert.AreEqual(0, privKey.GetIndex()); @@ -130,11 +130,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests Assert.AreEqual("hss private key shard is exhausted", e.Message); } - signer.Init(true, ((HSSPrivateKeyParameters)kp.Private)); + signer.Init(true, ((HssPrivateKeyParameters)kp.Private)); sig = signer.GenerateSignature(msg1); - Assert.AreEqual(3, ((HSSPrivateKeyParameters)kp.Private).GetIndex()); + Assert.AreEqual(3, ((HssPrivateKeyParameters)kp.Private).GetIndex()); Assert.False(Arrays.AreEqual(sig1, sig)); diff --git a/crypto/test/src/pqc/crypto/test/LMSTest.cs b/crypto/test/src/pqc/crypto/test/LMSTest.cs index 7d9cc6da5..4392d1851 100644 --- a/crypto/test/src/pqc/crypto/test/LMSTest.cs +++ b/crypto/test/src/pqc/crypto/test/LMSTest.cs @@ -20,14 +20,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests public void TestKeyGenAndSign() { byte[] msg = Strings.ToByteArray("Hello, world!"); - IAsymmetricCipherKeyPairGenerator kpGen = new LMSKeyPairGenerator(); + IAsymmetricCipherKeyPairGenerator kpGen = new LmsKeyPairGenerator(); - kpGen.Init(new LMSKeyGenerationParameters( - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), new SecureRandom())); + kpGen.Init(new LmsKeyGenerationParameters( + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), new SecureRandom())); AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - LMSSigner signer = new LMSSigner(); + LmsSigner signer = new LmsSigner(); signer.Init(true, kp.Private); @@ -44,18 +44,18 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests byte[] msg1 = Strings.ToByteArray("Hello, world!"); byte[] msg2 = Strings.ToByteArray("Now is the time"); - IAsymmetricCipherKeyPairGenerator kpGen = new LMSKeyPairGenerator(); + IAsymmetricCipherKeyPairGenerator kpGen = new LmsKeyPairGenerator(); - kpGen.Init(new LMSKeyGenerationParameters( - new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), new SecureRandom())); + kpGen.Init(new LmsKeyGenerationParameters( + new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4), new SecureRandom())); AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair(); - LMSPrivateKeyParameters privKey = ((LMSPrivateKeyParameters)kp.Private).ExtractKeyShard(2); + LmsPrivateKeyParameters privKey = ((LmsPrivateKeyParameters)kp.Private).ExtractKeyShard(2); - Assert.AreEqual(2, ((LMSPrivateKeyParameters)kp.Private).GetIndex()); + Assert.AreEqual(2, ((LmsPrivateKeyParameters)kp.Private).GetIndex()); - LMSSigner signer = new LMSSigner(); + LmsSigner signer = new LmsSigner(); Assert.AreEqual(2, privKey.GetUsagesRemaining()); Assert.AreEqual(0, privKey.GetIndex()); @@ -90,11 +90,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests Assert.AreEqual("ots private key exhausted", e.Message); } - signer.Init(true, ((LMSPrivateKeyParameters)kp.Private)); + signer.Init(true, ((LmsPrivateKeyParameters)kp.Private)); sig = signer.GenerateSignature(msg1); - Assert.AreEqual(3, ((LMSPrivateKeyParameters)kp.Private).GetIndex()); + Assert.AreEqual(3, ((LmsPrivateKeyParameters)kp.Private).GetIndex()); Assert.False(Arrays.AreEqual(sig1, sig)); @@ -105,7 +105,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests PrivateKeyInfo pInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private);//TODO AsymmetricKeyParameter pKey = PrivateKeyFactory.CreateKey(pInfo.GetEncoded()); - signer.Init(false, ((LMSPrivateKeyParameters)pKey).GetPublicKey()); + signer.Init(false, ((LmsPrivateKeyParameters)pKey).GetPublicKey()); Assert.True(signer.VerifySignature(msg1, sig1)); } |