Friday, 15 December 2017
arrow JDiceChecker 5.0.0.1 arrow CryptoRandomStream arrow BaseCryptoRandomStream.java (1)
BaseCryptoRandomStream.java (1st part) - (data storage base 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
 
//
// 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;
 
/**
 * Base class of crypto random streams
 * 
 * @author      Angel Ferré @ DiceLock Security
 * @version     5.0.0.1
 * @since       2011-09-28
 */
public abstract class BaseCryptoRandomStream {
 
    /**
     * Buffer to access CryptoRandomStream data
     */
    public ByteBuffer cryptoBuffer;
    /**
     * Length of CryptoRandomStream data in bits
     */
    public int bitLength;
    /**
     * Length of selected CryptoRandomStream data in bits if CryptoRandomStream has been reduced
     */
    public int reducedBitLength;
    /**
     * Current bit position of CryptoRandomStream
     */
    public int position;              // Current bit position
 
    /**
     * Inserts a bit in a byte at specified position, position goes from to 7. 
     * 
     * @param      byteInput   Byte to insert in a bit 
     * @param      bitPosition Position in byte where it will be isnerted the bit, from 0 to 7
     * @param      bitValue    Bit to be inserted
     * @return     byte: Byte with bit inserted
     */
    private byte SetByteBitPosition(byte byteInput, int bitPosition, byte bitValue) {
 
        switch (bitValue) {
        case 0:
            byteInput &= ~(1 << bitPosition);
            break;
        case 1:
            byteInput |= (1 << bitPosition);
            break;
        }
        return byteInput;
    }
 
  /**
   * Get the bit of a byte at specified position, position goes from to 7. 
   * 
   * @param      byteInput   Byte to get from the bit 
   * @param      bitPosition Position in byte where it's the bit, from 0 to 7
   * @return     byte:       Byte with bit value (0 or 1)
   */
    private byte GetByteBitPosition(byte byteInput, int bitPosition) {
        byte output;
      
        output = 0;
        output |= ((byteInput >> bitPosition) & 0x01);
        return output;
    }
 
  /**
   * Constructor, default
   * 
   */
    public BaseCryptoRandomStream() {
 
        this.bitLength = 0;
        this.reducedBitLength = 0;
        this.position = 0;
        this.cryptoBuffer = null;
    }
 
  /**
   * Destructor, zeroes all data
   * 
   */
    abstract protected void finalize();
 
  /**
   * Sets the BaseCryptoRandomStream position, minimum value 0
   * 
   * @param      position  Sets current CryptoRandomStreambit position at position parameter
   */
    public void SetBitPosition(int position) {
 
        this.position = position;
    }
 
  /**
   * Gets the current bit position.
   *
   * @return int:  The current bit position within CryptoRandomStream
   */
    public int GetBitPosition() {
 
        return this.position;
    }
 
  /**
   * Gets the stream length in bits
   * 
   * @return     int:  The current bit position within CryptoRandomStream
   */
    public int GetBitLength() {
 
        if ( this.reducedBitLength != 0 ) {
            return this.reducedBitLength;
        } else {
            return this.bitLength;
        }
    }
 
  /**
   * Gets the stream length in unsigned char type
   * 
   * @return     int:  CryptoRandomStream length in bytes (8 bits)
   */
    public int GetUCLength() {
 
        if ( this.reducedBitLength != 0 ) {
            return (this.reducedBitLength / TypeSizes.BYTE_BITS);
        } else {
            return (this.bitLength / TypeSizes.BYTE_BITS);
        }
    }
 
  /**
   * Gets the stream length in unsigned short type
   * 
   * @return     int:  CryptoRandomStream length in short integers (16 bits)
   */
    public int GetUSLength() {
 
        if ( this.reducedBitLength != 0 ) {
          return (this.reducedBitLength / TypeSizes.SHORT_BITS);
        } else {
            return (this.bitLength / TypeSizes.SHORT_BITS);
        }
    }
 
  /**
   * Gets the stream length in unsigned long type
   * 
   * @return     int:  CryptoRandomStream length in long integers (32 bits)
   */
    public int GetULLength() {
 
        if ( this.reducedBitLength != 0 ) {
          return (this.reducedBitLength / TypeSizes.INT_BITS);
        } else {
            return (this.bitLength / TypeSizes.INT_BITS);
        }
    }
 
  /**
   * Sets an empty stream with the indicated length in bits
   * 
   * @param      bitLength  length in bits of created CryptoRandomStream
   */
    abstract public void SetCryptoRandomStreamBit(int bitLength);
 
  /**
   * Sets an empty stream with the indicated length in unsigned chars
   * 
   * @param      ucLength  length in bytes (8 bits) of created CryptoRandomStream
   */
    abstract public void SetCryptoRandomStreamUC(int ucLength);
 
  /**
   * Sets an empty stream with the indicated length in unsigned shorts
   * 
   * @param      usLength  length in short integers (16 bits) of created CryptoRandomStream
   */
    abstract public void SetCryptoRandomStreamUS(int usLength);
 
  /**
   * Sets an empty stream with the indicated length in unsigned long int
   * 
   * @param      ulLength  length in long integers (32 bits) of created CryptoRandomStream
   */
    abstract public void SetCryptoRandomStreamUL(int ulLength);
 
  /**
   * Set the pointed stream as hexadecimal string
   * 
   * @param      hex  hexadecimal string representing a byte stream
   * @throws     OutOfMemoryError, DataFormatException
   */
    abstract public void SetCryptoRandomStreamHexString(String hex) throws OutOfMemoryError, DataFormatException;
 
  /**
   * Gets the buffer to the memory stream
   * 
   * @return    ByteBuffer: a pointer to CryptoRandomStream data 
   */
    public ByteBuffer GetCryptoRandomStreamMemory() {
 
        return this.cryptoBuffer;
    }
 
  /**
   * Sets the BaseCryptoRandomStream bit at current position and moves pointer to the following bit
   * 
   * @param     bit  byte representing a single bit, value 0 or 1
   */
    public void SetBitForward(byte bit) {
 
        this.SetBitPosition(this.position, bit);
        this.position++;
    }
 
  /**
   * Sets the BaseCryptoRandomStream bit at current position and moves pointer to the previous bit
   * 
   * @param     bit  byte representing a single bit, value 0 or 1
   */
    public void SetBitReverse(byte bit) {
 
        this.SetBitPosition(this.position, bit);
        this.position--;
    }
 
  /**
   * Gets the BaseCryptoRandomStream bit at current position and moves pointer to the following bit
   * 
   * @return    byte:  representing a single bit, value 0 or 1
   */
    public byte GetBitForward() {
        byte byteVAlue;
 
        byteVAlue = this.GetBitPosition(this.position);
        this.position++;
        return byteVAlue;
    }
 
  /**
   * Gets the BaseCryptoRandomStream bit at current position and moves pointer to the previous bit
   * 
   * @return    byte:  representing a single bit, value 0 or 1
   */
    public byte GetBitReverse() {
      byte byteVAlue;
 
      byteVAlue = this.GetBitPosition(this.position);
      this.position--;
      return byteVAlue;
    }
 
  /**
   * Sets the stream to an specified bit value (0 or 1)
   * 
   * @param     bit  byte representing a single bit, value 0 or 1
   */
    public void FillBit(byte bit) {
        byte byteValue;
      
        byteValue = (byte)0xff;
        if ( bit == 0) {
          byteValue = 0x00; 
        }
        if ( bit == 1) {
          byteValue = (byte)0xff; 
        }
        this.FillUC(byteValue);
    }
 
  /**
   * Sets the stream to an specified byte value
   * 
   * @param     uChar  byte value (8 bits)
   */
    public void FillUC(byte uChar) {
        
        for (int i = 0; i < this.cryptoBuffer.limit(); i++) {
          this.cryptoBuffer.put(i, uChar);
        }
    }
 
  /**
   * Sets the stream to an specified short value
   * 
   * @param     uShort  short value (16 bits)
   */
    public void FillUS(short uShort) {
      
      for (int i = 0; i < this.cryptoBuffer.limit()/TypeSizes.SHORT_BYTES; i++) {
        this.cryptoBuffer.putShort(i * TypeSizes.SHORT_BYTES, uShort);
      }
    }
 
  /**
   * Sets the stream to an specified bit unsigned long int value
   * 
   * @param     uLong  int value (32 bits)
   */
    public void FillUL(int uLong) {
      
      for (int i = 0; i < this.cryptoBuffer.limit()/TypeSizes.INT_BYTES; i++) {
        this.cryptoBuffer.putLong(i * TypeSizes.INT_BYTES, uLong);
      }
    }
 
  /**
   * Sets the bit value (value 0 or 1) at specified postion, position based in array of bits
   * 
   * @param     position  position where to set the bit value
   * @param     bit       bit value, 0 or 1
   */
    public void SetBitPosition(int position, byte bit) {
        int bytePosition;
        int bitPosition;
 
        bytePosition = (int)Math.floor(position / TypeSizes.BYTE_BITS);
        bitPosition = position % TypeSizes.BYTE_BITS;
        this.cryptoBuffer.put(bytePosition, this.SetByteBitPosition(this.cryptoBuffer.get(bytePosition), bitPosition, bit));
    }
 
  /**
   * Sets the byte value at specified postion, position based in array of bytes
   * 
   * @param     position  position where to set the byte value
   * @param     uChar     byte value
   */
    public void SetUCPosition(int position, byte uChar) {
        
        this.cryptoBuffer.put(position, uChar);
    }
 
  /**
   * Sets the short value at specified postion, position based in array of shorts
   * 
   * @param     position  position where to set the short value
   * @param     uShort    short value
   */
    public void SetUSPosition(int position, short uShort) {
        
        this.cryptoBuffer.putShort(position * TypeSizes.SHORT_BYTES, uShort);
    }
 
  /**
   * Sets the int value at specified postion, position based in array of ints
   * 
   * @param     position  position where to set the int value
   * @param     uInt      int value
   */
    public void SetULPosition(int position, int uInt) {
 
      
        this.cryptoBuffer.putInt(position * TypeSizes.INT_BYTES, uInt);
    }
 
  /**
   * Sets the long value at specified postion, position based in array of longs
   * 
   * @param     position  position where to set the long value
   * @param     uLong     long value
   */
    public void Set64Position(int position, long uLong) {
 
      
        this.cryptoBuffer.putLong(position * TypeSizes.LONG_BYTES, uLong);
    }