Wednesday, 13 December 2017

DiceLockSecurity Knowledge

DicelockSecurity (version 9.X)

Windows - Source Code

DiceLock 9.0.0.1
DiceChecker 6.0.0.1
HashDigester 6.0.0.1
DiceLockTime 4.0.0.1
Previous Versions
DiceLock 8.0.0.1
arrow Previous Versions arrow DiceLock 8.0.0.1 arrow physicalCryptoRandomStream.cpp
physicalCryptoRandomStream.cpp (Physical Memory Crypto Random Stream C++ source code file) Print E-mail
Get C++ DiceLock cipher architecture source code packages of DiceLock for Microsoft Visual Studio 2013 and DiceLock-x for Linux with Test Driver Programs and Response Test Vector files to verify that both them work as expected.
DiceLock for Windows and Linux
DiceLock Security governing software licenses are Free/Libre Source Code License and Educational and Research License

DiceLock is protected by US patent 7508945 and European Patent 1182777 where applicable.

DiceLock and DiceLock logo are trademarks or registered trademarks in the EC, USA and others.

License information can be obtained at our corporate web site




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
 
//
// Creator:    http://www.dicelocksecurity.com
// Version:    vers.8.0.0.1
//
// Copyright (C) 2008-2012 DiceLock Security, LLC. All rigths reserved.
//
//                               DISCLAIMER
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// DICELOCK IS A REGISTERED TRADEMARK OR TRADEMARK OF THE OWNERS.
// 
 
#include "physicalCryptoRandomStream.h"
 
 
namespace DiceLockSecurity {
 
  namespace CryptoRandomStream {
 
  // Enumerator name
  const CryptoRandomStreams  PhysicalCryptoRandomStream::cryptoRandomStreamType = PhysicalStream;
 
  // Function to obtain the privilege of locking physical pages.
  BOOL PhysicalCryptoRandomStream::LoggedSetLockPagesPrivilege (void) {
    struct {
      DWORD Count;
      LUID_AND_ATTRIBUTES Privilege[1];
    } Info;
    HANDLE Token;
    BOOL Result;
 
    // Open the token.
    Result = OpenProcessToken ( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, & Token);
    if( Result != TRUE ) {
      return FALSE;
    }
    // Enable or disable?
    Info.Count = 1;
    Info.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
    // Get the LUID.
    Result = LookupPrivilegeValue ( NULL, SE_LOCK_MEMORY_NAME, &(Info.Privilege[0].Luid));
    if( Result != TRUE ) {
      return FALSE;
    }
    // Adjust the privilege.
    Result = AdjustTokenPrivileges ( Token, FALSE, (PTOKEN_PRIVILEGES)&Info, 0, NULL, NULL);
    // Check the result.
    if( Result != TRUE ) {
      return FALSE;
    } 
    else {
      if(GetLastError() != ERROR_SUCCESS) {
        return FALSE;
      }
    }
    CloseHandle( Token );
    return TRUE;
  }
 
  // Allocates physical memory pages, memoryLengthRequested parameter in bytes 
  void PhysicalCryptoRandomStream::AllocatePhysical(unsigned long int memoryLengthRequested) {
 
    GetSystemInfo(&(this->sSysInfo));  // fill the system information structure
    // Calculate the number of pages of memory to request.
    if ( memoryLengthRequested % this->sSysInfo.dwPageSize ) {
      this->NumberOfPages = (memoryLengthRequested / this->sSysInfo.dwPageSize) + 1;
    }
    else {
      this->NumberOfPages = memoryLengthRequested / this->sSysInfo.dwPageSize;
    }
    // Calculate the size of the user PFN array.
    this->PFNArraySize = this->NumberOfPages * sizeof (ULONG_PTR);
    this->aPFNs = (ULONG_PTR *)HeapAlloc(GetProcessHeap(), 0, this->PFNArraySize);
    if ( this->aPFNs == NULL ) {
      throw("Failed to allocate on heap.\n");
      return;
    }
    // Enable the privilege.
    if( ! this->LoggedSetLockPagesPrivilege() ) {
      return;
    }
    // Allocate the physical memory.
    this->NumberOfPagesInitial = this->NumberOfPages;
    this->bResult = AllocateUserPhysicalPages(GetCurrentProcess(), &(this->NumberOfPages), this->aPFNs);
    if( this->bResult != TRUE ) {
      throw("Cannot allocate physical pages (%u)\n", GetLastError());
      return;
    }
    if ( this->NumberOfPagesInitial != this->NumberOfPages ) {
      throw("Allocated only %p pages.\n", this->NumberOfPages);
      return;
    }
    // Reserve the virtual memory.
    this->cryptoStream = (unsigned char *)VirtualAlloc(NULL, memoryLengthRequested, MEM_RESERVE | MEM_PHYSICAL, PAGE_READWRITE);
    if( this->cryptoStream == NULL ) {
      throw("Cannot reserve virtual memory.\n");
      return;
    }
    // Map the physical memory into the physical memory window.
    this->bResult = MapUserPhysicalPages(this->cryptoStream, this->NumberOfPages, this->aPFNs);
    if( this->bResult != TRUE ) {
      throw("MapUserPhysicalPages failed (%u)\n", GetLastError());
      return;
    }
    this->autoMemory = true;
  }
 
  // Frees allocated memory pages
  void PhysicalCryptoRandomStream::FreePhysical() {
 
    // Unmap the physical memory from the physical memory window.
    this->bResult = MapUserPhysicalPages(this->cryptoStream, this->NumberOfPages, NULL);
    if( this->bResult != TRUE ) {
      throw("MapUserPhysicalPages failed (%u)\n", GetLastError());
      return;
    }
    // Free the physical pages.
    this->bResult = FreeUserPhysicalPages( GetCurrentProcess(), &(this->NumberOfPages), this->aPFNs);
    if( this->bResult != TRUE ) {
      throw("Cannot free physical pages, error %u.\n", GetLastError());
      return;
    }
    // Free virtual memory.
    this->bResult = VirtualFree(this->cryptoStream, 0, MEM_RELEASE);
    // Release the aPFNs array.
    this->bResult = HeapFree(GetProcessHeap(), 0, this->aPFNs);
    if( this->bResult == FALSE ) {
      throw("Call to HeapFree has failed (%u)\n", GetLastError());
    }
  }
 
  // Constructor, default 
  PhysicalCryptoRandomStream::PhysicalCryptoRandomStream(void) {
 
    this->cryptoStream = NULL;
    this->autoMemory = false;
    this->bitLength = 0;
    this->position = 0;
  }
 
  // Constructor, creates an empty stream with the indicated bit length 
  PhysicalCryptoRandomStream::PhysicalCryptoRandomStream(unsigned long int streamLength) {
    unsigned long int bytes;
 
    try {
      bytes = streamLength/BYTEBITS;
      if (streamLength % BYTEBITS) {
        bytes++;
      }
      this->AllocatePhysical(bytes);
      memset(this->cryptoStream, 0, bytes);
      this->bitLength = streamLength;
      this->position = 0;
    }
    catch (char* str) {
      throw str;
    }
  }
 
  // Constructor, sets the pointed stream of the indicated length in bits
  PhysicalCryptoRandomStream::PhysicalCryptoRandomStream(void* stream, unsigned long int streamLength) {
    unsigned long int bytes;
 
    if ( stream != NULL ) {
      bytes = streamLength/BYTEBITS;
      if (streamLength % BYTEBITS) {
        bytes++;
      }
      this->AllocatePhysical(bytes);
      memcpy(this->cryptoStream, stream, bytes);
      this->bitLength = streamLength;
      this->position = 0;
    }
  }
 
  // Destructor
  PhysicalCryptoRandomStream::~PhysicalCryptoRandomStream() {
 
    if ( this->autoMemory ) {
      this->FreePhysical(); 
    }
    this->cryptoStream = NULL;
    this->bitLength = 0;
    this->position = 0;
    this->autoMemory = false;
  }
 
  // Sets an empty stream with the indicated length in bits
  void PhysicalCryptoRandomStream::SetCryptoRandomStreamBit(unsigned long int streamLength) {
    unsigned long int bytes;
 
    if (this->cryptoStream == NULL) {
      try {
        bytes = streamLength/BYTEBITS;
        if (streamLength % BYTEBITS) {
          bytes++;
        }
        this->AllocatePhysical(bytes);
        memset(this->cryptoStream, 0, bytes);
        this->bitLength = streamLength;
        this->position = 0;
      }
      catch (char* str) {
        throw str;
      }
    }
  }
 
  // Sets the pointed stream of indicated length in bits
  void PhysicalCryptoRandomStream::SetCryptoRandomStreamBit(void* stream, unsigned long int streamLength) {
    unsigned long int streamLengthBytes;
 
    if ( stream != NULL ) {
      if (streamLength % BYTEBITS) {
        streamLengthBytes = streamLength/BYTEBITS + 1;
      }
      else {
        streamLengthBytes = streamLength/BYTEBITS;
      }
      this->AllocatePhysical(streamLengthBytes);
      memcpy(this->cryptoStream, stream, streamLengthBytes);
      this->bitLength = streamLength;
      this->position = 0;
    }
  }
 
  // Sets an empty stream with the indicated length in unsigned chars
  void PhysicalCryptoRandomStream::SetCryptoRandomStreamUC(unsigned long int streamLength) {
 
    if (this->cryptoStream == NULL) {
      try {
        this->AllocatePhysical(streamLength);
        memset(this->cryptoStream, 0, streamLength);
        this->bitLength = streamLength * BYTEBITS * sizeof(unsigned char);
        this->position = 0;
      }
      catch (char* str) {
        throw str;
      }
    }
  }
 
  // Sets the pointed stream of indicated length in unsigned chars
  void PhysicalCryptoRandomStream::SetCryptoRandomStreamUC(void* stream, unsigned long int streamLength) {
 
    if (stream != NULL) {
      this->AllocatePhysical(streamLength);
      memcpy(this->cryptoStream, stream, streamLength);
      this->bitLength = streamLength * BYTEBITS * sizeof(unsigned char);
      this->position = 0;
    }
  }
 
  // Sets an empty stream with the indicated length in unsigned shorts
  void PhysicalCryptoRandomStream::SetCryptoRandomStreamUS(unsigned long int streamLength) {
 
    if (this->cryptoStream == NULL) {
      try {
        this->AllocatePhysical(streamLength * sizeof(unsigned short int));
        memset(this->cryptoStream, 0, streamLength * sizeof(unsigned short int));
        this->bitLength = streamLength * BYTEBITS * sizeof(unsigned short int);
        this->position = 0;
      }
      catch (char* str) {
        throw str;
      }
    }
  }
 
  // Sets the pointed stream of indicated length in unsigned shorts
  void PhysicalCryptoRandomStream::SetCryptoRandomStreamUS(void* stream, unsigned long int streamLength) {
    unsigned long int streamLengthBytes;
 
    if (stream != NULL) {
      streamLengthBytes = streamLength * sizeof(unsigned short int);
      this->AllocatePhysical(streamLengthBytes);
      memcpy(this->cryptoStream, stream, streamLengthBytes);
      this->bitLength = streamLengthBytes * BYTEBITS;
      this->position = 0;
    }
  }
 
  // Sets an empty stream with the indicated length in unsigned longs
  void PhysicalCryptoRandomStream::SetCryptoRandomStreamUL(unsigned long int streamLength) {
 
    if (this->cryptoStream == NULL) {
      try {
        this->AllocatePhysical(streamLength * sizeof(unsigned long int));
        memset(this->cryptoStream, 0, streamLength * sizeof(unsigned long int));
        this->bitLength = streamLength * BYTEBITS * sizeof(unsigned long int);
        this->position = 0;
      }
      catch (char* str) {
        throw str;
      }
    }
  }
 
  // Sets the pointed stream of indicated length in unsigned longs
  void PhysicalCryptoRandomStream::SetCryptoRandomStreamUL(void* stream, unsigned long int streamLength) {
    unsigned long int streamLengthBytes;
 
    if (stream != NULL) {
      streamLengthBytes = streamLength * sizeof(unsigned long int);
      this->AllocatePhysical(streamLengthBytes);
      memcpy(this->cryptoStream, stream, streamLengthBytes);
      this->bitLength = streamLengthBytes * BYTEBITS;
      this->position = 0;
    }
  }
 
  // Set the pointed stream as hexadecimal string
  void PhysicalCryptoRandomStream::SetCryptoRandomStreamHexString(const char* hexStream) {
  unsigned long int lengthUC, i, streamLength;
  unsigned char byte;
 
    try {
      streamLength = strlen(hexStream);
      if ( streamLength % 2 ) {
        throw "Erroneous hexadecimal string!";
      }
      else {
        lengthUC = streamLength / 2;
        this->AllocatePhysical(lengthUC);
        if (this->cryptoStream == NULL ) 
          throw "Memory allocation failure!";
        else {
          this->autoMemory = true;
          this->bitLength = lengthUC * BYTEBITS * sizeof(unsigned char);
          this->position = 0;
          for ( i = 0; i < lengthUC; i++ ) {
            byte = 0;
            switch ( hexStream[ 2 * i ] ) {
              case '0' : byte = 0x00; break;
              case '1' : byte = 0x10; break;
              case '2' : byte = 0x20; break;
              case '3' : byte = 0x30; break;
              case '4' : byte = 0x40; break;
              case '5' : byte = 0x50; break;
              case '6' : byte = 0x60; break;
              case '7' : byte = 0x70; break;
              case '8' : byte = 0x80; break;
              case '9' : byte = 0x90; break;
              case 'A' : 
              case 'a' : byte = 0xa0; break;
              case 'B' : 
              case 'b' : byte = 0xb0; break;
              case 'C' : 
              case 'c' : byte = 0xc0; break;
              case 'D' : 
              case 'd' : byte = 0xd0; break;
              case 'E' : 
              case 'e' : byte = 0xe0; break;
              case 'F' : 
              case 'f' : byte = 0xf0; break;
              default : 
                throw "Erroneous hexadecimal string!";
                break;
            }
            switch ( hexStream[ (2 * i) + 1 ] ) {
              case '0' : byte = byte | 0x00; break;
              case '1' : byte = byte | 0x01; break;
              case '2' : byte = byte | 0x02; break;
              case '3' : byte = byte | 0x03; break;
              case '4' : byte = byte | 0x04; break;
              case '5' : byte = byte | 0x05; break;
              case '6' : byte = byte | 0x06; break;
              case '7' : byte = byte | 0x07; break;
              case '8' : byte = byte | 0x08; break;
              case '9' : byte = byte | 0x09; break;
              case 'A' : 
              case 'a' : byte = byte | 0x0a; break;
              case 'B' : 
              case 'b' : byte = byte | 0x0b; break;
              case 'C' : 
              case 'c' : byte = byte | 0x0c; break;
              case 'D' : 
              case 'd' : byte = byte | 0x0d; break;
              case 'E' : 
              case 'e' : byte = byte | 0x0e; break;
              case 'F' : 
              case 'f' : byte = byte | 0x0f; break;
              default : 
                throw "Erroneous hexadecimal string!";
                break;
            }
            this->SetUCPosition(i, byte);
          }
        }
      }
    }
    catch (char* str) {
      throw str;
    }
  }
 
  // Gets the CryptoRandomStream type of the object
  CryptoRandomStreams PhysicalCryptoRandomStream::GetCryptoRandomStreamType(void) {
 
    return this->cryptoRandomStreamType;
  }
  }
}