Tuesday, 21 November 2017
arrow HashDigester-x 6.0.0.1 arrow defaultCryptoRandomStream.cpp
defaultCryptoRandomStream.cpp (Default Memory Crypto Random Stream C++ 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
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
 
//
// Creator:    http://www.dicelocksecurity.com
// Version:    vers.6.0.0.1
//
// Copyright  2008-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.
// 
 
#include <stdlib.h>
#include <string.h>
#include "defaultCryptoRandomStream.h"
 
 
namespace DiceLockSecurity {
 
  namespace CryptoRandomStream {
 
  // Symmetric cipher type enumerator name
  const CryptoRandomStreams  DefaultCryptoRandomStream::cryptoRandomStreamType = DefaultStream;
 
  // Constructor, default 
  DefaultCryptoRandomStream::DefaultCryptoRandomStream(void) {
 
    this->cryptoStream = NULL;
    this->autoMemory = false;
    this->bitLength = 0;
    this->position = 0;
  }
 
  // Constructor, creates an empty stream with the indicated bit length 
  DefaultCryptoRandomStream::DefaultCryptoRandomStream(unsigned long int streamLength) {
 
    try {
      if (streamLength % BYTEBITS) {
        this->cryptoStream = (unsigned char *)calloc(streamLength/BYTEBITS + 1,sizeof(byteBits));
      }
      else {
        this->cryptoStream = (unsigned char *)calloc(streamLength/BYTEBITS,sizeof(byteBits));
      }
      if (this->cryptoStream == NULL )
        throw "Memory allocation failure!";
      else {
        this->bitLength = streamLength;
        this->position = 0;
        this->autoMemory = true;
      }
    }
    catch (char* str) {
      throw str;
    }
  }
 
  // Constructor, sets the pointed stream of the indicated length in bits
  DefaultCryptoRandomStream::DefaultCryptoRandomStream(void* stream, unsigned long int streamLength) {
 
    if ( stream != NULL ) {
      this->bitLength = streamLength;
      this->cryptoStream = (unsigned char *)stream;
      this->autoMemory = false;
    }
  }
 
  // Destructor
  DefaultCryptoRandomStream::~DefaultCryptoRandomStream() {
 
    if ( this->autoMemory ) {
      free(this->cryptoStream); 
    }
    this->cryptoStream = NULL;
    this->bitLength = 0;
    this->position = 0;
    this->autoMemory = false;
  }
 
  // Sets an empty stream with the indicated length in bits
  void DefaultCryptoRandomStream::SetCryptoRandomStreamBit(unsigned long int streamLength) {
 
    if (this->cryptoStream == NULL) {
      try {
        if (streamLength % BYTEBITS) {
          this->cryptoStream = (unsigned char *)calloc(streamLength/BYTEBITS + 1,sizeof(byteBits));
        }
        else {
          this->cryptoStream = (unsigned char *)calloc(streamLength/BYTEBITS,sizeof(byteBits));
        }
        if (this->cryptoStream == NULL )
          throw "Memory allocation failure!";
        else {
          this->autoMemory = true;
          this->bitLength = streamLength;
          this->position = 0;
        }
      }
      catch (char* str) {
        throw str;
      }
    }
  }
 
  // Sets the pointed stream of indicated length in bits
  void DefaultCryptoRandomStream::SetCryptoRandomStreamBit(void* stream, unsigned long int streamLength) {
 
    if (stream != NULL) {
      this->bitLength = streamLength;
      this->cryptoStream = (unsigned char *)stream;
      this->autoMemory = false;
    }
  }
 
  // Sets an empty stream with the indicated length in unsigned chars
  void DefaultCryptoRandomStream::SetCryptoRandomStreamUC(unsigned long int streamLength) {
 
    if (this->cryptoStream == NULL) {
      try {
        this->cryptoStream = (unsigned char *)calloc(streamLength,sizeof(unsigned char));
        if (this->cryptoStream == NULL )
          throw "Memory allocation failure!";
        else {
          this->autoMemory = true;
          this->bitLength = streamLength * BYTEBITS * sizeof(unsigned char);
          this->position = 0;
        }
      }
      catch (char* str) {
        throw str;
      }
    }
  }
 
  // Sets the pointed stream of indicated length in unsigned chars
  void DefaultCryptoRandomStream::SetCryptoRandomStreamUC(void* stream, unsigned long int streamLength) {
 
    if (stream != NULL) {
      this->bitLength = streamLength * BYTEBITS * sizeof(unsigned char);
      this->cryptoStream = (unsigned char *)stream;
      this->autoMemory = false;
    }
  }
 
  // Sets an empty stream with the indicated length in unsigned shorts
  void DefaultCryptoRandomStream::SetCryptoRandomStreamUS(unsigned long int streamLength) {
 
    if (this->cryptoStream == NULL) {
      try {
        this->cryptoStream = (unsigned char *)calloc(streamLength,sizeof(unsigned short int));
        if (this->cryptoStream == NULL )
          throw "Memory allocation failure!";
        else {
          this->autoMemory = true;
          this->bitLength = streamLength * BYTEBITS * sizeof(unsigned short int);
          this->position = 0;
        }
      }
      catch (char* str) {
        throw str;
      }
    }
  }
 
  // Sets the pointed stream of indicated length in unsigned shorts
  void DefaultCryptoRandomStream::SetCryptoRandomStreamUS(void* stream, unsigned long int streamLength) {
 
    if (stream != NULL) {
      this->bitLength = streamLength * BYTEBITS * sizeof(unsigned short int);
      this->cryptoStream = (unsigned char *)stream;
      this->autoMemory = false;
    }
  }
 
  // Sets an empty stream with the indicated length in unsigned longs
  void DefaultCryptoRandomStream::SetCryptoRandomStreamUL(unsigned long int streamLength) {
 
    if (this->cryptoStream == NULL) {
      try {
        this->cryptoStream = (unsigned char *)calloc(streamLength,sizeof(unsigned long int));
        if (this->cryptoStream == NULL )
          throw "Memory allocation failure!";
        else {
          this->autoMemory = true;
          this->bitLength = streamLength * BYTEBITS * sizeof(unsigned long int);
          this->position = 0;
        }
      }
      catch (char* str) {
        throw str;
      }
    }
  }
 
  // Sets the pointed stream of indicated length in unsigned longs
  void DefaultCryptoRandomStream::SetCryptoRandomStreamUL(void* stream, unsigned long int streamLength) {
 
    if (stream != NULL) {
      this->bitLength = streamLength * BYTEBITS * sizeof(unsigned long int);
      this->cryptoStream = (unsigned char *)stream;
      this->autoMemory = false;
    }
  }
 
  // Set the pointed stream as hexadecimal string
  void DefaultCryptoRandomStream::SetCryptoRandomStreamHexString(const char* hexStream) {
  unsigned long int lengthUC, i, streamLength;
  unsigned char byte;
 
    try {
      streamLength = strlen(hexStream);
      if ( streamLength % 2 ) {
        throw "Erroneous hexadecimal string!";
      }
      else {
        lengthUC = streamLength / 2;
        this->cryptoStream = (unsigned char *)calloc( lengthUC, sizeof(unsigned char));
        if (this->cryptoStream == NULL ) 
          throw "Memory allocation failure!";
        else {
          this->autoMemory = true;
          this->bitLength = lengthUC * BYTEBITS * sizeof(unsigned char);
          this->position = 0;
          for ( i = 0; i < lengthUC; i++ ) {
            byte = 0;
            switch ( hexStream[ 2 * i ] ) {
              case '0' : byte = 0x00; break;
              case '1' : byte = 0x10; break;
              case '2' : byte = 0x20; break;
              case '3' : byte = 0x30; break;
              case '4' : byte = 0x40; break;
              case '5' : byte = 0x50; break;
              case '6' : byte = 0x60; break;
              case '7' : byte = 0x70; break;
              case '8' : byte = 0x80; break;
              case '9' : byte = 0x90; break;
              case 'A' : 
              case 'a' : byte = 0xa0; break;
              case 'B' : 
              case 'b' : byte = 0xb0; break;
              case 'C' : 
              case 'c' : byte = 0xc0; break;
              case 'D' : 
              case 'd' : byte = 0xd0; break;
              case 'E' : 
              case 'e' : byte = 0xe0; break;
              case 'F' : 
              case 'f' : byte = 0xf0; break;
              default : 
                throw "Erroneous hexadecimal string!";
                break;
            }
            switch ( hexStream[ (2 * i) + 1 ] ) {
              case '0' : byte = byte | 0x00; break;
              case '1' : byte = byte | 0x01; break;
              case '2' : byte = byte | 0x02; break;
              case '3' : byte = byte | 0x03; break;
              case '4' : byte = byte | 0x04; break;
              case '5' : byte = byte | 0x05; break;
              case '6' : byte = byte | 0x06; break;
              case '7' : byte = byte | 0x07; break;
              case '8' : byte = byte | 0x08; break;
              case '9' : byte = byte | 0x09; break;
              case 'A' : 
              case 'a' : byte = byte | 0x0a; break;
              case 'B' : 
              case 'b' : byte = byte | 0x0b; break;
              case 'C' : 
              case 'c' : byte = byte | 0x0c; break;
              case 'D' : 
              case 'd' : byte = byte | 0x0d; break;
              case 'E' : 
              case 'e' : byte = byte | 0x0e; break;
              case 'F' : 
              case 'f' : byte = byte | 0x0f; break;
              default : 
                throw "Erroneous hexadecimal string!";
                break;
            }
            this->SetUCPosition(i, byte);
          }
        }
      }
    }
    catch (char* str) {
      throw str;
    }
  }
 
  // Gets the CryptoRandomStream type of the object
  CryptoRandomStreams DefaultCryptoRandomStream::GetCryptoRandomStreamType(void) {
 
    return this->cryptoRandomStreamType;
  }
  }
}