This commit is contained in:
2020-07-09 08:50:24 +08:00
parent 13d25f4707
commit c523462b82
1818 changed files with 174940 additions and 582 deletions

View File

@@ -0,0 +1,323 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
using Org.BouncyCastle.Crypto.Utilities;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Crypto.Modes.Gcm
{
internal abstract class GcmUtilities
{
private const uint E1 = 0xe1000000;
private const ulong E1L = (ulong)E1 << 32;
private static uint[] GenerateLookup()
{
uint[] lookup = new uint[256];
for (int c = 0; c < 256; ++c)
{
uint v = 0;
for (int i = 7; i >= 0; --i)
{
if ((c & (1 << i)) != 0)
{
v ^= (E1 >> (7 - i));
}
}
lookup[c] = v;
}
return lookup;
}
private static readonly uint[] LOOKUP = GenerateLookup();
internal static byte[] OneAsBytes()
{
byte[] tmp = new byte[16];
tmp[0] = 0x80;
return tmp;
}
internal static uint[] OneAsUints()
{
uint[] tmp = new uint[4];
tmp[0] = 0x80000000;
return tmp;
}
internal static ulong[] OneAsUlongs()
{
ulong[] tmp = new ulong[2];
tmp[0] = 1UL << 63;
return tmp;
}
internal static byte[] AsBytes(uint[] x)
{
return Pack.UInt32_To_BE(x);
}
internal static void AsBytes(uint[] x, byte[] z)
{
Pack.UInt32_To_BE(x, z, 0);
}
internal static byte[] AsBytes(ulong[] x)
{
byte[] z = new byte[16];
Pack.UInt64_To_BE(x, z, 0);
return z;
}
internal static void AsBytes(ulong[] x, byte[] z)
{
Pack.UInt64_To_BE(x, z, 0);
}
internal static uint[] AsUints(byte[] bs)
{
uint[] output = new uint[4];
Pack.BE_To_UInt32(bs, 0, output);
return output;
}
internal static void AsUints(byte[] bs, uint[] output)
{
Pack.BE_To_UInt32(bs, 0, output);
}
internal static ulong[] AsUlongs(byte[] x)
{
ulong[] z = new ulong[2];
Pack.BE_To_UInt64(x, 0, z);
return z;
}
public static void AsUlongs(byte[] x, ulong[] z)
{
Pack.BE_To_UInt64(x, 0, z);
}
internal static void Multiply(byte[] x, byte[] y)
{
uint[] t1 = GcmUtilities.AsUints(x);
uint[] t2 = GcmUtilities.AsUints(y);
GcmUtilities.Multiply(t1, t2);
GcmUtilities.AsBytes(t1, x);
}
internal static void Multiply(uint[] x, uint[] y)
{
uint r00 = x[0], r01 = x[1], r02 = x[2], r03 = x[3];
uint r10 = 0, r11 = 0, r12 = 0, r13 = 0;
for (int i = 0; i < 4; ++i)
{
int bits = (int)y[i];
for (int j = 0; j < 32; ++j)
{
uint m1 = (uint)(bits >> 31); bits <<= 1;
r10 ^= (r00 & m1);
r11 ^= (r01 & m1);
r12 ^= (r02 & m1);
r13 ^= (r03 & m1);
uint m2 = (uint)((int)(r03 << 31) >> 8);
r03 = (r03 >> 1) | (r02 << 31);
r02 = (r02 >> 1) | (r01 << 31);
r01 = (r01 >> 1) | (r00 << 31);
r00 = (r00 >> 1) ^ (m2 & E1);
}
}
x[0] = r10;
x[1] = r11;
x[2] = r12;
x[3] = r13;
}
internal static void Multiply(ulong[] x, ulong[] y)
{
ulong r00 = x[0], r01 = x[1], r10 = 0, r11 = 0;
for (int i = 0; i < 2; ++i)
{
long bits = (long)y[i];
for (int j = 0; j < 64; ++j)
{
ulong m1 = (ulong)(bits >> 63); bits <<= 1;
r10 ^= (r00 & m1);
r11 ^= (r01 & m1);
ulong m2 = (ulong)((long)(r01 << 63) >> 8);
r01 = (r01 >> 1) | (r00 << 63);
r00 = (r00 >> 1) ^ (m2 & E1L);
}
}
x[0] = r10;
x[1] = r11;
}
// P is the value with only bit i=1 set
internal static void MultiplyP(uint[] x)
{
uint m = (uint)((int)ShiftRight(x) >> 8);
x[0] ^= (m & E1);
}
internal static void MultiplyP(uint[] x, uint[] z)
{
uint m = (uint)((int)ShiftRight(x, z) >> 8);
z[0] ^= (m & E1);
}
internal static void MultiplyP8(uint[] x)
{
// for (int i = 8; i != 0; --i)
// {
// MultiplyP(x);
// }
uint c = ShiftRightN(x, 8);
x[0] ^= LOOKUP[c >> 24];
}
internal static void MultiplyP8(uint[] x, uint[] y)
{
uint c = ShiftRightN(x, 8, y);
y[0] ^= LOOKUP[c >> 24];
}
internal static uint ShiftRight(uint[] x)
{
uint b = x[0];
x[0] = b >> 1;
uint c = b << 31;
b = x[1];
x[1] = (b >> 1) | c;
c = b << 31;
b = x[2];
x[2] = (b >> 1) | c;
c = b << 31;
b = x[3];
x[3] = (b >> 1) | c;
return b << 31;
}
internal static uint ShiftRight(uint[] x, uint[] z)
{
uint b = x[0];
z[0] = b >> 1;
uint c = b << 31;
b = x[1];
z[1] = (b >> 1) | c;
c = b << 31;
b = x[2];
z[2] = (b >> 1) | c;
c = b << 31;
b = x[3];
z[3] = (b >> 1) | c;
return b << 31;
}
internal static uint ShiftRightN(uint[] x, int n)
{
uint b = x[0]; int nInv = 32 - n;
x[0] = b >> n;
uint c = b << nInv;
b = x[1];
x[1] = (b >> n) | c;
c = b << nInv;
b = x[2];
x[2] = (b >> n) | c;
c = b << nInv;
b = x[3];
x[3] = (b >> n) | c;
return b << nInv;
}
internal static uint ShiftRightN(uint[] x, int n, uint[] z)
{
uint b = x[0]; int nInv = 32 - n;
z[0] = b >> n;
uint c = b << nInv;
b = x[1];
z[1] = (b >> n) | c;
c = b << nInv;
b = x[2];
z[2] = (b >> n) | c;
c = b << nInv;
b = x[3];
z[3] = (b >> n) | c;
return b << nInv;
}
internal static void Xor(byte[] x, byte[] y)
{
int i = 0;
do
{
x[i] ^= y[i]; ++i;
x[i] ^= y[i]; ++i;
x[i] ^= y[i]; ++i;
x[i] ^= y[i]; ++i;
}
while (i < 16);
}
internal static void Xor(byte[] x, byte[] y, int yOff, int yLen)
{
while (--yLen >= 0)
{
x[yLen] ^= y[yOff + yLen];
}
}
internal static void Xor(byte[] x, byte[] y, byte[] z)
{
int i = 0;
do
{
z[i] = (byte)(x[i] ^ y[i]); ++i;
z[i] = (byte)(x[i] ^ y[i]); ++i;
z[i] = (byte)(x[i] ^ y[i]); ++i;
z[i] = (byte)(x[i] ^ y[i]); ++i;
}
while (i < 16);
}
internal static void Xor(uint[] x, uint[] y)
{
x[0] ^= y[0];
x[1] ^= y[1];
x[2] ^= y[2];
x[3] ^= y[3];
}
internal static void Xor(uint[] x, uint[] y, uint[] z)
{
z[0] = x[0] ^ y[0];
z[1] = x[1] ^ y[1];
z[2] = x[2] ^ y[2];
z[3] = x[3] ^ y[3];
}
internal static void Xor(ulong[] x, ulong[] y)
{
x[0] ^= y[0];
x[1] ^= y[1];
}
internal static void Xor(ulong[] x, ulong[] y, ulong[] z)
{
z[0] = x[0] ^ y[0];
z[1] = x[1] ^ y[1];
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9161f2ba060f64038a7d15dc37938e02
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
namespace Org.BouncyCastle.Crypto.Modes.Gcm
{
public interface IGcmExponentiator
{
void Init(byte[] x);
void ExponentiateX(long pow, byte[] output);
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d2de0b4d04da7432b876404ab38fd59b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
namespace Org.BouncyCastle.Crypto.Modes.Gcm
{
public interface IGcmMultiplier
{
void Init(byte[] H);
void MultiplyH(byte[] x);
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3e6b652ec310a4ff39aa6e37f36e9454
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,63 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
using System.Collections;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Crypto.Modes.Gcm
{
public class Tables1kGcmExponentiator
: IGcmExponentiator
{
// A lookup table of the power-of-two powers of 'x'
// - lookupPowX2[i] = x^(2^i)
private IList lookupPowX2;
public void Init(byte[] x)
{
uint[] y = GcmUtilities.AsUints(x);
if (lookupPowX2 != null && Arrays.AreEqual(y, (uint[])lookupPowX2[0]))
return;
lookupPowX2 = Platform.CreateArrayList(8);
lookupPowX2.Add(y);
}
public void ExponentiateX(long pow, byte[] output)
{
uint[] y = GcmUtilities.OneAsUints();
int bit = 0;
while (pow > 0)
{
if ((pow & 1L) != 0)
{
EnsureAvailable(bit);
GcmUtilities.Multiply(y, (uint[])lookupPowX2[bit]);
}
++bit;
pow >>= 1;
}
GcmUtilities.AsBytes(y, output);
}
private void EnsureAvailable(int bit)
{
int count = lookupPowX2.Count;
if (count <= bit)
{
uint[] tmp = (uint[])lookupPowX2[count - 1];
do
{
tmp = Arrays.Clone(tmp);
GcmUtilities.Multiply(tmp, tmp);
lookupPowX2.Add(tmp);
}
while (++count <= bit);
}
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9baec52f99b21430ea2a70d383656640
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,107 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
using Org.BouncyCastle.Crypto.Utilities;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Crypto.Modes.Gcm
{
public class Tables8kGcmMultiplier
: IGcmMultiplier
{
private byte[] H;
private uint[][][] M;
public void Init(byte[] H)
{
if (M == null)
{
M = new uint[32][][];
}
else if (Arrays.AreEqual(this.H, H))
{
return;
}
this.H = Arrays.Clone(H);
M[0] = new uint[16][];
M[1] = new uint[16][];
M[0][0] = new uint[4];
M[1][0] = new uint[4];
M[1][8] = GcmUtilities.AsUints(H);
for (int j = 4; j >= 1; j >>= 1)
{
uint[] tmp = (uint[])M[1][j + j].Clone();
GcmUtilities.MultiplyP(tmp);
M[1][j] = tmp;
}
{
uint[] tmp = (uint[])M[1][1].Clone();
GcmUtilities.MultiplyP(tmp);
M[0][8] = tmp;
}
for (int j = 4; j >= 1; j >>= 1)
{
uint[] tmp = (uint[])M[0][j + j].Clone();
GcmUtilities.MultiplyP(tmp);
M[0][j] = tmp;
}
for (int i = 0; ; )
{
for (int j = 2; j < 16; j += j)
{
for (int k = 1; k < j; ++k)
{
uint[] tmp = (uint[])M[i][j].Clone();
GcmUtilities.Xor(tmp, M[i][k]);
M[i][j + k] = tmp;
}
}
if (++i == 32) return;
if (i > 1)
{
M[i] = new uint[16][];
M[i][0] = new uint[4];
for (int j = 8; j > 0; j >>= 1)
{
uint[] tmp = (uint[])M[i - 2][j].Clone();
GcmUtilities.MultiplyP8(tmp);
M[i][j] = tmp;
}
}
}
}
public void MultiplyH(byte[] x)
{
uint[] z = new uint[4];
for (int i = 15; i >= 0; --i)
{
//GcmUtilities.Xor(z, M[i + i][x[i] & 0x0f]);
uint[] m = M[i + i][x[i] & 0x0f];
z[0] ^= m[0];
z[1] ^= m[1];
z[2] ^= m[2];
z[3] ^= m[3];
//GcmUtilities.Xor(z, M[i + i + 1][(x[i] & 0xf0) >> 4]);
m = M[i + i + 1][(x[i] & 0xf0) >> 4];
z[0] ^= m[0];
z[1] ^= m[1];
z[2] ^= m[2];
z[3] ^= m[3];
}
Pack.UInt32_To_BE(z, x, 0);
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f51ca98d86f5a499b849d03b824036ac
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: