Thursday, 23 November 2017

DiceLockSecurity Knowledge

DicelockSecurity (version 9.X)

Apple Mac OS X

XDiceLock 8.0.0.1
Previous Versions
arrow XDiceLock 8.0.0.1 arrow baseRipemd.cpp
baseRipemd.cpp (base Ripemd 128, 160, 256 & 320 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
 
//
//  baseRipemd.cpp
//  XDiceLock
//
//  Created by DiceLock on 1/4/12.
//  Copyright (c) 2009-2012 DiceLock Security, LLC. All rights reserved.
//
 
//
// Creator:    http://www.dicelocksecurity.com
// Version:    vers.8.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 <memory.h>
#include "baseRipemd.h"
 
 
namespace DiceLockSecurity {
  
    namespace Hash {
        
        // Number of data bits to compute hash
        const unsigned short int BaseRipemd::hashBlockBits = RIPEMD_BLOCKBITS;
        // Number of data unsigned chars to compute hash
        const unsigned short int BaseRipemd::hashBlockUCs = RIPEMD_BLOCKUCHARS;
        // Number of data unsigned short ints to compute hash
        const unsigned short int BaseRipemd::hashBlockUSs = RIPEMD_BLOCKUSHORTS;
        // Number of data unsigned ints to compute hash
        const unsigned short int BaseRipemd::hashBlockULs = RIPEMD_BLOCKUINTS;
        
        // Constants for all RIPEMD algorithms
        const unsigned int BaseRipemd::constant0 = 0x00000000UL;
        const unsigned int BaseRipemd::constant1 = 0x5A827999UL;
        const unsigned int BaseRipemd::constant2 = 0x6ED9EBA1UL;
        const unsigned int BaseRipemd::constant3 = 0x8F1BBCDCUL;
        const unsigned int BaseRipemd::constant5 = 0x50A28BE6UL;
        const unsigned int BaseRipemd::constant6 = 0x5C4DD124UL;
        const unsigned int BaseRipemd::constant7 = 0x6D703EF3UL;
        const unsigned int BaseRipemd::constant9 = 0x00000000UL;
        
        // Amounts of rotate left
        const unsigned short int BaseRipemd::rl_0_15[16] = {11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8};
        const unsigned short int BaseRipemd::rl_16_31[16] = {7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12};
        const unsigned short int BaseRipemd::rl_32_47[16] = {11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5};
        const unsigned short int BaseRipemd::rl_48_63[16] = {11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12};
        // Amounts of prime rotate left 
        const unsigned short int BaseRipemd::prime_rl_0_15[16] = {8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6};
        const unsigned short int BaseRipemd::prime_rl_16_31[16] = {9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11};
        const unsigned short int BaseRipemd::prime_rl_32_47[16] = {9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5};
        const unsigned short int BaseRipemd::prime_rl_48_63[16] = {15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8};
        
        // Initial states of all Ripemd algorithms
        const unsigned int BaseRipemd::inistate0 = 0x67452301;
        const unsigned int BaseRipemd::inistate1 = 0xEFCDAB89;
        const unsigned int BaseRipemd::inistate2 = 0x98BADCFE;
        const unsigned int BaseRipemd::inistate3 = 0X10325476;
        
        
        // Constructor
        BaseRipemd::BaseRipemd() {
            
            this->remainingBytesLength = 0;
            this->messageByteLengthHigh = 0;
            this->messageByteLengthLow = 0;
        }
        
        // Destructor
        BaseRipemd::~BaseRipemd() {
            
            this->remainingBytesLength = 0;
            this->messageByteLengthHigh = 0;
            this->messageByteLengthLow = 0;
        }
        
        // Initializes common states of all Ripmed algorithms  
        void BaseRipemd::Initialize() {
            
            this->messageDigest->SetULPosition(0, inistate0);
            this->messageDigest->SetULPosition(1, inistate1);
            this->messageDigest->SetULPosition(2, inistate2);
            this->messageDigest->SetULPosition(3, inistate3);
            this->remainingBytesLength = 0;
            this->messageByteLengthHigh = 0;
            this->messageByteLengthLow = 0;
        }
        
        // Computes the 64 byte chunk of stream information 
        void BaseRipemd::Add(BaseCryptoRandomStream* stream) {
            unsigned int chunk[RIPEMD_DATAUINTS];
            unsigned int startStreamByte = 0, numBytes = 0, processBytes = 0;
            unsigned int i = 0;
            unsigned char* pointerUC;
            
            // If bytes left from previous added stream, then they will be processed now with added data from new stream
            if (this->remainingBytesLength) {
                if ((this->remainingBytesLength + stream->GetUCLength()) > (RIPEMD_DATAUCHARS - 1)) {
                    // Setting the point to start the current stream processed
                    startStreamByte = RIPEMD_DATAUCHARS - this->remainingBytesLength;
                    processBytes = stream->GetUCLength() - (RIPEMD_DATAUCHARS - this->remainingBytesLength);
                    
                    memcpy(this->remainingBytes + this->remainingBytesLength, stream->GetUCAddressPosition(0), RIPEMD_DATAUCHARS - this->remainingBytesLength);
                    pointerUC = this->remainingBytes;
                    for (i = 0; i < RIPEMD_DATAUINTS; i++) {
                        chunk[i] = ((unsigned int) *((pointerUC) + 3) << 24) | ((unsigned int) *((pointerUC) + 2) << 16) | ((unsigned int) *((pointerUC) + 1) << 8) | ((unsigned int) *(pointerUC));
                        pointerUC += RIPEMD_DATASHIFT;
                    }
                    // Process remaining bytes of previous streams adn 64 byte padding of current stream
                    this->Compress(chunk);
                    // Updating message byt length processed
                    this->AddMessageLength(RIPEMD_DATAUCHARS);
                    // Remaining bytes of previous strema set to 0
                    this->remainingBytesLength = 0;
                }
                else {
                    processBytes = stream->GetUCLength();
                }
            }
            else {
                processBytes = stream->GetUCLength();
                startStreamByte = 0;
            }
            
            for (numBytes = processBytes; numBytes > (RIPEMD_DATAUCHARS - 1); numBytes -= RIPEMD_DATAUCHARS) {
                for (i = 0; i < RIPEMD_DATAUINTS; i++) {
                    pointerUC = stream->GetUCAddressPosition(startStreamByte + (processBytes - numBytes) + (i<<2));
                    chunk[i] = ((unsigned int) *((pointerUC) + 3) << 24) | ((unsigned int) *((pointerUC) + 2) << 16) | ((unsigned int) *((pointerUC) + 1) << 8) | ((unsigned int) *(pointerUC));
                }
                this->Compress(chunk);
                // Updating message byt length processed
                this->AddMessageLength(RIPEMD_DATAUCHARS); 
            }
            
            // If remaining bytes left, they will be copied for the next added stream
            if (numBytes > 0) {
                memcpy(this->remainingBytes + this->remainingBytesLength, stream->GetUCAddressPosition(stream->GetUCLength() - numBytes), numBytes);
                this->remainingBytesLength += numBytes;
            }
        }
        
        // Finalize the hash
        void BaseRipemd::Finalize(void) {
            unsigned int i, length;
            unsigned int X[RIPEMD_DATAUINTS];
            unsigned char* leftBytes;
            
            if (this->remainingBytesLength > 0) {
                this->AddMessageLength(this->remainingBytesLength);
                leftBytes = this->remainingBytes;
                
            }
            else {
                leftBytes = NULL;
            }
            length = this->messageByteLengthLow;
            
            memset(X, 0, RIPEMD_DATAUINTS*sizeof(unsigned int));
            
            /* put bytes from strptr into X */
            for (i = 0; i < (this->messageByteLengthLow&63); i++) {
                /* byte i goes into word X[i div 4] at pos.  8*(i mod 4)  */
                X[i>>2] ^= (unsigned int) *leftBytes++ << (8 * (i&3));
            }
            
            /* append the bit m_n == 1 */
            X[(this->messageByteLengthLow>>2) % RIPEMD_DATAUINTS] ^= (unsigned int)1 << (8*(this->messageByteLengthLow&3) + 7);
            
            if ((this->messageByteLengthLow % RIPEMD_DATAUCHARS) > 55) {
                /* length goes to next block */
                this->Compress(X);
                memset(X, 0, RIPEMD_DATAUINTS * sizeof(unsigned int));
            }
            
            /* append length in bits*/
            X[RIPEMD_DATAUINTS - 2] = this->messageByteLengthLow << 3;
            X[RIPEMD_DATAUINTS - 1] = (this->messageByteLengthLow >> 29) | (this->messageByteLengthHigh << 3);
            this->Compress(X);
        }
        
        // Adds messaage length in bytes processed, if it is greater than unsigned int makes use
        // of another usigned int to store overflow
        void BaseRipemd::AddMessageLength(unsigned int byteLength) {
            
            if ((this->messageByteLengthLow + byteLength) < this->messageByteLengthLow) 
                // add overflow of unsigned int
                this->messageByteLengthHigh++;
            this->messageByteLengthLow += byteLength;
        }
        
        // Gets the number of bits in the hash block to be hashed
        unsigned short int BaseRipemd::GetBitHashBlockLength(void) {
            
            return this->hashBlockBits;
        }
        
        // Gets the number of unsigned chars in the hash block to be hashed
        unsigned short int BaseRipemd::GetUCHashBlockLength(void) {
            
            return this->hashBlockUCs;
        }
        
        // Gets the number of unsigned short ints in the hash block to be hashed
        unsigned short int BaseRipemd::GetUSHashBlockLength(void) {
            
            return this->hashBlockUSs;
        }
        
        // Gets the number of unsigned ints in the hash block to be hashed
        unsigned short int BaseRipemd::GetULHashBlockLength(void) {
            
            return this->hashBlockULs;
        }
        
    }
}