1 /* 2 The MIT License (MIT) 3 4 Copyright (c) 2019 DarkRiDDeR 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in all 14 copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 SOFTWARE. 23 */ 24 25 module zero_memory; 26 27 /** 28 * Fills a block of memory with zeros. It is designed to be a more secure version of ZeroMemory. 29 * 30 * !!! function secureZeroMemory processes data by byte. 31 * 32 * Use this function instead of ZeroMemory when you want to ensure that your data will be overwritten promptly, 33 * as some compilers can optimize a call to ZeroMemory by removing it entirely. 34 */ 35 void secureZeroMemory (void *p, in size_t length) 36 pure nothrow @nogc 37 { 38 version (D_InlineAsm_X86_64) 39 { 40 asm 41 pure nothrow @nogc 42 { 43 mov RBX, [p]; 44 mov RDX, p; 45 mov RCX, length; 46 iter: 47 xor RBX, RBX; 48 mov [RDX], RBX; 49 inc RDX; 50 loop iter; 51 } 52 } 53 else version (D_InlineAsm_X86) 54 { 55 asm 56 pure nothrow @nogc 57 { 58 mov EBX, [p]; 59 mov EDX, p; 60 mov ECX, length; 61 iter: 62 xor EBX, EBX; 63 mov [EDX], EBX; 64 inc EDX; 65 loop iter; 66 } 67 } 68 else version (LDC) 69 { 70 import ldc.intrinsics : llvm_memset; 71 llvm_memset(p, 0, length, true); // The "true" means volatile. 72 } 73 else 74 { 75 static if (__VERSION__ >= 2089) 76 import core..volatile : volatileStore; 77 else 78 import core.bitop : volatileStore; 79 static void zero(void* p, size_t remaining) @nogc nothrow 80 { 81 for (; remaining != 0 && (cast(size_t) p) % size_t.alignof != 0; ++p, --remaining) 82 volatileStore(cast(ubyte*) p, ubyte(0)); 83 for (; remaining >= size_t.sizeof; p += size_t.sizeof, remaining -= size_t.sizeof) 84 volatileStore(cast(size_t*) p, size_t(0)); 85 for (; remaining != 0; ++p, --remaining) 86 volatileStore(cast(ubyte*) p, ubyte(0)); 87 } 88 // Workaround because volatileStore is not annotated "pure". 89 (cast(void function(void*, size_t) @nogc nothrow pure) &zero)(p, length); 90 } 91 } 92 93 void secureZeroMemory (void[] ar) 94 pure nothrow @nogc 95 { 96 if (ar.length == 0) 97 return; 98 99 secureZeroMemory(ar.ptr, ar.length); 100 } 101 102 103 unittest 104 { 105 auto ar = new ubyte[255]; 106 auto ar2 = ar.dup; 107 108 foreach (i, ref e; ar2) 109 e = cast(ubyte)i; 110 assert(ar != ar2); 111 112 secureZeroMemory(ar2.ptr, ar2.length); 113 assert(ar == ar2); 114 115 116 uint[] i = [0, 0, 0, 0, 0 ]; 117 uint[] i2 = [8, 5, 99, 5, 99]; 118 // !!! function secureZeroMemory processes data by byte. Therefore, it is wrong: 119 secureZeroMemory(i2.ptr, i2.length); 120 assert(i != i2); 121 // Need to calculate the length: 122 secureZeroMemory(i2.ptr, uint.sizeof * i2.length); 123 assert(i == i2); 124 125 // or use a cast to type void[] 126 i2 = [8, 5, 99, 5, 99]; 127 secureZeroMemory(cast(void[])i2); 128 assert(i == i2); 129 }