Tuesday, 17 October 2017
Sha512.java (2nd part) (Sha 512 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
 
    /**
     * Computes the stream block of information
     *
     * @param   digest    BaseCryptoRandomStream hash object
     * @param   stream    bit stream to be added to hash
     */
    protected void Compress(BaseCryptoRandomStream digest, BaseCryptoRandomStream stream) {
        BaseHash_Long a = new BaseHash_Long();
        BaseHash_Long b = new BaseHash_Long();
        BaseHash_Long c = new BaseHash_Long();
        BaseHash_Long d = new BaseHash_Long();
        BaseHash_Long e = new BaseHash_Long();
        BaseHash_Long f = new BaseHash_Long();
        BaseHash_Long g = new BaseHash_Long();
        BaseHash_Long h = new BaseHash_Long();
        long temp1 = 0, temp2 = 0;
        short i;
 
        // Initializing working variables
        a.setValue(digest.Get64Position(0));
        b.setValue(digest.Get64Position(1));
        c.setValue(digest.Get64Position(2));
        d.setValue(digest.Get64Position(3));
        e.setValue(digest.Get64Position(4));
        f.setValue(digest.Get64Position(5));
        g.setValue(digest.Get64Position(6));
        h.setValue(digest.Get64Position(7));
 
        for (i = 0; i < Sha512.HASHBLOCK64S; i++) {
            messageSchedule[i] =
                    (((long)(stream.GetUCPosition(i * 8) & 0x000000ffL) << 56) | ((long)(stream.GetUCPosition(i * 8 + 1) & 0x000000ffL) << 48) |
                     ((long)(stream.GetUCPosition(i * 8 + 2) & 0x000000ffL) << 40) |
                     ((long)(stream.GetUCPosition(i * 8 + 3) & 0x000000ffL) << 32) |
                     ((long)(stream.GetUCPosition(i * 8 + 4) & 0x000000ffL) << 24) |
                     ((long)(stream.GetUCPosition(i * 8 + 5) & 0x000000ffL) << 16) | ((long)(stream.GetUCPosition(i * 8 + 6) & 0x000000ffL) << 8) |
                     ((long)(stream.GetUCPosition(i * 8 + 7) & 0x000000ffL)));
        }
 
        //  0 <= t <= 19
        for (i = 0; i < 16; i++) {
            this.Sha512_Operation_Ini(a, b, c, d, e, f, g, h, temp1, temp2, i);
        }
        // 16 <= t <= 79
        for (i = 16; i < Sha512.OPERATIONS; i++) {
            this.Sha512_Operation_Tail(a, b, c, d, e, f, g, h, temp1, temp2, i);
        }
 
        // Upgrading hash values
        digest.Set64Position(0, digest.Get64Position(0) + a.getValue());
        digest.Set64Position(1, digest.Get64Position(1) + b.getValue());
        digest.Set64Position(2, digest.Get64Position(2) + c.getValue());
        digest.Set64Position(3, digest.Get64Position(3) + d.getValue());
        digest.Set64Position(4, digest.Get64Position(4) + e.getValue());
        digest.Set64Position(5, digest.Get64Position(5) + f.getValue());
        digest.Set64Position(6, digest.Get64Position(6) + g.getValue());
        digest.Set64Position(7, digest.Get64Position(7) + h.getValue());
    }
 
    /**
     * Constructor, default
     */
    public Sha512() {
        super();
    }
 
    /**
     * Destructor
     */
    public void finalize() {
 
    }
 
    /**
     * Initializes common states of Sha512 algorithm
     */
    public void Initialize() {
        int i;
 
        this.messageDigest.Set64Position(0, Sha512.INITIALS[0]);
        this.messageDigest.Set64Position(1, Sha512.INITIALS[1]);
        this.messageDigest.Set64Position(2, Sha512.INITIALS[2]);
        this.messageDigest.Set64Position(3, Sha512.INITIALS[3]);
        this.messageDigest.Set64Position(4, Sha512.INITIALS[4]);
        this.messageDigest.Set64Position(5, Sha512.INITIALS[5]);
        this.messageDigest.Set64Position(6, Sha512.INITIALS[6]);
        this.messageDigest.Set64Position(7, Sha512.INITIALS[7]);
        this.remainingBytesLength = 0;
        for (i = 0; i < this.remainingBytes.GetUCLength(); i++) {
            this.remainingBytes.SetUCPosition(i, (byte)0);
        }
        this.messageBitLengthHigh = 0;
        this.messageBitLengthLow = 0;
        for (i = 0; i < Sha512.SCHEDULENUMBER; i++) {
            this.messageSchedule[i] = 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;
        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 and 64 byte padding of current stream
                this.Compress(this.messageDigest, 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.messageDigest, 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;
        }
    }
 
    /**
     * Finalizes hash
     */
    public void Finalize() {
        int 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.messageDigest, 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.messageDigest, this.remainingBytes);
    }
 
    /**
     * Gets hash length in bits
     *
     * @return   short:   hash length in bits
     */
    public short GetBitHashLength() {
 
        return Sha512.HASHBITS;
    }
 
    /**
     * Gets hash length in bytes
     *
     * @return   short:   hash length in bytes
     */
    public short GetUCHashLength() {
 
        return Sha512.HASHUCS;
    }
 
    /**
     * Gets hash length in shorts
     *
     * @return   short:   hash length in shorts
     */
    public short GetUSHashLength() {
 
        return Sha512.HASHUSS;
    }
 
    /**
     * Gets hash length in ints
     *
     * @return   short:   hash length in ints
     */
    public short GetULHashLength() {
 
        return Sha512.HASHULS;
    }
 
    /**
     * Gets hash length in longs (64 bits)
     *
     * @return   short:   hash length in longs
     */
    public short Get64HashLength() {
 
        return Sha512.HASH64S;
    }
 
    /**
     * 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 Sha512.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 Sha512.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 Sha512.HASHBLOCKUSS;
    }
 
    /**
     * Gets the number of ints in the hash block to be hashed
     *
     * @return    short:    number of ints in the hash block to be hashed
     */
    public short GetULHashBlockLength() {
 
        return Sha512.HASHBLOCKULS;
    }
 
    /**
     * Gets the type of the object
     *
     * @return    Hashes:     Sha 512 enumerator name
     */
    public Hashes GetType() {
 
        return Sha512.HASHNAME;
    }
 
}