summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/pqc/crypto/lms/HSSPublicKeyParameters.cs6
-rw-r--r--crypto/test/src/pqc/crypto/test/HSSTest.cs40
2 files changed, 40 insertions, 6 deletions
diff --git a/crypto/src/pqc/crypto/lms/HSSPublicKeyParameters.cs b/crypto/src/pqc/crypto/lms/HSSPublicKeyParameters.cs
index 85b781228..01bac93b8 100644
--- a/crypto/src/pqc/crypto/lms/HSSPublicKeyParameters.cs
+++ b/crypto/src/pqc/crypto/lms/HSSPublicKeyParameters.cs
@@ -96,7 +96,11 @@ namespace Org.BouncyCastle.Pqc.Crypto.Lms
             }
 
             LmsSignedPubKey[] signedPubKeys = signature.GetSignedPubKeys();
-            LmsPublicKeyParameters key = signedPubKeys[signedPubKeys.Length - 1].GetPublicKey();
+            LmsPublicKeyParameters key = LmsPublicKey;
+            if (signedPubKeys.Length != 0)
+            {
+                key = signedPubKeys[signedPubKeys.Length - 1].GetPublicKey();
+            }
 
             return key.GenerateOtsContext(signature.Signature).WithSignedPublicKeys(signedPubKeys);
         }
diff --git a/crypto/test/src/pqc/crypto/test/HSSTest.cs b/crypto/test/src/pqc/crypto/test/HSSTest.cs
index 29b5e1f5a..45cabb906 100644
--- a/crypto/test/src/pqc/crypto/test/HSSTest.cs
+++ b/crypto/test/src/pqc/crypto/test/HSSTest.cs
@@ -13,16 +13,46 @@ namespace Org.BouncyCastle.Pqc.Crypto.Tests
     public class HSSTest
     {
         [Test]
+        public void TestOneLevelKeyGenAndSign()
+        {
+            byte[] msg = Strings.ToByteArray("Hello, world!");
+            IAsymmetricCipherKeyPairGenerator kpGen = new HssKeyPairGenerator();
+
+            var lmsParameters = new LmsParameters[]
+            {
+                new LmsParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4)
+            };
+            kpGen.Init(new HssKeyGenerationParameters(lmsParameters, new SecureRandom()));
+
+            AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();
+
+            HssSigner signer = new HssSigner();
+
+            signer.Init(true, kp.Private);
+
+            byte[] sig = signer.GenerateSignature(msg);
+
+            signer.Init(false, kp.Public);
+
+            Assert.True(signer.VerifySignature(msg, sig));
+
+            HssPublicKeyParameters hssPubKey = (HssPublicKeyParameters)kp.Public;
+
+            hssPubKey.GenerateLmsContext(sig);
+        }
+
+        [Test]
 		public void TestKeyGenAndSign()
         {
             byte[] msg = Strings.ToByteArray("Hello, world!");
             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)
-                }, new SecureRandom()));
+            var lmsParameters = 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(lmsParameters, new SecureRandom()));
 
             AsymmetricCipherKeyPair kp = kpGen.GenerateKeyPair();