// Token: 0x06000122 RID: 290 RVA: 0x0000936C File Offset: 0x0000756C private static uint Inv_Mcol(uint x) { uint num = x ^ AesEngine.Shift(x, 8); uint num2 = x ^ AesEngine.FFmulX(num); num ^= AesEngine.FFmulX2(num2); return(num2 ^ (num ^ AesEngine.Shift(num, 16))); }
private static uint Inv_Mcol(uint x) { uint num = AesEngine.FFmulX(x); uint num2 = AesEngine.FFmulX(num); uint num3 = AesEngine.FFmulX(num2); uint num4 = x ^ num3; return(num ^ num2 ^ num3 ^ AesEngine.Shift(num ^ num4, 8) ^ AesEngine.Shift(num2 ^ num4, 16) ^ AesEngine.Shift(num4, 24)); }
private uint[][] GenerateWorkingKey(byte[] key, bool forEncryption) { int num = key.Length / 4; if (num != 4 && num != 6 && num != 8) { throw new ArgumentException("Key length not 128/192/256 bits."); } this.ROUNDS = num + 6; uint[][] array = new uint[this.ROUNDS + 1][]; for (int i = 0; i <= this.ROUNDS; i++) { array[i] = new uint[4]; } int num2 = 0; int j = 0; while (j < key.Length) { array[num2 >> 2][num2 & 3] = Pack.LE_To_UInt32(key, j); j += 4; num2++; } int num3 = this.ROUNDS + 1 << 2; for (int k = num; k < num3; k++) { uint num4 = array[k - 1 >> 2][k - 1 & 3]; if (k % num == 0) { num4 = (AesEngine.SubWord(AesEngine.Shift(num4, 8)) ^ (uint)AesEngine.rcon[k / num - 1]); } else if (num > 6 && k % num == 4) { num4 = AesEngine.SubWord(num4); } array[k >> 2][k & 3] = (array[k - num >> 2][k - num & 3] ^ num4); } if (!forEncryption) { for (int l = 1; l < this.ROUNDS; l++) { uint[] array2 = array[l]; for (int m = 0; m < 4; m++) { array2[m] = AesEngine.Inv_Mcol(array2[m]); } } } return(array); }
public byte[] TransformKey(byte[] transformSeed, int rounds) { var block = BufferEx.Clone(_hash); var cipher = new AesEngine(); cipher.Init(true, new KeyParameter(transformSeed)); var aesEcb = SymmetricKeyAlgorithmProvider .OpenAlgorithm(SymmetricAlgorithmNames.AesEcb); var key = aesEcb.CreateSymmetricKey( CryptographicBuffer.CreateFromByteArray(transformSeed)); for (int i = 0; i < rounds; i++) { cipher.ProcessBlock(block, 0, block, 0); cipher.ProcessBlock(block, 16, block, 16); } return BufferEx.GetHash(block); }
// Token: 0x0600012F RID: 303 RVA: 0x0000A0A4 File Offset: 0x000082A4 private void DecryptBlock(uint[][] KW) { uint[] array = KW[this.ROUNDS]; uint num = this.C0 ^ array[0]; uint num2 = this.C1 ^ array[1]; uint num3 = this.C2 ^ array[2]; uint num4 = this.C3 ^ array[3]; int i = this.ROUNDS - 1; uint num5; uint num6; uint num7; while (i > 1) { array = KW[i--]; num5 = (AesEngine.Tinv0[(int)(num & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num4 >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num3 >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num2 >> 24 & 255U)], 8) ^ array[0]); num6 = (AesEngine.Tinv0[(int)(num2 & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num4 >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num3 >> 24 & 255U)], 8) ^ array[1]); num7 = (AesEngine.Tinv0[(int)(num3 & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num2 >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num4 >> 24 & 255U)], 8) ^ array[2]); num4 = (AesEngine.Tinv0[(int)(num4 & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num3 >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num2 >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num >> 24 & 255U)], 8) ^ array[3]); array = KW[i--]; num = (AesEngine.Tinv0[(int)(num5 & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num4 >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num7 >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num6 >> 24 & 255U)], 8) ^ array[0]); num2 = (AesEngine.Tinv0[(int)(num6 & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num5 >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num4 >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num7 >> 24 & 255U)], 8) ^ array[1]); num3 = (AesEngine.Tinv0[(int)(num7 & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num6 >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num5 >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num4 >> 24 & 255U)], 8) ^ array[2]); num4 = (AesEngine.Tinv0[(int)(num4 & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num7 >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num6 >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num5 >> 24 & 255U)], 8) ^ array[3]); } array = KW[1]; num5 = (AesEngine.Tinv0[(int)(num & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num4 >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num3 >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num2 >> 24 & 255U)], 8) ^ array[0]); num6 = (AesEngine.Tinv0[(int)(num2 & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num4 >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num3 >> 24 & 255U)], 8) ^ array[1]); num7 = (AesEngine.Tinv0[(int)(num3 & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num2 >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num4 >> 24 & 255U)], 8) ^ array[2]); num4 = (AesEngine.Tinv0[(int)(num4 & 255U)] ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num3 >> 8 & 255U)], 24) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num2 >> 16 & 255U)], 16) ^ AesEngine.Shift(AesEngine.Tinv0[(int)(num >> 24 & 255U)], 8) ^ array[3]); array = KW[0]; this.C0 = (uint)((int)AesEngine.Si[(int)(num5 & 255U)] ^ (int)this.s[(int)(num4 >> 8 & 255U)] << 8 ^ (int)this.s[(int)(num7 >> 16 & 255U)] << 16 ^ (int)AesEngine.Si[(int)(num6 >> 24 & 255U)] << 24 ^ (int)array[0]); this.C1 = (uint)((int)this.s[(int)(num6 & 255U)] ^ (int)this.s[(int)(num5 >> 8 & 255U)] << 8 ^ (int)AesEngine.Si[(int)(num4 >> 16 & 255U)] << 16 ^ (int)this.s[(int)(num7 >> 24 & 255U)] << 24 ^ (int)array[1]); this.C2 = (uint)((int)this.s[(int)(num7 & 255U)] ^ (int)AesEngine.Si[(int)(num6 >> 8 & 255U)] << 8 ^ (int)AesEngine.Si[(int)(num5 >> 16 & 255U)] << 16 ^ (int)this.s[(int)(num4 >> 24 & 255U)] << 24 ^ (int)array[2]); this.C3 = (uint)((int)AesEngine.Si[(int)(num4 & 255U)] ^ (int)this.s[(int)(num7 >> 8 & 255U)] << 8 ^ (int)this.s[(int)(num6 >> 16 & 255U)] << 16 ^ (int)this.s[(int)(num5 >> 24 & 255U)] << 24 ^ (int)array[3]); }
private void EncryptBlock(uint[][] KW) { uint[] array = KW[0]; uint num = this.C0 ^ array[0]; uint num2 = this.C1 ^ array[1]; uint num3 = this.C2 ^ array[2]; uint num4 = this.C3 ^ array[3]; int i = 1; uint num5; uint num6; uint num7; while (i < this.ROUNDS - 1) { array = KW[i++]; num5 = (AesEngine.T0[(int)((UIntPtr)(num & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num2 >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num3 >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num4 >> 24 & 255u))], 8) ^ array[0]); num6 = (AesEngine.T0[(int)((UIntPtr)(num2 & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num3 >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num4 >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num >> 24 & 255u))], 8) ^ array[1]); num7 = (AesEngine.T0[(int)((UIntPtr)(num3 & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num4 >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num2 >> 24 & 255u))], 8) ^ array[2]); num4 = (AesEngine.T0[(int)((UIntPtr)(num4 & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num2 >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num3 >> 24 & 255u))], 8) ^ array[3]); array = KW[i++]; num = (AesEngine.T0[(int)((UIntPtr)(num5 & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num6 >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num7 >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num4 >> 24 & 255u))], 8) ^ array[0]); num2 = (AesEngine.T0[(int)((UIntPtr)(num6 & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num7 >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num4 >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num5 >> 24 & 255u))], 8) ^ array[1]); num3 = (AesEngine.T0[(int)((UIntPtr)(num7 & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num4 >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num5 >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num6 >> 24 & 255u))], 8) ^ array[2]); num4 = (AesEngine.T0[(int)((UIntPtr)(num4 & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num5 >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num6 >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num7 >> 24 & 255u))], 8) ^ array[3]); } array = KW[i++]; num5 = (AesEngine.T0[(int)((UIntPtr)(num & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num2 >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num3 >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num4 >> 24 & 255u))], 8) ^ array[0]); num6 = (AesEngine.T0[(int)((UIntPtr)(num2 & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num3 >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num4 >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num >> 24 & 255u))], 8) ^ array[1]); num7 = (AesEngine.T0[(int)((UIntPtr)(num3 & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num4 >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num2 >> 24 & 255u))], 8) ^ array[2]); num4 = (AesEngine.T0[(int)((UIntPtr)(num4 & 255u))] ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num >> 8 & 255u))], 24) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num2 >> 16 & 255u))], 16) ^ AesEngine.Shift(AesEngine.T0[(int)((UIntPtr)(num3 >> 24 & 255u))], 8) ^ array[3]); array = KW[i]; this.C0 = (uint)((int)AesEngine.S[(int)((UIntPtr)(num5 & 255u))] ^ (int)AesEngine.S[(int)((UIntPtr)(num6 >> 8 & 255u))] << 8 ^ (int)AesEngine.S[(int)((UIntPtr)(num7 >> 16 & 255u))] << 16 ^ (int)AesEngine.S[(int)((UIntPtr)(num4 >> 24 & 255u))] << 24 ^ (int)array[0]); this.C1 = (uint)((int)AesEngine.S[(int)((UIntPtr)(num6 & 255u))] ^ (int)AesEngine.S[(int)((UIntPtr)(num7 >> 8 & 255u))] << 8 ^ (int)AesEngine.S[(int)((UIntPtr)(num4 >> 16 & 255u))] << 16 ^ (int)AesEngine.S[(int)((UIntPtr)(num5 >> 24 & 255u))] << 24 ^ (int)array[1]); this.C2 = (uint)((int)AesEngine.S[(int)((UIntPtr)(num7 & 255u))] ^ (int)AesEngine.S[(int)((UIntPtr)(num4 >> 8 & 255u))] << 8 ^ (int)AesEngine.S[(int)((UIntPtr)(num5 >> 16 & 255u))] << 16 ^ (int)AesEngine.S[(int)((UIntPtr)(num6 >> 24 & 255u))] << 24 ^ (int)array[2]); this.C3 = (uint)((int)AesEngine.S[(int)((UIntPtr)(num4 & 255u))] ^ (int)AesEngine.S[(int)((UIntPtr)(num5 >> 8 & 255u))] << 8 ^ (int)AesEngine.S[(int)((UIntPtr)(num6 >> 16 & 255u))] << 16 ^ (int)AesEngine.S[(int)((UIntPtr)(num7 >> 24 & 255u))] << 24 ^ (int)array[3]); }
private static void TestRijndael() { // Test vector (official ECB test vector #356) byte[] pbIV = new byte[16]; byte[] pbTestKey = new byte[32]; byte[] pbTestData = new byte[16]; byte[] pbReferenceCT = new byte[16] { 0x75, 0xD1, 0x1B, 0x0E, 0x3A, 0x68, 0xC4, 0x22, 0x3D, 0x88, 0xDB, 0xF0, 0x17, 0x97, 0x7D, 0xD7 }; int i; for(i = 0; i < 16; ++i) pbIV[i] = 0; for(i = 0; i < 32; ++i) pbTestKey[i] = 0; for(i = 0; i < 16; ++i) pbTestData[i] = 0; pbTestData[0] = 0x04; #if KeePassUAP AesEngine r = new AesEngine(); r.Init(true, new KeyParameter(pbTestKey)); if(r.GetBlockSize() != pbTestData.Length) throw new SecurityException(KLRes.EncAlgorithmAes + " (BS)."); r.ProcessBlock(pbTestData, 0, pbTestData, 0); #else RijndaelManaged r = new RijndaelManaged(); if(r.BlockSize != 128) // AES block size { Debug.Assert(false); r.BlockSize = 128; } r.IV = pbIV; r.KeySize = 256; r.Key = pbTestKey; r.Mode = CipherMode.ECB; ICryptoTransform iCrypt = r.CreateEncryptor(); iCrypt.TransformBlock(pbTestData, 0, 16, pbTestData, 0); #endif if(!MemUtil.ArraysEqual(pbTestData, pbReferenceCT)) throw new SecurityException(KLRes.EncAlgorithmAes + "."); }
/// <summary> /// Encapsulates the specified links into a DLC container. /// </summary> /// <param name="name">The name of the package.</param> /// <param name="links">The links.</param> /// <returns> /// Base-64-encoded DLC container. /// </returns> public static string CreateDLC(string name, string[] links) { var sb = new StringBuilder(); sb.Append("<dlc>"); sb.Append("<header>"); sb.Append("<generator>"); sb.Append("<app>" + Convert.ToBase64String(Encoding.UTF8.GetBytes("RS TV Show Tracker")) + "</app>"); sb.Append("<version>" + Convert.ToBase64String(Encoding.UTF8.GetBytes("1.0")) + "</version>"); sb.Append("<url>" + Convert.ToBase64String(Encoding.UTF8.GetBytes("http://lab.rolisoft.net/")) + "</url>"); sb.Append("</generator>"); sb.Append("<dlcxmlversion>" + Convert.ToBase64String(Encoding.UTF8.GetBytes("20_02_2008")) + "</dlcxmlversion>"); sb.Append("</header>"); sb.Append("<content>"); sb.Append("<package name=\"" + Convert.ToBase64String(Encoding.UTF8.GetBytes(name)) + "\">"); foreach (var link in links) { sb.Append("<file>"); sb.Append("<url>" + Convert.ToBase64String(Encoding.UTF8.GetBytes(link)) + "</url>"); //sb.Append("<filename></filename>"); //sb.Append("<size></size>"); sb.Append("</file>"); } sb.Append("</package>"); sb.Append("</content>"); sb.Append("</dlc>"); var xml = Convert.ToBase64String(Encoding.UTF8.GetBytes(sb.ToString())); var key = BitConverter.ToString(new SHA256CryptoServiceProvider().ComputeHash(BitConverter.GetBytes(DateTime.Now.ToBinary()))).Replace("-", string.Empty).Substring(0, 16); var srv = Utils.GetURL(DLCCrypt, "&data=" + key + "&lid=" + Convert.ToBase64String(Encoding.UTF8.GetBytes("http://lab.rolisoft.net/_3600")) + "&version=1.0&client=rstvshowtracker"); var rcr = Regex.Match(srv, @"<rc>(.+)</rc>"); if (!rcr.Groups[1].Success) { throw new Exception("The jDownloader DLC encryption service did not return an encryption key."); } var enc = rcr.Groups[1].Value; var aes = new AesEngine(); var cbc = new CbcBlockCipher(aes); var zbp = new ZeroBytePadding(); var pad = new PaddedBufferedBlockCipher(cbc, zbp); pad.Init(true, new ParametersWithIV(new KeyParameter(Encoding.ASCII.GetBytes(key)), Encoding.ASCII.GetBytes(key))); var xm2 = Convert.ToBase64String(pad.DoFinal(Encoding.ASCII.GetBytes(xml))); return xm2 + enc; }
/// <summary> /// Encapsulates the specified links into an RSDF container. /// </summary> /// <param name="links">The links.</param> /// <returns> /// Base-16-encoded RSDF container. /// </returns> public static string CreateRSDF(string[] links) { var aes = new AesEngine(); var cfb = new CfbBlockCipher(aes, 8); var pad = new BufferedBlockCipher(cfb); var sb = new StringBuilder(); pad.Init(true, new ParametersWithIV(new KeyParameter(RSDFKey), RSDFIV)); foreach (var link in links) { var input = Encoding.UTF8.GetBytes(link); var output = new byte[input.Length]; for (var i = 0; i < input.Length; i++) { output[i] = pad.ProcessByte(input[i])[0]; } sb.Append(Convert.ToBase64String(output)); sb.Append(Environment.NewLine); } return BitConverter.ToString(Encoding.ASCII.GetBytes(sb.ToString())).Replace("-", string.Empty); }
/// <summary> /// Encapsulates the specified links into a CCF container. /// </summary> /// <param name="name">The name of the package.</param> /// <param name="links">The links.</param> /// <returns> /// CCF container. /// </returns> public static byte[] CreateCCF(string name, string[] links) { var sb = new StringBuilder(); sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sb.Append("<CryptLoad>"); sb.Append("<Package service=\"\" name=\"" + name + "\" url=\"Directlinks\">"); foreach (var link in links) { sb.Append("<Download Url=\"" + link + "\">"); sb.Append("<Url>" + link + "</Url>"); //sb.Append("<FileName></FileName>"); //sb.Append("<FileSize></FileSize>"); sb.Append("</Download>"); } sb.Append("</Package>"); sb.Append("</CryptLoad>"); var aes = new AesEngine(); var cbc = new CbcBlockCipher(aes); var pk7 = new Pkcs7Padding(); var pad = new PaddedBufferedBlockCipher(cbc, pk7); pad.Init(true, new ParametersWithIV(new KeyParameter(CCFKey), CCFIV)); return pad.DoFinal(Encoding.UTF8.GetBytes(sb.ToString())); }
public static bool TransformKeyManaged(byte[] pbNewKey32, byte[] pbKeySeed32, ulong uNumRounds) { #if KeePassRT KeyParameter kp = new KeyParameter(pbKeySeed32); AesEngine aes = new AesEngine(); aes.Init(true, kp); for(ulong i = 0; i < uNumRounds; ++i) { aes.ProcessBlock(pbNewKey32, 0, pbNewKey32, 0); aes.ProcessBlock(pbNewKey32, 16, pbNewKey32, 16); } #else byte[] pbIV = new byte[16]; Array.Clear(pbIV, 0, pbIV.Length); RijndaelManaged r = new RijndaelManaged(); if(r.BlockSize != 128) // AES block size { Debug.Assert(false); r.BlockSize = 128; } r.IV = pbIV; r.Mode = CipherMode.ECB; r.KeySize = 256; r.Key = pbKeySeed32; ICryptoTransform iCrypt = r.CreateEncryptor(); // !iCrypt.CanReuseTransform -- doesn't work with Mono if((iCrypt == null) || (iCrypt.InputBlockSize != 16) || (iCrypt.OutputBlockSize != 16)) { Debug.Assert(false, "Invalid ICryptoTransform."); Debug.Assert((iCrypt.InputBlockSize == 16), "Invalid input block size!"); Debug.Assert((iCrypt.OutputBlockSize == 16), "Invalid output block size!"); return false; } for(ulong i = 0; i < uNumRounds; ++i) { iCrypt.TransformBlock(pbNewKey32, 0, 16, pbNewKey32, 0); iCrypt.TransformBlock(pbNewKey32, 16, 16, pbNewKey32, 16); } #endif return true; }
/// <summary> /// Benchmark the <c>TransformKey</c> method. Within /// <paramref name="uMilliseconds"/> ms, random keys will be transformed /// and the number of performed transformations are returned. /// </summary> /// <param name="uMilliseconds">Test duration in ms.</param> /// <param name="uStep">Stepping. /// <paramref name="uStep" /> should be a prime number. For fast processors /// (PCs) a value of <c>3001</c> is recommended, for slower processors (PocketPC) /// a value of <c>401</c> is recommended.</param> /// <returns>Number of transformations performed in the specified /// amount of time. Maximum value is <c>uint.MaxValue</c>.</returns> public static ulong TransformKeyBenchmark(uint uMilliseconds, ulong uStep) { ulong uRounds; // Try native method if(NativeLib.TransformKeyBenchmark256(uMilliseconds, out uRounds)) return uRounds; byte[] pbKey = new byte[32]; byte[] pbNewKey = new byte[32]; for(int i = 0; i < pbKey.Length; ++i) { pbKey[i] = (byte)i; pbNewKey[i] = (byte)i; } #if KeePassRT KeyParameter kp = new KeyParameter(pbKey); AesEngine aes = new AesEngine(); aes.Init(true, kp); #else byte[] pbIV = new byte[16]; Array.Clear(pbIV, 0, pbIV.Length); RijndaelManaged r = new RijndaelManaged(); if(r.BlockSize != 128) // AES block size { Debug.Assert(false); r.BlockSize = 128; } r.IV = pbIV; r.Mode = CipherMode.ECB; r.KeySize = 256; r.Key = pbKey; ICryptoTransform iCrypt = r.CreateEncryptor(); // !iCrypt.CanReuseTransform -- doesn't work with Mono if((iCrypt == null) || (iCrypt.InputBlockSize != 16) || (iCrypt.OutputBlockSize != 16)) { Debug.Assert(false, "Invalid ICryptoTransform."); Debug.Assert(iCrypt.InputBlockSize == 16, "Invalid input block size!"); Debug.Assert(iCrypt.OutputBlockSize == 16, "Invalid output block size!"); return PwDefs.DefaultKeyEncryptionRounds; } #endif uRounds = 0; int tStart = Environment.TickCount; while(true) { for(ulong j = 0; j < uStep; ++j) { #if KeePassRT aes.ProcessBlock(pbNewKey, 0, pbNewKey, 0); aes.ProcessBlock(pbNewKey, 16, pbNewKey, 16); #else iCrypt.TransformBlock(pbNewKey, 0, 16, pbNewKey, 0); iCrypt.TransformBlock(pbNewKey, 16, 16, pbNewKey, 16); #endif } uRounds += uStep; if(uRounds < uStep) // Overflow check { uRounds = ulong.MaxValue; break; } uint tElapsed = (uint)(Environment.TickCount - tStart); if(tElapsed > uMilliseconds) break; } return uRounds; }
private static Stream CreateStream(Stream s, bool bEncrypt, byte[] pbKey, byte[] pbIV) { StandardAesEngine.ValidateArguments(s, bEncrypt, pbKey, pbIV); byte[] pbLocalIV = new byte[16]; Array.Copy(pbIV, pbLocalIV, 16); byte[] pbLocalKey = new byte[32]; Array.Copy(pbKey, pbLocalKey, 32); #if !KeePassRT RijndaelManaged r = new RijndaelManaged(); if(r.BlockSize != 128) // AES block size { Debug.Assert(false); r.BlockSize = 128; } r.IV = pbLocalIV; r.KeySize = 256; r.Key = pbLocalKey; r.Mode = m_rCipherMode; r.Padding = m_rCipherPadding; ICryptoTransform iTransform = (bEncrypt ? r.CreateEncryptor() : r.CreateDecryptor()); Debug.Assert(iTransform != null); if(iTransform == null) throw new SecurityException("Unable to create Rijndael transform!"); return new CryptoStream(s, iTransform, bEncrypt ? CryptoStreamMode.Write : CryptoStreamMode.Read); #else AesEngine aes = new AesEngine(); CbcBlockCipher cbc = new CbcBlockCipher(aes); PaddedBufferedBlockCipher bc = new PaddedBufferedBlockCipher(cbc, new Pkcs7Padding()); KeyParameter kp = new KeyParameter(pbLocalKey); ParametersWithIV prmIV = new ParametersWithIV(kp, pbLocalIV); bc.Init(bEncrypt, prmIV); IBufferedCipher cpRead = (bEncrypt ? null : bc); IBufferedCipher cpWrite = (bEncrypt ? bc : null); return new CipherStream(s, cpRead, cpWrite); #endif }
private IBufferedCipher createCipher(bool forEncryption) { // IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CFB/NoPadding"); IBlockCipher blockCipher = new AesEngine(); int bits = 8 * blockCipher.GetBlockSize(); // TODO Is this right? blockCipher = new CfbBlockCipher(blockCipher, bits); IBufferedCipher cipher = new BufferedBlockCipher(blockCipher); // SecureRandom random = new SecureRandom(); byte[] keyBytes = new byte[32]; //random.NextBytes(keyBytes); KeyParameter key = new KeyParameter(keyBytes); byte[] iv = new byte[cipher.GetBlockSize()]; //random.NextBytes(iv); cipher.Init(forEncryption, new ParametersWithIV(key, iv)); return cipher; }
// Token: 0x06000124 RID: 292 RVA: 0x000093FC File Offset: 0x000075FC private uint[][] GenerateWorkingKey(byte[] key, bool forEncryption) { int num = key.Length; if (num < 16 || num > 32 || (num & 7) != 0) { throw new ArgumentException("Key length not 128/192/256 bits."); } int num2 = num >> 2; this.ROUNDS = num2 + 6; uint[][] array = new uint[this.ROUNDS + 1][]; for (int i = 0; i <= this.ROUNDS; i++) { array[i] = new uint[4]; } switch (num2) { case 4: { uint num3 = Pack.LE_To_UInt32(key, 0); array[0][0] = num3; uint num4 = Pack.LE_To_UInt32(key, 4); array[0][1] = num4; uint num5 = Pack.LE_To_UInt32(key, 8); array[0][2] = num5; uint num6 = Pack.LE_To_UInt32(key, 12); array[0][3] = num6; for (int j = 1; j <= 10; j++) { uint num7 = AesEngine.SubWord(AesEngine.Shift(num6, 8)) ^ (uint)AesEngine.rcon[j - 1]; num3 ^= num7; array[j][0] = num3; num4 ^= num3; array[j][1] = num4; num5 ^= num4; array[j][2] = num5; num6 ^= num5; array[j][3] = num6; } goto IL_4EC; } case 6: { uint num8 = Pack.LE_To_UInt32(key, 0); array[0][0] = num8; uint num9 = Pack.LE_To_UInt32(key, 4); array[0][1] = num9; uint num10 = Pack.LE_To_UInt32(key, 8); array[0][2] = num10; uint num11 = Pack.LE_To_UInt32(key, 12); array[0][3] = num11; uint num12 = Pack.LE_To_UInt32(key, 16); array[1][0] = num12; uint num13 = Pack.LE_To_UInt32(key, 20); array[1][1] = num13; uint num14 = 1U; uint num15 = AesEngine.SubWord(AesEngine.Shift(num13, 8)) ^ num14; num14 <<= 1; num8 ^= num15; array[1][2] = num8; num9 ^= num8; array[1][3] = num9; num10 ^= num9; array[2][0] = num10; num11 ^= num10; array[2][1] = num11; num12 ^= num11; array[2][2] = num12; num13 ^= num12; array[2][3] = num13; for (int k = 3; k < 12; k += 3) { num15 = (AesEngine.SubWord(AesEngine.Shift(num13, 8)) ^ num14); num14 <<= 1; num8 ^= num15; array[k][0] = num8; num9 ^= num8; array[k][1] = num9; num10 ^= num9; array[k][2] = num10; num11 ^= num10; array[k][3] = num11; num12 ^= num11; array[k + 1][0] = num12; num13 ^= num12; array[k + 1][1] = num13; num15 = (AesEngine.SubWord(AesEngine.Shift(num13, 8)) ^ num14); num14 <<= 1; num8 ^= num15; array[k + 1][2] = num8; num9 ^= num8; array[k + 1][3] = num9; num10 ^= num9; array[k + 2][0] = num10; num11 ^= num10; array[k + 2][1] = num11; num12 ^= num11; array[k + 2][2] = num12; num13 ^= num12; array[k + 2][3] = num13; } num15 = (AesEngine.SubWord(AesEngine.Shift(num13, 8)) ^ num14); num8 ^= num15; array[12][0] = num8; num9 ^= num8; array[12][1] = num9; num10 ^= num9; array[12][2] = num10; num11 ^= num10; array[12][3] = num11; goto IL_4EC; } case 8: { uint num16 = Pack.LE_To_UInt32(key, 0); array[0][0] = num16; uint num17 = Pack.LE_To_UInt32(key, 4); array[0][1] = num17; uint num18 = Pack.LE_To_UInt32(key, 8); array[0][2] = num18; uint num19 = Pack.LE_To_UInt32(key, 12); array[0][3] = num19; uint num20 = Pack.LE_To_UInt32(key, 16); array[1][0] = num20; uint num21 = Pack.LE_To_UInt32(key, 20); array[1][1] = num21; uint num22 = Pack.LE_To_UInt32(key, 24); array[1][2] = num22; uint num23 = Pack.LE_To_UInt32(key, 28); array[1][3] = num23; uint num24 = 1U; uint num25; for (int l = 2; l < 14; l += 2) { num25 = (AesEngine.SubWord(AesEngine.Shift(num23, 8)) ^ num24); num24 <<= 1; num16 ^= num25; array[l][0] = num16; num17 ^= num16; array[l][1] = num17; num18 ^= num17; array[l][2] = num18; num19 ^= num18; array[l][3] = num19; num25 = AesEngine.SubWord(num19); num20 ^= num25; array[l + 1][0] = num20; num21 ^= num20; array[l + 1][1] = num21; num22 ^= num21; array[l + 1][2] = num22; num23 ^= num22; array[l + 1][3] = num23; } num25 = (AesEngine.SubWord(AesEngine.Shift(num23, 8)) ^ num24); num16 ^= num25; array[14][0] = num16; num17 ^= num16; array[14][1] = num17; num18 ^= num17; array[14][2] = num18; num19 ^= num18; array[14][3] = num19; goto IL_4EC; } } throw new InvalidOperationException("Should never get here"); IL_4EC: if (!forEncryption) { for (int m = 1; m < this.ROUNDS; m++) { uint[] array2 = array[m]; for (int n = 0; n < 4; n++) { array2[n] = AesEngine.Inv_Mcol(array2[n]); } } } return(array); }
private BufferedBlockCipher CreateScryptoEngine() { IBlockCipher engine; switch (_MSec.Algorithm) { case ESec.SCRYPTO_AES: engine = new AesEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_AESFAST: engine = new AesFastEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_AESLIGHT: engine = new AesLightEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_BLOWFISH: engine = new BlowfishEngine(); _MSec.KeySize = 56; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAMELLIA: engine = new CamelliaEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAMELLIALIGHT: engine = new CamelliaLightEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAST5: engine = new Cast5Engine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAST6: engine = new Cast6Engine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_DES: engine = new DesEngine(); _MSec.KeySize = 8; _MSec.IVSize = 0; break; case ESec.SCRYPTO_DESEDE: engine = new DesEdeEngine(); _MSec.KeySize = 24; _MSec.IVSize = 0; break; case ESec.SCRYPTO_GOST28147: engine = new Gost28147Engine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_NOEKEON: engine = new NoekeonEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_NULL: engine = new NullEngine(); _MSec.KeySize = 32; _MSec.IVSize = 16; break; case ESec.SCRYPTO_RC2: engine = new RC2Engine(); _MSec.KeySize = 128; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RC532: engine = new RC532Engine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RC564: engine = new RC564Engine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RC6: engine = new RC6Engine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RIJNDAEL: engine = new RijndaelEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_SEED: engine = new SeedEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_SERPENT: engine = new SerpentEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_SKIPJACK: engine = new SkipjackEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_TEA: engine = new TeaEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_TWOFISH: engine = new TwofishEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_XTEA: engine = new XteaEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; default: engine = new AesEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; } switch (_MSec.Mode) { case ESec.MODE_CBC: engine = new CbcBlockCipher(engine); break; case ESec.MODE_CFB: engine = new CfbBlockCipher(engine, 8); break; case ESec.MODE_GOFB: engine = new GOfbBlockCipher(engine); break; case ESec.MODE_OFB: engine = new OfbBlockCipher(engine, 8); break; case ESec.MODE_OPENPGPCFB: engine = new OpenPgpCfbBlockCipher(engine); break; case ESec.MODE_SIC: engine = new SicBlockCipher(engine); break; default: engine = new CbcBlockCipher(engine); break; } IBlockCipherPadding padding = null; switch (_MSec.Padding) { case ESec.PADDING_ISO10126d2: padding = new ISO10126d2Padding(); break; case ESec.PADDING_ISO7816d4: padding = new ISO7816d4Padding(); break; case ESec.PADDING_PKCS7: padding = new Pkcs7Padding(); break; case ESec.PADDING_TBC: padding = new TbcPadding(); break; case ESec.PADDING_X923: padding = new X923Padding(); break; case ESec.PADDING_ZEROBYTE: padding = new ZeroBytePadding(); break; default: padding = new Pkcs7Padding(); break; } return new PaddedBufferedBlockCipher(engine, padding); }