Friday, 15 December 2017
arrow JDiceChecker 5.0.0.1 arrow CryptoRandomStream arrow DefaultCryptoRandomStream.java (1)
DefaultCryptoRandomStream.java (1st part) - (data storage default class) 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
 
//
// Creator:    http://www.dicelocksecurity.com
// Version:    vers.5.0.0.1
//
// Copyright 2011 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.jdicechecker.CryptoRandomStream;
 
import com.dicelocksecurity.jdicechecker.TypeSizes;
import java.nio.ByteBuffer;
import java.util.zip.DataFormatException;
 
/**
 * Default type class of crypto random streams. Uses default Java memory management.
 * 
 * @author      Angel Ferré @ DiceLock Security
 * @version     5.0.0.1
 * @since       2011-09-28
 */
public class DefaultCryptoRandomStream extends BaseCryptoRandomStream {
 
  /**
   * Type of stream object
   */
    private static final CryptoRandomStreams cryptoRandomStreamType = CryptoRandomStreams.DefaultStream;
 
  /**
   * Pointer to base byte array of DefaultCryptoRandomStream data
   */
    public byte    baseCryptoStream[];
  /**
   * Starting point (byte) of this DefaultCryptoRandomStream inside baseCryptoStream
   */
    public int     baseOffset;
  /**
   * Boolean indicating if base byte array has been internally created
   *  true:     this object must release byte array instantiated when finalized
   *  false:    base byte array has been instantiated outside this object
   */
    public boolean autoMemory;
 
  /** 
   * Constructor, default
   */
    public DefaultCryptoRandomStream() {
 
      super();
        this.baseCryptoStream = null;
        this.baseOffset = 0;
        this.autoMemory = false;
    }
 
  /**    
   * Constructor, creates an empty stream with the indicated bit length 
   * 
   * @param     streamLength    stream length in bits of new DefaultCryptoRandomStream
   * @throws    OutOfMemoryError
   */
    public DefaultCryptoRandomStream(int streamLength) throws OutOfMemoryError {
        
      super();
 
      int byteLength;
   
        if ( (streamLength % TypeSizes.BYTE_BITS) != 0 ) {
          byteLength = streamLength/TypeSizes.BYTE_BITS + 1;
        }
        else {
          byteLength = streamLength/TypeSizes.BYTE_BITS;
        }
        try {
          this.baseCryptoStream = new byte[byteLength];
          if (this.baseCryptoStream == null )
            throw new OutOfMemoryError();
          else {
            this.bitLength = streamLength;
            this.cryptoBuffer = ByteBuffer.wrap(this.baseCryptoStream, 0, byteLength).slice();
            this.baseOffset = 0;
            this.autoMemory = true;
          }
        }
        catch (OutOfMemoryError e) {
          throw e;
        }
    }
   
  /**    
   * Constructor, sets the pointed stream of the indicated length in bits as DefaultCryptoRandomStream
   * 
   * @param     stream          assigns byte array as DefaultCryptoRandomStream data
   * @param     streamLength    stream length in bits of stream parameter
   */
    public DefaultCryptoRandomStream(byte[] stream, int streamLength) {
      super();   
 
      int byteLength;
 
      if ( stream != null ) {
        if ( (streamLength % TypeSizes.BYTE_BITS) != 0 ) {
          byteLength = streamLength/TypeSizes.BYTE_BITS + 1;
        }
        else {
          byteLength = streamLength/TypeSizes.BYTE_BITS;
        }
        this.baseCryptoStream = stream;
        this.bitLength = streamLength;
        this.cryptoBuffer = ByteBuffer.wrap(this.baseCryptoStream, 0, byteLength).slice();
        this.position = 0;
        this.baseOffset = 0;
        this.autoMemory = false;
      }
    }
   
  /**    
   * Destructor, zeroes all data
   */
    public void finalize() {
   
      if ( this.autoMemory ) {
        this.FillUC((byte)0x00); 
      }
      this.baseCryptoStream = null; 
      this.bitLength = 0;
      this.position = 0;
      this.baseOffset = 0;
      this.cryptoBuffer = null;
      this.autoMemory = false;
    }
   
  /**    
   * Sets an empty stream with the indicated length in bits
   * 
   * @param       streamLength    stream length in bits of new DefaultCryptoRandomStream
   * @throws      OutOfMemoryError
   */
    public void SetCryptoRandomStreamBit(int streamLength) throws OutOfMemoryError {
      int byteLength;
   
      if (this.baseCryptoStream == null) {
        if ( (streamLength % TypeSizes.BYTE_BITS) != 0 ) {
          byteLength = streamLength/TypeSizes.BYTE_BITS + 1;
        }
        else {
          byteLength = streamLength/TypeSizes.BYTE_BITS;
        }
        try {
          this.baseCryptoStream = new byte[byteLength];
          if (this.baseCryptoStream == null )
            throw new OutOfMemoryError();
          else {
            this.bitLength = streamLength;
            this.cryptoBuffer = ByteBuffer.wrap(this.baseCryptoStream, 0, byteLength).slice();
            this.autoMemory = true;
            this.position = 0;
            this.baseOffset = 0;
          }
        }
        catch (OutOfMemoryError e) {
          throw e;
        }
      }
    }
   
  /**    
   * Sets the pointed stream of indicated length in bits
   * 
   * @param      stream          assigns byte array as DefaultCryptoRandomStream data
   * @param      streamLength    stream length in bits of stream parameter
   */
    public void SetCryptoRandomStreamBit(byte[] stream, int streamLength) {
      int byteLength;
   
      if (stream != null) {
        if ( (streamLength % TypeSizes.BYTE_BITS) != 0 ) {
          byteLength = streamLength/TypeSizes.BYTE_BITS + 1;
        }
        else {
          byteLength = streamLength/TypeSizes.BYTE_BITS;
        }
        this.autoMemory = false;
        this.bitLength = streamLength;
        this.baseCryptoStream = stream;
        this.cryptoBuffer = ByteBuffer.wrap(this.baseCryptoStream, 0, byteLength).slice();
        this.position = 0;
        this.baseOffset = 0;
      }
    }
   
  /**    
   * Sets an empty stream with the indicated length in bytes
   * 
   * @param      streamLength    stream length in bytes of new DefaultCryptoRandomStream
   * @throws     OutOfMemoryError
   */
    public void SetCryptoRandomStreamUC(int streamLength) throws OutOfMemoryError {
   
      if (this.baseCryptoStream == null) {
        try {
          this.baseCryptoStream = new byte[streamLength];
          if (this.baseCryptoStream == null )
            throw new OutOfMemoryError();
          else {
            this.autoMemory = true;
            this.bitLength = streamLength * TypeSizes.BYTE_BITS;
            this.cryptoBuffer = ByteBuffer.wrap(this.baseCryptoStream, 0, streamLength).slice();
            this.position = 0;
            this.baseOffset = 0;
          }
        }
        catch (OutOfMemoryError e) {
          throw e;
        }
      }
    }
   
  /**    
   * Sets the pointed stream of indicated length in bytes
   * 
   * @param     stream          assigns byte array as DefaultCryptoRandomStream data
   * @param     streamLength    stream length in bytes of stream parameter
   */
    public void SetCryptoRandomStreamUC(byte[] stream, int streamLength) {
   
      if (stream != null) {
        this.bitLength = streamLength * TypeSizes.BYTE_BITS;
        this.baseCryptoStream = stream;
        this.cryptoBuffer = ByteBuffer.wrap(this.baseCryptoStream, 0, streamLength).slice();
        this.position = 0;
        this.baseOffset = 0;
        this.autoMemory = false;
      }
    }
   
  /**    
   * Sets an empty stream with the indicated length in shorts
   * 
   * @param      streamLength    stream length in shorts of new DefaultCryptoRandomStream
   * @throws     OutOfMemoryError
   */
    public void SetCryptoRandomStreamUS(int streamLength) throws OutOfMemoryError {
      int byteLength;
   
      if (this.baseCryptoStream == null) {
        byteLength = streamLength * TypeSizes.SHORT_BYTES;
        try {
          this.baseCryptoStream = new byte[byteLength];
          if (this.baseCryptoStream == null )
            throw new OutOfMemoryError();
          else {
            this.autoMemory = true;
            this.bitLength = streamLength * TypeSizes.SHORT_BITS;
            this.cryptoBuffer = ByteBuffer.wrap(this.baseCryptoStream, 0, byteLength).slice();
            this.position = 0;
            this.baseOffset = 0;
          }
        }
        catch (OutOfMemoryError e) {
          throw e;
        }
      }
    }
   
  /**    
   * Sets the pointed stream of indicated length in shorts
   * 
   * @param     stream          assigns byte array as DefaultCryptoRandomStream data
   * @param     streamLength    stream length in shorts of stream parameter
   */
    public void SetCryptoRandomStreamUS(byte[] stream, int streamLength) {
      int byteLength;
   
      if (stream != null) {
        this.autoMemory = false;
        byteLength = streamLength * TypeSizes.SHORT_BYTES;
        this.bitLength = streamLength * TypeSizes.SHORT_BITS;
        this.baseCryptoStream = stream;
        this.cryptoBuffer = ByteBuffer.wrap(this.baseCryptoStream, 0, byteLength).slice();
        this.position = 0;
        this.baseOffset = 0;
      }
    }
   
  /**    
   * Sets an empty stream with the indicated length in unsigned ints
   * 
   * @param      streamLength    stream length in ints of new DefaultCryptoRandomStream
   * @throws     OutOfMemoryError
   */
    public void SetCryptoRandomStreamUL(int streamLength) throws OutOfMemoryError {
      int byteLength;
   
      if (this.baseCryptoStream == null) {
        byteLength = streamLength * TypeSizes.INT_BYTES;
        try {
          this.baseCryptoStream = new byte[byteLength];
          if (this.baseCryptoStream == null )
            throw new OutOfMemoryError();
          else {
            this.autoMemory = true;
            this.bitLength = streamLength * TypeSizes.INT_BITS;
            this.cryptoBuffer = ByteBuffer.wrap(this.baseCryptoStream, 0, byteLength).slice();
            this.position = 0;
            this.baseOffset = 0;
          }
        }
        catch (OutOfMemoryError e) {
          throw e;
        }
      }
    }
   
  /**    
   * Sets the pointed stream of indicated length in ints
   * 
   * @param     stream          assigns byte array as DefaultCryptoRandomStream data
   * @param     streamLength    stream length in ints of stream parameter
   */
    public void SetCryptoRandomStreamUL(byte[] stream, int streamLength) {
      int byteLength;
   
      if (stream != null) {
        byteLength = streamLength * TypeSizes.INT_BYTES;
        this.autoMemory = false;
        this.bitLength = streamLength * TypeSizes.INT_BITS;
        this.baseCryptoStream = stream;
        this.cryptoBuffer = ByteBuffer.wrap(this.baseCryptoStream, 0, byteLength).slice();
        this.position = 0;
        this.baseOffset = 0;
      }
    }
   
  /**    
   * Set the pointed stream as hexadecimal string
   * 
   * @param     hexStream       String parameter of hexadecimal data
   * @throws    OutOfMemoryError, DataFormatException
   */
    public void SetCryptoRandomStreamHexString(String hexStream) throws OutOfMemoryError, DataFormatException {
      int  lengthUC, i, streamLength;
      byte character;
   
      try {
        streamLength = hexStream.length();
        if ( (streamLength % 2) != 0 ) {
          throw new DataFormatException("Erroneous hexadecimal string!");
        }
        else {
          lengthUC = streamLength / 2;
          this.baseCryptoStream = new byte[lengthUC];
          if (this.baseCryptoStream == null ) 
            throw new OutOfMemoryError();
          else {
            this.autoMemory = true;
            this.bitLength = lengthUC * TypeSizes.BYTE_BITS;
            this.cryptoBuffer = ByteBuffer.wrap(this.baseCryptoStream, 0, lengthUC).slice();
            this.position = 0;
            this.baseOffset = 0;
            for ( i = 0; i < lengthUC; i++ ) {
              character = 0;
              switch ( hexStream.charAt( 2 * i ) ) {
                case '0' : character = 0x00; break;
                case '1' : character = 0x10; break;
                case '2' : character = 0x20; break;
                case '3' : character = 0x30; break;
                case '4' : character = 0x40; break;
                case '5' : character = 0x50; break;
                case '6' : character = 0x60; break;
                case '7' : character = 0x70; break;
                case '8' : character = (byte)0x80; break;
                case '9' : character = (byte)0x90; break;
                case 'A' : 
                case 'a' : character = (byte)0xa0; break;
                case 'B' : 
                case 'b' : character = (byte)0xb0; break;
                case 'C' : 
                case 'c' : character = (byte)0xc0; break;
                case 'D' : 
                case 'd' : character = (byte)0xd0; break;
                case 'E' : 
                case 'e' : character = (byte)0xe0; break;
                case 'F' : 
                case 'f' : character = (byte)0xf0; break;
                default : 
                  throw new DataFormatException("Erroneous hexadecimal string!");
              }
              switch ( hexStream.charAt( (2 * i) + 1 ) ) {
                case '0' : character |= 0x00; break;
                case '1' : character |= 0x01; break;
                case '2' : character |= 0x02; break;
                case '3' : character |= 0x03; break;
                case '4' : character |= 0x04; break;
                case '5' : character |= 0x05; break;
                case '6' : character |= 0x06; break;
                case '7' : character |= 0x07; break;
                case '8' : character |= 0x08; break;
                case '9' : character |= 0x09; break;
                case 'A' : 
                case 'a' : character |= 0x0a; break;
                case 'B' : 
                case 'b' : character |= 0x0b; break;
                case 'C' : 
                case 'c' : character |= 0x0c; break;
                case 'D' : 
                case 'd' : character |= 0x0d; break;
                case 'E' : 
                case 'e' : character |= 0x0e; break;
                case 'F' : 
                case 'f' : character |= 0x0f; break;
                default : 
                  throw new DataFormatException("Erroneous hexadecimal string!");
              }
              this.SetUCPosition(i, character);
            }
          }
        }
      }
      catch (OutOfMemoryError e) {
        throw e;
      }
    }