Tuesday, 21 November 2017

DiceLockSecurity Knowledge

DicelockSecurity (version 9.X)

Apple Mac OS X

XDiceLock 8.0.0.1
Previous Versions
XDiceLock 7.0.0.1
hashSuite.cpp (Hash algorithm suite 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
 
//
//  hashSuite.cpp
//  XDiceLock
//
//  Created by DiceLock on 1/4/12.
//  Copyright (c) 2012 DiceLock Security, LLC. All rights reserved.
//
 
//
// Creator:    http://www.dicelocksecurity.com
// Version:    vers.7.0.0.1
//
// Copyright (c) 2009-2012 DiceLock Security, LLC. All rights 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 <stdlib.h>
#include "hashSuite.h"
 
 
namespace DiceLockSecurity {
  
    namespace Hash {
        
    // Points the first hash in the suite
    const Hashes HashSuite::firstHash = SHA_1;
        
    // Constructor, default, initializes suite 
    HashSuite::HashSuite() {
      int i;
            
      for (i=this->GetFirstHash(); i<this->GetMaximumNumberOfHashes(); i++) {
        this->suite[i] = NULL;
        this->selfCreatedHash[i] = false;
      }
      this->instantiatedHashes = 0;
    }
        
    // Destructor
    HashSuite::~HashSuite() {
      int i;
            
      for (i=this->GetFirstHash(); i<this->GetMaximumNumberOfHashes(); i++) {
        if (this->suite[i] != NULL) {
          delete this->suite[i];
          this->suite[i] = NULL;
            this->selfCreatedHash[i] = false;
        }
      }
      this->instantiatedHashes = 0;
    }
        
    // ADDING HASHES
        
    // Adds a random test to the suite
    void HashSuite::Add(BaseHash* hash) {
            
      if (hash != NULL) {
        this->suite[hash->GetType()] = hash;
        this->selfCreatedHash[hash->GetType()] = false;
        this->instantiatedHashes++;
      }
    }
        
    // Creates and adds a random test to the suite based in the enumerated random tests
    void HashSuite::Add(Hashes hash) {
            
      switch (hash) {
        case SHA_1: 
          if (this->suite[SHA_1] == NULL) {
            this->suite[SHA_1] = new Sha1();
            this->instantiatedHashes++;
          }
          break;
        case SHA_224: 
          if (this->suite[SHA_224] == NULL) {
            this->suite[SHA_224] = new Sha224();
            this->instantiatedHashes++;
          }
          break;
        case SHA_256: 
          if (this->suite[SHA_256] == NULL) {
            this->suite[SHA_256] = new Sha256();
            this->instantiatedHashes++;
          }
          break;
        case SHA_384: 
          if (this->suite[SHA_384] == NULL) {
            this->suite[SHA_384] = new Sha384();
            this->instantiatedHashes++;
          }
          break;
        case SHA_512: 
          if (this->suite[SHA_512] == NULL) {
            this->suite[SHA_512] = new Sha512();
            this->instantiatedHashes++;
          }
          break;
        case RIPEMD_128: 
          if (this->suite[RIPEMD_128] == NULL) {
            this->suite[RIPEMD_128] = new Ripemd128();
            this->instantiatedHashes++;
          }
          break;
        case RIPEMD_160: 
          if (this->suite[RIPEMD_160] == NULL) {
            this->suite[RIPEMD_160] = new Ripemd160();
            this->instantiatedHashes++;
          }
          break;
        case RIPEMD_256: 
          if (this->suite[RIPEMD_256] == NULL) {
            this->suite[RIPEMD_256] = new Ripemd256();
            this->instantiatedHashes++;
          }
          break;
        case RIPEMD_320: 
          if (this->suite[RIPEMD_320] == NULL) {
            this->suite[RIPEMD_320] = new Ripemd320();
            this->instantiatedHashes++;
          }
          break;
        default:
          break;
      }
      this->selfCreatedHash[hash] = true;
    }
        
    // Creates and adds all hash algorithms to the suite 
    void HashSuite::AddAll(void) {
      int i;
            
      this->suite[SHA_1] = new Sha1();
      this->suite[SHA_224] = new Sha224();
      this->suite[SHA_256] = new Sha256();
      this->suite[SHA_384] = new Sha384();
      this->suite[SHA_512] = new Sha512();
      this->suite[RIPEMD_128] = new Ripemd128();
      this->suite[RIPEMD_160] = new Ripemd160();
      this->suite[RIPEMD_256] = new Ripemd256();
      this->suite[RIPEMD_320] = new Ripemd320();
      for (i=this->GetFirstHash(); i<this->GetMaximumNumberOfHashes(); i++) {
        this->selfCreatedHash[i] = true;
      }
      this->instantiatedHashes = NumberOfHashes;
            
    }
        
    // Creates and adds the defined hash to the suite
    void HashSuite::AddSha1(void) {
            
      this->suite[SHA_1] = new Sha1();
      this->selfCreatedHash[SHA_1] = true;
      this->instantiatedHashes++;
    }
        
    // Creates and adds the defined hash to the suite
    void HashSuite::AddSha224(void) {
            
      this->suite[SHA_224] = new Sha224();
      this->selfCreatedHash[SHA_224] = true;
      this->instantiatedHashes++;
    }
        
    // Creates and adds the defined hash to the suite
    void HashSuite::AddSha256(void) {
            
      this->suite[SHA_256] = new Sha256();
      this->selfCreatedHash[SHA_256] = true;
      this->instantiatedHashes++;
    }
        
    // Creates and adds the defined hash to the suite
    void HashSuite::AddSha384(void) {
            
      this->suite[SHA_384] = new Sha384();
      this->selfCreatedHash[SHA_384] = true;
      this->instantiatedHashes++;
    }
        
    // Creates and adds the defined hash to the suite
    void HashSuite::AddSha512(void) {
            
      this->suite[SHA_512] = new Sha512();
      this->selfCreatedHash[SHA_512] = true;
      this->instantiatedHashes++;
    }
        
    // Creates and adds the defined hash to the suite
    void HashSuite::AddRipemd128(void) {
            
      this->suite[RIPEMD_128] = new Ripemd128();
      this->selfCreatedHash[RIPEMD_128] = true;
      this->instantiatedHashes++;
    }
        
    // Creates and adds the defined hash to the suite
    void HashSuite::AddRipemd160(void) {
            
      this->suite[RIPEMD_160] = new Ripemd160();
      this->selfCreatedHash[RIPEMD_160] = true;
      this->instantiatedHashes++;
    }
        
    // Creates and adds the defined hash to the suite
    void HashSuite::AddRipemd256(void) {
            
      this->suite[RIPEMD_256] = new Ripemd256();
      this->selfCreatedHash[RIPEMD_256] = true;
      this->instantiatedHashes++;
    }
        
    // Creates and adds the defined hash to the suite
    void HashSuite::AddRipemd320(void) {
            
      this->suite[RIPEMD_320] = new Ripemd320();
      this->selfCreatedHash[RIPEMD_320] = true;
      this->instantiatedHashes++;
    }
        
    // GETTING HASH OBJECT
        
    // Gets a hash algorithm from the suite based in the enumerated hash
    BaseHash* HashSuite::GetMessageDigest(Hashes hash) {
            
      return this->suite[hash];
    }
        
    // Gets the defined hash from the suite
    Sha1* HashSuite::GetSha1(void) {
            
      return (Sha1 *)this->suite[SHA_1];
    }
        
    // Gets the defined hash from the suite
    Sha224* HashSuite::GetSha224(void) {
            
      return (Sha224 *)this->suite[SHA_224];
    }
        
    // Gets the defined hash from the suite
    Sha256* HashSuite::GetSha256(void) {
            
      return (Sha256 *)this->suite[SHA_256];
    }
        
    // Gets the defined hash from the suite
    Sha384* HashSuite::GetSha384(void) {
            
      return (Sha384 *)this->suite[SHA_384];
    }
        
    // Gets the defined hash from the suite
    Sha512* HashSuite::GetSha512(void) {
            
      return (Sha512 *)this->suite[SHA_512];
    }
        
    // Gets the defined hash from the suite
    Ripemd128* HashSuite::GetRipemd128(void) {
            
      return (Ripemd128 *)this->suite[RIPEMD_128];
    }
        
    // Gets the defined hash from the suite
    Ripemd160* HashSuite::GetRipemd160(void) {
            
      return (Ripemd160 *)this->suite[RIPEMD_160];
    }
        
    // Gets the defined hash from the suite
    Ripemd256* HashSuite::GetRipemd256(void) {
            
      return (Ripemd256 *)this->suite[RIPEMD_256];
    }
        
    // Gets the defined hash from the suite
    Ripemd320* HashSuite::GetRipemd320(void) {
            
      return (Ripemd320 *)this->suite[RIPEMD_320];
    }
        
    // REMOVING HASH ALGORITHMS
        
    // Removes a pointed hash algorithm from the suite
    void HashSuite::Remove(BaseHash* hashObject) {
      Hashes hash;
            
      hash = hashObject->GetType();
      if ((this->suite[hash] != NULL) && (this->suite[hash] == hashObject)) {
        if (this->selfCreatedHash[hash]) {
          delete this->suite[hash];
        }
        this->suite[hash] = NULL;
        this->selfCreatedHash[hash] = false;
        this->instantiatedHashes--;
      }
    }
        
    // Removes a hash algorithm from the suite based in the enumerated hash algorithm
    void HashSuite::Remove(Hashes hash) {
            
      if (this->suite[hash] != NULL) {
        if (this->selfCreatedHash[hash]) {
          delete this->suite[hash];
        }
        this->suite[hash] = NULL;
        this->selfCreatedHash[hash] = false;
        this->instantiatedHashes--;
      }
    }
        
    // Removes all hash algorithms from the suite
    void HashSuite::RemoveAll(void) {
      int i;
            
      for (i=this->GetFirstHash(); i<this->GetMaximumNumberOfHashes(); i++) {
        if (this->suite[i] != NULL) {
          if (this->selfCreatedHash[i]) {
            delete this->suite[i];
          }
          this->suite[i] = NULL;
            this->selfCreatedHash[i] = false;
        }
      }
      this->instantiatedHashes = 0;
    }
        
    // Removes the defined hash from the suite
    void HashSuite::RemoveSha1(void) {
            
      if (this->suite[SHA_1] != NULL) {
        if (this->selfCreatedHash[SHA_1]) {
          delete this->suite[SHA_1];
        }
        this->suite[SHA_1] = NULL;
          this->selfCreatedHash[SHA_1] = false;
        this->instantiatedHashes--;
      }
    }
        
    // Removes the defined hash from the suite
    void HashSuite::RemoveSha224(void) {
            
      if (this->suite[SHA_224] != NULL) {
        if (this->selfCreatedHash[SHA_224]) {
          delete this->suite[SHA_224];
        }
        this->suite[SHA_224] = NULL;
          this->selfCreatedHash[SHA_224] = false;
        this->instantiatedHashes--;
      }
    }
        
    // Removes the defined hash from the suite
    void HashSuite::RemoveSha256(void) {
            
      if (this->suite[SHA_256] != NULL) {
        if (this->selfCreatedHash[SHA_256]) {
          delete this->suite[SHA_256];
        }
        this->suite[SHA_256] = NULL;
          this->selfCreatedHash[SHA_256] = false;
        this->instantiatedHashes--;
      }
    }
        
    // Removes the defined hash from the suite
    void HashSuite::RemoveSha384(void) {
            
      if (this->suite[SHA_384] != NULL) {
        if (this->selfCreatedHash[SHA_384]) {
          delete this->suite[SHA_384];
        }
        this->suite[SHA_384] = NULL;
          this->selfCreatedHash[SHA_384] = false;
        this->instantiatedHashes--;
      }
    }
        
    // Removes the defined hash from the suite
    void HashSuite::RemoveSha512(void) {
            
      if (this->suite[SHA_512] != NULL) {
        if (this->selfCreatedHash[SHA_512]) {
          delete this->suite[SHA_512];
        }
        this->suite[SHA_512] = NULL;
          this->selfCreatedHash[SHA_512] = false;
        this->instantiatedHashes--;
      }
    }
        
    // Removes the defined hash from the suite
    void HashSuite::RemoveRipemd128(void) {
            
      if (this->suite[RIPEMD_128] != NULL) {
        if (this->selfCreatedHash[RIPEMD_128]) {
          delete this->suite[RIPEMD_128];
        }
        this->suite[RIPEMD_128] = NULL;
          this->selfCreatedHash[RIPEMD_128] = false;
        this->instantiatedHashes--;
      }
    }
        
    // Removes the defined hash from the suite
    void HashSuite::RemoveRipemd160(void) {
            
      if (this->suite[RIPEMD_160] != NULL) {
        if (this->selfCreatedHash[RIPEMD_160]) {
          delete this->suite[RIPEMD_160];
        }
        this->suite[RIPEMD_160] = NULL;
          this->selfCreatedHash[RIPEMD_160] = false;
        this->instantiatedHashes--;
      }
    }
        
    // Removes the defined hash from the suite
    void HashSuite::RemoveRipemd256(void) {
            
      if (this->suite[RIPEMD_256] != NULL) {
        if (this->selfCreatedHash[RIPEMD_256]) {
          delete this->suite[RIPEMD_256];
        }
        this->suite[RIPEMD_256] = NULL;
          this->selfCreatedHash[RIPEMD_256] = false;
        this->instantiatedHashes--;
      }
    }
        
    // Removes the defined hash from the suite
    void HashSuite::RemoveRipemd320(void) {
            
      if (this->suite[RIPEMD_320] != NULL) {
        if (this->selfCreatedHash[RIPEMD_320]) {
          delete this->suite[RIPEMD_320];
        }
        this->suite[RIPEMD_320] = NULL;
          this->selfCreatedHash[RIPEMD_320] = false;
        this->instantiatedHashes--;
      }
    }
        
    // PERFORMING HASH
        
    // Performs the hash algorithms of BaseCryptoRandomStream with all instantiated hash 
    void HashSuite::Hash(BaseCryptoRandomStream* stream) {
      int i;
      
      this->Initialize();
      for (i=this->GetFirstHash(); i<this->GetMaximumNumberOfHashes(); i++) {
        if (this->suite[i] != NULL) {
          this->suite[i]->Add(stream); 
          this->suite[i]->Finalize(); 
        }
      }
    }
        
    // INITIALIZE SUITE
        
    // Initializes all hash algorithms in the suite
    void HashSuite::Initialize(void) {
      int i;
      
      for (i=this->GetFirstHash(); i<this->GetMaximumNumberOfHashes(); i++) {
        if (this->suite[i] != NULL) {
          this->suite[i]->Initialize();
        }
      }
    }
        
    // ADDS STREAM TO THE SUITE
        
    // Adds BaseCryptoRandomStream stream to hash algorithms in the suite
    void HashSuite::Add(BaseCryptoRandomStream* stream) {
      int i;
      
      for (i=this->GetFirstHash(); i<this->GetMaximumNumberOfHashes(); i++) {
        if (this->suite[i] != NULL) {
          this->suite[i]->Add(stream);
        }
      }
    }
        
    // FINALIZE THE SUITE
        
    // Finalize hash algorithms in the suite
    void HashSuite::Finalize(void) {
      int i;
      
      for (i=this->GetFirstHash(); i<this->GetMaximumNumberOfHashes(); i++) {
        if (this->suite[i] != NULL) {
          this->suite[i]->Finalize();
        }
      }
    }
        
    // GETTING SUITE INFORMATION
        
    // Gets the number of hash algorithms that contains the suite
    unsigned int HashSuite::GetInstantiatedHashes(void) {
            
      return this->instantiatedHashes;
    }
        
    // Indicates if a hash algorithm object exists in the suite
    bool HashSuite::Exist(Hashes hash) {
            
      return (this->suite[hash] != NULL);
    }
        
    // Gets the first hash algorithm in the HashSuite
    Hashes HashSuite::GetFirstHash(void) {
            
      return this->firstHash;
    }
        
    // Gets the number of hash algorithms that can be used in the HahsSuite
    Hashes HashSuite::GetMaximumNumberOfHashes(void) {
            
      return NumberOfHashes;
    }
  }
}