1 module crypto.utils;
2 
3 /*
4 The MIT License (MIT)
5 
6 Copyright (c) 2019 DarkRiDDeR
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 */
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) pure nothrow @nogc
36 {
37     version (D_InlineAsm_X86_64)
38     {
39         asm pure nothrow @nogc
40         {
41             mov RBX, [p];
42             mov RDX, p;
43             mov RCX, length;
44             iter:
45             xor RBX, RBX;
46             mov [RDX], RBX;
47             inc RDX;
48             loop iter;
49         }
50     }
51     else version (D_InlineAsm_X86)
52     {
53         asm pure nothrow @nogc
54         {
55             mov EBX, [p];
56             mov EDX, p;
57             mov ECX, length;
58             iter:
59             xor EBX, EBX;
60             mov [EDX], EBX;
61             inc EDX;
62             loop iter;
63         }
64     }
65     else
66     {
67         assert(0, "Only X86 and X86-64 platform supported");
68     }
69 }
70 
71 void secureZeroMemory(void[] ar) pure nothrow @nogc
72 {
73     if (ar.length == 0)
74     {
75         return;
76     }
77 
78     secureZeroMemory(ar.ptr, ar.length);
79 }
80 
81 unittest
82 {
83     auto ar = new ubyte[255];
84     auto ar2 = ar.dup;
85 
86     foreach (i, ref e; ar2)
87         e = cast(ubyte)i;
88     assert(ar != ar2);
89 
90     secureZeroMemory(ar2.ptr, ar2.length);
91     assert(ar == ar2);
92 
93 
94     uint[] i  = [0, 0, 0,  0, 0 ];
95     uint[] i2 = [8, 5, 99, 5, 99];
96     // !!! function secureZeroMemory processes data by byte. Therefore, it is wrong:
97     secureZeroMemory(i2.ptr, i2.length);
98     assert(i != i2);
99     // Need to calculate the length:
100     secureZeroMemory(i2.ptr, uint.sizeof * i2.length);
101     assert(i == i2);
102 
103     // or use a cast to type void[]
104     i2 = [8, 5, 99, 5, 99];
105     secureZeroMemory(cast(void[])i2);
106     assert(i == i2);
107 }