Friday, 15 December 2017

DiceLockSecurity Knowledge

DicelockSecurity (version 9.X)

Apple Mac OS X

XDiceLock 8.0.0.1
Previous Versions
md2.cpp (Message Digest 2 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

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
 
//
//  md2.cpp
//  XDiceLockCipher
//
//  Created by svp on 11/5/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
 
//
// Creator:    http://www.dicelocksecurity.com
// Version:    vers.8.0.0.1
//
// Copyright (C) 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 <memory.h>
#include "defaultCryptoRandomStream.h"
#include "md2.h"
 
 
namespace DiceLockSecurity {
  
    namespace Hash {
        
        // Hash Algorithms Class enumerator name
        const Hashes Md2::hashName = MD_2;
        
        // Number of hash bits
        const unsigned short int Md2::hashBits = MD2_DIGESTBITS;
        // Number of hash unsigned chars
        const unsigned short int Md2::hashUCs = MD2_DIGESTUCHARS;
        // Number of hash unsigned short ints
        const unsigned short int Md2::hashUSs = MD2_DIGESTUSHORTS;
        // Number of hash unsigned ints
        const unsigned short int Md2::hashULs = MD2_DIGESTULONGS;
        
        // Permutation of 0..255 constructed from the digits of pi.
        const unsigned int Md2::S[256] = {
            41,  46,  67, 201, 162, 216, 124,   1,  61,  54,  84, 161, 236, 240,   6,  19,
            98, 167,   5, 243, 192, 199, 115, 140, 152, 147,  43, 217, 188,  76, 130, 202,
            30, 155,  87,  60, 253, 212, 224,  22, 103,  66, 111,  24, 138,  23, 229,  18,
            190,  78, 196, 214, 218, 158, 222,  73, 160, 251, 245, 142, 187,  47, 238, 122,
            169, 104, 121, 145,  21, 178,   7,  63, 148, 194,  16, 137,  11,  34,  95,  33,
            128, 127,  93, 154,  90, 144,  50,  39,  53,  62, 204, 231, 191, 247, 151,   3,
            255,  25,  48, 179,  72, 165, 181, 209, 215,  94, 146,  42, 172,  86, 170, 198,
            79, 184,  56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116,   4, 241,
            69, 157, 112,  89, 100, 113, 135,  32, 134,  91, 207, 101, 230,  45, 168,   2,
            27,  96,  37, 173, 174, 176, 185, 246,  28,  70,  97, 105,  52,  64, 126,  15,
            85,  71, 163,  35, 221,  81, 175,  58, 195,  92, 249, 206, 186, 197, 234,  38,
            44,  83,  13, 110, 133,  40, 132,   9, 211, 223, 205, 244,  65, 129,  77,  82,
            106, 220,  55, 200, 108, 193, 171, 250,  36, 225, 123,   8,  12, 189, 177,  74,
            120, 136, 149, 139, 227,  99, 232, 109, 233, 203, 213, 254,  59,   0,  29,  57,
            242, 239, 183,  14, 102,  88, 208, 228, 166, 119, 114, 248, 235, 117,  75,  10,
            49,  68,  80, 180, 143, 237,  31,  26, 219, 153, 141,  51, 159,  17, 131,  20
        };
        
        // Handles BaseCryptoRandomStream on input
        void Md2::Transform(BaseCryptoRandomStream* block, unsigned int offset) {
            unsigned int i, j, t;
            
            for ( i = 0; i < MD2_BLOCK_LENGTH; i++ ) {
                this->X[16 + i] = block->GetUCPosition(offset + i) & 0xFF;
                this->X[32 + i] = this->X[i] ^ this->X[16 + i];
            }
            // Encrypt block (18 rounds)
            t = 0;
            for ( i = 0; i < 18; i++ ) {
                for ( j = 0; j < 48; j++ ) {
                    t = this->X[j] ^= this->S[t];
                }
                t = (t + i) & 0xFF;
            }
            // update checksum
            t = this->checksum[MD2_BLOCK_LENGTH - 1];
            for ( i = 0; i < MD2_BLOCK_LENGTH; i++ ) {
                t = this->checksum[i] ^= this->S[(block->GetUCPosition(offset + i) & 0xFF) ^ t];
            }
        }
        
        // Handles unsigned char array on input
        void Md2::Transform(unsigned char* block) {
            unsigned int i, j, t;
            
            for ( i = 0; i < MD2_BLOCK_LENGTH; i++ ) {
                this->X[16 + i] = block[i] & 0xFF;
                this->X[32 + i] = this->X[i] ^ this->X[16 + i];
            }
            // Encrypt block (18 rounds)
            t = 0;
            for ( i = 0; i < 18; i++ ) {
                for ( j = 0; j < 48; j++ ) {
                    t = this->X[j] ^= this->S[t];
                }
                t = (t + i) & 0xFF;
            }
            // update checksum
            t = this->checksum[MD2_BLOCK_LENGTH - 1];
            for ( i = 0; i < MD2_BLOCK_LENGTH; i++ ) {
                t = this->checksum[i] ^= this->S[(block[i] & 0xFF) ^ t];
            }
        }
        
        // Handles unsigned int array on input
        void Md2::Transform (unsigned int* block) {
            unsigned int i, j, t;
            
            for ( i = 0; i < MD2_BLOCK_LENGTH; i++ ) {
                this->X[16 + i] = block[i] & 0xFF;
                this->X[32 + i] = this->X[i] ^ this->X[16 + i];
            }
            // Encrypt block (18 rounds)
            t = 0;
            for ( i = 0; i < 18; i++ ) {
                for ( j = 0; j < 48; j++ ) {
                    t = this->X[j] ^= this->S[t];
                }
                t = (t + i) & 0xFF;
            }
            // update checksum
            t = this->checksum[MD2_BLOCK_LENGTH - 1];
            for ( i = 0; i < MD2_BLOCK_LENGTH; i++ ) {
                t = this->checksum[i] ^= this->S[(block[i] & 0xFF) ^ t];
            }
        }
        
        // Constructor, default 
        Md2::Md2() {
            
            this->Initialize();
        }
        
        // Destructor
        Md2::~Md2() {
            
            this->Initialize();
        }
        
        // Initializes common states of Md2 algorithm
        void Md2::Initialize(void) {
            unsigned short int i;
            
            this->count = 0;
            for ( i = 0; i < MD2_BLOCK_LENGTH; i++ ) {
                this->X[i] = 0;
                this->checksum[i] = 0;
            }
        }
        
        // Adds the BaseCryptoRandomStream stream to the hash
        void Md2::Add(BaseCryptoRandomStream* stream) {
            unsigned int i, j, partLen;
            
            // compute number of bytes still unhashed; ie. present in buffer
            partLen = MD2_BLOCK_LENGTH - this->count;
            i = 0;
            if ( stream->GetUCLength() >= partLen ) {
                // fill buffer and hash it
                for ( j = 0; j < partLen; j++ ) {
                    this->buffer[this->count + j] = stream->GetUCPosition(j);
                }
                this->Transform(this->buffer);
                this->count = 0;
                
                // hash as many BLOCK_LENGTH from remaining input as feasible
                for ( i = partLen; i + MD2_BLOCK_LENGTH - 1 < stream->GetUCLength(); i+= MD2_BLOCK_LENGTH )
                    this->Transform(stream, i);
            }
            // buffer remaining input
            if ( i < stream->GetUCLength() ) {
                for ( j = 0; j < stream->GetUCLength() - i; j++) {
                    this->buffer[count + j] = stream->GetUCPosition(i + j);
                }
                this->count += stream->GetUCLength() - i;    // update number of bytes
            }
        }
        
        // Finalizes hash
        void Md2::Finalize(void) {
            unsigned int i, padLen;
            
            // pad output to 0 mod 16;
            padLen = MD2_BLOCK_LENGTH - this->count;
            
            // padding is n bytes each of value n (similar to PKCS#5)
            // but we reserve an extra 16-bytes for the checksum
            for ( i = this->count; i < MD2_BLOCK_LENGTH; i++ ) {
                this->buffer[i] = (unsigned char )(padLen & 0xFF);
            }
            
            this->Transform(this->buffer);
            
            // encrypt and process checksum as the last block for this digest operation
            this->Transform(this->checksum);
            
            // cast this MD2's context (first 16 ints of X) into message digest
            for (int i = 0; i < MD2_BLOCK_LENGTH; i++) {
                this->messageDigest->SetUCPosition(i, (unsigned char)X[i]);
            }
        }
        
        // Gets hash length in bits
        unsigned short int Md2::GetBitHashLength(void) {
            
            return this->hashBits;
        }
        
        // Gets hash length in unsigned chars
        unsigned short int Md2::GetUCHashLength(void) {
            
            return this->hashUCs;
        }
        
        // Gets hash length in unsigned short ints
        unsigned short int Md2::GetUSHashLength(void) {
            
            return this->hashUSs;
        }
        
        // Gets hash length in unsigned ints
        unsigned short int Md2::GetULHashLength(void) {
            
            return this->hashULs;
        }
        
        // Gets the type of the object
        Hashes Md2::GetType(void) {
            
            return this->hashName;
        }
    }
}