Saturday, 22 July 2017

DiceLockSecurity Knowledge

DicelockSecurity (version 9.X)

Linux - Source Code

DiceLock-x 9.0.0.1
DiceChecker-x 6.0.0.1
HashDigester-x 6.0.0.1
Previous Versions
md4.cpp (Message Digest 4 hash digest algorithm 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
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
 
//
// Creator:    http://www.dicelocksecurity.com
// Version:    vers.9.0.0.1
//
// Copyright (C) 2012-2013 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.
// 
// DICELOCK IS PROTECTED BY US PATENT 7508945 AND EUROPEAN PATENT 1182777 WHERE APPLICABLE.
//
// LICENSE INFORMATION CAN BE OBTAINED AT CORPORATE WEB SITE
//
 
#include <memory.h>
#include "defaultCryptoRandomStream.h"
#include "md4.h"
 
/* Constants for MD4Transform routine.
 */
#define S11 3
#define S12 7
#define S13 11
#define S14 19
#define S21 3
#define S22 5
#define S23 9
#define S24 13
#define S31 3
#define S32 9
#define S33 11
#define S34 15
 
// F, G and H are basic MD4 functions.
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
 
// ROTATE_LEFT rotates x left n bits.
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
 
// FF, GG and HH transformations for rounds 1, 2 and 3
#define FF(a, b, c, d, x, s) { \
    (a) += F ((b), (c), (d)) + (x); \
    (a) = ROTATE_LEFT ((a), (s)); \
  }
#define GG(a, b, c, d, x, s) { \
    (a) += G ((b), (c), (d)) + (x) + (unsigned long int)0x5a827999; \
    (a) = ROTATE_LEFT ((a), (s)); \
  }
#define HH(a, b, c, d, x, s) { \
    (a) += H ((b), (c), (d)) + (x) + (unsigned long int)0x6ed9eba1; \
    (a) = ROTATE_LEFT ((a), (s)); \
  }
 
 
namespace DiceLockSecurity {
  
  namespace Hash {
 
  // Hash Algorithms Class enumerator name
  const Hashes Md4::hashName = MD_4;
 
  // Number of hash bits
  const unsigned short int Md4::hashBits = MD4_DIGESTBITS;
  // Number of hash unsigned chars
  const unsigned short int Md4::hashUCs = MD4_DIGESTUCHARS;
  // Number of hash unsigned short ints
  const unsigned short int Md4::hashUSs = MD4_DIGESTUSHORTS;
  // Number of hash unsigned long ints
  const unsigned short int Md4::hashULs = MD4_DIGESTULONGS;
 
  //  Encodes input (unsigned long int) into output (BaseCryptoRandomStream). Assumes length is a multiple of 4.
  void Md4::Encode(BaseCryptoRandomStream* output, unsigned long int* input) {
    unsigned long int i, j;
 
    for (i = 0, j = 0; j < output->GetUCLength(); i++, j += 4) {
      output->SetUCPosition(j, (unsigned char)(input[i] & 0xff));
      output->SetUCPosition(j + 1, (unsigned char)((input[i] >> 8) & 0xff));
      output->SetUCPosition(j + 2, (unsigned char)((input[i] >> 16) & 0xff));
      output->SetUCPosition(j + 3, (unsigned char)((input[i] >> 24) & 0xff));
    }
  }
 
  //  Encodes input (unsigned char) into output (BaseCryptoRandomStream). Assumes length is a multiple of 4.
  void Md4::Encode(BaseCryptoRandomStream* output, unsigned char* input) {
    unsigned long int i;
 
    for ( i = 0; i < output->GetUCLength(); i++ ) {
      output->SetUCPosition(i, input[i]);
    }
  }
 
  //  Encodes input (unsigned long int) into output (unsigned char). Assumes length is a multiple of 4.
  void Md4::Encode(unsigned char* output, unsigned long int* input, unsigned long int length) {
    unsigned long int i, j;
 
    for (i = 0, j = 0; j < length; i++, j += 4) {
      output[j] = (unsigned char)(input[i] & 0xff);
      output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff);
      output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff);
      output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff);
    }
  }
 
  //  Decodes input (BaseCryptoRandomStream) into output (unsigned long int). Assumes length is a multiple of 4.
  void Md4::Decode(unsigned long int* output, BaseCryptoRandomStream* input) {
    unsigned long int i, j;
 
    for (i = 0, j = 0; j < input->GetUCLength(); i++, j += 4)
      output[i] = ((unsigned long int)input->GetUCPosition(j)) | (((unsigned long int)input->GetUCPosition(j + 1)) << 8) | (((unsigned long int)input->GetUCPosition(j + 2)) << 16) | (((unsigned long int)input->GetUCPosition(j + 3)) << 24);
  }
 
  //  Decodes input (unsigned char) into output (unsigned long int). Assumes length is a multiple of 4.
  void Md4::Decode(unsigned long int* output, unsigned char* input, unsigned long int length) {
    unsigned long int i, j;
 
    for (i = 0, j = 0; j < length; i++, j += 4)
      output[i] = ((unsigned long int)input[j]) | (((unsigned long int)input[j + 1]) << 8) | (((unsigned long int)input[j + 2]) << 16) | (((unsigned long int)input[j + 3]) << 24);
  }
 
    // Transforms context based on 512 bits from input
  void Md4::Transform(BaseCryptoRandomStream* stream) {
    unsigned long int a, b, c, d;
    unsigned long int x[16];
 
    a = this->state[0];
    b = this->state[1];
    c = this->state[2];
    d = this->state[3];
 
    this->Decode(x, stream);
 
    // Round 1
    FF (a, b, c, d, x[ 0], S11); /* 1 */
    FF (d, a, b, c, x[ 1], S12); /* 2 */
    FF (c, d, a, b, x[ 2], S13); /* 3 */
    FF (b, c, d, a, x[ 3], S14); /* 4 */
    FF (a, b, c, d, x[ 4], S11); /* 5 */
    FF (d, a, b, c, x[ 5], S12); /* 6 */
    FF (c, d, a, b, x[ 6], S13); /* 7 */
    FF (b, c, d, a, x[ 7], S14); /* 8 */
    FF (a, b, c, d, x[ 8], S11); /* 9 */
    FF (d, a, b, c, x[ 9], S12); /* 10 */
    FF (c, d, a, b, x[10], S13); /* 11 */
    FF (b, c, d, a, x[11], S14); /* 12 */
    FF (a, b, c, d, x[12], S11); /* 13 */
    FF (d, a, b, c, x[13], S12); /* 14 */
    FF (c, d, a, b, x[14], S13); /* 15 */
    FF (b, c, d, a, x[15], S14); /* 16 */
 
    /* Round 2 */
    GG (a, b, c, d, x[ 0], S21); /* 17 */
    GG (d, a, b, c, x[ 4], S22); /* 18 */
    GG (c, d, a, b, x[ 8], S23); /* 19 */
    GG (b, c, d, a, x[12], S24); /* 20 */
    GG (a, b, c, d, x[ 1], S21); /* 21 */
    GG (d, a, b, c, x[ 5], S22); /* 22 */
    GG (c, d, a, b, x[ 9], S23); /* 23 */
    GG (b, c, d, a, x[13], S24); /* 24 */
    GG (a, b, c, d, x[ 2], S21); /* 25 */
    GG (d, a, b, c, x[ 6], S22); /* 26 */
    GG (c, d, a, b, x[10], S23); /* 27 */
    GG (b, c, d, a, x[14], S24); /* 28 */
    GG (a, b, c, d, x[ 3], S21); /* 29 */
    GG (d, a, b, c, x[ 7], S22); /* 30 */
    GG (c, d, a, b, x[11], S23); /* 31 */
    GG (b, c, d, a, x[15], S24); /* 32 */
 
    /* Round 3 */
    HH (a, b, c, d, x[ 0], S31); /* 33 */
    HH (d, a, b, c, x[ 8], S32); /* 34 */
    HH (c, d, a, b, x[ 4], S33); /* 35 */
    HH (b, c, d, a, x[12], S34); /* 36 */
    HH (a, b, c, d, x[ 2], S31); /* 37 */
    HH (d, a, b, c, x[10], S32); /* 38 */
    HH (c, d, a, b, x[ 6], S33); /* 39 */
    HH (b, c, d, a, x[14], S34); /* 40 */
    HH (a, b, c, d, x[ 1], S31); /* 41 */
    HH (d, a, b, c, x[ 9], S32); /* 42 */
    HH (c, d, a, b, x[ 5], S33); /* 43 */
    HH (b, c, d, a, x[13], S34); /* 44 */
    HH (a, b, c, d, x[ 3], S31); /* 45 */
    HH (d, a, b, c, x[11], S32); /* 46 */
    HH (c, d, a, b, x[ 7], S33); /* 47 */
    HH (b, c, d, a, x[15], S34); /* 48 */
 
    this->state[0] += a;
    this->state[1] += b;
    this->state[2] += c;
    this->state[3] += d;
    }
 
  // Constructor, default 
  Md4::Md4() {
 
    this->Initialize();
  }
 
  // Destructor
  Md4::~Md4() {
 
    this->Initialize();
  }
 
  // Initializes common states of Md4 algorithm
  void Md4::Initialize(void) {
    unsigned short int i;
  
    this->count[0] = this->count[1] = 0;
    this->state[0] = 0x67452301;
    this->state[1] = 0xefcdab89;
    this->state[2] = 0x98badcfe;
    this->state[3] = 0x10325476;
    for ( i = 0; i < MD4_BLOCK_LENGTH; i++ ) {
      this->buffer[i] = 0;
    }
    }
 
  // Adds the BaseCryptoRandomStream stream to the hash
  void Md4::Add(BaseCryptoRandomStream* stream) {
    unsigned long int i, j, index, partLen;
    DefaultCryptoRandomStream* bufferStream;
    DefaultCryptoRandomStream* block;
 
        // compute number of bytes still unhashed
        index = (unsigned long int)((this->count[0] >> 3) & 0x3F);
    // Update number of bits
    if ((this->count[0] += ((unsigned long int)stream->GetUCLength() << 3)) < ((unsigned long int)stream->GetUCLength() << 3)) {
      this->count[1]++;
    }
    this->count[1] += ((unsigned long int)stream->GetUCLength() >> 29);
 
        partLen = MD4_BLOCK_LENGTH - index;
 
    if (stream->GetUCLength() >= partLen) {
      for ( j = 0; j < partLen; j++ ) {
        this->buffer[index + j] = stream->GetUCPosition(j); 
      }
      bufferStream = new DefaultCryptoRandomStream(MD4_BLOCK_LENGTH * 8);
      this->Encode(bufferStream, this->buffer);
      this->Transform(bufferStream);
      delete bufferStream;
      block = new DefaultCryptoRandomStream();
      for (i = partLen; i + (MD4_BLOCK_LENGTH - 1) < stream->GetUCLength(); i+= MD4_BLOCK_LENGTH) {
        stream->GetUCSubRandomStream(block, i, MD4_BLOCK_LENGTH);
                this->Transform(block);
      }
      delete block;
            index = 0;
        }
    else {
      i = 0;
    }
        // buffer remaining input
    if (i < stream->GetUCLength()) {
      for ( j = 0; j < (stream->GetUCLength() - i); j++ ) {
        this->buffer[index + j] = stream->GetUCPosition(i + j); 
      }
    }
  }
 
  // Finalizes hash
  void Md4::Finalize(void) {
    unsigned long int index, padLen;
    DefaultCryptoRandomStream* tail;
    DefaultCryptoRandomStream* bits;
 
    bits = new DefaultCryptoRandomStream(64);
    this->Encode(bits, this->count);
        // pad output to 56 mod 64
        index = (unsigned long int)((this->count[0] >> 3) & 0x3f);
        padLen = (index < 56) ? (56 - index) : (120 - index);
 
        // padding is alwas binary 1 followed by binary 0s
    tail = new DefaultCryptoRandomStream(padLen * 8);
    tail->FillUC(0);
    tail->SetUCPosition(0, 0x80);
    this->Add(tail);
 
        // append length before final transform
    // Save number of bits
    this->Add(bits);
    this->Encode(this->messageDigest, this->state);
  
    delete bits;
    delete tail;
  }
 
  // Gets hash length in bits
  unsigned short int Md4::GetBitHashLength(void) {
 
    return this->hashBits;
  }
 
  // Gets hash length in unsigned chars
  unsigned short int Md4::GetUCHashLength(void) {
 
    return this->hashUCs;
  }
 
  // Gets hash length in unsigned short ints
  unsigned short int Md4::GetUSHashLength(void) {
 
    return this->hashUSs;
  }
 
  // Gets hash length in unsigned long ints
  unsigned short int Md4::GetULHashLength(void) {
 
    return this->hashULs;
  }
 
  // Gets the type of the object
  Hashes Md4::GetType(void) {
 
    return this->hashName;
  }
  }
}