internal void Free() { MarshalExtensions.ZeroMemory(hHash, HashSize); MarshalExtensions.ZeroMemory(hBlock, BlockSize); Marshal.FreeHGlobal(hHash); Marshal.FreeHGlobal(hBlock); }
public unsafe PosixProtectedMemory(int size) { if (size < 0) { throw new ArgumentException("Fatal: cannot allocate less than zero."); } ContentLength = size; if (size == 0) { size = 1; } uint pageSize = (uint)getpagesize(); if (pageSize == 0x0) { Debug.WriteLine("getpagesize() returned 0! Defaulting to 4096 Bytes ..."); pageSize = 4096; } uint requiredPages = (uint)Math.Ceiling((double)size / pageSize); allocatedSize = requiredPages * pageSize; Size = (int)allocatedSize; void *memptr = null; _ = posix_memalign(&memptr, pageSize, allocatedSize); directHandle = (IntPtr)memptr; MarshalExtensions.ZeroMemory(directHandle, Size); ContentLength = size; _ = mprotect(directHandle, allocatedSize, (int)PROT_FLAGS.PROT_NONE); }
public static bool Set(string value) { var clipboardManagedString = MarshalExtensions.NativeUtf8FromString(value); var ret = setClipboard(clipboardManagedString); return(ret); }
private static unsafe void Pbkdf2HmacSha256(ProtectedMemory password, byte *salt, int saltLength, long c, int dkLen, byte *result) { if (c < 1) { throw new ArgumentException("The count " + nameof(c) + " cannot be less than 1!"); } const int digestLength = 0x20; int blockCount = (int)Math.Ceiling((double)dkLen / digestLength); int saltBufferLength = saltLength + sizeof(int); IntPtr hSaltBuffer = Marshal.AllocHGlobal(saltBufferLength); byte * saltBuffer = (byte *)hSaltBuffer; Unsafe.CopyBlock(saltBuffer, salt, (uint)saltLength); for (int i = 1; i <= blockCount; i++) { MarshalExtensions.WriteInt32BigEndian(saltBuffer + saltLength, i); Sha256ProtectedCryptoProvider sha256 = new Sha256ProtectedCryptoProvider(); (IntPtr hU, _) = sha256.ComputeHmacUnsafe(password, saltBuffer, saltBufferLength); Unsafe.CopyBlock(result + ((i - 1) * digestLength), (void *)hU, digestLength); MarshalExtensions.ZeroFree(hU, digestLength); for (long j = 1; j < c; j++) { (IntPtr hUi, _) = sha256.ComputeHmacUnsafe(password, saltBuffer, digestLength); byte *ui = (byte *)hUi; for (int k = 0; k < digestLength; k++) { (result + ((i - 1) * digestLength))[k] ^= ui[k]; } MarshalExtensions.ZeroFree(hUi, digestLength); } } MarshalExtensions.ZeroFree(hSaltBuffer, saltBufferLength); }
public static string Get() { IntPtr clipboardManagedString = getClipboard(); var clipboardString = MarshalExtensions.StringFromNativeUtf8(clipboardManagedString); freeString(clipboardManagedString); return(clipboardString); }
internal static void PrintInt32BigEndianArray(IntPtr ptr, int size) { for (int i = 0; i < size / sizeof(int); i++) { Console.Write(MarshalExtensions.ReadInt32BigEndian(ptr + (i * sizeof(int))).ToString() + " "); } Console.WriteLine(""); }
public uint GetUniformLocation(string name) { var pname = MarshalExtensions.StringToPtrAnsi(name); var handle = NativeGl.glGetUniformLocation(this.ProgramId, pname); Marshal.FreeHGlobal(pname); return(handle); }
public ProtectedMemory ComputeHashProtected(ProtectedMemory protectedMemory, int n, int r, int p, int desiredKeyLength, byte[] salt) { IntPtr hash = Digest(protectedMemory, salt, n, r, p, desiredKeyLength); ProtectedMemory protectedResult = ProtectedMemory.Allocate(desiredKeyLength); using (ProtectedMemoryAccess access = new ProtectedMemoryAccess(protectedResult)) { MarshalExtensions.Copy(hash, 0, access.Handle, 0, desiredKeyLength); } MarshalExtensions.ZeroFree(hash, desiredKeyLength); return(protectedResult); }
public string ComputeHash(ProtectedMemory protectedMemory) { IntPtr hash = Digest(protectedMemory); byte[] resultBytes = new byte[digestLength]; Marshal.Copy(hash, resultBytes, 0, digestLength); string result = ByteArrayToString(resultBytes); MarshalExtensions.ZeroMemory(hash, digestLength); Marshal.FreeHGlobal(hash); return(result); }
public static ulong Show(double timeoutSeconds, MessageBox.Type type, string title, string message, string buttonOneLabel, string buttonTwoLabel, string buttonThreeLabel) { var titleManagedString = MarshalExtensions.NativeUtf8FromString(title); var messageManagedString = MarshalExtensions.NativeUtf8FromString(message); var buttonOneLabelManagedString = MarshalExtensions.NativeUtf8FromString(buttonOneLabel); var buttonTwoLabelManagedString = MarshalExtensions.NativeUtf8FromString(buttonTwoLabel); var buttonThreeLabelManagedString = MarshalExtensions.NativeUtf8FromString(buttonThreeLabel); ulong response = showMessageBox(timeoutSeconds, Convert.ToUInt64(type), titleManagedString, messageManagedString, buttonOneLabelManagedString, buttonTwoLabelManagedString, buttonThreeLabelManagedString); return(response); }
public unsafe (IntPtr, int) ComputeHmacUnsafe(byte *key, int keyLength, byte *message, int messageLength, bool freeKey = false) { if (keyLength > blockSize) { (IntPtr reducedKey, _) = ComputeHashUnsafe(key, keyLength); return(ComputeHmacUnsafe((byte *)reducedKey, digestLength, message, messageLength, freeKey = true)); } IntPtr hPaddedKey = Marshal.AllocHGlobal(blockSize); MarshalExtensions.ZeroMemory(hPaddedKey, blockSize); Unsafe.CopyBlock((byte *)hPaddedKey, key, (uint)keyLength); if (freeKey) { MarshalExtensions.ZeroFree((IntPtr)key, keyLength); } IntPtr hOuterKeyPadded = Marshal.AllocHGlobal(blockSize); IntPtr hInnerKeyPadded = Marshal.AllocHGlobal(blockSize); byte * outerKeyPadded = (byte *)hOuterKeyPadded; byte * innerKeyPadded = (byte *)hInnerKeyPadded; Unsafe.CopyBlock(outerKeyPadded, (void *)hPaddedKey, blockSize); Unsafe.CopyBlock(innerKeyPadded, (void *)hPaddedKey, blockSize); MarshalExtensions.ZeroFree(hPaddedKey, blockSize); for (int i = 0; i < blockSize; i++) { outerKeyPadded[i] ^= 0x5c; } for (int i = 0; i < blockSize; i++) { innerKeyPadded[i] ^= 0x36; } int innerInputLength = blockSize + messageLength; IntPtr hInnerInput = Marshal.AllocHGlobal(innerInputLength); byte * innerInput = (byte *)hInnerInput; Unsafe.CopyBlock(innerInput, innerKeyPadded, blockSize); MarshalExtensions.ZeroFree(hInnerKeyPadded, blockSize); Unsafe.CopyBlock(innerInput + blockSize, message, (uint)messageLength); (IntPtr hInnerHash, _) = ComputeHashUnsafe(innerInput, innerInputLength); MarshalExtensions.ZeroFree(hInnerInput, innerInputLength); const int inputLength = blockSize + digestLength; IntPtr hInput = Marshal.AllocHGlobal(inputLength); byte * input = (byte *)hInput; Unsafe.CopyBlock(input, outerKeyPadded, blockSize); Unsafe.CopyBlock(input + blockSize, (void *)hInnerHash, digestLength); (IntPtr hResult, int resultLength) = ComputeHashUnsafe(input, inputLength); MarshalExtensions.ZeroFree(hOuterKeyPadded, blockSize); MarshalExtensions.ZeroFree(hInnerHash, digestLength); MarshalExtensions.ZeroFree(hInput, inputLength); return(hResult, resultLength); }
public ProtectedMemory ComputeHashProtected(ProtectedMemory protectedMemory) { IntPtr pHash = Digest(protectedMemory); ProtectedMemory result = ProtectedMemory.Allocate(digestLength); using (ProtectedMemoryAccess access = new ProtectedMemoryAccess(protectedMemory)) { MarshalExtensions.Copy(pHash, 0, access.Handle, 0, digestLength); } MarshalExtensions.ZeroMemory(pHash, digestLength); Marshal.FreeHGlobal(pHash); return(result); }
public void ShaderSource(string source) { unsafe { int length = source.Length; IntPtr[] ptrArray = new IntPtr[1]; IntPtr strPtr = MarshalExtensions.StringToPtrAnsi(source); ptrArray[0] = strPtr; NativeGl.glShaderSource(this.ShaderId, 1, ptrArray, &length); Marshal.FreeHGlobal(strPtr); } }
public PosixFrobnicatedMemory(int size) { if (size < 0) { throw new ArgumentException("Fatal: cannot allocate less than zero."); } ContentLength = size; if (size == 0) { size = 1; } Size = size; directHandle = Marshal.AllocHGlobal(size); MarshalExtensions.ZeroMemory(directHandle, size); Protect(); }
public string ComputeHash(ProtectedMemory protectedMemory, byte[] salt, int n, int r, int p, int desiredKeyLength) { byte[] resultBytes = new byte[desiredKeyLength]; IntPtr hash = Digest(protectedMemory, salt, n, r, p, desiredKeyLength); Marshal.Copy(hash, resultBytes, 0, desiredKeyLength); MarshalExtensions.ZeroFree(hash, desiredKeyLength); StringBuilder stringBuilder = new StringBuilder(desiredKeyLength + salt.Length + 20); stringBuilder.Append("$s2$"); stringBuilder.Append(n.ToString()).Append('$'); stringBuilder.Append(r.ToString()).Append('$'); stringBuilder.Append(p.ToString()).Append('$'); stringBuilder.Append(Convert.ToBase64String(salt)).Append('$'); stringBuilder.Append(Convert.ToBase64String(resultBytes)); return(stringBuilder.ToString()); }
internal ProgramList(IntPtr programList) { if (programList == IntPtr.Zero) { return; } _count = Native.LibVLCPlayerProgramListCount(programList); _programs = new Program?[_count]; for (var i = 0; i < _count; i++) { var program = Native.LibVLCPlayerProgramListAt(programList, (uint)i); _programs[i] = MarshalExtensions.BuildProgram(program); } Native.LibVLCPlayerProgramListDelete(programList); }
public Win32EncryptedMemory(int size) { if (size < 0) { throw new ArgumentException("Fatal: cannot allocate less than zero."); } ContentLength = size; if (size == 0) { size = 1; } uint requiredBlocks = (uint)Math.Ceiling((double)size / CRYPTPROTECTMEMORY_BLOCK_SIZE); Size = (int)(requiredBlocks * CRYPTPROTECTMEMORY_BLOCK_SIZE); directHandle = Marshal.AllocHGlobal(Size); MarshalExtensions.ZeroMemory(directHandle, Size); Protect(); }
private unsafe IntPtr Digest(int digestLength, ProtectedMemory protectedMemory) { Blake2bHashState blake2 = default; blake2.Init(digestLength, null); int length = protectedMemory.ContentLength; IntPtr hInput = Marshal.AllocHGlobal(length); using (ProtectedMemoryAccess access = new ProtectedMemoryAccess(protectedMemory)) { MarshalExtensions.Copy(access.Handle, 0, hInput, 0, length); } byte *input = (byte *)hInput; blake2.Update(input, length); MarshalExtensions.ZeroMemory(hInput, length); Marshal.FreeHGlobal(hInput); IntPtr hash = blake2.Finish(); blake2.Free(); return(hash); }
internal unsafe ScryptHashFunction(int costFactor, int blockSizeFactor, int parallelizationFactor, int desiredKeyLength) { if (desiredKeyLength < 1) { throw new ArgumentException(nameof(desiredKeyLength) + " must be larger than 0."); } n = costFactor; r = blockSizeFactor; p = parallelizationFactor; outLength = desiredKeyLength; blockSize = (uint)(128 * blockSizeFactor); blockSizeZeros = new byte[(int)blockSize]; allocatedSize = p * (sizeof(byte *) + (int)blockSize); hB = Marshal.AllocHGlobal(allocatedSize); MarshalExtensions.ZeroMemory(hB, allocatedSize); B = (byte **)hB; for (int i = 0; i < p; i++) { long offset = (p * sizeof(byte *)) + (i * blockSize); B[i] = (byte *)B + offset; } }
internal void Init(int digestLength, ProtectedMemory key) { uint keyLength = key == null ? 0u : (uint)(key?.ContentLength); hHash = Marshal.AllocHGlobal(HashSize); hash = (ulong *)hHash; hBlock = Marshal.AllocHGlobal(BlockSize); block = (byte *)hBlock; if (digestLength == 0 || (uint)digestLength > HashSize) { throw new ArgumentOutOfRangeException(nameof(digestLength), "Value must be between 1 and " + HashSize); } if (keyLength > MaxKeyBytes) { throw new ArgumentException("Key must be between 0 and " + MaxKeyBytes + " bytes in length", nameof(key)); } outlen = (uint)digestLength; fixed(byte *pIv = iv) { Unsafe.CopyBlock(hash, pIv, HashSize); } hash[0] ^= 0x01010000u ^ (keyLength << 8) ^ outlen; if (keyLength != 0) { MarshalExtensions.ZeroMemory(hBlock, BlockSize); using (ProtectedMemoryAccess access = new ProtectedMemoryAccess(key)) { Unsafe.CopyBlockUnaligned(block, (byte *)access.Handle, keyLength); } c = BlockSize; } }
public Win32ProtectedMemory(int size) { if (size < 0) { throw new ArgumentException("Fatal: cannot allocate less than zero."); } ContentLength = size; if (size == 0) { size = 1; } GetNativeSystemInfo(out SYSTEM_INFO systemInfo); uint pageSize = systemInfo.dwPageSize; uint requiredPages = (uint)Math.Ceiling((double)size / pageSize); allocatedSize = (int)((requiredPages + 2) * pageSize); Size = (int)(requiredPages * pageSize); pUsableSize = new UIntPtr((uint)Size); rawHandle = Marshal.AllocHGlobal(allocatedSize); directHandle = rawHandle + (int)pageSize; MarshalExtensions.ZeroMemory(directHandle, Size); ContentLength = size; Protect(); }
public override void Free() { Unprotect(); MarshalExtensions.ZeroMemory(directHandle, Size); Marshal.FreeHGlobal(rawHandle); }
internal unsafe IntPtr Digest(ProtectedMemory password, byte[] salt) { if (outLength == 0) { throw new InvalidOperationException("Hash not initialized."); } fixed(byte *pSalt = salt) { Pbkdf2HmacSha256(password, pSalt, salt.Length, 1, (int)blockSize * p, B[0]); } int iBlockSize = (int)blockSize; int vLength = n * iBlockSize; IntPtr hV = Marshal.AllocHGlobal(vLength); IntPtr hBuffer = Marshal.AllocHGlobal(iBlockSize); IntPtr hTempBuffer = Marshal.AllocHGlobal(64); byte * v = (byte *)hV; byte * buffer = (byte *)hBuffer; byte * tempBuffer = (byte *)hTempBuffer; for (int i = 0; i < p; i++) { /* https://en.wikipedia.org/wiki/Scrypt * Function ROMix(Block, Iterations) * * Create Iterations copies of X * X ← Block * for i ← 0 to Iterations−1 do * Vi ← X * X ← BlockMix(X) * * for i ← 0 to Iterations−1 do * j ← Integerify(X) mod Iterations * X ← BlockMix(X xor Vj) * * return X */ byte *source = B[i] + (((2 * r) - 1) * 64); for (int k = 0; k < n; k++) { Unsafe.CopyBlock(v + (k * blockSize), B[i], blockSize); Unsafe.CopyBlock(tempBuffer, source, 64u); BlockMix(B[i], v + (k * blockSize), tempBuffer); } long j; int len; for (int k = 0; k < n; k++) { uint *temp = (uint *)(B[i] + (((blockSize >> 6) - 1) * 64)); // C# uses little-endian integers by default! j = (((long)temp[1] << 32) + temp[0]) & (n - 1); len = iBlockSize; byte *d = B[i]; byte *s = v + (j * blockSize); while (len >= 8) { *(ulong *)d ^= *(ulong *)s; d += 8; s += 8; len -= 8; } if (len >= 4) { *(uint *)d ^= *(uint *)s; d += 4; s += 4; len -= 4; } if (len >= 2) { *(ushort *)d ^= *(ushort *)s; d += 2; s += 2; len -= 2; } if (len >= 1) { *d ^= *s; } Unsafe.CopyBlock(buffer, B[i], blockSize); Unsafe.CopyBlock(tempBuffer, source, 64u); BlockMix(B[i], buffer, tempBuffer); } } Marshal.Copy(blockSizeZeros, 0, hBuffer, iBlockSize); Marshal.FreeHGlobal(hBuffer); Marshal.Copy(zeros64, 0, hTempBuffer, 64); Marshal.FreeHGlobal(hTempBuffer); MarshalExtensions.ZeroFree(hV, vLength); IntPtr hOutput = Marshal.AllocHGlobal(outLength); byte * output = (byte *)hOutput; Pbkdf2HmacSha256(password, B[0], (int)blockSize * p, 1, outLength, output); return(hOutput); }
private IntPtr Digest(ProtectedMemory protectedMemory) { // convert string msg into 512-bit blocks (array of 16 32-bit integers) [§5.2.1] int contentLength = protectedMemory.ContentLength; double length = (contentLength / 4) + 3; // length (in 32-bit integers) of content length + ‘1’ + appended length int blockCount = (int)Math.Ceiling(length / 16d); // number of 16-integer (512-bit) blocks required to hold 'l' ints int allocatedSize = blockCount * 16 * sizeof(int); IntPtr messageBuffer = Marshal.AllocHGlobal(allocatedSize); MarshalExtensions.ZeroMemory(messageBuffer, allocatedSize); using (ProtectedMemoryAccess access = new ProtectedMemoryAccess(protectedMemory)) { MarshalExtensions.Copy(access.Handle, 0, messageBuffer, 0, contentLength); } // append padding Marshal.WriteByte(messageBuffer + contentLength, 0x80); IntPtr buffer = Marshal.AllocHGlobal(allocatedSize); MarshalExtensions.ZeroMemory(buffer, allocatedSize); for (int i = 0; i < blockCount; i++) { IntPtr rowPointer = messageBuffer + (i * 64); // encode 4 chars per integer (64 per block), big-endian encoding for (int j = 0; j < 16; j++) { int value = MarshalExtensions.ReadInt32BigEndian(rowPointer + (j * sizeof(int))); Marshal.WriteInt32(buffer + (sizeof(int) * ((i * 16) + j)), value); } } // zero-free message buffer MarshalExtensions.ZeroMemory(messageBuffer, allocatedSize); Marshal.FreeHGlobal(messageBuffer); // add length (in bits) into final pair of 32-bit integers (big-endian) long len = contentLength * 8; int lenHi = (int)(len >> 32); int lenLo = (int)len; Marshal.WriteInt32(buffer + allocatedSize - sizeof(long), lenHi); Marshal.WriteInt32(buffer + allocatedSize - sizeof(int), lenLo); // allocate message schedule IntPtr messageScheduleBuffer = Marshal.AllocHGlobal(msgSchedBufSize); // allocate memory for hash and copy constants. IntPtr pHash = Marshal.AllocHGlobal(digestLength); byte[] managedHash = new byte[H.Length * sizeof(uint)]; Buffer.BlockCopy(H, 0, managedHash, 0, managedHash.Length); Marshal.Copy(managedHash, 0, pHash, managedHash.Length); // HASH COMPUTATION for (int i = 0; i < blockCount; i++) { // prepare message schedule for (int j = 0; j < 16; j++) { int value = Marshal.ReadInt32(buffer + (sizeof(int) * ((i * 16) + j))); Marshal.WriteInt32(messageScheduleBuffer + (j * sizeof(int)), value); } for (int j = 16; j < 64; j++) { uint value = sigma1((uint)Marshal.ReadInt32(messageScheduleBuffer + ((j - 2) * sizeof(int)))) + (uint)Marshal.ReadInt32(messageScheduleBuffer + ((j - 7) * sizeof(int))) + sigma0((uint)Marshal.ReadInt32(messageScheduleBuffer + ((j - 15) * sizeof(int)))) + (uint)Marshal.ReadInt32(messageScheduleBuffer + ((j - 16) * sizeof(int))); Marshal.WriteInt32(messageScheduleBuffer + (j * sizeof(int)), (int)value); } // initialize working variables a, b, c, d, e, f, g, h with previous hash value uint a = (uint)Marshal.ReadInt32(pHash + (0 * sizeof(int))); uint b = (uint)Marshal.ReadInt32(pHash + (1 * sizeof(int))); uint c = (uint)Marshal.ReadInt32(pHash + (2 * sizeof(int))); uint d = (uint)Marshal.ReadInt32(pHash + (3 * sizeof(int))); uint e = (uint)Marshal.ReadInt32(pHash + (4 * sizeof(int))); uint f = (uint)Marshal.ReadInt32(pHash + (5 * sizeof(int))); uint g = (uint)Marshal.ReadInt32(pHash + (6 * sizeof(int))); uint h = (uint)Marshal.ReadInt32(pHash + (7 * sizeof(int))); // main loop for (int j = 0; j < 64; j++) { uint t1 = h + sum1(e) + Ch(e, f, g) + K[j] + (uint)Marshal.ReadInt32(messageScheduleBuffer + (j * sizeof(int))); uint t2 = sum0(a) + Maj(a, b, c); h = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; } // compute the new intermediate hash value Marshal.WriteInt32(pHash + (0 * sizeof(int)), (int)((uint)Marshal.ReadInt32(pHash + (0 * sizeof(int))) + a)); Marshal.WriteInt32(pHash + (1 * sizeof(int)), (int)((uint)Marshal.ReadInt32(pHash + (1 * sizeof(int))) + b)); Marshal.WriteInt32(pHash + (2 * sizeof(int)), (int)((uint)Marshal.ReadInt32(pHash + (2 * sizeof(int))) + c)); Marshal.WriteInt32(pHash + (3 * sizeof(int)), (int)((uint)Marshal.ReadInt32(pHash + (3 * sizeof(int))) + d)); Marshal.WriteInt32(pHash + (4 * sizeof(int)), (int)((uint)Marshal.ReadInt32(pHash + (4 * sizeof(int))) + e)); Marshal.WriteInt32(pHash + (5 * sizeof(int)), (int)((uint)Marshal.ReadInt32(pHash + (5 * sizeof(int))) + f)); Marshal.WriteInt32(pHash + (6 * sizeof(int)), (int)((uint)Marshal.ReadInt32(pHash + (6 * sizeof(int))) + g)); Marshal.WriteInt32(pHash + (7 * sizeof(int)), (int)((uint)Marshal.ReadInt32(pHash + (7 * sizeof(int))) + h)); } MarshalExtensions.Int32LittleEndianArrayToBigEndian(pHash, digestLength); // zero-free used buffers MarshalExtensions.ZeroMemory(messageScheduleBuffer, msgSchedBufSize); Marshal.FreeHGlobal(messageScheduleBuffer); MarshalExtensions.ZeroMemory(buffer, allocatedSize); Marshal.FreeHGlobal(buffer); // return pointer to computed hash (needs to be freed by caller). return(pHash); }
public void Dispose() { MarshalExtensions.ZeroMemory(hB, allocatedSize); Marshal.FreeHGlobal(hB); }
public override void Free() { MarshalExtensions.ZeroFree(directHandle, Size); }