Thursday, 14 December 2017
Sha384.java (Sha 384 secure 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
348
349
350
351
 
//
// 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.CryptoRandomStream.BaseCryptoRandomStream;
import com.dicelocksecurity.jdicelock.CryptoRandomStream.DefaultCryptoRandomStream;
import com.dicelocksecurity.jdicelock.TypeSizes;
 
/**
 * Sha 384 hash algorithm implementation
 *
 * @author      Angel Ferré @ DiceLock Security
 * @version     6.0.0.1
 * @since       2011-10-05
 */
public class Sha384 extends Sha512 {
 
    /**
     * Hash Algorithms Class enumerator name
     */
    private static final Hashes HASHNAME = Hashes.SHA_384;
 
    /**
     * Number of hash bits
     */
    private static final short HASHBITS = 384;
 
    /**
     * Number of hash bytes
     */
    private static final short HASHUCS = 48;
 
    /**
     * Number of hash shorts
     */
    private static final short HASHUSS = 24;
 
    /**
     * Number of hash ints
     */
    private static final short HASHULS = 12;
 
    /**
     * Number of hash unsigned 64 bits
     */
    private static final short HASH64S = 6;
 
    /**
     * Initial hash values of SHA512
     */
    private static final long[] INITIALS =
    { 0xcbbb9d5dc1059ed8L, 0x629a292a367cd507L, 0x9159015a3070dd17L, 0x152fecd8f70e5939L, 0x67332667ffc00b31L,
      0x8eb44a8768581511L, 0xdb0c2e0d64f98fa7L, 0x47b5481dbefa4fa4L };
 
    /**
     * Pointer to BaseCryptoRandomStream digest for SHA 384 hash algorithm
     */
    private BaseCryptoRandomStream workingDigest512;
 
    /**
     * Boolean pointing if meesaageDigest for SHA 512 has been created automatically
     */
    private boolean autoWorkingDigest;
 
    /**
     * Constructor, default
     */
    public Sha384() {
        super();
 
        this.workingDigest512 = null;
        this.autoWorkingDigest = false;
    }
 
    /**
     * Destructor
     */
    public void finalize() {
 
        if (autoWorkingDigest) {
            this.workingDigest512 = null;
            this.autoWorkingDigest = false;
        }
    }
 
    /**
     * Set the Working Digest  BaseCryptoRandomStream for underlying SHA512 algorithm
     *
     * @param   workDigest      BaseCryptoRandomStream working digest to compute underlying Sha 512 hash algorithm
     */
    public void SetWorkingDigest(BaseCryptoRandomStream workDigest) {
 
        this.workingDigest512 = workDigest;
    }
 
    /**
     * Get the Working Digest  BaseCryptoRandomStream for underlying SHA512 algorithm length in bits
     *
     * @return    short:    underlying Sha 512 digest length in bits
     */
    public short GetWorkingDigestBitLength() {
 
        return super.GetBitHashLength();
    }
 
    /**
     * Get the Working Digest  BaseCryptoRandomStream for underlying SHA512 algorithm length in bytes
     *
     * @return    short:    underlying Sha 512 digest length in bytes
     */
    public short GetWorkingDigestUCLength() {
 
        return super.GetUCHashLength();
    }
 
    /**
     * Get the Working Digest  BaseCryptoRandomStream for underlying SHA512 algorithm length in shorts
     *
     * @return    short:    underlying Sha 512 digest length in shorts
     */
    public short GetWorkingDigestUSLength() {
 
        return super.GetUSHashLength();
    }
 
    /**
     * Get the Working Digest  BaseCryptoRandomStream for underlying SHA512 algorithm length in ints
     *
     * @return    short:    underlying Sha 512 digest length in ints
     */
    public short GetWorkingDigestULLength() {
 
        return super.GetULHashLength();
    }
 
    /**
     * Initializes common states of Sha384 algorithm
     */
    public void Initialize() {
 
        if (this.workingDigest512 == null) {
            this.workingDigest512 = new DefaultCryptoRandomStream();
            this.workingDigest512.SetCryptoRandomStreamUC(this.GetWorkingDigestUCLength());
            this.autoWorkingDigest = true;
        }
        this.workingDigest512.Set64Position(0, Sha384.INITIALS[0]);
        this.workingDigest512.Set64Position(1, Sha384.INITIALS[1]);
        this.workingDigest512.Set64Position(2, Sha384.INITIALS[2]);
        this.workingDigest512.Set64Position(3, Sha384.INITIALS[3]);
        this.workingDigest512.Set64Position(4, Sha384.INITIALS[4]);
        this.workingDigest512.Set64Position(5, Sha384.INITIALS[5]);
        this.workingDigest512.Set64Position(6, Sha384.INITIALS[6]);
        this.workingDigest512.Set64Position(7, Sha384.INITIALS[7]);
        this.remainingBytesLength = 0;
        this.messageBitLengthHigh = 0;
        this.messageBitLengthLow = 0;
    }
 
    /**
     * Adds the BaseCryptoRandomStream to the hash
     *
     * @param     stream    bit stream to be added to the hash
     */
    public void Add(BaseCryptoRandomStream stream) {
        int startStreamByte = 0, processBytes = 0;
        int numBytes = 0;
        int i = 0;
        DefaultCryptoRandomStream partialStream = null;
 
        // 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()) > ((int)this.GetUCHashBlockLength() - 1)) {
                // Setting the point to start the current stream processed
                startStreamByte = this.GetUCHashBlockLength() - this.remainingBytesLength;
                processBytes = stream.GetUCLength() - (this.GetUCHashBlockLength() - this.remainingBytesLength);
 
                for (i = 0; i < (this.GetUCHashBlockLength() - this.remainingBytesLength); i++) {
                    this.remainingBytes.SetUCPosition(this.remainingBytesLength + i, stream.GetUCPosition(i));
                }
                // Process remaining bytes of previous streams adn 64 byte padding of current stream
                this.Compress(this.workingDigest512, this.remainingBytes);
                // Updating message byte length processed
                this.AddMessageLength(this.GetUCHashBlockLength());
                // Remaining bytes of previous stream set to 0
                this.remainingBytesLength = 0;
            } else {
                processBytes = stream.GetUCLength();
            }
        } else {
            processBytes = stream.GetUCLength();
            startStreamByte = 0;
        }
 
        partialStream = new DefaultCryptoRandomStream();
        for (numBytes = 0; processBytes > ((int)this.GetUCHashBlockLength() - 1);
             numBytes += this.GetUCHashBlockLength()) {
            // Process the chunk
            stream.GetUCSubRandomStream(partialStream, startStreamByte + numBytes, stream.GetUCLength() - startStreamByte - numBytes);
            this.Compress(this.workingDigest512, partialStream);
            // Updating message byte length processed
            this.AddMessageLength(this.GetUCHashBlockLength());
            processBytes -= this.GetUCHashBlockLength();
        }
        partialStream = null;
 
        // If remaining bytes left, they will be copied for the next added stream
        if (processBytes > 0) {
            for (i = 0; i < processBytes; i++) {
                this.remainingBytes.SetUCPosition(this.remainingBytesLength + i, stream.GetUCPosition(stream.GetUCLength() - processBytes + i));
            }
            this.remainingBytesLength += processBytes;
        }
        for (i = 0; i < this.Get64HashLength(); i++) {
            this.messageDigest.Set64Position(i, this.workingDigest512.Get64Position(i));
        }
    }
 
    /**
     * Finalizes hash
     */
    public void Finalize() {
        short i;
 
        this.remainingBytes.SetUCPosition(this.remainingBytesLength, (byte)0x80);
        if ((this.remainingBytesLength * TypeSizes.BYTE_BITS) % Sha512.HASHBLOCKBITS >= Sha512.EQUATIONMODULO) {
            for (i = 0; i < (this.GetUCHashBlockLength() - this.remainingBytesLength - 1); i++) {
                this.remainingBytes.SetUCPosition(this.remainingBytesLength + 1 + i, (byte)0);
            }
            this.Compress(this.workingDigest512, this.remainingBytes);
            this.AddMessageLength(this.remainingBytesLength);
            for (i = 0; i < this.GetUCHashBlockLength(); i++) {
                this.remainingBytes.SetUCPosition(i, (byte)0);
            }
            this.remainingBytesLength = 0;
        } else {
            for (i = 0; i < (this.GetUCHashBlockLength() - this.remainingBytesLength - 1); i++) {
                this.remainingBytes.SetUCPosition(this.remainingBytesLength + 1 + i, (byte)0);
            }
        }
        this.AddMessageLength(this.remainingBytesLength);
        this.remainingBytes.SetUCPosition(112, (byte)((((long)this.messageBitLengthHigh) >> 56) & 255));
        this.remainingBytes.SetUCPosition(113, (byte)((((long)this.messageBitLengthHigh) >> 48) & 255));
        this.remainingBytes.SetUCPosition(114, (byte)((((long)this.messageBitLengthHigh) >> 40) & 255));
        this.remainingBytes.SetUCPosition(115, (byte)((((long)this.messageBitLengthHigh) >> 32) & 255));
        this.remainingBytes.SetUCPosition(116, (byte)((((long)this.messageBitLengthHigh) >> 24) & 255));
        this.remainingBytes.SetUCPosition(117, (byte)((((long)this.messageBitLengthHigh) >> 16) & 255));
        this.remainingBytes.SetUCPosition(118, (byte)((((long)this.messageBitLengthHigh) >> 8) & 255));
        this.remainingBytes.SetUCPosition(119, (byte)((((long)this.messageBitLengthHigh)) & 255));
        this.remainingBytes.SetUCPosition(120, (byte)((((long)this.messageBitLengthLow) >> 56) & 255));
        this.remainingBytes.SetUCPosition(121, (byte)((((long)this.messageBitLengthLow) >> 48) & 255));
        this.remainingBytes.SetUCPosition(122, (byte)((((long)this.messageBitLengthLow) >> 40) & 255));
        this.remainingBytes.SetUCPosition(123, (byte)((((long)this.messageBitLengthLow) >> 32) & 255));
        this.remainingBytes.SetUCPosition(124, (byte)((((long)this.messageBitLengthLow) >> 24) & 255));
        this.remainingBytes.SetUCPosition(125, (byte)((((long)this.messageBitLengthLow) >> 16) & 255));
        this.remainingBytes.SetUCPosition(126, (byte)((((long)this.messageBitLengthLow) >> 8) & 255));
        this.remainingBytes.SetUCPosition(127, (byte)((((long)this.messageBitLengthLow)) & 255));
        this.Compress(this.workingDigest512, this.remainingBytes);
        for (i = 0; i < this.Get64HashLength(); i++) {
            this.messageDigest.Set64Position(i, this.workingDigest512.Get64Position(i));
        }
    }
 
    /**
     * Gets hash length in bits
     *
     * @return   short:   hash length in bits
     */
    public short GetBitHashLength() {
 
        return Sha384.HASHBITS;
    }
 
    /**
     * Gets hash length in bytes
     *
     * @return   short:   hash length in bytes
     */
    public short GetUCHashLength() {
 
        return Sha384.HASHUCS;
    }
 
    /**
     * Gets hash length in shorts
     *
     * @return   short:   hash length in shorts
     */
    public short GetUSHashLength() {
 
        return Sha384.HASHUSS;
    }
 
    /**
     * Gets hash length in ints
     *
     * @return   short:   hash length in ints
     */
    public short GetULHashLength() {
 
        return Sha384.HASHULS;
    }
 
    /**
     * Gets hash length in longs (64 bits)
     *
     * @return   short:   hash length in longs
     */
    public short Get64HashLength() {
 
        return Sha384.HASH64S;
    }
 
    /**
     * Gets the type of the object
     *
     * @return    Hashes:     Sha 384 enumerator name
     */
    public Hashes GetType() {
 
        return Sha384.HASHNAME;
    }
 
}