summary refs log tree commit diff
diff options
context:
space:
mode:
authorOren Novotny <oren@novotny.org>2018-05-19 18:34:05 -0400
committerOren Novotny <oren@novotny.org>2018-05-19 18:34:05 -0400
commit5a698b04dfbf8cd3376e16eb0f5fd145f95737bb (patch)
tree6cbdf3e22691f0a50133027ec57e1c8fb023c972
parentmerge master into netstandard (diff)
downloadBouncyCastle.NET-ed25519-5a698b04dfbf8cd3376e16eb0f5fd145f95737bb.tar.xz
Update after merge
-rw-r--r--crypto/src/crypto.csproj2
-rw-r--r--crypto/src/crypto/generators/OpenBsdBCrypt.cs4
-rw-r--r--crypto/src/math/ec/rfc7748/X25519.cs81
-rw-r--r--crypto/src/math/ec/rfc7748/X448.cs89
-rw-r--r--crypto/test/crypto.test.csproj9
5 files changed, 98 insertions, 87 deletions
diff --git a/crypto/src/crypto.csproj b/crypto/src/crypto.csproj
index bb3de0c9c..7a246c171 100644
--- a/crypto/src/crypto.csproj
+++ b/crypto/src/crypto.csproj
@@ -35,7 +35,7 @@
     <DefineConstants>$(DefineConstants);LIB</DefineConstants>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(TargetFramework)' == 'portable-net4+sl5+wp8+win8+wpa81' ">
-    <DefineConstants>$(DefineConstants);PORTABLE;NO_THREADS</DefineConstants>
+    <DefineConstants>$(DefineConstants);PORTABLE;NO_THREADS;LEGACY_PCL</DefineConstants>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="..\bzip2\src\**\*.cs" LinkBase="bzip2" />
diff --git a/crypto/src/crypto/generators/OpenBsdBCrypt.cs b/crypto/src/crypto/generators/OpenBsdBCrypt.cs
index 49f79f95b..399d12391 100644
--- a/crypto/src/crypto/generators/OpenBsdBCrypt.cs
+++ b/crypto/src/crypto/generators/OpenBsdBCrypt.cs
@@ -174,7 +174,11 @@ namespace Org.BouncyCastle.Crypto.Generators
             }
             catch (Exception nfe)
             {
+#if LEGACY_PCL
+                throw new ArgumentException("Invalid cost factor (bcryptString): " + bcryptString.Substring(4, 2), nfe);
+#else
                 throw new ArgumentException("Invalid cost factor: " + bcryptString.Substring(4, 2), "bcryptString", nfe);
+#endif
             }
             if (cost < 4 || cost > 31)
                 throw new ArgumentException("Invalid cost factor: " + cost + ", 4 < cost < 31 expected.");
diff --git a/crypto/src/math/ec/rfc7748/X25519.cs b/crypto/src/math/ec/rfc7748/X25519.cs
index 16f680d90..8c52a21ac 100644
--- a/crypto/src/math/ec/rfc7748/X25519.cs
+++ b/crypto/src/math/ec/rfc7748/X25519.cs
@@ -54,63 +54,68 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748
             X25519Field.Mul(z, A, z);
         }
 
-        [MethodImpl(MethodImplOptions.Synchronized)]
+        // https://stackoverflow.com/questions/2223656/what-does-methodimploptions-synchronized-do
+        // Not available in lower .net standard versions
+        //[MethodImpl(MethodImplOptions.Synchronized)]
         public static void Precompute()
         {
-            if (precompBase != null)
-                return;
+            lock (typeof(X25519))
+            {
+                if (precompBase != null)
+                    return;
 
-            precompBase = new int[X25519Field.Size * 252];
+                precompBase = new int[X25519Field.Size * 252];
 
-            int[] xs = precompBase;
-            int[] zs = new int[X25519Field.Size * 251];
+                int[] xs = precompBase;
+                int[] zs = new int[X25519Field.Size * 251];
 
-            int[] x = X25519Field.Create();     x[0] = 9;          
-            int[] z = X25519Field.Create();     z[0] = 1;
+                int[] x = X25519Field.Create(); x[0] = 9;
+                int[] z = X25519Field.Create(); z[0] = 1;
 
-            int[] n = X25519Field.Create();
-            int[] d = X25519Field.Create();
+                int[] n = X25519Field.Create();
+                int[] d = X25519Field.Create();
 
-            X25519Field.Apm(x, z, n, d);
+                X25519Field.Apm(x, z, n, d);
 
-            int[] c = X25519Field.Create();     X25519Field.Copy(d, 0, c, 0);
+                int[] c = X25519Field.Create(); X25519Field.Copy(d, 0, c, 0);
 
-            int off = 0;
-            for (;;)
-            {
-                X25519Field.Copy(n, 0, xs, off);
+                int off = 0;
+                for (; ; )
+                {
+                    X25519Field.Copy(n, 0, xs, off);
 
-                if (off == (X25519Field.Size * 251))
-                    break;
+                    if (off == (X25519Field.Size * 251))
+                        break;
 
-                PointDouble(x, z);
+                    PointDouble(x, z);
 
-                X25519Field.Apm(x, z, n, d);
-                X25519Field.Mul(n, c, n);
-                X25519Field.Mul(c, d, c);
+                    X25519Field.Apm(x, z, n, d);
+                    X25519Field.Mul(n, c, n);
+                    X25519Field.Mul(c, d, c);
 
-                X25519Field.Copy(d, 0, zs, off);
+                    X25519Field.Copy(d, 0, zs, off);
 
-                off += X25519Field.Size;
-            }
+                    off += X25519Field.Size;
+                }
 
-            int[] u = X25519Field.Create();
-            X25519Field.Inv(c, u);
+                int[] u = X25519Field.Create();
+                X25519Field.Inv(c, u);
 
-            for (;;)
-            {
-                X25519Field.Copy(xs, off, x, 0);
+                for (; ; )
+                {
+                    X25519Field.Copy(xs, off, x, 0);
 
-                X25519Field.Mul(x, u, x);
-                //X25519Field.Normalize(x);
-                X25519Field.Copy(x, 0, precompBase, off);
+                    X25519Field.Mul(x, u, x);
+                    //X25519Field.Normalize(x);
+                    X25519Field.Copy(x, 0, precompBase, off);
 
-                if (off == 0)
-                    break;
+                    if (off == 0)
+                        break;
 
-                off -= X25519Field.Size;
-                X25519Field.Copy(zs, off, z, 0);
-                X25519Field.Mul(u, z, u);
+                    off -= X25519Field.Size;
+                    X25519Field.Copy(zs, off, z, 0);
+                    X25519Field.Mul(u, z, u);
+                }
             }
         }
 
diff --git a/crypto/src/math/ec/rfc7748/X448.cs b/crypto/src/math/ec/rfc7748/X448.cs
index 32a4a9e2a..8ded25a06 100644
--- a/crypto/src/math/ec/rfc7748/X448.cs
+++ b/crypto/src/math/ec/rfc7748/X448.cs
@@ -58,67 +58,72 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748
             X448Field.Mul(z, A, z);
         }
 
-        [MethodImpl(MethodImplOptions.Synchronized)]
+        // https://stackoverflow.com/questions/2223656/what-does-methodimploptions-synchronized-do
+        // Not available in lower .net standard versions
+        //[MethodImpl(MethodImplOptions.Synchronized)]
         public static void Precompute()
         {
-            if (precompBase != null)
-                return;
+            lock (typeof(X448))
+            {
+                if (precompBase != null)
+                    return;
 
-            precompBase = new uint[X448Field.Size * 446];
+                precompBase = new uint[X448Field.Size * 446];
 
-            uint[] xs = precompBase;
-            uint[] zs = new uint[X448Field.Size * 445];
+                uint[] xs = precompBase;
+                uint[] zs = new uint[X448Field.Size * 445];
 
-            uint[] x = X448Field.Create();     x[0] = 5;          
-            uint[] z = X448Field.Create();     z[0] = 1;
+                uint[] x = X448Field.Create(); x[0] = 5;
+                uint[] z = X448Field.Create(); z[0] = 1;
 
-            uint[] n = X448Field.Create();
-            uint[] d = X448Field.Create();
+                uint[] n = X448Field.Create();
+                uint[] d = X448Field.Create();
 
-            //X448Field.Apm(x, z, n, d);
-            X448Field.Add(x, z, n);
-            X448Field.Sub(x, z, d);
+                //X448Field.Apm(x, z, n, d);
+                X448Field.Add(x, z, n);
+                X448Field.Sub(x, z, d);
 
-            uint[] c = X448Field.Create();     X448Field.Copy(d, 0, c, 0);
+                uint[] c = X448Field.Create(); X448Field.Copy(d, 0, c, 0);
 
-            int off = 0;
-            for (;;)
-            {
-                X448Field.Copy(n, 0, xs, off);
+                int off = 0;
+                for (; ; )
+                {
+                    X448Field.Copy(n, 0, xs, off);
 
-                if (off == (X448Field.Size * 445))
-                    break;
+                    if (off == (X448Field.Size * 445))
+                        break;
 
-                PointDouble(x, z);
+                    PointDouble(x, z);
 
-                //X448Field.Apm(x, z, n, d);
-                X448Field.Add(x, z, n);
-                X448Field.Sub(x, z, d);
-                X448Field.Mul(n, c, n);
-                X448Field.Mul(c, d, c);
+                    //X448Field.Apm(x, z, n, d);
+                    X448Field.Add(x, z, n);
+                    X448Field.Sub(x, z, d);
+                    X448Field.Mul(n, c, n);
+                    X448Field.Mul(c, d, c);
 
-                X448Field.Copy(d, 0, zs, off);
+                    X448Field.Copy(d, 0, zs, off);
 
-                off += X448Field.Size;
-            }
+                    off += X448Field.Size;
+                }
 
-            uint[] u = X448Field.Create();
-            X448Field.Inv(c, u);
+                uint[] u = X448Field.Create();
+                X448Field.Inv(c, u);
 
-            for (;;)
-            {
-                X448Field.Copy(xs, off, x, 0);
+                for (; ; )
+                {
+                    X448Field.Copy(xs, off, x, 0);
 
-                X448Field.Mul(x, u, x);
-                //X448Field.Normalize(x);
-                X448Field.Copy(x, 0, precompBase, off);
+                    X448Field.Mul(x, u, x);
+                    //X448Field.Normalize(x);
+                    X448Field.Copy(x, 0, precompBase, off);
 
-                if (off == 0)
-                    break;
+                    if (off == 0)
+                        break;
 
-                off -= X448Field.Size;
-                X448Field.Copy(zs, off, z, 0);
-                X448Field.Mul(u, z, u);
+                    off -= X448Field.Size;
+                    X448Field.Copy(zs, off, z, 0);
+                    X448Field.Mul(u, z, u);
+                }
             }
         }
 
diff --git a/crypto/test/crypto.test.csproj b/crypto/test/crypto.test.csproj
index 8446f71a9..6c54181b0 100644
--- a/crypto/test/crypto.test.csproj
+++ b/crypto/test/crypto.test.csproj
@@ -14,14 +14,11 @@
     <EmbeddedResource Include="data\**\*.*" Exclude="**\README.txt" />
   </ItemGroup>
   <ItemGroup>
-    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
-    <PackageReference Include="NUnit" Version="3.7.1" />
-    <PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
+    <PackageReference Include="NUnit" Version="3.10.1" />
+    <PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\src\crypto.csproj" />
   </ItemGroup>
-  <ItemGroup>
-    <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
-  </ItemGroup>
 </Project>
\ No newline at end of file