Skip to content

Commit 8f60353

Browse files
committed
Delete unused code.
Signed-off-by: Bradley Grainger <[email protected]>
1 parent 869e5d4 commit 8f60353

21 files changed

+43
-2137
lines changed

src/MySqlConnector.Authentication.Ed25519/Chaos.NaCl/CryptoBytes.cs

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,6 @@ namespace Chaos.NaCl
55
{
66
internal static class CryptoBytes
77
{
8-
public static bool ConstantTimeEquals(byte[] x, byte[] y)
9-
{
10-
if (x == null)
11-
throw new ArgumentNullException("x");
12-
if (y == null)
13-
throw new ArgumentNullException("y");
14-
if (x.Length != y.Length)
15-
throw new ArgumentException("x.Length must equal y.Length");
16-
return InternalConstantTimeEquals(x, 0, y, 0, x.Length) != 0;
17-
}
18-
19-
public static bool ConstantTimeEquals(ArraySegment<byte> x, ArraySegment<byte> y)
20-
{
21-
if (x.Array == null)
22-
throw new ArgumentNullException("x.Array");
23-
if (y.Array == null)
24-
throw new ArgumentNullException("y.Array");
25-
if (x.Count != y.Count)
26-
throw new ArgumentException("x.Count must equal y.Count");
27-
28-
return InternalConstantTimeEquals(x.Array, x.Offset, y.Array, y.Offset, x.Count) != 0;
29-
}
30-
318
public static bool ConstantTimeEquals(byte[] x, int xOffset, byte[] y, int yOffset, int length)
329
{
3310
if (x == null)
@@ -63,26 +40,6 @@ public static void Wipe(byte[] data)
6340
InternalWipe(data, 0, data.Length);
6441
}
6542

66-
public static void Wipe(byte[] data, int offset, int count)
67-
{
68-
if (data == null)
69-
throw new ArgumentNullException("data");
70-
if (offset < 0)
71-
throw new ArgumentOutOfRangeException("offset");
72-
if (count < 0)
73-
throw new ArgumentOutOfRangeException("count", "Requires count >= 0");
74-
if ((uint)offset + (uint)count > (uint)data.Length)
75-
throw new ArgumentException("Requires offset + count <= data.Length");
76-
InternalWipe(data, offset, count);
77-
}
78-
79-
public static void Wipe(ArraySegment<byte> data)
80-
{
81-
if (data.Array == null)
82-
throw new ArgumentNullException("data.Array");
83-
InternalWipe(data.Array, data.Offset, data.Count);
84-
}
85-
8643
// Secure wiping is hard
8744
// * the GC can move around and copy memory
8845
// Perhaps this can be avoided by using unmanaged memory or by fixing the position of the array in memory
@@ -104,87 +61,5 @@ internal static void InternalWipe<T>(ref T data)
10461
{
10562
data = default(T);
10663
}
107-
108-
// constant time hex conversion
109-
// see http://stackoverflow.com/a/14333437/445517
110-
//
111-
// An explanation of the weird bit fiddling:
112-
//
113-
// 1. `bytes[i] >> 4` extracts the high nibble of a byte
114-
// `bytes[i] & 0xF` extracts the low nibble of a byte
115-
// 2. `b - 10`
116-
// is `< 0` for values `b < 10`, which will become a decimal digit
117-
// is `>= 0` for values `b > 10`, which will become a letter from `A` to `F`.
118-
// 3. Using `i >> 31` on a signed 32 bit integer extracts the sign, thanks to sign extension.
119-
// It will be `-1` for `i < 0` and `0` for `i >= 0`.
120-
// 4. Combining 2) and 3), shows that `(b-10)>>31` will be `0` for letters and `-1` for digits.
121-
// 5. Looking at the case for letters, the last summand becomes `0`, and `b` is in the range 10 to 15. We want to map it to `A`(65) to `F`(70), which implies adding 55 (`'A'-10`).
122-
// 6. Looking at the case for digits, we want to adapt the last summand so it maps `b` from the range 0 to 9 to the range `0`(48) to `9`(57). This means it needs to become -7 (`'0' - 55`).
123-
// Now we could just multiply with 7. But since -1 is represented by all bits being 1, we can instead use `& -7` since `(0 & -7) == 0` and `(-1 & -7) == -7`.
124-
//
125-
// Some further considerations:
126-
//
127-
// * I didn't use a second loop variable to index into `c`, since measurement shows that calculating it from `i` is cheaper.
128-
// * Using exactly `i < bytes.Length` as upper bound of the loop allows the JITter to eliminate bounds checks on `bytes[i]`, so I chose that variant.
129-
// * Making `b` an int avoids unnecessary conversions from and to byte.
130-
public static string ToHexStringUpper(byte[] data)
131-
{
132-
if (data == null)
133-
return null;
134-
char[] c = new char[data.Length * 2];
135-
int b;
136-
for (int i = 0; i < data.Length; i++)
137-
{
138-
b = data[i] >> 4;
139-
c[i * 2] = (char)(55 + b + (((b - 10) >> 31) & -7));
140-
b = data[i] & 0xF;
141-
c[i * 2 + 1] = (char)(55 + b + (((b - 10) >> 31) & -7));
142-
}
143-
return new string(c);
144-
}
145-
146-
// Explanation is similar to ToHexStringUpper
147-
// constant 55 -> 87 and -7 -> -39 to compensate for the offset 32 between lowercase and uppercase letters
148-
public static string ToHexStringLower(byte[] data)
149-
{
150-
if (data == null)
151-
return null;
152-
char[] c = new char[data.Length * 2];
153-
int b;
154-
for (int i = 0; i < data.Length; i++)
155-
{
156-
b = data[i] >> 4;
157-
c[i * 2] = (char)(87 + b + (((b - 10) >> 31) & -39));
158-
b = data[i] & 0xF;
159-
c[i * 2 + 1] = (char)(87 + b + (((b - 10) >> 31) & -39));
160-
}
161-
return new string(c);
162-
}
163-
164-
public static byte[] FromHexString(string hexString)
165-
{
166-
if (hexString == null)
167-
return null;
168-
if (hexString.Length % 2 != 0)
169-
throw new FormatException("The hex string is invalid because it has an odd length");
170-
var result = new byte[hexString.Length / 2];
171-
for (int i = 0; i < result.Length; i++)
172-
result[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
173-
return result;
174-
}
175-
176-
public static string ToBase64String(byte[] data)
177-
{
178-
if (data == null)
179-
return null;
180-
return Convert.ToBase64String(data);
181-
}
182-
183-
public static byte[] FromBase64String(string s)
184-
{
185-
if (s == null)
186-
return null;
187-
return Convert.FromBase64String(s);
188-
}
18964
}
19065
}
Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using System.Security.Cryptography;
23
using Chaos.NaCl.Internal.Ed25519Ref10;
34

45
namespace Chaos.NaCl
@@ -11,30 +12,6 @@ internal static class Ed25519
1112
public static readonly int PrivateKeySeedSizeInBytes = 32;
1213
public static readonly int SharedKeySizeInBytes = 32;
1314

14-
public static bool Verify(ArraySegment<byte> signature, ArraySegment<byte> message, ArraySegment<byte> publicKey)
15-
{
16-
if (signature.Count != SignatureSizeInBytes)
17-
throw new ArgumentException(string.Format("Signature size must be {0}", SignatureSizeInBytes), "signature.Count");
18-
if (publicKey.Count != PublicKeySizeInBytes)
19-
throw new ArgumentException(string.Format("Public key size must be {0}", PublicKeySizeInBytes), "publicKey.Count");
20-
return Ed25519Operations.crypto_sign_verify(signature.Array, signature.Offset, message.Array, message.Offset, message.Count, publicKey.Array, publicKey.Offset);
21-
}
22-
23-
public static bool Verify(byte[] signature, byte[] message, byte[] publicKey)
24-
{
25-
if (signature == null)
26-
throw new ArgumentNullException("signature");
27-
if (message == null)
28-
throw new ArgumentNullException("message");
29-
if (publicKey == null)
30-
throw new ArgumentNullException("publicKey");
31-
if (signature.Length != SignatureSizeInBytes)
32-
throw new ArgumentException(string.Format("Signature size must be {0}", SignatureSizeInBytes), "signature.Length");
33-
if (publicKey.Length != PublicKeySizeInBytes)
34-
throw new ArgumentException(string.Format("Public key size must be {0}", PublicKeySizeInBytes), "publicKey.Length");
35-
return Ed25519Operations.crypto_sign_verify(signature, 0, message, 0, message.Length, publicKey, 0);
36-
}
37-
3815
public static void Sign(ArraySegment<byte> signature, ArraySegment<byte> message, ArraySegment<byte> expandedPrivateKey)
3916
{
4017
if (signature.Array == null)
@@ -57,21 +34,16 @@ public static byte[] Sign(byte[] message, byte[] expandedPrivateKey)
5734
return signature;
5835
}
5936

60-
public static byte[] PublicKeyFromSeed(byte[] privateKeySeed)
61-
{
62-
byte[] privateKey;
63-
byte[] publicKey;
64-
KeyPairFromSeed(out publicKey, out privateKey, privateKeySeed);
65-
CryptoBytes.Wipe(privateKey);
66-
return publicKey;
67-
}
68-
6937
public static byte[] ExpandedPrivateKeyFromSeed(byte[] privateKeySeed)
7038
{
7139
byte[] privateKey;
7240
byte[] publicKey;
7341
KeyPairFromSeed(out publicKey, out privateKey, privateKeySeed);
42+
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
43+
CryptographicOperations.ZeroMemory(publicKey);
44+
#else
7445
CryptoBytes.Wipe(publicKey);
46+
#endif
7547
return privateKey;
7648
}
7749

@@ -87,25 +59,5 @@ public static void KeyPairFromSeed(out byte[] publicKey, out byte[] expandedPriv
8759
publicKey = pk;
8860
expandedPrivateKey = sk;
8961
}
90-
91-
public static void KeyPairFromSeed(ArraySegment<byte> publicKey, ArraySegment<byte> expandedPrivateKey, ArraySegment<byte> privateKeySeed)
92-
{
93-
if (publicKey.Array == null)
94-
throw new ArgumentNullException("publicKey.Array");
95-
if (expandedPrivateKey.Array == null)
96-
throw new ArgumentNullException("expandedPrivateKey.Array");
97-
if (privateKeySeed.Array == null)
98-
throw new ArgumentNullException("privateKeySeed.Array");
99-
if (publicKey.Count != PublicKeySizeInBytes)
100-
throw new ArgumentException("publicKey.Count");
101-
if (expandedPrivateKey.Count != ExpandedPrivateKeySizeInBytes)
102-
throw new ArgumentException("expandedPrivateKey.Count");
103-
if (privateKeySeed.Count != PrivateKeySeedSizeInBytes)
104-
throw new ArgumentException("privateKeySeed.Count");
105-
Ed25519Operations.crypto_sign_keypair(
106-
publicKey.Array, publicKey.Offset,
107-
expandedPrivateKey.Array, expandedPrivateKey.Offset,
108-
privateKeySeed.Array, privateKeySeed.Offset);
109-
}
11062
}
11163
}

0 commit comments

Comments
 (0)