namespace com.hitrust.Security.Cryptography { using com.hitrust.Security; using System; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; internal sealed class SymmetricKey : IDisposable { private static readonly byte[] ExponentOfOne = new byte[] { 7, 2, 0, 0, 0, 0xa4, 0, 0, 0x52, 0x53, 0x41, 50, 0, 4, 0, 0, 1, 0, 0, 0, 0x4b, 0x59, 0x4e, 0x26, 0xd0, 90, 0x33, 11, 0xbd, 0x5d, 0x44, 0x53, 0x19, 0xa3, 0x74, 0x8a, 0x1c, 0x90, 0x71, 0x75, 8, 0x41, 0x19, 0xf4, 0xbc, 0x92, 0x23, 4, 0x26, 0x67, 0x8d, 190, 0xe4, 0x6f, 0x74, 0x71, 0x47, 0x60, 0x60, 0x55, 0x1f, 0x72, 0x20, 0x79, 0xf2, 0x21, 0xab, 0x91, 0xc4, 0xc9, 0x5c, 180, 0x89, 0x67, 0x52, 0x10, 0x9c, 0x71, 0x52, 0x7b, 0xd4, 0x42, 0xae, 14, 0x93, 0xa6, 0xaf, 0x8d, 0x3a, 0x61, 0x70, 0x41, 0x98, 0xc3, 0x58, 220, 0xcf, 0x4c, 0xef, 0x3e, 0xc6, 0xf3, 0xe0, 180, 0xcd, 0xfb, 0xec, 0x81, 11, 0x7a, 0x75, 0x29, 0x7a, 190, 0x40, 0xf6, 0x4a, 0x3f, 0x40, 0xb7, 0x43, 240, 0x45, 0x3f, 150, 0xf1, 0x73, 0x2f, 0x71, 0xee, 0xa7, 0x70, 0x4d, 0xf9, 0x63, 0xb8, 0x52, 0x4c, 0xf1, 0x18, 0xf3, 60, 0x21, 0x13, 0x6a, 0x9a, 0x85, 0xb7, 0xa1, 0xfd, 0xb6, 0xa4, 0xf1, 0xeb, 3, 0xd6, 0x86, 5, 0x6a, 0x63, 0x93, 0xb2, 0xe7, 0xf9, 0x2a, 0x77, 9, 0xe4, 12, 0x90, 0x2d, 0x6a, 0xa2, 0xcd, 0x37, 11, 0xc0, 0xb6, 0x1c, 150, 0xc3, 0xa7, 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, 0x5c, 0xb5, 0x16, 4, 0xc4, 0x4e, 0x9d, 0x89, 0xef, 0xf1, 0x15, 0x26, 0x19, 0x16, 0x3e, 0xdd, 0xac, 0x4f, 0xe1, 170, 0x44, 0x7b, 160, 0xc5, 0xe9, 0x93, 0xc1, 0x34, 0x15, 0x67, 0x69, 0x2d, 0xc3, 0x83, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; private int m_ExponentOfOne; private int m_Handle; private bool m_OwnsProvider; private PaddingMode m_PaddingMode; private int m_Provider; private SymmetricKey(CryptoProvider provider) : this(false) { this.m_Provider = 0; if (((SspiProvider.CryptAcquireContext(ref this.m_Provider, IntPtr.Zero, null, (int) provider, 8) == 0) && (Marshal.GetLastWin32Error() == -2146893809)) && (SspiProvider.CryptAcquireContext(ref this.m_Provider, IntPtr.Zero, null, (int) provider, 0) == 0)) { throw new SecurityException("Cannot acquire crypto service provider."); } this.m_ExponentOfOne = this.CreateExponentOfOneKey(); this.m_PaddingMode = PaddingMode.None; } private SymmetricKey(bool ownsProvider) { this.m_OwnsProvider = ownsProvider; } public SymmetricKey(CryptoProvider provider, CryptoAlgorithm algorithm) : this(provider) { this.m_Handle = 0; if (SspiProvider.CryptGenKey(this.m_Provider, new IntPtr((int) algorithm), 1, ref this.m_Handle) == 0) { throw new SecurityException("Cannot generate session key."); } } public SymmetricKey(CryptoProvider provider, CryptoAlgorithm algorithm, byte[] buffer) : this(provider) { if (buffer == null) { throw new ArgumentNullException(); } this.m_Handle = this.KeyFromBytes(this.m_Provider, algorithm, buffer); } internal SymmetricKey(int provider, int key, bool ownsProvider) : this(ownsProvider) { if ((key == 0) || (provider == 0)) { throw new ArgumentNullException(); } this.m_Provider = provider; this.m_Handle = key; this.m_PaddingMode = PaddingMode.None; } private int CreateDynamicExponentOfOneKey() { int phKey = 0; int pdwDataLen = 0; IntPtr zero = IntPtr.Zero; try { if (SspiProvider.CryptGenKey(this.m_Provider, new IntPtr(1), 1, ref phKey) == 0) { throw new SecurityException("Cannot generate key pair."); } if (SspiProvider.CryptExportKey(phKey, 0, 7, 0, IntPtr.Zero, ref pdwDataLen) == 0) { throw new SecurityException("Cannot export generated key."); } zero = Marshal.AllocHGlobal(pdwDataLen); if (SspiProvider.CryptExportKey(phKey, 0, 7, 0, zero, ref pdwDataLen) == 0) { throw new SecurityException("Cannot export generated key."); } SspiProvider.CryptDestroyKey(phKey); phKey = 0; int num3 = Marshal.ReadInt32(zero, 12); int ofs = 0x10; for (int i = 0; i < 4; i++) { if (i == 0) { Marshal.WriteByte(zero, ofs, 1); } else { Marshal.WriteByte(zero, ofs + i, 0); } } ofs += 4; ofs += num3 / 8; ofs += num3 / 0x10; ofs += num3 / 0x10; for (int j = 0; j < (num3 / 0x10); j++) { if (j == 0) { Marshal.WriteByte(zero, ofs, 1); } else { Marshal.WriteByte(zero, ofs + j, 0); } } ofs += num3 / 0x10; for (int k = 0; k < (num3 / 0x10); k++) { if (k == 0) { Marshal.WriteByte(zero, ofs, 1); } else { Marshal.WriteByte(zero, ofs + k, 0); } } ofs += num3 / 0x10; ofs += num3 / 0x10; for (int m = 0; m < (num3 / 8); m++) { if (m == 0) { Marshal.WriteByte(zero, ofs, 1); } else { Marshal.WriteByte(zero, ofs + m, 0); } } if (SspiProvider.CryptImportKey(this.m_Provider, zero, pdwDataLen, 0, 1, ref phKey) == 0) { throw new SecurityException("Could not import modified key."); } } catch (Exception exception) { if (phKey != 0) { SspiProvider.CryptDestroyKey(phKey); } throw exception; } finally { if (zero != IntPtr.Zero) { Marshal.FreeHGlobal(zero); } } return phKey; } private int CreateExponentOfOneKey() { try { return this.CreateStaticExponentOfOneKey(); } catch { return this.CreateDynamicExponentOfOneKey(); } } private int CreateStaticExponentOfOneKey() { int phKey = 0; if (SspiProvider.CryptImportKey(this.m_Provider, ExponentOfOne, ExponentOfOne.Length, 0, 1, ref phKey) == 0) { throw new SecurityException("Could not import modified key."); } return phKey; } public void Dispose() { if (this.m_Handle != 0) { SspiProvider.CryptDestroyKey(this.m_Handle); } if (this.m_ExponentOfOne != 0) { SspiProvider.CryptDestroyKey(this.m_ExponentOfOne); } if (this.m_OwnsProvider && (this.m_Provider != 0)) { SspiProvider.CryptReleaseContext(this.m_Provider, 0); } this.m_Handle = this.m_ExponentOfOne = this.m_Provider = 0; try { GC.SuppressFinalize(this); } catch { } } ~SymmetricKey() { this.Dispose(); } private int GetPaddingMode(PaddingMode mode) { if (mode == PaddingMode.PKCS7) { return 1; } return 3; } private int KeyFromBytes(int provider, CryptoAlgorithm algorithm, byte[] key) { int dwFlags = 1; int pbData = 0; int num4 = 0; int ofs = 0; int phKey = 0; int num8 = 0; IntPtr algid = new IntPtr((int) algorithm); IntPtr zero = IntPtr.Zero; IntPtr ptr = IntPtr.Zero; IntPtr ptr4 = IntPtr.Zero; try { int size; bool flag = false; ptr4 = Marshal.AllocHGlobal((int) (0x54 + IntPtr.Size)); do { size = 0x54 + IntPtr.Size; if (SspiProvider.CryptGetProvParam(provider, 0x16, ptr4, ref size, dwFlags) == 0) { break; } dwFlags = 0; if (Marshal.ReadIntPtr(ptr4) == algid) { flag = true; } } while (!flag); if (!flag) { throw new SecurityException("CSP does not support selected algorithm."); } if (SspiProvider.CryptGenKey(provider, algid, 0, ref phKey) == 0) { throw new SecurityException("Cannot generate temporary key."); } size = 4; if (SspiProvider.CryptGetKeyParam(phKey, 9, ref pbData, ref size, 0) == 0) { throw new SecurityException("Cannot retrieve key parameters."); } if ((key.Length * 8) > pbData) { throw new SecurityException("Key too big."); } size = IntPtr.Size; if (SspiProvider.CryptGetKeyParam(this.m_ExponentOfOne, 7, ref zero, ref size, 0) == 0) { throw new SecurityException("Unable to get the private key's algorithm."); } size = 4; if (SspiProvider.CryptGetKeyParam(this.m_ExponentOfOne, 9, ref num4, ref size, 0) == 0) { throw new SecurityException("Unable to get the key length."); } int cb = (((num4 / 8) + IntPtr.Size) + 4) + IntPtr.Size; ptr = Marshal.AllocHGlobal(cb); PUBLICKEYSTRUC structure = new PUBLICKEYSTRUC { bType = 1, bVersion = 2, reserved = 0, aiKeyAlg = algid }; Marshal.StructureToPtr(structure, ptr, false); Marshal.WriteIntPtr(ptr, ofs = Marshal.SizeOf(structure), zero); ofs += IntPtr.Size; for (int i = 0; i < key.Length; i++) { Marshal.WriteByte(ptr, ((ofs + key.Length) - i) - 1, key[i]); } size = cb - ((((IntPtr.Size + IntPtr.Size) + 4) + key.Length) + 3); ofs += key.Length + 1; if (SspiProvider.CryptGenRandom(provider, size, new IntPtr(ptr.ToInt64() + ofs)) == 0) { throw new SecurityException("Could not generate random data."); } for (int j = 0; j < size; j++) { if (Marshal.ReadByte(ptr, ofs) == 0) { Marshal.WriteByte(ptr, ofs, 1); } ofs++; } Marshal.WriteByte(ptr, cb - 2, 2); if (SspiProvider.CryptImportKey(provider, ptr, cb, this.m_ExponentOfOne, 1, ref num8) == 0) { throw new SecurityException("Cannot import key [key has right size?]."); } } finally { if (ptr4 != IntPtr.Zero) { Marshal.FreeHGlobal(ptr4); } if (phKey != 0) { SspiProvider.CryptDestroyKey(phKey); } if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } } return num8; } public byte[] ToBytes() { byte[] buffer2; if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } IntPtr zero = IntPtr.Zero; try { int pdwDataLen = 0; if (SspiProvider.CryptExportKey(this.m_Handle, this.m_ExponentOfOne, 1, 0, IntPtr.Zero, ref pdwDataLen) == 0) { throw new SecurityException("Cannot export key."); } zero = Marshal.AllocHGlobal(pdwDataLen); if (SspiProvider.CryptExportKey(this.m_Handle, this.m_ExponentOfOne, 1, 0, zero, ref pdwDataLen) == 0) { throw new SecurityException("Cannot export key."); } int num2 = 4; int pbData = 0; if (SspiProvider.CryptGetKeyParam(this.m_Handle, 9, ref pbData, ref num2, 0) == 0) { throw new SecurityException("Cannot retrieve key parameters."); } pbData /= 8; byte[] destination = new byte[pbData]; int num4 = 4 + IntPtr.Size; num4 += IntPtr.Size; Marshal.Copy(new IntPtr(zero.ToInt64() + num4), destination, 0, destination.Length); Array.Reverse(destination); buffer2 = destination; } finally { if (zero != IntPtr.Zero) { Marshal.FreeHGlobal(zero); } } return buffer2; } public override string ToString() { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } byte[] buffer = this.ToBytes(); StringBuilder builder = new StringBuilder(buffer.Length * 2); for (int i = 0; i < buffer.Length; i++) { builder.Append(buffer[i].ToString("X2")); } return builder.ToString(); } public int FeedbackSize { get { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } int pbData = 0; int pdwDataLen = 4; if (SspiProvider.CryptGetKeyParam(this.m_Handle, 5, ref pbData, ref pdwDataLen, 0) == 0) { throw new CryptographicException("Could not get the feedback size."); } return pbData; } set { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } if (SspiProvider.CryptSetKeyParam(this.m_Handle, 5, ref value, 0) == 0) { throw new CryptographicException("Unable to set the feedback size."); } } } public int Handle { get { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } return this.m_Handle; } } public byte[] IV { get { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } int pdwDataLen = 0; if (SspiProvider.CryptGetKeyParam(this.m_Handle, 1, (byte[]) null, ref pdwDataLen, 0) == 0) { throw new CryptographicException("Could not get the IV."); } byte[] pbData = new byte[pdwDataLen]; if (SspiProvider.CryptGetKeyParam(this.m_Handle, 1, pbData, ref pdwDataLen, 0) == 0) { throw new CryptographicException("Could not get the IV."); } return pbData; } set { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } if (value == null) { throw new ArgumentNullException(); } if (SspiProvider.CryptSetKeyParam(this.m_Handle, 1, value, 0) == 0) { throw new CryptographicException("Unable to set the initialization vector."); } } } public CipherMode Mode { get { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } int pbData = 0; int pdwDataLen = 4; if (SspiProvider.CryptGetKeyParam(this.m_Handle, 4, ref pbData, ref pdwDataLen, 0) == 0) { throw new CryptographicException("Could not get the cipher mode."); } return (CipherMode) pbData; } set { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } int pbData = (int) value; if (SspiProvider.CryptSetKeyParam(this.m_Handle, 4, ref pbData, 0) == 0) { throw new CryptographicException("Unable to set the cipher mode."); } } } public PaddingMode Padding { get { return this.m_PaddingMode; } set { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } int paddingMode = this.GetPaddingMode(value); if (SspiProvider.CryptSetKeyParam(this.m_Handle, 3, ref paddingMode, 0) == 0) { throw new CryptographicException("Unable to set the padding type."); } this.m_PaddingMode = value; } } public int Provider { get { if (this.m_Handle == 0) { throw new ObjectDisposedException(base.GetType().FullName); } return this.m_Provider; } } } }