Skip to content

Commit e8d418e

Browse files
committed
Replace MethodImpl with lock
1 parent 9f71e07 commit e8d418e

File tree

9 files changed

+301
-280
lines changed

9 files changed

+301
-280
lines changed

crypto/src/crypto/signers/Ed25519Signer.cs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.CompilerServices;
43

54
using Org.BouncyCastle.Crypto.Parameters;
65
using Org.BouncyCastle.Math.EC.Rfc8032;
@@ -80,49 +79,55 @@ public virtual void Reset()
8079

8180
private class Buffer : MemoryStream
8281
{
83-
[MethodImpl(MethodImplOptions.Synchronized)]
8482
internal byte[] GenerateSignature(Ed25519PrivateKeyParameters privateKey, Ed25519PublicKeyParameters publicKey)
8583
{
84+
lock (this)
85+
{
8686
#if PORTABLE
87-
byte[] buf = ToArray();
88-
int count = buf.Length;
87+
byte[] buf = ToArray();
88+
int count = buf.Length;
8989
#else
90-
byte[] buf = GetBuffer();
91-
int count = (int)Position;
90+
byte[] buf = GetBuffer();
91+
int count = (int)Position;
9292
#endif
93-
byte[] signature = new byte[Ed25519PrivateKeyParameters.SignatureSize];
94-
privateKey.Sign(Ed25519.Algorithm.Ed25519, publicKey, null, buf, 0, count, signature, 0);
95-
Reset();
96-
return signature;
93+
byte[] signature = new byte[Ed25519PrivateKeyParameters.SignatureSize];
94+
privateKey.Sign(Ed25519.Algorithm.Ed25519, publicKey, null, buf, 0, count, signature, 0);
95+
Reset();
96+
return signature;
97+
}
9798
}
9899

99-
[MethodImpl(MethodImplOptions.Synchronized)]
100100
internal bool VerifySignature(Ed25519PublicKeyParameters publicKey, byte[] signature)
101101
{
102+
lock (this)
103+
{
102104
#if PORTABLE
103-
byte[] buf = ToArray();
104-
int count = buf.Length;
105+
byte[] buf = ToArray();
106+
int count = buf.Length;
105107
#else
106-
byte[] buf = GetBuffer();
107-
int count = (int)Position;
108+
byte[] buf = GetBuffer();
109+
int count = (int)Position;
108110
#endif
109-
byte[] pk = publicKey.GetEncoded();
110-
bool result = Ed25519.Verify(signature, 0, pk, 0, buf, 0, count);
111-
Reset();
112-
return result;
111+
byte[] pk = publicKey.GetEncoded();
112+
bool result = Ed25519.Verify(signature, 0, pk, 0, buf, 0, count);
113+
Reset();
114+
return result;
115+
}
113116
}
114117

115-
[MethodImpl(MethodImplOptions.Synchronized)]
116118
internal void Reset()
117119
{
118-
long count = Position;
120+
lock (this)
121+
{
122+
long count = Position;
119123
#if PORTABLE
120-
this.Position = 0L;
121-
Streams.WriteZeroes(this, count);
124+
this.Position = 0L;
125+
Streams.WriteZeroes(this, count);
122126
#else
123-
Array.Clear(GetBuffer(), 0, (int)count);
127+
Array.Clear(GetBuffer(), 0, (int)count);
124128
#endif
125-
this.Position = 0L;
129+
this.Position = 0L;
130+
}
126131
}
127132
}
128133
}

crypto/src/crypto/signers/Ed25519ctxSigner.cs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.CompilerServices;
43

54
using Org.BouncyCastle.Crypto.Parameters;
65
using Org.BouncyCastle.Math.EC.Rfc8032;
@@ -82,49 +81,55 @@ public virtual void Reset()
8281

8382
private class Buffer : MemoryStream
8483
{
85-
[MethodImpl(MethodImplOptions.Synchronized)]
8684
internal byte[] GenerateSignature(Ed25519PrivateKeyParameters privateKey, Ed25519PublicKeyParameters publicKey, byte[] ctx)
8785
{
86+
lock (this)
87+
{
8888
#if PORTABLE
89-
byte[] buf = ToArray();
90-
int count = buf.Length;
89+
byte[] buf = ToArray();
90+
int count = buf.Length;
9191
#else
92-
byte[] buf = GetBuffer();
93-
int count = (int)Position;
92+
byte[] buf = GetBuffer();
93+
int count = (int)Position;
9494
#endif
95-
byte[] signature = new byte[Ed25519PrivateKeyParameters.SignatureSize];
96-
privateKey.Sign(Ed25519.Algorithm.Ed25519ctx, publicKey, ctx, buf, 0, count, signature, 0);
97-
Reset();
98-
return signature;
95+
byte[] signature = new byte[Ed25519PrivateKeyParameters.SignatureSize];
96+
privateKey.Sign(Ed25519.Algorithm.Ed25519ctx, publicKey, ctx, buf, 0, count, signature, 0);
97+
Reset();
98+
return signature;
99+
}
99100
}
100101

101-
[MethodImpl(MethodImplOptions.Synchronized)]
102102
internal bool VerifySignature(Ed25519PublicKeyParameters publicKey, byte[] ctx, byte[] signature)
103103
{
104+
lock (this)
105+
{
104106
#if PORTABLE
105-
byte[] buf = ToArray();
106-
int count = buf.Length;
107+
byte[] buf = ToArray();
108+
int count = buf.Length;
107109
#else
108-
byte[] buf = GetBuffer();
109-
int count = (int)Position;
110+
byte[] buf = GetBuffer();
111+
int count = (int)Position;
110112
#endif
111-
byte[] pk = publicKey.GetEncoded();
112-
bool result = Ed25519.Verify(signature, 0, pk, 0, ctx, buf, 0, count);
113-
Reset();
114-
return result;
113+
byte[] pk = publicKey.GetEncoded();
114+
bool result = Ed25519.Verify(signature, 0, pk, 0, ctx, buf, 0, count);
115+
Reset();
116+
return result;
117+
}
115118
}
116119

117-
[MethodImpl(MethodImplOptions.Synchronized)]
118120
internal void Reset()
119121
{
120-
long count = Position;
122+
lock (this)
123+
{
124+
long count = Position;
121125
#if PORTABLE
122-
this.Position = 0L;
123-
Streams.WriteZeroes(this, count);
126+
this.Position = 0L;
127+
Streams.WriteZeroes(this, count);
124128
#else
125-
Array.Clear(GetBuffer(), 0, (int)count);
129+
Array.Clear(GetBuffer(), 0, (int)count);
126130
#endif
127-
this.Position = 0L;
131+
this.Position = 0L;
132+
}
128133
}
129134
}
130135
}

crypto/src/crypto/signers/Ed25519phSigner.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.CompilerServices;
43

54
using Org.BouncyCastle.Crypto.Parameters;
65
using Org.BouncyCastle.Math.EC.Rfc8032;

crypto/src/crypto/signers/Ed448Signer.cs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.CompilerServices;
43

54
using Org.BouncyCastle.Crypto.Parameters;
65
using Org.BouncyCastle.Math.EC.Rfc8032;
@@ -82,49 +81,55 @@ public virtual void Reset()
8281

8382
private class Buffer : MemoryStream
8483
{
85-
[MethodImpl(MethodImplOptions.Synchronized)]
8684
internal byte[] GenerateSignature(Ed448PrivateKeyParameters privateKey, Ed448PublicKeyParameters publicKey, byte[] ctx)
8785
{
86+
lock (this)
87+
{
8888
#if PORTABLE
8989
byte[] buf = ToArray();
9090
int count = buf.Length;
9191
#else
92-
byte[] buf = GetBuffer();
93-
int count = (int)Position;
92+
byte[] buf = GetBuffer();
93+
int count = (int)Position;
9494
#endif
95-
byte[] signature = new byte[Ed448PrivateKeyParameters.SignatureSize];
96-
privateKey.Sign(Ed448.Algorithm.Ed448, publicKey, ctx, buf, 0, count, signature, 0);
97-
Reset();
98-
return signature;
95+
byte[] signature = new byte[Ed448PrivateKeyParameters.SignatureSize];
96+
privateKey.Sign(Ed448.Algorithm.Ed448, publicKey, ctx, buf, 0, count, signature, 0);
97+
Reset();
98+
return signature;
99+
}
99100
}
100101

101-
[MethodImpl(MethodImplOptions.Synchronized)]
102102
internal bool VerifySignature(Ed448PublicKeyParameters publicKey, byte[] ctx, byte[] signature)
103103
{
104+
lock (this)
105+
{
104106
#if PORTABLE
105-
byte[] buf = ToArray();
106-
int count = buf.Length;
107+
byte[] buf = ToArray();
108+
int count = buf.Length;
107109
#else
108-
byte[] buf = GetBuffer();
109-
int count = (int)Position;
110+
byte[] buf = GetBuffer();
111+
int count = (int)Position;
110112
#endif
111-
byte[] pk = publicKey.GetEncoded();
112-
bool result = Ed448.Verify(signature, 0, pk, 0, ctx, buf, 0, count);
113-
Reset();
114-
return result;
113+
byte[] pk = publicKey.GetEncoded();
114+
bool result = Ed448.Verify(signature, 0, pk, 0, ctx, buf, 0, count);
115+
Reset();
116+
return result;
117+
}
115118
}
116119

117-
[MethodImpl(MethodImplOptions.Synchronized)]
118120
internal void Reset()
119121
{
120-
long count = Position;
122+
lock (this)
123+
{
124+
long count = Position;
121125
#if PORTABLE
122-
this.Position = 0L;
123-
Streams.WriteZeroes(this, count);
126+
this.Position = 0L;
127+
Streams.WriteZeroes(this, count);
124128
#else
125-
Array.Clear(GetBuffer(), 0, (int)count);
129+
Array.Clear(GetBuffer(), 0, (int)count);
126130
#endif
127-
this.Position = 0L;
131+
this.Position = 0L;
132+
}
128133
}
129134
}
130135
}

crypto/src/crypto/signers/Ed448phSigner.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.CompilerServices;
43

54
using Org.BouncyCastle.Crypto.Parameters;
65
using Org.BouncyCastle.Math.EC.Rfc8032;

crypto/src/math/ec/rfc7748/X25519.cs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Diagnostics;
3-
using System.Runtime.CompilerServices;
43

54
using Org.BouncyCastle.Utilities;
65

@@ -21,6 +20,7 @@ public abstract class X25519
2120
private static readonly int[] PsubS_x = { 0x03D48290, 0x02C7804D, 0x01207816, 0x028F5A68, 0x00881ED4, 0x00A2B71D,
2221
0x0217D1B7, 0x014CB523, 0x0088EC1A, 0x0042A264 };
2322

23+
private static readonly object precompLock = new object();
2424
private static int[] precompBase = null;
2525

2626
public static bool CalculateAgreement(byte[] k, int kOff, byte[] u, int uOff, byte[] r, int rOff)
@@ -65,63 +65,65 @@ private static void PointDouble(int[] x, int[] z)
6565
X25519Field.Mul(z, A, z);
6666
}
6767

68-
[MethodImpl(MethodImplOptions.Synchronized)]
6968
public static void Precompute()
7069
{
71-
if (precompBase != null)
72-
return;
70+
lock (precompLock)
71+
{
72+
if (precompBase != null)
73+
return;
7374

74-
precompBase = new int[X25519Field.Size * 252];
75+
precompBase = new int[X25519Field.Size * 252];
7576

76-
int[] xs = precompBase;
77-
int[] zs = new int[X25519Field.Size * 251];
77+
int[] xs = precompBase;
78+
int[] zs = new int[X25519Field.Size * 251];
7879

79-
int[] x = X25519Field.Create(); x[0] = 9;
80-
int[] z = X25519Field.Create(); z[0] = 1;
80+
int[] x = X25519Field.Create(); x[0] = 9;
81+
int[] z = X25519Field.Create(); z[0] = 1;
8182

82-
int[] n = X25519Field.Create();
83-
int[] d = X25519Field.Create();
83+
int[] n = X25519Field.Create();
84+
int[] d = X25519Field.Create();
8485

85-
X25519Field.Apm(x, z, n, d);
86+
X25519Field.Apm(x, z, n, d);
8687

87-
int[] c = X25519Field.Create(); X25519Field.Copy(d, 0, c, 0);
88+
int[] c = X25519Field.Create(); X25519Field.Copy(d, 0, c, 0);
8889

89-
int off = 0;
90-
for (;;)
91-
{
92-
X25519Field.Copy(n, 0, xs, off);
90+
int off = 0;
91+
for (; ; )
92+
{
93+
X25519Field.Copy(n, 0, xs, off);
9394

94-
if (off == (X25519Field.Size * 251))
95-
break;
95+
if (off == (X25519Field.Size * 251))
96+
break;
9697

97-
PointDouble(x, z);
98+
PointDouble(x, z);
9899

99-
X25519Field.Apm(x, z, n, d);
100-
X25519Field.Mul(n, c, n);
101-
X25519Field.Mul(c, d, c);
100+
X25519Field.Apm(x, z, n, d);
101+
X25519Field.Mul(n, c, n);
102+
X25519Field.Mul(c, d, c);
102103

103-
X25519Field.Copy(d, 0, zs, off);
104+
X25519Field.Copy(d, 0, zs, off);
104105

105-
off += X25519Field.Size;
106-
}
106+
off += X25519Field.Size;
107+
}
107108

108-
int[] u = X25519Field.Create();
109-
X25519Field.Inv(c, u);
109+
int[] u = X25519Field.Create();
110+
X25519Field.Inv(c, u);
110111

111-
for (;;)
112-
{
113-
X25519Field.Copy(xs, off, x, 0);
112+
for (; ; )
113+
{
114+
X25519Field.Copy(xs, off, x, 0);
114115

115-
X25519Field.Mul(x, u, x);
116-
//X25519Field.Normalize(x);
117-
X25519Field.Copy(x, 0, precompBase, off);
116+
X25519Field.Mul(x, u, x);
117+
//X25519Field.Normalize(x);
118+
X25519Field.Copy(x, 0, precompBase, off);
118119

119-
if (off == 0)
120-
break;
120+
if (off == 0)
121+
break;
121122

122-
off -= X25519Field.Size;
123-
X25519Field.Copy(zs, off, z, 0);
124-
X25519Field.Mul(u, z, u);
123+
off -= X25519Field.Size;
124+
X25519Field.Copy(zs, off, z, 0);
125+
X25519Field.Mul(u, z, u);
126+
}
125127
}
126128
}
127129

0 commit comments

Comments
 (0)