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
69         assert(0, "Only X86 and X86-64 platform supported");
70 }
71 
72 void secureZeroMemory (void[] ar)
73 pure nothrow @nogc
74 {
75     if (ar.length == 0)
76         return;
77 
78     secureZeroMemory(ar.ptr, ar.length);
79 }
80 
81 
82 unittest
83 {
84     auto ar = new ubyte[255];
85     auto ar2 = ar.dup;
86 
87     foreach (i, ref e; ar2)
88         e = cast(ubyte)i;
89     assert(ar != ar2);
90 
91     secureZeroMemory(ar2.ptr, ar2.length);
92     assert(ar == ar2);
93 
94 
95     uint[] i  = [0, 0, 0,  0, 0 ];
96     uint[] i2 = [8, 5, 99, 5, 99];
97     // !!! function secureZeroMemory processes data by byte. Therefore, it is wrong:
98     secureZeroMemory(i2.ptr, i2.length);
99     assert(i != i2);
100     // Need to calculate the length:
101     secureZeroMemory(i2.ptr, uint.sizeof * i2.length);
102     assert(i == i2);
103 
104     // or use a cast to type void[]
105     i2 = [8, 5, 99, 5, 99];
106     secureZeroMemory(cast(void[])i2);
107     assert(i == i2);
108 }