From ddca250a1787467d93ee97e3e5319425921d8095 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 8 Dec 2020 17:25:55 +0700 Subject: Refactor result normalization --- crypto/src/math/raw/Mod.cs | 64 ++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/crypto/src/math/raw/Mod.cs b/crypto/src/math/raw/Mod.cs index 1dcfcb6b3..49ac91351 100644 --- a/crypto/src/math/raw/Mod.cs +++ b/crypto/src/math/raw/Mod.cs @@ -79,22 +79,18 @@ namespace Org.BouncyCastle.Math.Raw } int signF = F[len30 - 1] >> 31; + CNegate30(len30, signF, F); /* * D is in the range (-2.M, M). First, conditionally add M if D is negative, to bring it * into the range (-M, M). Then normalize by conditionally negating (according to signF) * and/or then adding M, to bring it into the range [0, M). */ - int signD = D[len30 - 1] >> 31; - signD = CAdd30(len30, signD, D, M); CNormalize30(len30, signF, D, M); Decode30(bits, D, 0, z, 0); Debug.Assert(0 != Nat.LessThan(len32, z, m)); - signF = CNegate30(len30, signF, F); - Debug.Assert(0 == signF); - return (uint)(EqualTo(len30, F, 1) & EqualToZero(len30, G)); } @@ -228,24 +224,7 @@ namespace Org.BouncyCastle.Math.Raw return c; } - private static int CAdd30(int len30, int cond, int[] D, int[] M) - { - Debug.Assert(len30 > 0); - Debug.Assert(D.Length >= len30); - Debug.Assert(M.Length >= len30); - - int c = 0, last = len30 - 1; - for (int i = 0; i < last; ++i) - { - c += D[i] + (M[i] & cond); - D[i] = c & M30; c >>= 30; - } - c += D[last] + (M[last] & cond); - D[last] = c; c >>= 30; - return c; - } - - private static int CNegate30(int len30, int cond, int[] D) + private static void CNegate30(int len30, int cond, int[] D) { Debug.Assert(len30 > 0); Debug.Assert(D.Length >= len30); @@ -257,8 +236,7 @@ namespace Org.BouncyCastle.Math.Raw D[i] = c & M30; c >>= 30; } c += (D[last] ^ cond) - cond; - D[last] = c; c >>= 30; - return c; + D[last] = c; } private static void CNormalize30(int len30, int condNegate, int[] D, int[] M) @@ -267,16 +245,36 @@ namespace Org.BouncyCastle.Math.Raw Debug.Assert(D.Length >= len30); Debug.Assert(M.Length >= len30); - int c = 0, last = len30 - 1; - int condAdd = (D[last] >> 31) ^ condNegate; - for (int i = 0; i < last; ++i) + int last = len30 - 1; + { - c += (D[i] ^ condNegate) - condNegate + (M[i] & condAdd); - D[i] = c & M30; c >>= 30; + int c = 0, condAdd = D[last] >> 31; + for (int i = 0; i < last; ++i) + { + int di = D[i] + (M[i] & condAdd); + di = (di ^ condNegate) - condNegate; + c += di; D[i] = c & M30; c >>= 30; + } + { + int di = D[last] + (M[last] & condAdd); + di = (di ^ condNegate) - condNegate; + c += di; D[last] = c; + } + } + + { + int c = 0, condAdd = D[last] >> 31; + for (int i = 0; i < last; ++i) + { + int di = D[i] + (M[i] & condAdd); + c += di; D[i] = c & M30; c >>= 30; + } + { + int di = D[last] + (M[last] & condAdd); + c += di; D[last] = c; + } + Debug.Assert(c >> 30 == 0); } - c += (D[last] ^ condNegate) - condNegate + (M[last] & condAdd); - D[last] = c; - Debug.Assert(c >> 30 == 0); } private static void Decode30(int bits, int[] x, int xOff, uint[] z, int zOff) -- cgit 1.4.1 From 0c2afde8c313966d97a9fc64012bd04775ce51d7 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 8 Dec 2020 20:46:40 +0700 Subject: Cleanup --- crypto/test/src/crypto/test/CSHAKETest.cs | 4 +- crypto/test/src/crypto/test/NistEccTest.cs | 16 +++++--- crypto/test/src/pkcs/test/PKCS12StoreTest.cs | 4 +- crypto/test/src/tsp/test/NewTspTest.cs | 58 ++++++++++++++-------------- 4 files changed, 45 insertions(+), 37 deletions(-) diff --git a/crypto/test/src/crypto/test/CSHAKETest.cs b/crypto/test/src/crypto/test/CSHAKETest.cs index e7c6ce9e0..753663b4b 100644 --- a/crypto/test/src/crypto/test/CSHAKETest.cs +++ b/crypto/test/src/crypto/test/CSHAKETest.cs @@ -136,7 +136,7 @@ namespace Org.BouncyCastle.Crypto.Tests cshake.DoOutput(res, 0, res.Length); - Assert.IsTrue(!Arrays.AreEqual(Hex.Decode("c1c36925b6409a04f1b504fcbca9d82b4017277cb5ed2b2065fc1d3814d5aaf5"), res)); + Assert.IsTrue(!Arrays.AreEqual(Hex.Decode("c1c36925b6409a04f1b504fcbca9d82b4017277cb5ed2b2065fc1d3814d5aaf5"), res)); cshake.DoFinal(res, 0, res.Length); @@ -146,7 +146,7 @@ namespace Org.BouncyCastle.Crypto.Tests string s = Hex.ToHexString(res); - Console.WriteLine(s); + //Console.WriteLine(s); Assert.IsTrue(Arrays.AreEqual(Hex.Decode("c1c36925b6409a04f1b504fcbca9d82b4017277cb5ed2b2065fc1d3814d5aaf5"), res)); diff --git a/crypto/test/src/crypto/test/NistEccTest.cs b/crypto/test/src/crypto/test/NistEccTest.cs index 2b0edb63d..a8e4a6cb2 100644 --- a/crypto/test/src/crypto/test/NistEccTest.cs +++ b/crypto/test/src/crypto/test/NistEccTest.cs @@ -2,7 +2,9 @@ using System; using System.Collections; using System.IO; using System.Text.RegularExpressions; + using NUnit.Framework; + using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; @@ -13,7 +15,10 @@ namespace Org.BouncyCastle.Crypto.Tests [TestFixture] public class NistEccTest : SimpleTest { - public override string Name { get; } = "NistEcc"; + public override string Name + { + get { return "NistEcc"; } + } public override void PerformTest() { @@ -43,24 +48,25 @@ namespace Org.BouncyCastle.Crypto.Tests { Regex capture = new Regex(@"^ ?(\w+):? =? ?(\w+)", RegexOptions.Compiled); Match data = capture.Match(line); + if (!data.Success) + continue; - if (!data.Success) continue; string nistKey = data.Groups[1].Value; string nistValue = data.Groups[2].Value; switch (nistKey) { case "Curve": // Change curve name from LNNN to L-NNN ie: P256 to P-256 - curve = $"{nistValue.Substring(0, 1)}-{nistValue.Substring(1)}"; + curve = nistValue.Insert(1, "-"); break; case "k": k = new BigInteger(nistValue, 10); break; case "x": - x = new BigInteger(nistValue, radix: 16); + x = new BigInteger(nistValue, 16); break; case "y": - y = new BigInteger(nistValue, radix: 16); + y = new BigInteger(nistValue, 16); break; } diff --git a/crypto/test/src/pkcs/test/PKCS12StoreTest.cs b/crypto/test/src/pkcs/test/PKCS12StoreTest.cs index 19749278c..3ac28b99f 100644 --- a/crypto/test/src/pkcs/test/PKCS12StoreTest.cs +++ b/crypto/test/src/pkcs/test/PKCS12StoreTest.cs @@ -1334,8 +1334,8 @@ namespace Org.BouncyCastle.Pkcs.Tests public void TestFunction() { string resultText = Perform().ToString(); - Console.Out.WriteLine(resultText); - Assert.AreEqual(Name + ": Okay", resultText); + + Assert.AreEqual(Name + ": Okay", resultText); } } } diff --git a/crypto/test/src/tsp/test/NewTspTest.cs b/crypto/test/src/tsp/test/NewTspTest.cs index 74b3222ce..4c9da7edb 100644 --- a/crypto/test/src/tsp/test/NewTspTest.cs +++ b/crypto/test/src/tsp/test/NewTspTest.cs @@ -598,7 +598,7 @@ namespace Org.BouncyCastle.Tsp.Tests Assert.Fail("response validation failed on invalid nonce."); } - catch (TspValidationException e) + catch (TspValidationException) { // ignore } @@ -611,7 +611,7 @@ namespace Org.BouncyCastle.Tsp.Tests Assert.Fail("response validation failed on wrong digest."); } - catch (TspValidationException e) + catch (TspValidationException) { // ignore } @@ -624,7 +624,7 @@ namespace Org.BouncyCastle.Tsp.Tests Assert.Fail("response validation failed on wrong digest."); } - catch (TspValidationException e) + catch (TspValidationException) { // ignore } @@ -670,27 +670,20 @@ namespace Org.BouncyCastle.Tsp.Tests } - EssCertID essCertid = new EssCertID(certHash, issuerSerial); - EssCertIDv2 essCertidV2 = new EssCertIDv2(certHash256, issuerSerial); + EssCertID essCertID = new EssCertID(certHash, issuerSerial); + EssCertIDv2 essCertIDv2 = new EssCertIDv2(certHash256, issuerSerial); - signerInfoGenBuilder.WithSignedAttributeGenerator(new TestAttrGen() - { - EssCertID = essCertid, - EssCertIDv2 = essCertidV2 - }); + signerInfoGenBuilder.WithSignedAttributeGenerator(new TestAttrGen(essCertID, essCertIDv2)); Asn1SignatureFactory sigfact = new Asn1SignatureFactory("SHA1WithRSA", privateKey); - SignerInfoGenerator - signerInfoGenerator = signerInfoGenBuilder.Build(sigfact, cert); + SignerInfoGenerator signerInfoGenerator = signerInfoGenBuilder.Build(sigfact, cert); - TimeStampTokenGenerator tsTokenGen = new TimeStampTokenGenerator( - signerInfoGenerator, - Asn1DigestFactory.Get(OiwObjectIdentifiers.IdSha1), new DerObjectIdentifier("1.2"), true); + TimeStampTokenGenerator tsTokenGen = new TimeStampTokenGenerator(signerInfoGenerator, + Asn1DigestFactory.Get(OiwObjectIdentifiers.IdSha1), new DerObjectIdentifier("1.2"), true); + tsTokenGen.SetCertificates(certs); - tsTokenGen.SetCertificates(certs); - TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator(); TimeStampRequest request = reqGen.Generate(TspAlgorithms.Sha1, new byte[20], BigInteger.ValueOf(100)); @@ -904,28 +897,37 @@ namespace Org.BouncyCastle.Tsp.Tests .Build(sigfact, cert); } - - - - private class TestAttrGen : CmsAttributeTableGenerator + private class TestAttrGen : CmsAttributeTableGenerator { + private readonly EssCertID mEssCertID; + private readonly EssCertIDv2 mEssCertIDv2; - public EssCertID EssCertID { get; set; } + public TestAttrGen(EssCertID essCertID, EssCertIDv2 essCertIDv2) + { + this.mEssCertID = essCertID; + this.mEssCertIDv2 = essCertIDv2; + } + + public EssCertID EssCertID + { + get { return mEssCertID; } + } - public EssCertIDv2 EssCertIDv2 { get; set; } + public EssCertIDv2 EssCertIDv2 + { + get { return mEssCertIDv2; } + } - public Asn1.Cms.AttributeTable GetAttributes(IDictionary parameters) + public Asn1.Cms.AttributeTable GetAttributes(IDictionary parameters) { CmsAttributeTableGenerator attrGen = new DefaultSignedAttributeTableGenerator(); - Asn1.Cms.AttributeTable table = attrGen.GetAttributes(parameters); + Asn1.Cms.AttributeTable table = attrGen.GetAttributes(parameters); table = table.Add(PkcsObjectIdentifiers.IdAASigningCertificate, new SigningCertificate(EssCertID)); table = table.Add(PkcsObjectIdentifiers.IdAASigningCertificateV2, new SigningCertificateV2(new EssCertIDv2[] { EssCertIDv2 })); - return table; + return table; } } - } - } -- cgit 1.4.1 From 55c1998ff3a60c06df25c76854f9900046262beb Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 8 Dec 2020 21:01:06 +0700 Subject: Update versions and release notes for 1.8.9 --- crypto/NBuild.build | 2 +- crypto/Readme.html | 18 ++++++++++++++++++ crypto/src/AssemblyInfo.cs | 6 +++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/crypto/NBuild.build b/crypto/NBuild.build index 06d297aae..e14102139 100644 --- a/crypto/NBuild.build +++ b/crypto/NBuild.build @@ -16,7 +16,7 @@ - + diff --git a/crypto/Readme.html b/crypto/Readme.html index 9a368f47a..12d48e0a9 100644 --- a/crypto/Readme.html +++ b/crypto/Readme.html @@ -30,6 +30,8 @@
  • Notes:
      +
    1. + Release 1.8.9
    2. Release 1.8.8
    3. @@ -304,6 +306,22 @@ We state, where EC MQV has not otherwise been disabled or removed:

      Notes:

      +

      Release 1.8.9, Tuesday December 8, 2020

      + +
      Additional Features and Functionality
      +
        +
      • Added CSHAKE digest and KMAC.
      • +
      • Added support for PKCS#5 Scheme 2 to Pkcs12Store.
      • +
      • Improved performance for GCM.
      • +
      +
      Additional Notes
      +
        +
      • + See the (cumulative) list of GitHub pull requests that we have accepted at + bcgit/bc-csharp. +
      • +
      +

      Release 1.8.8, Monday September 21, 2020

      Additional Features and Functionality
      diff --git a/crypto/src/AssemblyInfo.cs b/crypto/src/AssemblyInfo.cs index 446c4d9a9..7332b4b25 100644 --- a/crypto/src/AssemblyInfo.cs +++ b/crypto/src/AssemblyInfo.cs @@ -33,9 +33,9 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("1.8.8.0")] -[assembly: AssemblyFileVersion("1.8.20265.1")] -[assembly: AssemblyInformationalVersion("1.8.8")] +[assembly: AssemblyVersion("1.8.9.0")] +[assembly: AssemblyFileVersion("1.8.20343.1")] +[assembly: AssemblyInformationalVersion("1.8.9")] // // In order to sign your assembly you must specify a key to use. Refer to the -- cgit 1.4.1