489 lines
14 KiB
C#
489 lines
14 KiB
C#
using System;
|
|
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
|
|
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
|
|
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
|
|
using Cpp2IlInjected;
|
|
|
|
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng.Drbg
|
|
{
|
|
// Token: 0x02001CB3 RID: 7347
|
|
[Token(Token = "0x2001CB3")]
|
|
public class CtrSP800Drbg : ISP80090Drbg
|
|
{
|
|
// Token: 0x0600B5DE RID: 46558 RVA: 0x002758C4 File Offset: 0x00273AC4
|
|
[Token(Token = "0x600B5DE")]
|
|
[Address(RVA = "0x2354090", Offset = "0x2353090", VA = "0x182354090")]
|
|
public CtrSP800Drbg(IBlockCipher engine, int keySizeInBits, int securityStrength, IEntropySource entropySource, byte[] personalizationString, byte[] nonce)
|
|
{
|
|
if (!this.IsTdea(engine) || keySizeInBits == 168)
|
|
{
|
|
}
|
|
this.mEntropySource = 0;
|
|
this.mEngine = engine;
|
|
this.mKeySizeInBits = keySizeInBits;
|
|
this.mSecurityStrength = securityStrength;
|
|
this.mSeedLength = (int)((uint)2);
|
|
bool flag = this.IsTdea(engine);
|
|
this.mIsTdea = flag;
|
|
byte[] entropy = this.GetEntropy();
|
|
}
|
|
|
|
// Token: 0x0600B5DF RID: 46559 RVA: 0x00275930 File Offset: 0x00273B30
|
|
[Token(Token = "0x600B5DF")]
|
|
[Address(RVA = "0x2352E00", Offset = "0x2351E00", VA = "0x182352E00")]
|
|
private void CTR_DRBG_Instantiate_algorithm(byte[] entropy, byte[] nonce, byte[] personalisationString)
|
|
{
|
|
byte[][] array = new byte[3][];
|
|
if (entropy != 0)
|
|
{
|
|
}
|
|
array[0] = entropy;
|
|
if (nonce != 0)
|
|
{
|
|
}
|
|
array[1] = nonce;
|
|
if (personalisationString != 0)
|
|
{
|
|
}
|
|
array[2] = personalisationString;
|
|
byte[] array2 = Arrays.ConcatenateAll(array);
|
|
int num = this.mSeedLength;
|
|
byte[] array3 = this.Block_Cipher_df(array2, num);
|
|
IBlockCipher blockCipher = this.mEngine;
|
|
int num2 = this.mKeySizeInBits;
|
|
num2 += 7;
|
|
byte[] array4 = new byte[0];
|
|
this.mKey = array4;
|
|
byte[] array5 = new byte[array3];
|
|
byte[] array6 = this.mKey;
|
|
this.mV = array5;
|
|
this.CTR_DRBG_Update(array3, array6, array5);
|
|
this.mReseedCounter = (long)((ulong)1L);
|
|
}
|
|
|
|
// Token: 0x0600B5E0 RID: 46560 RVA: 0x002759DC File Offset: 0x00273BDC
|
|
[Token(Token = "0x600B5E0")]
|
|
[Address(RVA = "0x23530D0", Offset = "0x23520D0", VA = "0x1823530D0")]
|
|
private void CTR_DRBG_Update(byte[] seed, byte[] key, byte[] v)
|
|
{
|
|
byte[] array = new byte[seed.Length];
|
|
IBlockCipher blockCipher = this.mEngine;
|
|
byte[] array2 = new byte[array];
|
|
IBlockCipher blockCipher2 = this.mEngine;
|
|
IBlockCipher blockCipher3 = this.mEngine;
|
|
KeyParameter keyParameter = new KeyParameter(this.ExpandKey(key));
|
|
int num = 0;
|
|
int num6;
|
|
if (num < seed.Length)
|
|
{
|
|
int num2 = v.Length;
|
|
uint num3;
|
|
num2 -= (int)num3;
|
|
byte b;
|
|
num3 += (uint)b;
|
|
IBlockCipher blockCipher4 = this.mEngine;
|
|
if (num < num2)
|
|
{
|
|
num += num;
|
|
num++;
|
|
}
|
|
int num4 = array.Length;
|
|
num4 -= num;
|
|
int num5 = 0;
|
|
num += array2;
|
|
num6 = 0;
|
|
num5 += num5;
|
|
num += 4;
|
|
num += 19;
|
|
}
|
|
int length = array.Length;
|
|
if (num6 < length)
|
|
{
|
|
num6++;
|
|
}
|
|
int num7 = 0;
|
|
int length2 = v.Length;
|
|
int num8 = 0;
|
|
Array.Copy(array, num7, v, num8, length2);
|
|
}
|
|
|
|
// Token: 0x0600B5E1 RID: 46561 RVA: 0x00275AC4 File Offset: 0x00273CC4
|
|
[Token(Token = "0x600B5E1")]
|
|
[Address(RVA = "0x2353020", Offset = "0x2352020", VA = "0x182353020")]
|
|
private void CTR_DRBG_Reseed_algorithm(byte[] additionalInput)
|
|
{
|
|
byte[] array = Arrays.Concatenate(this.GetEntropy(), additionalInput);
|
|
int num = this.mSeedLength;
|
|
byte[] array2 = this.Block_Cipher_df(array, num);
|
|
byte[] array3 = this.mV;
|
|
byte[] array4 = this.mKey;
|
|
this.CTR_DRBG_Update(array2, array4, array3);
|
|
this.mReseedCounter = (long)((ulong)1L);
|
|
}
|
|
|
|
// Token: 0x0600B5E2 RID: 46562 RVA: 0x00275B10 File Offset: 0x00273D10
|
|
[Token(Token = "0x600B5E2")]
|
|
[Address(RVA = "0x2353F30", Offset = "0x2352F30", VA = "0x182353F30")]
|
|
private void XOR(byte[] output, byte[] a, byte[] b, int bOff)
|
|
{
|
|
int num = 0;
|
|
int length = output.Length;
|
|
if (num < length)
|
|
{
|
|
num++;
|
|
}
|
|
}
|
|
|
|
// Token: 0x0600B5E3 RID: 46563 RVA: 0x00275B34 File Offset: 0x00273D34
|
|
[Token(Token = "0x600B5E3")]
|
|
[Address(RVA = "0x23525D0", Offset = "0x23515D0", VA = "0x1823525D0")]
|
|
private void AddOneTo(byte[] longer)
|
|
{
|
|
int num = longer.Length;
|
|
uint num2;
|
|
num -= (int)num2;
|
|
byte b;
|
|
num2 += (uint)b;
|
|
}
|
|
|
|
// Token: 0x0600B5E4 RID: 46564 RVA: 0x00275B58 File Offset: 0x00273D58
|
|
[Token(Token = "0x600B5E4")]
|
|
[Address(RVA = "0x2353950", Offset = "0x2352950", VA = "0x182353950")]
|
|
private byte[] GetEntropy()
|
|
{
|
|
IEntropySource entropySource = this.mEntropySource;
|
|
int num = this.mSecurityStrength;
|
|
num += 7;
|
|
num += typeof(IEntropySource).TypeHandle;
|
|
throw new NullReferenceException();
|
|
}
|
|
|
|
// Token: 0x0600B5E5 RID: 46565 RVA: 0x00275B84 File Offset: 0x00273D84
|
|
[Token(Token = "0x600B5E5")]
|
|
[Address(RVA = "0x2352920", Offset = "0x2351920", VA = "0x182352920")]
|
|
private byte[] Block_Cipher_df(byte[] inputString, int bitLength)
|
|
{
|
|
IBlockCipher blockCipher = this.mEngine;
|
|
int length = inputString.Length;
|
|
byte[] array = new byte[0];
|
|
int num = 0;
|
|
int num2 = 0;
|
|
this.copyIntToByteArray(array, length, num2);
|
|
this.copyIntToByteArray(array, length, 4);
|
|
int num3 = 0;
|
|
Array.Copy(inputString, num3, array, 8, length);
|
|
int num4 = this.mKeySizeInBits;
|
|
num3 += num4;
|
|
byte[] array2 = new byte[num3];
|
|
byte[] array3 = new byte[num3];
|
|
byte[] array4 = new byte[num3];
|
|
int num5 = this.mKeySizeInBits;
|
|
num3 += num5;
|
|
byte[] array5 = new byte[num3];
|
|
int num6 = 0;
|
|
int length2 = array5.Length;
|
|
int num7 = 0;
|
|
byte[] k_BITS = CtrSP800Drbg.K_BITS;
|
|
Array.Copy(k_BITS, num7, array5, num6, length2);
|
|
int num9;
|
|
if (k_BITS > 0)
|
|
{
|
|
array4[0] = (byte)num;
|
|
array4[0] = (byte)num;
|
|
array4[0] = (byte)num;
|
|
array4[0] = (byte)num;
|
|
this.BCC(array3, array5, array4, array);
|
|
int num8 = array2.Length;
|
|
num8 -= num;
|
|
num9 = 0;
|
|
Array.Copy(array3, num9, array2, num, num8);
|
|
int num10 = this.mKeySizeInBits;
|
|
num += num9;
|
|
num10 += num9;
|
|
num++;
|
|
}
|
|
byte[] array6 = new byte[num9];
|
|
int length3 = array5.Length;
|
|
int num11 = 0;
|
|
int num12 = 0;
|
|
Array.Copy(array2, num12, array5, num11, length3);
|
|
int length4 = array6.Length;
|
|
int num13 = 0;
|
|
int length5 = array5.Length;
|
|
Array.Copy(array2, length5, array6, num13, length4);
|
|
byte[] array7 = new byte[0];
|
|
IBlockCipher blockCipher2 = this.mEngine;
|
|
KeyParameter keyParameter = new KeyParameter(this.ExpandKey(array5));
|
|
int num14 = 0;
|
|
if (num14 < array7.Length)
|
|
{
|
|
IBlockCipher blockCipher3 = this.mEngine;
|
|
if (num14 < blockCipher2)
|
|
{
|
|
num14 += num14;
|
|
num14++;
|
|
}
|
|
int num15 = array7.Length;
|
|
num15 -= num14;
|
|
int num16 = 0;
|
|
Array.Copy(array6, num16, array7, num14, num15);
|
|
num16 += num16;
|
|
num14 += 4;
|
|
num14 += 19;
|
|
}
|
|
return array7;
|
|
}
|
|
|
|
// Token: 0x0600B5E6 RID: 46566 RVA: 0x00275D78 File Offset: 0x00273F78
|
|
[Token(Token = "0x600B5E6")]
|
|
[Address(RVA = "0x2352630", Offset = "0x2351630", VA = "0x182352630")]
|
|
private void BCC(byte[] bccOut, byte[] k, byte[] iV, byte[] data)
|
|
{
|
|
IBlockCipher blockCipher = this.mEngine;
|
|
byte[] array = new byte[0];
|
|
byte[] array2 = new byte[0];
|
|
IBlockCipher blockCipher2 = this.mEngine;
|
|
KeyParameter keyParameter = new KeyParameter(this.ExpandKey(k));
|
|
IBlockCipher blockCipher3 = this.mEngine;
|
|
int num = 0;
|
|
int length = array2.Length;
|
|
if (num < length)
|
|
{
|
|
num++;
|
|
}
|
|
IBlockCipher blockCipher4 = this.mEngine;
|
|
int num2 = 0;
|
|
if (num2 < num)
|
|
{
|
|
num2 += num2;
|
|
num2++;
|
|
}
|
|
num++;
|
|
}
|
|
|
|
// Token: 0x0600B5E7 RID: 46567 RVA: 0x00275E08 File Offset: 0x00274008
|
|
[Token(Token = "0x600B5E7")]
|
|
[Address(RVA = "0x23542C0", Offset = "0x23532C0", VA = "0x1823542C0")]
|
|
private void copyIntToByteArray(byte[] buf, int value, int offSet)
|
|
{
|
|
int num = offSet + 1;
|
|
}
|
|
|
|
// Token: 0x17001C92 RID: 7314
|
|
// (get) Token: 0x0600B5E8 RID: 46568 RVA: 0x00275E20 File Offset: 0x00274020
|
|
[Token(Token = "0x17001C92")]
|
|
public int BlockSize
|
|
{
|
|
[Token(Token = "0x600B5E8")]
|
|
[Address(RVA = "0x2354360", Offset = "0x2353360", VA = "0x182354360", Slot = "4")]
|
|
get
|
|
{
|
|
return this.mV.Length;
|
|
}
|
|
}
|
|
|
|
// Token: 0x0600B5E9 RID: 46569 RVA: 0x00275E40 File Offset: 0x00274040
|
|
[Token(Token = "0x600B5E9")]
|
|
[Address(RVA = "0x23534B0", Offset = "0x23524B0", VA = "0x1823534B0", Slot = "5")]
|
|
public int Generate(byte[] output, byte[] additionalInput, bool predictionResistant)
|
|
{
|
|
long num = this.mReseedCounter;
|
|
if (!this.mIsTdea)
|
|
{
|
|
int num2 = CtrSP800Drbg.AES_MAX_BITS_REQUEST;
|
|
if (output == 0)
|
|
{
|
|
goto IL_34;
|
|
}
|
|
num2 += typeof(DrbgUtilities).TypeHandle;
|
|
}
|
|
int num3 = CtrSP800Drbg.TDEA_MAX_BITS_REQUEST;
|
|
if (output != 0)
|
|
{
|
|
num3 += typeof(DrbgUtilities).TypeHandle;
|
|
}
|
|
IL_34:
|
|
int num4 = 0;
|
|
if (predictionResistant)
|
|
{
|
|
this.CTR_DRBG_Reseed_algorithm(additionalInput);
|
|
}
|
|
int num5 = this.mSeedLength;
|
|
if (num4 == 0)
|
|
{
|
|
byte[] array = new byte[num5];
|
|
}
|
|
byte[] array2 = this.mV;
|
|
byte[] array3 = this.mKey;
|
|
byte[] array4;
|
|
this.CTR_DRBG_Update(array4, array3, array2);
|
|
byte[] array5 = new byte[this.mV.Length];
|
|
byte[] array6 = this.mKey;
|
|
IBlockCipher blockCipher = this.mEngine;
|
|
KeyParameter keyParameter = new KeyParameter(this.ExpandKey(array6));
|
|
int num6 = output.Length;
|
|
int num7 = array5.Length;
|
|
int num8 = num7 * num4;
|
|
num6 -= num8;
|
|
int length = this.mV.Length;
|
|
num7 = num6;
|
|
int num9 = length * num4;
|
|
num7 -= num9;
|
|
if (num7 != 0)
|
|
{
|
|
byte[] array7 = this.mV;
|
|
this.AddOneTo(array7);
|
|
IBlockCipher blockCipher2 = this.mEngine;
|
|
int num10 = array5.Length * num4;
|
|
int num11 = 0;
|
|
Array.Copy(array5, num11, output, num10, num7);
|
|
}
|
|
num4++;
|
|
byte[] array8 = this.mV;
|
|
byte[] array9 = this.mKey;
|
|
this.CTR_DRBG_Update(array4, array9, array8);
|
|
return output.Length;
|
|
}
|
|
|
|
// Token: 0x0600B5EA RID: 46570 RVA: 0x00275FA4 File Offset: 0x002741A4
|
|
[Token(Token = "0x600B5EA")]
|
|
[Address(RVA = "0x2353F20", Offset = "0x2352F20", VA = "0x182353F20", Slot = "6")]
|
|
public void Reseed(byte[] additionalInput)
|
|
{
|
|
this.CTR_DRBG_Reseed_algorithm(additionalInput);
|
|
}
|
|
|
|
// Token: 0x0600B5EB RID: 46571 RVA: 0x00275FB8 File Offset: 0x002741B8
|
|
[Token(Token = "0x600B5EB")]
|
|
[Address(RVA = "0x2353AA0", Offset = "0x2352AA0", VA = "0x182353AA0")]
|
|
private bool IsTdea(IBlockCipher cipher)
|
|
{
|
|
bool flag;
|
|
bool flag2;
|
|
return flag || flag2;
|
|
}
|
|
|
|
// Token: 0x0600B5EC RID: 46572 RVA: 0x00275FD4 File Offset: 0x002741D4
|
|
[Token(Token = "0x600B5EC")]
|
|
[Address(RVA = "0x23539F0", Offset = "0x23529F0", VA = "0x1823539F0")]
|
|
private int GetMaxSecurityStrength(IBlockCipher cipher, int keySizeInBits)
|
|
{
|
|
if (!this.IsTdea(cipher) || keySizeInBits == 168)
|
|
{
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// Token: 0x0600B5ED RID: 46573 RVA: 0x00275FFC File Offset: 0x002741FC
|
|
[Token(Token = "0x600B5ED")]
|
|
[Address(RVA = "0x23533E0", Offset = "0x23523E0", VA = "0x1823533E0")]
|
|
private byte[] ExpandKey(byte[] key)
|
|
{
|
|
if (!this.mIsTdea)
|
|
{
|
|
return key;
|
|
}
|
|
byte[] array = new byte[24];
|
|
int num = 0;
|
|
int num2 = 0;
|
|
this.PadKey(key, num2, array, num);
|
|
ulong num3;
|
|
this.PadKey(key, 7, array, (int)num3);
|
|
ulong num4;
|
|
this.PadKey(key, 14, array, (int)num4);
|
|
return array;
|
|
}
|
|
|
|
// Token: 0x0600B5EE RID: 46574 RVA: 0x00276044 File Offset: 0x00274244
|
|
[Token(Token = "0x600B5EE")]
|
|
[Address(RVA = "0x2353B30", Offset = "0x2352B30", VA = "0x182353B30")]
|
|
private void PadKey(byte[] keyMaster, int keyOff, byte[] tmp, int tmpOff)
|
|
{
|
|
int length = keyMaster.Length;
|
|
int num = keyOff + 1;
|
|
byte b;
|
|
tmp[0] = b;
|
|
int length2 = keyMaster.Length;
|
|
byte b2;
|
|
tmp[0] = b2;
|
|
int length3 = keyMaster.Length;
|
|
byte b3;
|
|
tmp[0] = b3;
|
|
int length4 = keyMaster.Length;
|
|
byte b4;
|
|
tmp[0] = b4;
|
|
int length5 = keyMaster.Length;
|
|
byte b5;
|
|
tmp[0] = b5;
|
|
int length6 = keyMaster.Length;
|
|
byte b6;
|
|
b6 += b6;
|
|
}
|
|
|
|
// Token: 0x0400732C RID: 29484
|
|
[Token(Token = "0x400732C")]
|
|
private static readonly long TDEA_RESEED_MAX = (long)((uint)int.MinValue);
|
|
|
|
// Token: 0x0400732D RID: 29485
|
|
[Token(Token = "0x400732D")]
|
|
private static readonly long AES_RESEED_MAX = (long)((ulong)140737488355328L);
|
|
|
|
// Token: 0x0400732E RID: 29486
|
|
[Token(Token = "0x400732E")]
|
|
private static readonly int TDEA_MAX_BITS_REQUEST;
|
|
|
|
// Token: 0x0400732F RID: 29487
|
|
[Token(Token = "0x400732F")]
|
|
private static readonly int AES_MAX_BITS_REQUEST;
|
|
|
|
// Token: 0x04007330 RID: 29488
|
|
[FieldOffset(Offset = "0x10")]
|
|
[Token(Token = "0x4007330")]
|
|
private readonly IEntropySource mEntropySource;
|
|
|
|
// Token: 0x04007331 RID: 29489
|
|
[FieldOffset(Offset = "0x18")]
|
|
[Token(Token = "0x4007331")]
|
|
private readonly IBlockCipher mEngine;
|
|
|
|
// Token: 0x04007332 RID: 29490
|
|
[FieldOffset(Offset = "0x20")]
|
|
[Token(Token = "0x4007332")]
|
|
private readonly int mKeySizeInBits;
|
|
|
|
// Token: 0x04007333 RID: 29491
|
|
[FieldOffset(Offset = "0x24")]
|
|
[Token(Token = "0x4007333")]
|
|
private readonly int mSeedLength;
|
|
|
|
// Token: 0x04007334 RID: 29492
|
|
[FieldOffset(Offset = "0x28")]
|
|
[Token(Token = "0x4007334")]
|
|
private readonly int mSecurityStrength;
|
|
|
|
// Token: 0x04007335 RID: 29493
|
|
[FieldOffset(Offset = "0x30")]
|
|
[Token(Token = "0x4007335")]
|
|
private byte[] mKey;
|
|
|
|
// Token: 0x04007336 RID: 29494
|
|
[FieldOffset(Offset = "0x38")]
|
|
[Token(Token = "0x4007336")]
|
|
private byte[] mV;
|
|
|
|
// Token: 0x04007337 RID: 29495
|
|
[FieldOffset(Offset = "0x40")]
|
|
[Token(Token = "0x4007337")]
|
|
private long mReseedCounter;
|
|
|
|
// Token: 0x04007338 RID: 29496
|
|
[FieldOffset(Offset = "0x48")]
|
|
[Token(Token = "0x4007338")]
|
|
private bool mIsTdea;
|
|
|
|
// Token: 0x04007339 RID: 29497
|
|
[Token(Token = "0x4007339")]
|
|
private static readonly byte[] K_BITS = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
|
|
}
|
|
}
|