diff options
author | David Hook <dgh@cryptoworkshop.com> | 2023-08-29 11:47:05 +1000 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2023-08-29 11:47:05 +1000 |
commit | 58cc3a5c25258977ef0d4d3898d0081575ad5ef2 (patch) | |
tree | 6e0c0fc09699dbb85949fd50d5bde4581a4fbb5a /crypto/src | |
parent | WAPI oid update - relates to github bc-java #1478 (diff) | |
parent | modified Kyber/Dilithium private keys to be in line with OQS (diff) | |
download | BouncyCastle.NET-ed25519-58cc3a5c25258977ef0d4d3898d0081575ad5ef2.tar.xz |
Merge remote-tracking branch 'refs/remotes/origin/master'
Diffstat (limited to 'crypto/src')
14 files changed, 111 insertions, 92 deletions
diff --git a/crypto/src/pqc/crypto/crystals/dilithium/DilithiumEngine.cs b/crypto/src/pqc/crypto/crystals/dilithium/DilithiumEngine.cs index b03da9748..9bbdcdad4 100644 --- a/crypto/src/pqc/crypto/crystals/dilithium/DilithiumEngine.cs +++ b/crypto/src/pqc/crypto/crystals/dilithium/DilithiumEngine.cs @@ -33,6 +33,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium public int Gamma1 { get; private set; } public int Gamma2 { get; private set; } public int Omega { get; private set; } + public int CTilde { get; private set; } public int PolyVecHPackedBytes { get; private set; } @@ -63,6 +64,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium PolyZPackedBytes = 576; PolyW1PackedBytes = 192; PolyEtaPackedBytes = 96; + CTilde = 32; break; case 3: K = 6; @@ -76,6 +78,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium PolyZPackedBytes = 640; PolyW1PackedBytes = 128; PolyEtaPackedBytes = 128; + CTilde = 48; break; case 5: K = 8; @@ -89,6 +92,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium PolyZPackedBytes = 640; PolyW1PackedBytes = 128; PolyEtaPackedBytes = 96; + CTilde = 64; break; default: throw new ArgumentException("The mode " + mode + "is not supported by Crystals Dilithium!"); @@ -106,7 +110,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium PolyVecHPackedBytes = Omega + K; CryptoPublicKeyBytes = SeedBytes + K * PolyT1PackedBytes; CryptoSecretKeyBytes = 3 * SeedBytes + L * PolyEtaPackedBytes + K * PolyEtaPackedBytes + K * PolyT0PackedBytes; - CryptoBytes = SeedBytes + L * PolyZPackedBytes + PolyVecHPackedBytes; + CryptoBytes = CTilde + L * PolyZPackedBytes + PolyVecHPackedBytes; if (Gamma1 == (1 << 17)) { @@ -231,9 +235,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium ShakeDigest256.BlockUpdate(mu, 0, CrhBytes); ShakeDigest256.BlockUpdate(sig, 0, K * PolyW1PackedBytes); - ShakeDigest256.OutputFinal(sig, 0, SeedBytes); + ShakeDigest256.OutputFinal(sig, 0, CTilde); - cp.Challenge(sig); + cp.Challenge(sig); // use only first SeedBytes of sig cp.PolyNtt(); @@ -284,7 +288,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium public bool SignVerify(byte[] sig, int siglen, byte[] msg, int msglen, byte[] rho, byte[] encT1) { - byte[] buf = new byte[K * PolyW1PackedBytes], mu = new byte[CrhBytes], c = new byte[SeedBytes], c2 = new byte[SeedBytes]; + byte[] buf = new byte[K * PolyW1PackedBytes], mu = new byte[CrhBytes], c, c2 = new byte[CTilde]; Poly cp = new Poly(this); PolyVecMatrix Matrix = new PolyVecMatrix(this); PolyVecL z = new PolyVecL(this); @@ -296,14 +300,12 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium } t1 = Packing.UnpackPublicKey(t1, encT1, this); - - if (!Packing.UnpackSignature(z, h, sig, this)) { return false; } - c = Arrays.CopyOfRange(sig, 0, SeedBytes); + c = Arrays.CopyOfRange(sig, 0, CTilde); if (z.CheckNorm(Gamma1 - Beta)) { @@ -343,9 +345,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium Shake256Digest.BlockUpdate(mu, 0, CrhBytes); Shake256Digest.BlockUpdate(buf, 0, K * PolyW1PackedBytes); - Shake256Digest.OutputFinal(c2, 0, SeedBytes); + Shake256Digest.OutputFinal(c2, 0, CTilde); - for (int i = 0; i < SeedBytes; ++i) + for (int i = 0; i < CTilde; ++i) { if (c[i] != c2[i]) { diff --git a/crypto/src/pqc/crypto/crystals/dilithium/DilithiumParameters.cs b/crypto/src/pqc/crypto/crystals/dilithium/DilithiumParameters.cs index e6aa6ab21..4636fe6b0 100644 --- a/crypto/src/pqc/crypto/crystals/dilithium/DilithiumParameters.cs +++ b/crypto/src/pqc/crypto/crystals/dilithium/DilithiumParameters.cs @@ -1,5 +1,6 @@ using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Security; +using System; namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium { @@ -7,12 +8,15 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium : ICipherParameters { public static DilithiumParameters Dilithium2 = new DilithiumParameters(2, false); + [Obsolete("Parameter set to be removed")] public static DilithiumParameters Dilithium2Aes = new DilithiumParameters(2, true); public static DilithiumParameters Dilithium3 = new DilithiumParameters(3, false); + [Obsolete("Parameter set to be removed")] public static DilithiumParameters Dilithium3Aes = new DilithiumParameters(3, true); public static DilithiumParameters Dilithium5 = new DilithiumParameters(5, false); + [Obsolete("Parameter set to be removed")] public static DilithiumParameters Dilithium5Aes = new DilithiumParameters(5, true); private int k; diff --git a/crypto/src/pqc/crypto/crystals/dilithium/DilithiumPrivateKeyParameters.cs b/crypto/src/pqc/crypto/crystals/dilithium/DilithiumPrivateKeyParameters.cs index 66a518c93..cf18ce56e 100644 --- a/crypto/src/pqc/crypto/crystals/dilithium/DilithiumPrivateKeyParameters.cs +++ b/crypto/src/pqc/crypto/crystals/dilithium/DilithiumPrivateKeyParameters.cs @@ -27,6 +27,28 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium m_t1 = Arrays.Clone(t1); } + public DilithiumPrivateKeyParameters(DilithiumParameters parameters, byte[] encoding, DilithiumPublicKeyParameters pubKey) + : base(true, parameters) + { + DilithiumEngine eng = parameters.GetEngine(null); + + int index = 0; + m_rho = Arrays.CopyOfRange(encoding, 0, DilithiumEngine.SeedBytes); index += DilithiumEngine.SeedBytes; + m_k = Arrays.CopyOfRange(encoding, index, index + DilithiumEngine.SeedBytes); index += DilithiumEngine.SeedBytes; + m_tr = Arrays.CopyOfRange(encoding, index, index + DilithiumEngine.TrBytes); index += DilithiumEngine.TrBytes; + int delta = eng.L * eng.PolyEtaPackedBytes; + m_s1 = Arrays.CopyOfRange(encoding, index, index + delta); index += delta; + delta = eng.K * eng.PolyEtaPackedBytes; + m_s2 = Arrays.CopyOfRange(encoding, index, index + delta); index += delta; + delta = eng.K * DilithiumEngine.PolyT0PackedBytes; + m_t0 = Arrays.CopyOfRange(encoding, index, index + delta); + + if (pubKey != null) + { + m_t1 = Arrays.Clone(pubKey.GetT1()); + } + } + public byte[] GetEncoded() => Arrays.ConcatenateAll(m_rho, m_k, m_tr, m_s1, m_s2, m_t0); public byte[] K => Arrays.Clone(m_k); diff --git a/crypto/src/pqc/crypto/crystals/dilithium/Packing.cs b/crypto/src/pqc/crypto/crystals/dilithium/Packing.cs index 0f1fec1d2..eaae195ee 100644 --- a/crypto/src/pqc/crypto/crystals/dilithium/Packing.cs +++ b/crypto/src/pqc/crypto/crystals/dilithium/Packing.cs @@ -70,8 +70,8 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium { int i, j, k, end = 0; - Array.Copy(c, 0, sig, 0, DilithiumEngine.SeedBytes); - end += DilithiumEngine.SeedBytes; + Array.Copy(c, 0, sig, 0, engine.CTilde); + end += engine.CTilde; for (i = 0; i < engine.L; ++i) { @@ -101,31 +101,31 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium } - public static bool UnpackSignature(PolyVecL z, PolyVecK h, byte[] sig, DilithiumEngine Engine) + public static bool UnpackSignature(PolyVecL z, PolyVecK h, byte[] sig, DilithiumEngine engine) { int i, j, k; - int end = DilithiumEngine.SeedBytes; - for (i = 0; i < Engine.L; ++i) + int end = engine.CTilde; + for (i = 0; i < engine.L; ++i) { - z.Vec[i].UnpackZ(Arrays.CopyOfRange(sig, end + i * Engine.PolyZPackedBytes, end + (i + 1) * Engine.PolyZPackedBytes)); + z.Vec[i].UnpackZ(Arrays.CopyOfRange(sig, end + i * engine.PolyZPackedBytes, end + (i + 1) * engine.PolyZPackedBytes)); } - end += Engine.L * Engine.PolyZPackedBytes; + end += engine.L * engine.PolyZPackedBytes; k = 0; - for (i = 0; i < Engine.K; ++i) + for (i = 0; i < engine.K; ++i) { for (j = 0; j < DilithiumEngine.N; ++j) { h.Vec[i].Coeffs[j] = 0; } - if ((sig[end + Engine.Omega + i] & 0xFF) < k || (sig[end + Engine.Omega + i] & 0xFF) > Engine.Omega) + if ((sig[end + engine.Omega + i] & 0xFF) < k || (sig[end + engine.Omega + i] & 0xFF) > engine.Omega) { return false; } - for (j = k; j < (sig[end + Engine.Omega + i] & 0xFF); ++j) + for (j = k; j < (sig[end + engine.Omega + i] & 0xFF); ++j) { if (j > k && (sig[end + j] & 0xFF) <= (sig[end + j - 1] & 0xFF)) { @@ -134,9 +134,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium h.Vec[i].Coeffs[sig[end + j] & 0xFF] = 1; } - k = (int)(sig[end + Engine.Omega + i]); + k = (int)(sig[end + engine.Omega + i]); } - for (j = k; j < Engine.Omega; ++j) + for (j = k; j < engine.Omega; ++j) { if ((sig[end + j] & 0xFF) != 0) { diff --git a/crypto/src/pqc/crypto/crystals/kyber/KyberParameters.cs b/crypto/src/pqc/crypto/crystals/kyber/KyberParameters.cs index 195831433..229ca7c3c 100644 --- a/crypto/src/pqc/crypto/crystals/kyber/KyberParameters.cs +++ b/crypto/src/pqc/crypto/crystals/kyber/KyberParameters.cs @@ -1,15 +1,19 @@ using Org.BouncyCastle.Crypto; +using System; namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Kyber { public sealed class KyberParameters : IKemParameters { - public static KyberParameters kyber512 = new KyberParameters("kyber512", 2, 128, false); - public static KyberParameters kyber768 = new KyberParameters("kyber768", 3, 192, false); + public static KyberParameters kyber512 = new KyberParameters("kyber512", 2, 256, false); + public static KyberParameters kyber768 = new KyberParameters("kyber768", 3, 256, false); public static KyberParameters kyber1024 = new KyberParameters("kyber1024", 4, 256, false); - public static KyberParameters kyber512_aes = new KyberParameters("kyber512-aes", 2, 128, true); - public static KyberParameters kyber768_aes = new KyberParameters("kyber768-aes", 3, 192, true); + [Obsolete("Parameter set to be removed")] + public static KyberParameters kyber512_aes = new KyberParameters("kyber512-aes", 2, 256, true); + [Obsolete("Parameter set to be removed")] + public static KyberParameters kyber768_aes = new KyberParameters("kyber768-aes", 3, 256, true); + [Obsolete("Parameter set to be removed")] public static KyberParameters kyber1024_aes = new KyberParameters("kyber1024-aes", 4, 256, true); private string m_name; diff --git a/crypto/src/pqc/crypto/crystals/kyber/KyberPrivateKeyParameters.cs b/crypto/src/pqc/crypto/crystals/kyber/KyberPrivateKeyParameters.cs index 08b4fbe86..594df1e05 100644 --- a/crypto/src/pqc/crypto/crystals/kyber/KyberPrivateKeyParameters.cs +++ b/crypto/src/pqc/crypto/crystals/kyber/KyberPrivateKeyParameters.cs @@ -22,6 +22,19 @@ namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Kyber m_rho = Arrays.Clone(rho); } + public KyberPrivateKeyParameters(KyberParameters parameters, byte[] encoding) + : base(true, parameters) + { + KyberEngine eng = parameters.Engine; + + int index = 0; + m_s = Arrays.CopyOfRange(encoding, 0, eng.IndCpaSecretKeyBytes); index += eng.IndCpaSecretKeyBytes; + m_t = Arrays.CopyOfRange(encoding, index, index + eng.IndCpaPublicKeyBytes - KyberEngine.SymBytes); index += eng.IndCpaPublicKeyBytes - KyberEngine.SymBytes; + m_rho = Arrays.CopyOfRange(encoding, index, index + 32); index += 32; + m_hpk = Arrays.CopyOfRange(encoding, index, index + 32); index += 32; + m_nonce = Arrays.CopyOfRange(encoding, index, index + KyberEngine.SymBytes); + } + public byte[] GetEncoded() => Arrays.ConcatenateAll(m_s, m_t, m_rho, m_hpk, m_nonce); public byte[] GetHpk() => Arrays.Clone(m_hpk); diff --git a/crypto/src/pqc/crypto/ntru/parametersets/NtruHps2048509.cs b/crypto/src/pqc/crypto/ntru/parametersets/NtruHps2048509.cs index dcbf47636..134882380 100644 --- a/crypto/src/pqc/crypto/ntru/parametersets/NtruHps2048509.cs +++ b/crypto/src/pqc/crypto/ntru/parametersets/NtruHps2048509.cs @@ -2,7 +2,7 @@ { internal class NtruHps2048509 : NtruHpsParameterSet { - internal NtruHps2048509() : base(509, 11, 32, 32, 16) + internal NtruHps2048509() : base(509, 11, 32, 32, 32) { } } diff --git a/crypto/src/pqc/crypto/ntru/parametersets/NtruHps2048677.cs b/crypto/src/pqc/crypto/ntru/parametersets/NtruHps2048677.cs index 2076f160d..0c2b2c85b 100644 --- a/crypto/src/pqc/crypto/ntru/parametersets/NtruHps2048677.cs +++ b/crypto/src/pqc/crypto/ntru/parametersets/NtruHps2048677.cs @@ -2,7 +2,7 @@ { internal class NtruHps2048677 : NtruHpsParameterSet { - internal NtruHps2048677() : base(677, 11, 32, 32, 24) + internal NtruHps2048677() : base(677, 11, 32, 32, 32) { } } diff --git a/crypto/src/pqc/crypto/ntru/parametersets/NtruHrss701.cs b/crypto/src/pqc/crypto/ntru/parametersets/NtruHrss701.cs index 9e795265e..55ceac3b5 100644 --- a/crypto/src/pqc/crypto/ntru/parametersets/NtruHrss701.cs +++ b/crypto/src/pqc/crypto/ntru/parametersets/NtruHrss701.cs @@ -2,7 +2,7 @@ { internal class NtruHrss701 : NtruHrssParameterSet { - internal NtruHrss701() : base(701, 13, 32, 32, 24) + internal NtruHrss701() : base(701, 13, 32, 32, 32) { } } diff --git a/crypto/src/pqc/crypto/sphincsplus/SPHINCSPlusParameters.cs b/crypto/src/pqc/crypto/sphincsplus/SPHINCSPlusParameters.cs index 57b1bcabd..4fbd62735 100644 --- a/crypto/src/pqc/crypto/sphincsplus/SPHINCSPlusParameters.cs +++ b/crypto/src/pqc/crypto/sphincsplus/SPHINCSPlusParameters.cs @@ -16,19 +16,24 @@ namespace Org.BouncyCastle.Pqc.Crypto.SphincsPlus public sealed class SphincsPlusParameters { // SHA-2 - + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters sha2_128f = new SphincsPlusParameters( 0x010101, "sha2-128f-robust", new Sha2EngineProvider(true, 16, 16, 22, 6, 33, 66)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters sha2_128s = new SphincsPlusParameters( 0x010102, "sha2-128s-robust", new Sha2EngineProvider(true, 16, 16, 7, 12, 14, 63)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters sha2_192f = new SphincsPlusParameters( 0x010103, "sha2-192f-robust", new Sha2EngineProvider(true, 24, 16, 22, 8, 33, 66)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters sha2_192s = new SphincsPlusParameters( 0x010104, "sha2-192s-robust", new Sha2EngineProvider(true, 24, 16, 7, 14, 17, 63)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters sha2_256f = new SphincsPlusParameters( 0x010105, "sha2-256f-robust", new Sha2EngineProvider(true, 32, 16, 17, 9, 35, 68)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters sha2_256s = new SphincsPlusParameters( 0x010106, "sha2-256s-robust", new Sha2EngineProvider(true, 32, 16, 8, 14, 22, 64)); @@ -48,19 +53,24 @@ namespace Org.BouncyCastle.Pqc.Crypto.SphincsPlus 0x010206, "sha2-256s-simple", new Sha2EngineProvider(false, 32, 16, 8, 14, 22, 64)); // SHAKE-256. - + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters shake_128f = new SphincsPlusParameters( 0x020101, "shake-128f-robust", new Shake256EngineProvider(true, 16, 16, 22, 6, 33, 66)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters shake_128s = new SphincsPlusParameters( 0x020102, "shake-128s-robust", new Shake256EngineProvider(true, 16, 16, 7, 12, 14, 63)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters shake_192f = new SphincsPlusParameters( 0x020103, "shake-192f-robust", new Shake256EngineProvider(true, 24, 16, 22, 8, 33, 66)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters shake_192s = new SphincsPlusParameters( 0x020104, "shake-192s-robust", new Shake256EngineProvider(true, 24, 16, 7, 14, 17, 63)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters shake_256f = new SphincsPlusParameters( 0x020105, "shake-256f-robust", new Shake256EngineProvider(true, 32, 16, 17, 9, 35, 68)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters shake_256s = new SphincsPlusParameters( 0x020106, "shake-256s-robust", new Shake256EngineProvider(true, 32, 16, 8, 14, 22, 64)); @@ -80,19 +90,24 @@ namespace Org.BouncyCastle.Pqc.Crypto.SphincsPlus 0x020206, "shake-256s-simple", new Shake256EngineProvider(false, 32, 16, 8, 14, 22, 64)); // Haraka. - + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters haraka_128f = new SphincsPlusParameters( 0x030101, "haraka-128f-robust", new HarakaSEngineProvider(true, 16, 16, 22, 6, 33, 66)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters haraka_128s = new SphincsPlusParameters( 0x030102, "haraka-128s-robust", new HarakaSEngineProvider(true, 16, 16, 7, 12, 14, 63)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters haraka_192f = new SphincsPlusParameters( 0x030103, "haraka-192f-robust", new HarakaSEngineProvider(true, 24, 16, 22, 8, 33, 66)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters haraka_192s = new SphincsPlusParameters( 0x030104, "haraka-192s-robust", new HarakaSEngineProvider(true, 24, 16, 7, 14, 17, 63)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters haraka_256f = new SphincsPlusParameters( 0x030105, "haraka-256f-robust", new HarakaSEngineProvider(true, 32, 16, 17, 9, 35, 68)); + [Obsolete("Parameter set to be removed")] public static readonly SphincsPlusParameters haraka_256s = new SphincsPlusParameters( 0x030106, "haraka-256s-robust", new HarakaSEngineProvider(true, 32, 16, 8, 14, 22, 64)); diff --git a/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs b/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs index c1e3feb1b..8058ed695 100644 --- a/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs +++ b/crypto/src/pqc/crypto/utils/PqcPrivateKeyFactory.cs @@ -138,54 +138,29 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities } if (algOid.On(BCObjectIdentifiers.pqc_kem_kyber)) { - KyberPrivateKey kyberKey = KyberPrivateKey.GetInstance(keyInfo.ParsePrivateKey()); + Asn1OctetString kyberKey = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()); KyberParameters kyberParams = PqcUtilities.KyberParamsLookup(algOid); - -#pragma warning disable CS0618 // Type or member is obsolete - KyberPublicKey pubKey = kyberKey.PublicKey; -#pragma warning restore CS0618 // Type or member is obsolete - if (pubKey != null) - { - return new KyberPrivateKeyParameters(kyberParams, kyberKey.GetS(), kyberKey.GetHpk(), - kyberKey.GetNonce(), pubKey.T, pubKey.Rho); - } - return new KyberPrivateKeyParameters(kyberParams, kyberKey.GetS(), kyberKey.GetHpk(), - kyberKey.GetNonce(), null, null); + + return new KyberPrivateKeyParameters(kyberParams, kyberKey.GetOctets()); } if (algOid.Equals(BCObjectIdentifiers.dilithium2) || algOid.Equals(BCObjectIdentifiers.dilithium3) || - algOid.Equals(BCObjectIdentifiers.dilithium5) || - algOid.Equals(BCObjectIdentifiers.dilithium2_aes) || - algOid.Equals(BCObjectIdentifiers.dilithium3_aes) || - algOid.Equals(BCObjectIdentifiers.dilithium5_aes)) + algOid.Equals(BCObjectIdentifiers.dilithium5)) { - Asn1Sequence keyEnc = Asn1Sequence.GetInstance(keyInfo.ParsePrivateKey()); + Asn1OctetString keyEnc = Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()); DilithiumParameters spParams = PqcUtilities.DilithiumParamsLookup(algOid); - int version = DerInteger.GetInstance(keyEnc[0]).IntValueExact; - if (version != 0) - throw new IOException("unknown private key version: " + version); - - byte[] t1 = null; - DerBitString publicKeyData = keyInfo.PublicKey; if (publicKeyData != null) { var pubParams = PqcPublicKeyFactory.DilithiumConverter.GetPublicKeyParameters(spParams, publicKeyData); - t1 = pubParams.GetT1(); + return new DilithiumPrivateKeyParameters(spParams, keyEnc.GetOctets(), pubParams); } - return new DilithiumPrivateKeyParameters(spParams, - DerBitString.GetInstance(keyEnc[1]).GetOctets(), - DerBitString.GetInstance(keyEnc[2]).GetOctets(), - DerBitString.GetInstance(keyEnc[3]).GetOctets(), - DerBitString.GetInstance(keyEnc[4]).GetOctets(), - DerBitString.GetInstance(keyEnc[5]).GetOctets(), - DerBitString.GetInstance(keyEnc[6]).GetOctets(), - t1); // encT1 + return new DilithiumPrivateKeyParameters(spParams, keyEnc.GetOctets(), null); } if (algOid.Equals(BCObjectIdentifiers.falcon_512) || algOid.Equals(BCObjectIdentifiers.falcon_1024)) diff --git a/crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs b/crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs index ad079d5c6..1895bf891 100644 --- a/crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs +++ b/crypto/src/pqc/crypto/utils/PqcPrivateKeyInfoFactory.cs @@ -133,33 +133,17 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities { AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( PqcUtilities.KyberOidLookup(kyberPrivateKeyParameters.Parameters)); - -#pragma warning disable CS0618 // Type or member is obsolete - KyberPublicKey kyberPub = new KyberPublicKey(kyberPrivateKeyParameters.GetT(), - kyberPrivateKeyParameters.GetRho()); -#pragma warning restore CS0618 // Type or member is obsolete - KyberPrivateKey kyberPriv = new KyberPrivateKey(0, kyberPrivateKeyParameters.GetS(), - kyberPrivateKeyParameters.GetHpk(), kyberPrivateKeyParameters.GetNonce(), kyberPub); - - return new PrivateKeyInfo(algorithmIdentifier, kyberPriv, attributes); + + return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(kyberPrivateKeyParameters.GetEncoded()), attributes); } if (privateKey is DilithiumPrivateKeyParameters dilithiumPrivateKeyParameters) { - Asn1EncodableVector v = new Asn1EncodableVector(7); - v.Add(new DerInteger(0)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.Rho)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.K)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.Tr)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.S1)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.S2)); - v.Add(new DerBitString(dilithiumPrivateKeyParameters.T0)); - - AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( PqcUtilities.DilithiumOidLookup(dilithiumPrivateKeyParameters.Parameters)); DilithiumPublicKeyParameters pubParams = dilithiumPrivateKeyParameters.GetPublicKeyParameters(); - return new PrivateKeyInfo(algorithmIdentifier, new DerSequence(v), attributes, pubParams.GetEncoded()); + return new PrivateKeyInfo(algorithmIdentifier, new DerOctetString(dilithiumPrivateKeyParameters.GetEncoded()), attributes, pubParams.GetEncoded()); } if (privateKey is BikePrivateKeyParameters bikePrivateKeyParameters) { diff --git a/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs b/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs index 73a553d0e..5aef69818 100644 --- a/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs +++ b/crypto/src/pqc/crypto/utils/PqcPublicKeyFactory.cs @@ -243,7 +243,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities SphincsPlusParameters spParams = PqcUtilities.SphincsPlusParamsLookup(keyInfo.Algorithm.Algorithm); - return new SPHINCSPlusPublicKeyParameters(spParams, keyEnc); + return new SphincsPlusPublicKeyParameters(spParams, keyEnc); } } } diff --git a/crypto/src/pqc/crypto/utils/PqcUtilities.cs b/crypto/src/pqc/crypto/utils/PqcUtilities.cs index c0093c33c..21254331c 100644 --- a/crypto/src/pqc/crypto/utils/PqcUtilities.cs +++ b/crypto/src/pqc/crypto/utils/PqcUtilities.cs @@ -262,14 +262,14 @@ namespace Org.BouncyCastle.Pqc.Crypto.Utilities sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_shake_256f_r3] = SphincsPlusParameters.shake_256f; sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_haraka_256s_r3] = SphincsPlusParameters.haraka_256s; sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_haraka_256f_r3] = SphincsPlusParameters.haraka_256f; - sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_128s_simple = SphincsPlusParameters.sha2_128s_simple; - sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_128f_simple = SphincsPlusParameters.sha2_128f_simple; - sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_shake_128f_simple = SphincsPlusParameters.shake_128f_simple; - sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_192s_simple = SphincsPlusParameters.sha2_192s_simple; - sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_192f_simple = SphincsPlusParameters.sha2_192f_simple; - sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_shake_192f_simple = SphincsPlusParameters.shake_192f_simple; - sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_256s_simple = SphincsPlusParameters.sha2_256s_simple; - sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_256f_simple = SphincsPlusParameters.sha2_256f_simple; + sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_128s_simple] = SphincsPlusParameters.sha2_128s_simple; + sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_128f_simple] = SphincsPlusParameters.sha2_128f_simple; + sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_shake_128f_simple] = SphincsPlusParameters.shake_128f_simple; + sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_192s_simple] = SphincsPlusParameters.sha2_192s_simple; + sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_192f_simple] = SphincsPlusParameters.sha2_192f_simple; + sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_shake_192f_simple] = SphincsPlusParameters.shake_192f_simple; + sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_256s_simple] = SphincsPlusParameters.sha2_256s_simple; + sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_sha2_256f_simple] = SphincsPlusParameters.sha2_256f_simple; sphincsPlusParams[BCObjectIdentifiers.sphincsPlus_shake_256f_simple] = SphincsPlusParameters.shake_256f_simple; } |