diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2020-10-30 23:43:25 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2020-10-30 23:43:25 +0700 |
commit | cf9d584cebc3b589ba865ecf74d2af9d91bae631 (patch) | |
tree | 601689d830d03d870a0b4eef4ab8c4f9eb84f74b | |
parent | Add new files to projects (diff) | |
download | BouncyCastle.NET-ed25519-cf9d584cebc3b589ba865ecf74d2af9d91bae631.tar.xz |
Cleanup after recent changes
-rw-r--r-- | crypto/src/asn1/nist/KMACwithSHAKE128_params.cs | 25 | ||||
-rw-r--r-- | crypto/src/asn1/nist/KMACwithSHAKE256_params.cs | 25 | ||||
-rw-r--r-- | crypto/src/crypto/digests/CSHAKEDigest.cs | 26 | ||||
-rw-r--r-- | crypto/src/crypto/digests/XofUtils.cs | 9 | ||||
-rw-r--r-- | crypto/src/crypto/macs/KMac.cs | 44 | ||||
-rw-r--r-- | crypto/test/src/asn1/test/KMacParamsTest.cs | 62 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/CSHAKETest.cs | 69 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/KMACTest.cs | 39 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/Poly1305Test.cs | 3 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/RegressionTest.cs | 4 | ||||
-rw-r--r-- | crypto/test/src/tsp/test/NewTspTest.cs | 60 | ||||
-rw-r--r-- | crypto/test/src/tsp/test/TSPTest.cs | 35 | ||||
-rw-r--r-- | crypto/test/src/tsp/test/TSPTestUtil.cs | 219 |
13 files changed, 416 insertions, 204 deletions
diff --git a/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs b/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs index cd73ce2bc..a1fd8f403 100644 --- a/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs +++ b/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs @@ -10,50 +10,44 @@ namespace Org.BouncyCastle.Asn1.Nist /// customizationString OCTET STRING DEFAULT ''H /// } /// </summary> -public class KMACwithSHAKE128_params : Asn1Encodable +public class KMacWithShake128Params : Asn1Encodable { - private static readonly byte[] EMPTY_STRING = new byte[0]; private static readonly int DEF_LENGTH = 256; private readonly int outputLength; private readonly byte[] customizationString; - - public KMACwithSHAKE128_params(int outputLength) + public KMacWithShake128Params(int outputLength) { this.outputLength = outputLength; this.customizationString = EMPTY_STRING; } - public KMACwithSHAKE128_params(int outputLength, byte[] customizationString) + public KMacWithShake128Params(int outputLength, byte[] customizationString) { this.outputLength = outputLength; this.customizationString = Arrays.Clone(customizationString); } - - public static KMACwithSHAKE128_params getInstance(Object o) + public static KMacWithShake128Params GetInstance(object o) { - if (o is KMACwithSHAKE128_params) + if (o is KMacWithShake128Params) { - return (KMACwithSHAKE128_params)o; + return (KMacWithShake128Params)o; } else if (o != null) { - return new KMACwithSHAKE128_params(Asn1Sequence.GetInstance(o)); + return new KMacWithShake128Params(Asn1Sequence.GetInstance(o)); } return null; } - - private KMACwithSHAKE128_params(Asn1Sequence seq) + private KMacWithShake128Params(Asn1Sequence seq) { if (seq.Count > 2) - { throw new InvalidOperationException("sequence size greater than 2"); - } if (seq.Count == 2) { @@ -80,8 +74,6 @@ public class KMACwithSHAKE128_params : Asn1Encodable } } - - public int OutputLength { get { return outputLength; } @@ -92,7 +84,6 @@ public class KMACwithSHAKE128_params : Asn1Encodable get { return Arrays.Clone(customizationString); } } - public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(); diff --git a/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs b/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs index e70fc807d..fa7471913 100644 --- a/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs +++ b/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs @@ -9,50 +9,44 @@ namespace Org.BouncyCastle.Asn1.Nist /// customizationString OCTET STRING DEFAULT ''H /// } /// </summary> -public class KMACwithSHAKE256_params : Asn1Encodable +public class KMacWithShake256Params : Asn1Encodable { - private static readonly byte[] EMPTY_STRING = new byte[0]; private static readonly int DEF_LENGTH = 512; private readonly int outputLength; private readonly byte[] customizationString; - - public KMACwithSHAKE256_params(int outputLength) + public KMacWithShake256Params(int outputLength) { this.outputLength = outputLength; this.customizationString = EMPTY_STRING; } - public KMACwithSHAKE256_params(int outputLength, byte[] customizationString) + public KMacWithShake256Params(int outputLength, byte[] customizationString) { this.outputLength = outputLength; this.customizationString = Arrays.Clone(customizationString); } - - public static KMACwithSHAKE256_params getInstance(Object o) + public static KMacWithShake256Params GetInstance(Object o) { - if (o is KMACwithSHAKE256_params) + if (o is KMacWithShake256Params) { - return (KMACwithSHAKE256_params)o; + return (KMacWithShake256Params)o; } else if (o != null) { - return new KMACwithSHAKE256_params(Asn1Sequence.GetInstance(o)); + return new KMacWithShake256Params(Asn1Sequence.GetInstance(o)); } return null; } - - private KMACwithSHAKE256_params(Asn1Sequence seq) + private KMacWithShake256Params(Asn1Sequence seq) { if (seq.Count > 2) - { throw new InvalidOperationException("sequence size greater than 2"); - } if (seq.Count == 2) { @@ -79,8 +73,6 @@ public class KMACwithSHAKE256_params : Asn1Encodable } } - - public int OutputLength { get { return outputLength; } @@ -91,7 +83,6 @@ public class KMACwithSHAKE256_params : Asn1Encodable get { return Arrays.Clone(customizationString); } } - public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(); diff --git a/crypto/src/crypto/digests/CSHAKEDigest.cs b/crypto/src/crypto/digests/CSHAKEDigest.cs index 5c42b4171..c070a47c2 100644 --- a/crypto/src/crypto/digests/CSHAKEDigest.cs +++ b/crypto/src/crypto/digests/CSHAKEDigest.cs @@ -1,15 +1,13 @@ -using Org.BouncyCastle.Utilities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System; + +using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Digests { /// <summary> /// Customizable SHAKE function. /// </summary> - public class CSHAKEDigest : ShakeDigest + public class CShakeDigest : ShakeDigest { private static readonly byte[] padding = new byte[100]; private readonly byte[] diff; @@ -20,7 +18,7 @@ namespace Org.BouncyCastle.Crypto.Digests /// <param name="bitLength">bit length of the underlying SHAKE function, 128 or 256.</param> /// <param name="N">the function name string, note this is reserved for use by NIST. Avoid using it if not required.</param> /// <param name="S">the customization string - available for local use.</param> - public CSHAKEDigest(int bitLength, byte[] N, byte[] S) : base(bitLength) + public CShakeDigest(int bitLength, byte[] N, byte[] S) : base(bitLength) { if ((N == null || N.Length == 0) && (S == null || S.Length == 0)) { @@ -28,12 +26,11 @@ namespace Org.BouncyCastle.Crypto.Digests } else { - diff = Arrays.ConcatenateAll(XofUtils.leftEncode(rate / 8), encodeString(N), encodeString(S)); + diff = Arrays.ConcatenateAll(XofUtilities.LeftEncode(rate / 8), encodeString(N), encodeString(S)); DiffPadAndAbsorb(); } } - // bytepad in SP 800-185 private void DiffPadAndAbsorb() { @@ -61,13 +58,16 @@ namespace Org.BouncyCastle.Crypto.Digests { if (str == null || str.Length == 0) { - return XofUtils.leftEncode(0); + return XofUtilities.LeftEncode(0); } - return Arrays.Concatenate(XofUtils.leftEncode(str.Length * 8L), str); + return Arrays.Concatenate(XofUtilities.LeftEncode(str.Length * 8L), str); } - public override string AlgorithmName => "CSHAKE" + fixedOutputLength; + public override string AlgorithmName + { + get { return "CSHAKE" + fixedOutputLength; } + } public override int DoFinal(byte[] output, int outOff) { @@ -102,7 +102,7 @@ namespace Org.BouncyCastle.Crypto.Digests } } - public void Reset() + public override void Reset() { base.Reset(); diff --git a/crypto/src/crypto/digests/XofUtils.cs b/crypto/src/crypto/digests/XofUtils.cs index e4c893e01..5c197e0e6 100644 --- a/crypto/src/crypto/digests/XofUtils.cs +++ b/crypto/src/crypto/digests/XofUtils.cs @@ -1,13 +1,10 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Org.BouncyCastle.Crypto.Digests { - public class XofUtils + internal class XofUtilities { - public static byte[] leftEncode(long strLen) + internal static byte[] LeftEncode(long strLen) { byte n = 1; @@ -29,7 +26,7 @@ namespace Org.BouncyCastle.Crypto.Digests return b; } - public static byte[] rightEncode(long strLen) + internal static byte[] RightEncode(long strLen) { byte n = 1; diff --git a/crypto/src/crypto/macs/KMac.cs b/crypto/src/crypto/macs/KMac.cs index 38697a9a9..889131d8c 100644 --- a/crypto/src/crypto/macs/KMac.cs +++ b/crypto/src/crypto/macs/KMac.cs @@ -1,19 +1,18 @@ -using Org.BouncyCastle.Crypto.Digests; +using System; +using System.Text; + +using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Org.BouncyCastle.Crypto.Macs { - public class KMac : IMac, IXof + public class KMac + : IMac, IXof { - private static readonly byte[] padding = new byte[100]; - private readonly CSHAKEDigest cshake; + private readonly CShakeDigest cshake; private readonly int bitLength; private readonly int outputLength; @@ -21,23 +20,22 @@ namespace Org.BouncyCastle.Crypto.Macs private bool initialised; private bool firstOutput; - public KMac(int bitLength, byte[] S) { - this.cshake = new CSHAKEDigest(bitLength, Encoding.ASCII.GetBytes("KMAC"),S); + this.cshake = new CShakeDigest(bitLength, Encoding.ASCII.GetBytes("KMAC"), S); this.bitLength = bitLength; this.outputLength = bitLength * 2 / 8; } - - public string AlgorithmName => "KMAC" + cshake.AlgorithmName.Substring(6); + public string AlgorithmName + { + get { return "KMAC" + cshake.AlgorithmName.Substring(6); } + } public void BlockUpdate(byte[] input, int inOff, int len) { if (!initialised) - { throw new InvalidOperationException("KMAC not initialized"); - } cshake.BlockUpdate(input, inOff, len); } @@ -47,11 +45,9 @@ namespace Org.BouncyCastle.Crypto.Macs if (firstOutput) { if (!initialised) - { throw new InvalidOperationException("KMAC not initialized"); - } - byte[] encOut = XofUtils.rightEncode(GetMacSize() * 8); + byte[] encOut = XofUtilities.RightEncode(GetMacSize() * 8); cshake.BlockUpdate(encOut, 0, encOut.Length); } @@ -68,11 +64,9 @@ namespace Org.BouncyCastle.Crypto.Macs if (firstOutput) { if (!initialised) - { throw new InvalidOperationException("KMAC not initialized"); - } - byte[] encOut = XofUtils.rightEncode(outLen * 8); + byte[] encOut = XofUtilities.RightEncode(outLen * 8); cshake.BlockUpdate(encOut, 0, encOut.Length); } @@ -89,11 +83,9 @@ namespace Org.BouncyCastle.Crypto.Macs if (firstOutput) { if (!initialised) - { throw new InvalidOperationException("KMAC not initialized"); - } - byte[] encOut = XofUtils.rightEncode(0); + byte[] encOut = XofUtilities.RightEncode(0); cshake.BlockUpdate(encOut, 0, encOut.Length); @@ -147,7 +139,7 @@ namespace Org.BouncyCastle.Crypto.Macs private void bytePad(byte[] X, int w) { - byte[] bytes = XofUtils.leftEncode(w); + byte[] bytes = XofUtilities.LeftEncode(w); BlockUpdate(bytes, 0, bytes.Length); byte[] encX = encode(X); BlockUpdate(encX, 0, encX.Length); @@ -168,15 +160,13 @@ namespace Org.BouncyCastle.Crypto.Macs private static byte[] encode(byte[] X) { - return Arrays.Concatenate(XofUtils.leftEncode(X.Length * 8), X); + return Arrays.Concatenate(XofUtilities.LeftEncode(X.Length * 8), X); } public void Update(byte input) { if (!initialised) - { throw new InvalidOperationException("KMAC not initialized"); - } cshake.Update(input); } diff --git a/crypto/test/src/asn1/test/KMacParamsTest.cs b/crypto/test/src/asn1/test/KMacParamsTest.cs index 1c080f755..a3d82694b 100644 --- a/crypto/test/src/asn1/test/KMacParamsTest.cs +++ b/crypto/test/src/asn1/test/KMacParamsTest.cs @@ -1,4 +1,7 @@ -using NUnit.Framework; +using System; + +using NUnit.Framework; + using Org.BouncyCastle.Asn1.Nist; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Test; @@ -6,63 +9,66 @@ using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Asn1.Tests { [TestFixture] - public class KMacParamsTest :SimpleTest + public class KMacParamsTest + : SimpleTest { - public override string Name => "KMacParamsTest"; + public override string Name + { + get { return "KMacParamsTest"; } + } - public override void PerformTest() { - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256).GetEncoded(), new DerSequence().GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512).GetEncoded(), new DerSequence().GetEncoded())); + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake128Params(256).GetEncoded(), new DerSequence().GetEncoded())); + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake256Params(512).GetEncoded(), new DerSequence().GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512).GetEncoded(), new DerSequence(new DerInteger(512)).GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256).GetEncoded(), new DerSequence(new DerInteger(256)).GetEncoded())); + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake128Params(512).GetEncoded(), new DerSequence(new DerInteger(512)).GetEncoded())); + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake256Params(256).GetEncoded(), new DerSequence(new DerInteger(256)).GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512).GetEncoded(), KMACwithSHAKE128_params.getInstance(new DerSequence(new DerInteger(512))).GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256).GetEncoded(), KMACwithSHAKE256_params.getInstance(new DerSequence(new DerInteger(256))).GetEncoded())); + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake128Params(512).GetEncoded(), KMacWithShake128Params.GetInstance(new DerSequence(new DerInteger(512))).GetEncoded())); + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake256Params(256).GetEncoded(), KMacWithShake256Params.GetInstance(new DerSequence(new DerInteger(256))).GetEncoded())); byte[] customizationString = Strings.ToByteArray("hello, world!"); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512, customizationString).GetEncoded(), new DerSequence( + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake128Params(512, customizationString).GetEncoded(), new DerSequence( new Asn1Encodable[] { new DerInteger(512), new DerOctetString(customizationString) }).GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256, customizationString).GetEncoded(), new DerSequence( + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake256Params(256, customizationString).GetEncoded(), new DerSequence( new Asn1Encodable[] { new DerInteger(256), new DerOctetString(customizationString) }).GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512, customizationString).GetEncoded(), - KMACwithSHAKE128_params.getInstance( + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake128Params(512, customizationString).GetEncoded(), + KMacWithShake128Params.GetInstance( new DerSequence(new Asn1Encodable[] { new DerInteger(512), new DerOctetString(customizationString) })).GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256, customizationString).GetEncoded(), - KMACwithSHAKE256_params.getInstance(new DerSequence( + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake256Params(256, customizationString).GetEncoded(), + KMacWithShake256Params.GetInstance(new DerSequence( new Asn1Encodable[] { new DerInteger(256), new DerOctetString(customizationString) })).GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256, customizationString).GetEncoded(), new DerSequence( + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake128Params(256, customizationString).GetEncoded(), new DerSequence( new Asn1Encodable[] { new DerOctetString(customizationString) }).GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512, customizationString).GetEncoded(), new DerSequence( + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake256Params(512, customizationString).GetEncoded(), new DerSequence( new Asn1Encodable[] { new DerOctetString(customizationString) }).GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256, customizationString).GetEncoded(), - KMACwithSHAKE128_params.getInstance( + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake128Params(256, customizationString).GetEncoded(), + KMacWithShake128Params.GetInstance( new DerSequence(new Asn1Encodable[] { new DerOctetString(customizationString) })).GetEncoded())); - Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512, customizationString).GetEncoded(), - KMACwithSHAKE256_params.getInstance(new DerSequence( + Assert.IsTrue(Arrays.AreEqual(new KMacWithShake256Params(512, customizationString).GetEncoded(), + KMacWithShake256Params.GetInstance(new DerSequence( new Asn1Encodable[] { new DerOctetString(customizationString) })).GetEncoded())); - KMACwithSHAKE128_params p128 = new KMACwithSHAKE128_params(256, customizationString); + KMacWithShake128Params p128 = new KMacWithShake128Params(256, customizationString); Assert.AreEqual(256, p128.OutputLength); Assert.IsTrue(Arrays.AreEqual(customizationString, p128.CustomizationString)); - Assert.IsTrue(p128 == KMACwithSHAKE128_params.getInstance(p128)); + Assert.IsTrue(p128 == KMacWithShake128Params.GetInstance(p128)); - KMACwithSHAKE256_params p256 = new KMACwithSHAKE256_params(512, customizationString); + KMacWithShake256Params p256 = new KMacWithShake256Params(512, customizationString); Assert.AreEqual(512, p256.OutputLength); Assert.IsTrue(Arrays.AreEqual(customizationString, p256.CustomizationString)); - Assert.IsTrue(p256 == KMACwithSHAKE256_params.getInstance(p256)); + Assert.IsTrue(p256 == KMacWithShake256Params.GetInstance(p256)); - p128 = new KMACwithSHAKE128_params(512); + p128 = new KMacWithShake128Params(512); Assert.AreEqual(512, p128.OutputLength); Assert.IsTrue(Arrays.AreEqual(new byte[0], p128.CustomizationString)); - p256 = new KMACwithSHAKE256_params(256); + p256 = new KMacWithShake256Params(256); Assert.AreEqual(256, p256.OutputLength); Assert.IsTrue(Arrays.AreEqual(new byte[0], p256.CustomizationString)); } diff --git a/crypto/test/src/crypto/test/CSHAKETest.cs b/crypto/test/src/crypto/test/CSHAKETest.cs index 80f0eb1f7..e7c6ce9e0 100644 --- a/crypto/test/src/crypto/test/CSHAKETest.cs +++ b/crypto/test/src/crypto/test/CSHAKETest.cs @@ -1,20 +1,26 @@ -using NUnit.Framework; +using System; + +using NUnit.Framework; + using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - public class CSHAKETest + [TestFixture] + public class CShakeTest + : SimpleTest { - [Test] - public void PerformTest() + public override string Name { - CSHAKEDigest cshake = new CSHAKEDigest(128, new byte[0], Strings.ToByteArray("Email Signature")); + get { return "CSHAKE"; } + } + + public override void PerformTest() + { + CShakeDigest cshake = new CShakeDigest(128, new byte[0], Strings.ToByteArray("Email Signature")); cshake.BlockUpdate(Hex.Decode("00010203"), 0, 4); @@ -24,7 +30,7 @@ namespace Org.BouncyCastle.Crypto.Tests Assert.IsTrue(Arrays.AreEqual(Hex.Decode("c1c36925b6409a04f1b504fcbca9d82b4017277cb5ed2b2065fc1d3814d5aaf5"), res)); - cshake = new CSHAKEDigest(128, new byte[0], Strings.ToByteArray("Email Signature")); + cshake = new CShakeDigest(128, new byte[0], Strings.ToByteArray("Email Signature")); cshake.BlockUpdate(Hex.Decode( "000102030405060708090A0B0C0D0E0F" + @@ -47,7 +53,7 @@ namespace Org.BouncyCastle.Crypto.Tests Assert.IsTrue(Arrays.AreEqual(Hex.Decode("C5221D50E4F822D96A2E8881A961420F294B7B24FE3D2094BAED2C6524CC166B "), res)); - cshake = new CSHAKEDigest(256, new byte[0], Strings.ToByteArray("Email Signature")); + cshake = new CShakeDigest(256, new byte[0], Strings.ToByteArray("Email Signature")); cshake.BlockUpdate(Hex.Decode("00010203"), 0, 4); @@ -61,7 +67,7 @@ namespace Org.BouncyCastle.Crypto.Tests "64020E2BE0560858D9C00C037E34A969" + "37C561A74C412BB4C746469527281C8C"), res)); - cshake = new CSHAKEDigest(256, new byte[0], Strings.ToByteArray("Email Signature")); + cshake = new CShakeDigest(256, new byte[0], Strings.ToByteArray("Email Signature")); cshake.BlockUpdate(Hex.Decode( "000102030405060708090A0B0C0D0E0F" + @@ -92,33 +98,33 @@ namespace Org.BouncyCastle.Crypto.Tests longBlockTest(); checkZeroPadZ(); - checkSHAKE(128, new CSHAKEDigest(128, new byte[0], new byte[0]), Hex.Decode("eeaabeef")); - checkSHAKE(256, new CSHAKEDigest(256, new byte[0], null), Hex.Decode("eeaabeef")); - checkSHAKE(128, new CSHAKEDigest(128, null, new byte[0]), Hex.Decode("eeaabeef")); - checkSHAKE(128, new CSHAKEDigest(128, null, null), Hex.Decode("eeaabeef")); - checkSHAKE(256, new CSHAKEDigest(256, null, null), Hex.Decode("eeaabeef")); + checkSHAKE(128, new CShakeDigest(128, new byte[0], new byte[0]), Hex.Decode("eeaabeef")); + checkSHAKE(256, new CShakeDigest(256, new byte[0], null), Hex.Decode("eeaabeef")); + checkSHAKE(128, new CShakeDigest(128, null, new byte[0]), Hex.Decode("eeaabeef")); + checkSHAKE(128, new CShakeDigest(128, null, null), Hex.Decode("eeaabeef")); + checkSHAKE(256, new CShakeDigest(256, null, null), Hex.Decode("eeaabeef")); } private void checkZeroPadZ() { byte[] buf = new byte[20]; - CSHAKEDigest cshake1 = new CSHAKEDigest(256, new byte[0], new byte[265]); + CShakeDigest cshake1 = new CShakeDigest(256, new byte[0], new byte[265]); cshake1.DoOutput(buf, 0, buf.Length); Assert.IsTrue(Arrays.AreEqual(Hex.Decode("6e393540387004f087c4180db008acf6825190cf"), buf)); - CSHAKEDigest cshake2 = new CSHAKEDigest(128, new byte[0], new byte[329]); + CShakeDigest cshake2 = new CShakeDigest(128, new byte[0], new byte[329]); cshake2.DoOutput(buf, 0, buf.Length); Assert.IsTrue(Arrays.AreEqual(Hex.Decode("309bd7c285fcf8b839c9686b2cc00bd578947bee"), buf)); - cshake2 = new CSHAKEDigest(128, new byte[29], new byte[300]); + cshake2 = new CShakeDigest(128, new byte[29], new byte[300]); cshake2.DoOutput(buf, 0, buf.Length); Assert.IsTrue(Arrays.AreEqual(Hex.Decode("ff6aafd83b8d22fc3e2e9b9948b581967ed9c5e7"), buf)); } private void doFinalTest() { - CSHAKEDigest cshake = new CSHAKEDigest(128, new byte[0], Strings.ToByteArray("Email Signature")); + CShakeDigest cshake = new CShakeDigest(128, new byte[0], Strings.ToByteArray("Email Signature")); cshake.BlockUpdate(Hex.Decode("00010203"), 0, 4); @@ -138,7 +144,7 @@ namespace Org.BouncyCastle.Crypto.Tests cshake.DoFinal(res, 0, res.Length); - var s = Hex.ToHexString(res); + string s = Hex.ToHexString(res); Console.WriteLine(s); @@ -167,7 +173,7 @@ namespace Org.BouncyCastle.Crypto.Tests for (int i = 10000; i != data.Length; i++) { - CSHAKEDigest cshake_ = new CSHAKEDigest(128, new byte[0], Arrays.CopyOfRange(data, 0, i)); + CShakeDigest cshake_ = new CShakeDigest(128, new byte[0], Arrays.CopyOfRange(data, 0, i)); cshake_.BlockUpdate(Hex.Decode("00010203"), 0, 4); @@ -175,7 +181,7 @@ namespace Org.BouncyCastle.Crypto.Tests } - CSHAKEDigest cshake = new CSHAKEDigest(256, new byte[0], new byte[200]); + CShakeDigest cshake = new CShakeDigest(256, new byte[0], new byte[200]); cshake.BlockUpdate(Arrays.CopyOfRange(data, 0, 200), 0, 200); @@ -184,7 +190,7 @@ namespace Org.BouncyCastle.Crypto.Tests Assert.IsTrue(Arrays.AreEqual(Hex.Decode("4a899b5be460d85a9789215bc17f88b8f8ac049bd3b519f561e7b5d3870dafa3"), res)); } - private void checkSHAKE(int bitSize, CSHAKEDigest cshake, byte[] msg) + private void checkSHAKE(int bitSize, CShakeDigest cshake, byte[] msg) { ShakeDigest ref_ = new ShakeDigest(bitSize); @@ -201,5 +207,18 @@ namespace Org.BouncyCastle.Crypto.Tests Assert.IsTrue(Arrays.AreEqual(res1, res2)); } + public static void Main( + string[] args) + { + RunTest(new CShakeTest()); + } + + [Test] + public void TestFunction() + { + string resultText = Perform().ToString(); + + Assert.AreEqual(Name + ": Okay", resultText); + } } } diff --git a/crypto/test/src/crypto/test/KMACTest.cs b/crypto/test/src/crypto/test/KMACTest.cs index c7f3843c1..bf9c7d034 100644 --- a/crypto/test/src/crypto/test/KMACTest.cs +++ b/crypto/test/src/crypto/test/KMACTest.cs @@ -1,19 +1,26 @@ -using NUnit.Framework; +using System; +using System.Text; + +using NUnit.Framework; + using Org.BouncyCastle.Crypto.Macs; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - public class KMACTest + [TestFixture] + public class KMacTest + : SimpleTest { - [Test] - public void performTest() + public override string Name + { + get { return "KMAC"; } + } + + public override void PerformTest() { KMac kmac = new KMac(128, new byte[0] { }); @@ -133,7 +140,7 @@ namespace Org.BouncyCastle.Crypto.Tests } - public void doFinalTest() + private void doFinalTest() { KMac kmac = new KMac(128, Encoding.ASCII.GetBytes("My Tagged Application")); @@ -241,5 +248,19 @@ namespace Org.BouncyCastle.Crypto.Tests Assert.IsTrue(Arrays.AreEqual(res1, res2)); } + + public static void Main( + string[] args) + { + RunTest(new KMacTest()); + } + + [Test] + public void TestFunction() + { + string resultText = Perform().ToString(); + + Assert.AreEqual(Name + ": Okay", resultText); + } } } diff --git a/crypto/test/src/crypto/test/Poly1305Test.cs b/crypto/test/src/crypto/test/Poly1305Test.cs index 7aaceb99e..fd4053cd4 100644 --- a/crypto/test/src/crypto/test/Poly1305Test.cs +++ b/crypto/test/src/crypto/test/Poly1305Test.cs @@ -13,8 +13,7 @@ using Org.BouncyCastle.Security; namespace Org.BouncyCastle.Crypto.Tests { - /* - */ + [TestFixture] public class Poly1305Test : SimpleTest { diff --git a/crypto/test/src/crypto/test/RegressionTest.cs b/crypto/test/src/crypto/test/RegressionTest.cs index aa9cd0483..4b7b11651 100644 --- a/crypto/test/src/crypto/test/RegressionTest.cs +++ b/crypto/test/src/crypto/test/RegressionTest.cs @@ -143,7 +143,9 @@ namespace Org.BouncyCastle.Crypto.Tests new Ed448Test(), new KdfCounterTests(), new KdfDoublePipelineTests(), - new KdfFeedbackCounterTests() + new KdfFeedbackCounterTests(), + new CShakeTest(), + new KMacTest() }; public static void Main(string[] args) diff --git a/crypto/test/src/tsp/test/NewTspTest.cs b/crypto/test/src/tsp/test/NewTspTest.cs index dd2f084f2..74b3222ce 100644 --- a/crypto/test/src/tsp/test/NewTspTest.cs +++ b/crypto/test/src/tsp/test/NewTspTest.cs @@ -1,7 +1,11 @@ -using NUnit.Framework; +using System; +using System.Collections; +using System.IO; + +using NUnit.Framework; + using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cmp; -using Org.BouncyCastle.Asn1.Cms; using Org.BouncyCastle.Asn1.Ess; using Org.BouncyCastle.Asn1.Nist; using Org.BouncyCastle.Asn1.Oiw; @@ -15,20 +19,13 @@ using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Date; using Org.BouncyCastle.X509; using Org.BouncyCastle.X509.Store; -using System; -using System.Collections; -using System.IO; - namespace Org.BouncyCastle.Tsp.Tests { - - public class NewTspTest { private static DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0); - [Test] public void TestGeneral() { @@ -38,19 +35,18 @@ namespace Org.BouncyCastle.Tsp.Tests string origDN = "CN=Eric H. Echidna, E=eric@bouncycastle.org, O=Bouncy Castle, C=AU"; AsymmetricCipherKeyPair origKP = TspTestUtil.MakeKeyPair(); - var privateKey = origKP.Private; + AsymmetricKeyParameter privateKey = origKP.Private; - var cert = TspTestUtil.MakeCertificate(origKP, origDN, signKP, signDN); + X509Certificate cert = TspTestUtil.MakeCertificate(origKP, origDN, signKP, signDN); IList certList = new ArrayList(); certList.Add(cert); certList.Add(signCert); - var certs = X509StoreFactory.Create( + IX509Store certs = X509StoreFactory.Create( "Certificate/Collection", new X509CollectionStoreParameters(certList)); - basicTest(origKP.Private, cert, certs); resolutionTest(origKP.Private, cert, certs, Resolution.R_SECONDS, "19700101000009Z"); resolutionTest(origKP.Private, cert, certs, Resolution.R_TENTHS_OF_SECONDS, "19700101000009.9Z"); @@ -71,7 +67,6 @@ namespace Org.BouncyCastle.Tsp.Tests testNoNonse(origKP.Private, cert, certs); extensionTest(origKP.Private, cert, certs); additionalExtensionTest(origKP.Private, cert, certs); - } private void additionalExtensionTest(AsymmetricKeyParameter privateKey, X509Certificate cert, IX509Store certs) @@ -378,15 +373,15 @@ namespace Org.BouncyCastle.Tsp.Tests { tsToken.Validate(cert); } - catch (TspValidationException e) + catch (TspValidationException) { Assert.Fail("certReq(false) verification of token failed."); } - var store = tsToken.GetCertificates(); - var certsColl = store.GetMatches(null); + IX509Store store = tsToken.GetCertificates(); + ICollection certsColl = store.GetMatches(null); - if (certsColl.Count >0) + if (certsColl.Count > 0) { Assert.Fail("certReq(false) found certificates in response."); } @@ -654,7 +649,7 @@ namespace Org.BouncyCastle.Tsp.Tests IStreamCalculator calc = digCalc.CreateCalculator(); using (Stream s = calc.Stream) { - var crt = cert.GetEncoded(); + byte[] crt = cert.GetEncoded(); s.Write(crt, 0, crt.Length); } @@ -667,7 +662,7 @@ namespace Org.BouncyCastle.Tsp.Tests IStreamCalculator calc = digCalc.CreateCalculator(); using (Stream s = calc.Stream) { - var crt = cert.GetEncoded(); + byte[] crt = cert.GetEncoded(); s.Write(crt, 0, crt.Length); } @@ -761,13 +756,12 @@ namespace Org.BouncyCastle.Tsp.Tests private void basicSha256Test(AsymmetricKeyParameter privateKey, X509Certificate cert, IX509Store certs) { - var sInfoGenerator = makeInfoGenerator(privateKey, cert, TspAlgorithms.Sha256, null, null); - TimeStampTokenGenerator tsTokenGen = new TimeStampTokenGenerator( - sInfoGenerator, - Asn1DigestFactory.Get(NistObjectIdentifiers.IdSha256), new DerObjectIdentifier("1.2"), true); - + SignerInfoGenerator sInfoGenerator = makeInfoGenerator(privateKey, cert, TspAlgorithms.Sha256, null, null); + TimeStampTokenGenerator tsTokenGen = new TimeStampTokenGenerator( + sInfoGenerator, + Asn1DigestFactory.Get(NistObjectIdentifiers.IdSha256), new DerObjectIdentifier("1.2"), true); - tsTokenGen.SetCertificates(certs); + tsTokenGen.SetCertificates(certs); TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator(); TimeStampRequest request = reqGen.Generate(TspAlgorithms.Sha256, new byte[32], BigInteger.ValueOf(100)); @@ -792,7 +786,7 @@ namespace Org.BouncyCastle.Tsp.Tests IStreamCalculator calc = digCalc.CreateCalculator(); using (Stream s = calc.Stream) { - var crt = cert.GetEncoded(); + byte[] crt = cert.GetEncoded(); s.Write(crt, 0, crt.Length); } @@ -803,7 +797,6 @@ namespace Org.BouncyCastle.Tsp.Tests Assert.IsTrue(Arrays.AreEqual(certHash, sigCertV2.GetCerts()[0].GetCertHash())); } - private void resolutionTest(AsymmetricKeyParameter privateKey, X509.X509Certificate cert, IX509Store certs, Resolution resoution, string timeString) { TimeStampTokenGenerator tsTokenGen = new TimeStampTokenGenerator( @@ -882,8 +875,6 @@ namespace Org.BouncyCastle.Tsp.Tests Asn1.Cms.AttributeTable signedAttr, Asn1.Cms.AttributeTable unsignedAttr) { - - TspUtil.ValidateCertificate(cert); // @@ -896,13 +887,12 @@ namespace Org.BouncyCastle.Tsp.Tests } else { - signedAttrs = Platform.CreateHashtable(); + signedAttrs = new Hashtable(); } - - - string digestName = CmsSignedHelper.Instance.GetDigestAlgName(digestOID); - string signatureName = digestName + "with" + CmsSignedHelper.Instance.GetEncryptionAlgName(CmsSignedHelper.Instance.GetEncOid(key, digestOID)); + string digestName = TspTestUtil.GetDigestAlgName(digestOID); + string signatureName = digestName + "with" + TspTestUtil.GetEncryptionAlgName( + TspTestUtil.GetEncOid(key, digestOID)); Asn1SignatureFactory sigfact = new Asn1SignatureFactory(signatureName, key); return new SignerInfoGeneratorBuilder() diff --git a/crypto/test/src/tsp/test/TSPTest.cs b/crypto/test/src/tsp/test/TSPTest.cs index cb72f967b..968929b6d 100644 --- a/crypto/test/src/tsp/test/TSPTest.cs +++ b/crypto/test/src/tsp/test/TSPTest.cs @@ -558,8 +558,7 @@ namespace Org.BouncyCastle.Tsp.Tests [Test] public void TestBasicSha256() { - - var sInfoGenerator = makeInfoGenerator(privateKey, cert, TspAlgorithms.Sha256, null, null); + SignerInfoGenerator sInfoGenerator = MakeInfoGenerator(privateKey, cert, TspAlgorithms.Sha256, null, null); TimeStampTokenGenerator tsTokenGen = new TimeStampTokenGenerator( sInfoGenerator, Asn1DigestFactory.Get(NistObjectIdentifiers.IdSha256),new DerObjectIdentifier("1.2"),true); @@ -586,29 +585,21 @@ namespace Org.BouncyCastle.Tsp.Tests AttributeTable table = tsToken.SignedAttributes; - var r = table.Get(PkcsObjectIdentifiers.IdAASigningCertificateV2); + Asn1.Cms.Attribute r = table[PkcsObjectIdentifiers.IdAASigningCertificateV2]; Assert.NotNull(r); Assert.AreEqual(PkcsObjectIdentifiers.IdAASigningCertificateV2, r.AttrType); - var set = r.AttrValues; + Asn1Set set = r.AttrValues; SigningCertificateV2 sCert = SigningCertificateV2.GetInstance(set[0]); - var issSerNum = sCert.GetCerts()[0].IssuerSerial; + Asn1.X509.IssuerSerial issSerNum = sCert.GetCerts()[0].IssuerSerial; Assert.AreEqual(cert.SerialNumber, issSerNum.Serial.Value); - } - internal static SignerInfoGenerator makeInfoGenerator( - AsymmetricKeyParameter key, - X509Certificate cert, - string digestOID, - - Asn1.Cms.AttributeTable signedAttr, - Asn1.Cms.AttributeTable unsignedAttr) - { - - - TspUtil.ValidateCertificate(cert); + internal static SignerInfoGenerator MakeInfoGenerator(AsymmetricKeyParameter key, X509Certificate cert, + string digestOID, Asn1.Cms.AttributeTable signedAttr, Asn1.Cms.AttributeTable unsignedAttr) + { + TspUtil.ValidateCertificate(cert); // // Add the ESSCertID attribute @@ -620,13 +611,12 @@ namespace Org.BouncyCastle.Tsp.Tests } else { - signedAttrs = Platform.CreateHashtable(); + signedAttrs = new Hashtable(); } - - - string digestName = CmsSignedHelper.Instance.GetDigestAlgName(digestOID); - string signatureName = digestName + "with" + CmsSignedHelper.Instance.GetEncryptionAlgName(CmsSignedHelper.Instance.GetEncOid(key, digestOID)); + string digestName = TspTestUtil.GetDigestAlgName(digestOID); + string signatureName = digestName + "with" + TspTestUtil.GetEncryptionAlgName( + TspTestUtil.GetEncOid(key, digestOID)); Asn1SignatureFactory sigfact = new Asn1SignatureFactory(signatureName, key); return new SignerInfoGeneratorBuilder() @@ -637,6 +627,5 @@ namespace Org.BouncyCastle.Tsp.Tests new SimpleAttributeTableGenerator(unsignedAttr)) .Build(sigfact, cert); } - } } diff --git a/crypto/test/src/tsp/test/TSPTestUtil.cs b/crypto/test/src/tsp/test/TSPTestUtil.cs index f7b0c833a..c8c6a63c0 100644 --- a/crypto/test/src/tsp/test/TSPTestUtil.cs +++ b/crypto/test/src/tsp/test/TSPTestUtil.cs @@ -1,13 +1,21 @@ using System; +using System.Collections; using System.Text; using Org.BouncyCastle.Asn1; +using Org.BouncyCastle.Asn1.CryptoPro; +using Org.BouncyCastle.Asn1.Eac; +using Org.BouncyCastle.Asn1.Nist; +using Org.BouncyCastle.Asn1.Oiw; +using Org.BouncyCastle.Asn1.Pkcs; +using Org.BouncyCastle.Asn1.TeleTrust; using Org.BouncyCastle.Asn1.X509; +using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Security; +using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.X509; @@ -26,6 +34,44 @@ namespace Org.BouncyCastle.Tsp.Tests public static readonly bool Debug = true; public static DerObjectIdentifier EuroPkiTsaTestPolicy = new DerObjectIdentifier("1.3.6.1.4.1.5255.5.1"); + private static readonly string EncryptionECDsaWithSha1 = X9ObjectIdentifiers.ECDsaWithSha1.Id; + private static readonly string EncryptionECDsaWithSha224 = X9ObjectIdentifiers.ECDsaWithSha224.Id; + private static readonly string EncryptionECDsaWithSha256 = X9ObjectIdentifiers.ECDsaWithSha256.Id; + private static readonly string EncryptionECDsaWithSha384 = X9ObjectIdentifiers.ECDsaWithSha384.Id; + private static readonly string EncryptionECDsaWithSha512 = X9ObjectIdentifiers.ECDsaWithSha512.Id; + + public static readonly string DigestSha1 = OiwObjectIdentifiers.IdSha1.Id; + public static readonly string DigestSha224 = NistObjectIdentifiers.IdSha224.Id; + public static readonly string DigestSha256 = NistObjectIdentifiers.IdSha256.Id; + public static readonly string DigestSha384 = NistObjectIdentifiers.IdSha384.Id; + public static readonly string DigestSha512 = NistObjectIdentifiers.IdSha512.Id; + public static readonly string DigestMD5 = PkcsObjectIdentifiers.MD5.Id; + public static readonly string DigestGost3411 = CryptoProObjectIdentifiers.GostR3411.Id; + public static readonly string DigestRipeMD128 = TeleTrusTObjectIdentifiers.RipeMD128.Id; + public static readonly string DigestRipeMD160 = TeleTrusTObjectIdentifiers.RipeMD160.Id; + public static readonly string DigestRipeMD256 = TeleTrusTObjectIdentifiers.RipeMD256.Id; + + public static readonly string EncryptionRsa = PkcsObjectIdentifiers.RsaEncryption.Id; + public static readonly string EncryptionDsa = X9ObjectIdentifiers.IdDsaWithSha1.Id; + public static readonly string EncryptionECDsa = X9ObjectIdentifiers.ECDsaWithSha1.Id; + public static readonly string EncryptionRsaPss = PkcsObjectIdentifiers.IdRsassaPss.Id; + public static readonly string EncryptionGost3410 = CryptoProObjectIdentifiers.GostR3410x94.Id; + public static readonly string EncryptionECGost3410 = CryptoProObjectIdentifiers.GostR3410x2001.Id; + + private static readonly IDictionary encryptionAlgs = new Hashtable(); + private static readonly IDictionary digestAlgs = new Hashtable(); + private static readonly IDictionary digestAliases = new Hashtable(); + + private static readonly ISet noParams = new HashSet(); + private static readonly IDictionary ecAlgorithms = new Hashtable(); + + private static void AddEntries(DerObjectIdentifier oid, string digest, string encryption) + { + string alias = oid.Id; + digestAlgs.Add(alias, digest); + encryptionAlgs.Add(alias, encryption); + } + static TspTestUtil() { rand = new SecureRandom(); @@ -50,6 +96,83 @@ namespace Org.BouncyCastle.Tsp.Tests rc2128kg.Init(new KeyGenerationParameters(rand, 128)); serialNumber = BigInteger.One; + + AddEntries(NistObjectIdentifiers.DsaWithSha224, "SHA224", "DSA"); + AddEntries(NistObjectIdentifiers.DsaWithSha256, "SHA256", "DSA"); + AddEntries(NistObjectIdentifiers.DsaWithSha384, "SHA384", "DSA"); + AddEntries(NistObjectIdentifiers.DsaWithSha512, "SHA512", "DSA"); + AddEntries(OiwObjectIdentifiers.DsaWithSha1, "SHA1", "DSA"); + AddEntries(OiwObjectIdentifiers.MD4WithRsa, "MD4", "RSA"); + AddEntries(OiwObjectIdentifiers.MD4WithRsaEncryption, "MD4", "RSA"); + AddEntries(OiwObjectIdentifiers.MD5WithRsa, "MD5", "RSA"); + AddEntries(OiwObjectIdentifiers.Sha1WithRsa, "SHA1", "RSA"); + AddEntries(PkcsObjectIdentifiers.MD2WithRsaEncryption, "MD2", "RSA"); + AddEntries(PkcsObjectIdentifiers.MD4WithRsaEncryption, "MD4", "RSA"); + AddEntries(PkcsObjectIdentifiers.MD5WithRsaEncryption, "MD5", "RSA"); + AddEntries(PkcsObjectIdentifiers.Sha1WithRsaEncryption, "SHA1", "RSA"); + AddEntries(PkcsObjectIdentifiers.Sha224WithRsaEncryption, "SHA224", "RSA"); + AddEntries(PkcsObjectIdentifiers.Sha256WithRsaEncryption, "SHA256", "RSA"); + AddEntries(PkcsObjectIdentifiers.Sha384WithRsaEncryption, "SHA384", "RSA"); + AddEntries(PkcsObjectIdentifiers.Sha512WithRsaEncryption, "SHA512", "RSA"); + AddEntries(X9ObjectIdentifiers.ECDsaWithSha1, "SHA1", "ECDSA"); + AddEntries(X9ObjectIdentifiers.ECDsaWithSha224, "SHA224", "ECDSA"); + AddEntries(X9ObjectIdentifiers.ECDsaWithSha256, "SHA256", "ECDSA"); + AddEntries(X9ObjectIdentifiers.ECDsaWithSha384, "SHA384", "ECDSA"); + AddEntries(X9ObjectIdentifiers.ECDsaWithSha512, "SHA512", "ECDSA"); + AddEntries(X9ObjectIdentifiers.IdDsaWithSha1, "SHA1", "DSA"); + AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_1, "SHA1", "ECDSA"); + AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_224, "SHA224", "ECDSA"); + AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_256, "SHA256", "ECDSA"); + AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_384, "SHA384", "ECDSA"); + AddEntries(EacObjectIdentifiers.id_TA_ECDSA_SHA_512, "SHA512", "ECDSA"); + AddEntries(EacObjectIdentifiers.id_TA_RSA_v1_5_SHA_1, "SHA1", "RSA"); + AddEntries(EacObjectIdentifiers.id_TA_RSA_v1_5_SHA_256, "SHA256", "RSA"); + AddEntries(EacObjectIdentifiers.id_TA_RSA_PSS_SHA_1, "SHA1", "RSAandMGF1"); + AddEntries(EacObjectIdentifiers.id_TA_RSA_PSS_SHA_256, "SHA256", "RSAandMGF1"); + + encryptionAlgs.Add(X9ObjectIdentifiers.IdDsa.Id, "DSA"); + encryptionAlgs.Add(PkcsObjectIdentifiers.RsaEncryption.Id, "RSA"); + encryptionAlgs.Add(TeleTrusTObjectIdentifiers.TeleTrusTRsaSignatureAlgorithm.Id, "RSA"); + encryptionAlgs.Add(X509ObjectIdentifiers.IdEARsa.Id, "RSA"); + encryptionAlgs.Add(EncryptionRsaPss, "RSAandMGF1"); + encryptionAlgs.Add(CryptoProObjectIdentifiers.GostR3410x94.Id, "GOST3410"); + encryptionAlgs.Add(CryptoProObjectIdentifiers.GostR3410x2001.Id, "ECGOST3410"); + encryptionAlgs.Add("1.3.6.1.4.1.5849.1.6.2", "ECGOST3410"); + encryptionAlgs.Add("1.3.6.1.4.1.5849.1.1.5", "GOST3410"); + + digestAlgs.Add(PkcsObjectIdentifiers.MD2.Id, "MD2"); + digestAlgs.Add(PkcsObjectIdentifiers.MD4.Id, "MD4"); + digestAlgs.Add(PkcsObjectIdentifiers.MD5.Id, "MD5"); + digestAlgs.Add(OiwObjectIdentifiers.IdSha1.Id, "SHA1"); + digestAlgs.Add(NistObjectIdentifiers.IdSha224.Id, "SHA224"); + digestAlgs.Add(NistObjectIdentifiers.IdSha256.Id, "SHA256"); + digestAlgs.Add(NistObjectIdentifiers.IdSha384.Id, "SHA384"); + digestAlgs.Add(NistObjectIdentifiers.IdSha512.Id, "SHA512"); + digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD128.Id, "RIPEMD128"); + digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD160.Id, "RIPEMD160"); + digestAlgs.Add(TeleTrusTObjectIdentifiers.RipeMD256.Id, "RIPEMD256"); + digestAlgs.Add(CryptoProObjectIdentifiers.GostR3411.Id, "GOST3411"); + digestAlgs.Add("1.3.6.1.4.1.5849.1.2.1", "GOST3411"); + + digestAliases.Add("SHA1", new string[] { "SHA-1" }); + digestAliases.Add("SHA224", new string[] { "SHA-224" }); + digestAliases.Add("SHA256", new string[] { "SHA-256" }); + digestAliases.Add("SHA384", new string[] { "SHA-384" }); + digestAliases.Add("SHA512", new string[] { "SHA-512" }); + + noParams.Add(EncryptionDsa); + //noParams.Add(EncryptionECDsa); + noParams.Add(EncryptionECDsaWithSha1); + noParams.Add(EncryptionECDsaWithSha224); + noParams.Add(EncryptionECDsaWithSha256); + noParams.Add(EncryptionECDsaWithSha384); + noParams.Add(EncryptionECDsaWithSha512); + + ecAlgorithms.Add(DigestSha1, EncryptionECDsaWithSha1); + ecAlgorithms.Add(DigestSha224, EncryptionECDsaWithSha224); + ecAlgorithms.Add(DigestSha256, EncryptionECDsaWithSha256); + ecAlgorithms.Add(DigestSha384, EncryptionECDsaWithSha384); + ecAlgorithms.Add(DigestSha512, EncryptionECDsaWithSha512); } public static string DumpBase64( @@ -75,6 +198,100 @@ namespace Org.BouncyCastle.Tsp.Tests return buf.ToString(); } + public static string GetDigestAlgName(string digestAlgOid) + { + string algName = (string)digestAlgs[digestAlgOid]; + + if (algName != null) + { + return algName; + } + + return digestAlgOid; + } + + public static string GetEncryptionAlgName(string encryptionAlgOid) + { + string algName = (string)encryptionAlgs[encryptionAlgOid]; + + if (algName != null) + { + return algName; + } + + return encryptionAlgOid; + } + + internal static string GetEncOid( + AsymmetricKeyParameter key, + string digestOID) + { + string encOID = null; + + if (key is RsaKeyParameters) + { + if (!((RsaKeyParameters)key).IsPrivate) + throw new ArgumentException("Expected RSA private key"); + + encOID = EncryptionRsa; + } + else if (key is DsaPrivateKeyParameters) + { + if (digestOID.Equals(DigestSha1)) + { + encOID = EncryptionDsa; + } + else if (digestOID.Equals(DigestSha224)) + { + encOID = NistObjectIdentifiers.DsaWithSha224.Id; + } + else if (digestOID.Equals(DigestSha256)) + { + encOID = NistObjectIdentifiers.DsaWithSha256.Id; + } + else if (digestOID.Equals(DigestSha384)) + { + encOID = NistObjectIdentifiers.DsaWithSha384.Id; + } + else if (digestOID.Equals(DigestSha512)) + { + encOID = NistObjectIdentifiers.DsaWithSha512.Id; + } + else + { + throw new ArgumentException("can't mix DSA with anything but SHA1/SHA2"); + } + } + else if (key is ECPrivateKeyParameters) + { + ECPrivateKeyParameters ecPrivKey = (ECPrivateKeyParameters)key; + string algName = ecPrivKey.AlgorithmName; + + if (algName == "ECGOST3410") + { + encOID = EncryptionECGost3410; + } + else + { + // TODO Should we insist on algName being one of "EC" or "ECDSA", as Java does? + encOID = (string)ecAlgorithms[digestOID]; + + if (encOID == null) + throw new ArgumentException("can't mix ECDSA with anything but SHA family digests"); + } + } + else if (key is Gost3410PrivateKeyParameters) + { + encOID = EncryptionGost3410; + } + else + { + throw new ArgumentException("Unknown algorithm in CmsSignedGenerator.GetEncOid"); + } + + return encOID; + } + public static AsymmetricCipherKeyPair MakeKeyPair() { return kpg.GenerateKeyPair(); |