Sunday, 22 October 2017
BaseRipemd.java (base Ripemd 128, 160, 256 & 320 hash digest algorithm Java 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
 
//
// Creator:    http://www.dicelocksecurity.com
// Version:    vers.6.0.0.1
//
// Copyright (C) 2011-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.
//
// Environment:
// java version "1.6.0_29"
// Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
// Java HotSpot(TM) Server VM (build 20.4-b02, mixed mode)
//
 
package com.dicelocksecurity.jdicelock.Hash;
 
import com.dicelocksecurity.jdicelock.Hash.BaseHash;
import com.dicelocksecurity.jdicelock.CryptoRandomStream.BaseCryptoRandomStream;
 
/**
 * Base ripemd hash algorithm class
 *
 * @author      Angel Ferré @ DiceLock Security
 * @version     6.0.0.1
 * @since       2011-10-03
 */
public abstract class BaseRipemd extends BaseHash {
 
    protected static final short RIPEMD_DATAUCHARS = 64;
    protected static final short RIPEMD_DATAULONGS = 16;
    protected static final short RIPEMD_DATASHIFT = 4;
 
    /**
     * Number of block bits to compute hash
     */
    protected static final short HASHBLOCKBITS = 512;
 
    /**
     * Number of block unsigned chars to compute hash
     */
    protected static final short HASHBLOCKUCS = 64;
 
    /**
     * Number of block unsigned short ints to compute hash
     */
    protected static final short HASHBLOCKUSS = 32;
 
    /**
     * Number of block unsigned long ints to compute hash
     */
    protected static final short HASHBLOCKULS = 16;
 
    /**
     * Array to store remaining bytes of intermediate hash operation
     */
    protected byte[] remainingBytes = new byte[RIPEMD_DATAUCHARS];
    protected long remainingBytesLength;
 
    /**
     * Total processed message length in bytes
     */
    protected long messageByteLengthHigh;
    protected long messageByteLengthLow;
 
    /**
     * Common operation values to all RIPEMD algorithms
     */
    protected static final int CONSTANT0 = 0x00000000;
    protected static final int CONSTANT1 = 0x5A827999;
    protected static final int CONSTANT2 = 0x6ED9EBA1;
    protected static final int CONSTANT3 = 0x8F1BBCDC;
    protected static final int CONSTANT5 = 0x50A28BE6;
    protected static final int CONSTANT6 = 0x5C4DD124;
    protected static final int CONSTANT7 = 0x6D703EF3;
    protected static final int CONSTANT9 = 0x00000000;
 
    /**
     * Amounts of rotate left
     */
    protected static final short RL_0_15[] = { 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8 };
    protected static final short RL_16_31[] = { 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12 };
    protected static final short RL_32_47[] = { 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5 };
    protected static final short RL_48_63[] = { 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12 };
 
    /**
     * Amounts of prime rotate left
     */
    protected static final short PRIME_RL_0_15[] = { 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6 };
    protected static final short PRIME_RL_16_31[] = { 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11 };
    protected static final short PRIME_RL_32_47[] = { 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5 };
    protected static final short PRIME_RL_48_63[] = { 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8 };
 
    /**
     * Initial states of all Ripemd algorithms
     */
    protected static final int INISTATE0 = 0x67452301;
    protected static final int INISTATE1 = 0xEFCDAB89;
    protected static final int INISTATE2 = 0x98BADCFE;
    protected static final int INISTATE3 = 0X10325476;
 
    /**
     * Swaps final digest to accomodate to big endian coding
     */
    private void SwapFinalDigest() {
        byte tmp;
 
        for (int i = 0; i < this.messageDigest.GetULLength(); i++) {
            tmp = this.messageDigest.GetUCPosition(i * 4);
            this.messageDigest.SetUCPosition(i * 4, this.messageDigest.GetUCPosition((i * 4) + 3));
            this.messageDigest.SetUCPosition((i * 4) + 3, tmp);
            tmp = this.messageDigest.GetUCPosition((i * 4) + 1);
            this.messageDigest.SetUCPosition((i * 4) + 1, this.messageDigest.GetUCPosition((i * 4) + 2));
            this.messageDigest.SetUCPosition((i * 4) + 2, tmp);
        }
    }
 
    /**
     * Adds messaage length processed, if it is greater than unsigned long makes use
     * of another usigned long to store overflow
     *
     * @param     byteLength    number of stream bytes added to compute the hash
     */
    protected void AddMessageLength(long byteLength) {
 
        if ((this.messageByteLengthLow + byteLength) < this.messageByteLengthLow) {
            // add overflow of unsigned long
            this.messageByteLengthHigh++;
        }
        this.messageByteLengthLow += byteLength;
    }
 
    /**
     * Computes the stream of information
     *
     * @param     stream     portion of bit stream to be added to compute the hash
     */
    abstract protected void Compress(int[] stream);
 
    /**
     * Constructor, default
     */
    public BaseRipemd() {
        super();
 
        this.remainingBytesLength = 0;
        this.messageByteLengthHigh = 0;
        this.messageByteLengthLow = 0;
    }
 
    /**
     * Destructor
     */
    public void finalize() {
 
        this.remainingBytesLength = 0;
        this.messageByteLengthHigh = 0;
        this.messageByteLengthLow = 0;
    }
 
    /**
     * Initializes common states of all Ripemd algorithms
     */
    public void Initialize() {
 
        this.messageDigest.SetULPosition(0, BaseRipemd.INISTATE0);
        this.messageDigest.SetULPosition(1, BaseRipemd.INISTATE1);
        this.messageDigest.SetULPosition(2, BaseRipemd.INISTATE2);
        this.messageDigest.SetULPosition(3, BaseRipemd.INISTATE3);
        this.remainingBytesLength = 0;
        this.messageByteLengthHigh = 0;
        this.messageByteLengthLow = 0;
    }
 
    /**
     * Adds the BaseCryptoRandomStream to the hash
     *
     * @param     stream    bit stream that is added to produce the hash
     */
    public void Add(BaseCryptoRandomStream stream) {
        int[] chunk = new int[BaseRipemd.RIPEMD_DATAULONGS];
        int startStreamByte = 0, numBytes = 0, processBytes = 0;
        int i, j;
        byte[] pointerUC;
 
        // If bytes left from previous added stream, then they will be processed now with added data from new stream
        if (this.remainingBytesLength != 0) {
            if ((this.remainingBytesLength + stream.GetUCLength()) > (BaseRipemd.RIPEMD_DATAUCHARS - 1)) {
                // Setting the point to start the current stream processed
                startStreamByte = (int)(BaseRipemd.RIPEMD_DATAUCHARS - this.remainingBytesLength);
                processBytes =
                        (int)(stream.GetUCLength() - (BaseRipemd.RIPEMD_DATAUCHARS - this.remainingBytesLength));
 
                for (i = 0; i < (BaseRipemd.RIPEMD_DATAUCHARS - this.remainingBytesLength); i++) {
                    this.remainingBytes[((int)this.remainingBytesLength) + i] = stream.GetUCPosition(i);
                }
                pointerUC = this.remainingBytes;
                j = 0;
                for (i = 0; i < BaseRipemd.RIPEMD_DATAULONGS; i++) {
                    chunk[i] =
                            ((int)((pointerUC[j + 3] << 24)) | (int)(pointerUC[j + 2] << 16) | (int)(pointerUC[i + 1] <<
                                                                                                     8) |
                             (int)(pointerUC[i]));
                    j += BaseRipemd.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(BaseRipemd.RIPEMD_DATAUCHARS);
                // Remaining bytes of previous strema set to 0
                this.remainingBytesLength = 0;
            } else {
                processBytes = stream.GetUCLength();
            }
        } else {
            processBytes = stream.GetUCLength();
            startStreamByte = 0;
        }
 
        j = 0;
        for (numBytes = processBytes; numBytes > (BaseRipemd.RIPEMD_DATAUCHARS - 1);
             numBytes -= BaseRipemd.RIPEMD_DATAUCHARS) {
            for (i = 0; i < BaseRipemd.RIPEMD_DATAULONGS; i++) {
                j = startStreamByte + (processBytes - numBytes) + (i << 2);
                chunk[i] =
                        ((int)(stream.GetUCPosition(j + 3) << 24) | ((int)(stream.GetUCPosition(j + 2) << 16)) | ((int)(stream.GetUCPosition(j +
                                                                                                                                             1) <<
                                                                                                                        8)) |
                         ((int)stream.GetUCPosition(j)));
            }
            this.Compress(chunk);
            // Updating message byt length processed
            this.AddMessageLength(BaseRipemd.RIPEMD_DATAUCHARS);
        }
 
        // If remaining bytes left, they will be copied for the next added stream
        if (numBytes > 0) {
            for (i = 0; i < numBytes; i++) {
                this.remainingBytes[((int)this.remainingBytesLength) + i] =
                        stream.GetUCPosition(stream.GetUCLength() - numBytes + i);
            }
            this.remainingBytesLength += numBytes;
        }
    }
 
    /**
     * Finalize the hash
     */
    public void Finalize() {
        int i, j;
        int[] X = new int[BaseRipemd.RIPEMD_DATAULONGS];
        byte[] leftBytes;
 
        if (this.remainingBytesLength > 0) {
            this.AddMessageLength(this.remainingBytesLength);
            leftBytes = this.remainingBytes;
        } else {
            leftBytes = null;
        }
 
        for (i = 0; i < X.length; i++) {
            X[i] = 0;
        }
 
        // put bytes into X
        j = 0;
        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] ^= (leftBytes[j] << (8 * (i & 3)));
            j++;
        }
 
        // append the bit m_n == 1
        X[(int)((this.messageByteLengthLow >>> 2) % BaseRipemd.RIPEMD_DATAULONGS)] ^=
                (1 << (8 * (this.messageByteLengthLow & 3) + 7));
 
        if ((this.messageByteLengthLow % BaseRipemd.RIPEMD_DATAUCHARS) > 55) {
            // length goes to next block
            this.Compress(X);
            for (i = 0; i < BaseRipemd.RIPEMD_DATAULONGS; i++) {
                X[i] = 0;
            }
        }
 
        // append length in bits
        X[BaseRipemd.RIPEMD_DATAULONGS - 2] = (int)(this.messageByteLengthLow << 3);
        X[BaseRipemd.RIPEMD_DATAULONGS - 1] =
                (int)((this.messageByteLengthLow >>> 29) | (this.messageByteLengthHigh << 3));
        this.Compress(X);
        this.SwapFinalDigest();
    }
 
    /**
     * Gets the number of bits in the hash block to be hashed
     *
     * @return    short:    number of bits in the hash block to be hashed
     */
    public short GetBitHashBlockLength() {
 
        return BaseRipemd.HASHBLOCKBITS;
    }
 
    /**
     * Gets the number of bytes in the hash block to be hashed
     *
     * @return    short:    number of bytes in the hash block to be hashed
     */
    public short GetUCHashBlockLength() {
 
        return BaseRipemd.HASHBLOCKUCS;
    }
 
    /**
     * Gets the number of shorts in the hash block to be hashed
     *
     * @return    short:    number of shorts in the hash block to be hashed
     */
    public short GetUSHashBlockLength() {
 
        return BaseRipemd.HASHBLOCKUSS;
    }
 
    /**
     * Gets the number of unsigned long ints in the hash block to be hashed
     *
     * @return    short:    number of ints in the hash block to be hashed
     */
    public short GetULHashBlockLength() {
 
        return BaseRipemd.HASHBLOCKULS;
    }
}
v