/[dtapublic]/projs/trunk/projs/20120418_blackjacksim/source/bjcceval/sha512.c
ViewVC logotype

Annotation of /projs/trunk/projs/20120418_blackjacksim/source/bjcceval/sha512.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (hide annotations) (download)
Fri Oct 7 03:35:12 2016 UTC (8 years, 1 month ago) by dashley
Original Path: swprojs/trunk/projs/20120418_blackjacksim/source/bjcceval/sha512.c
File MIME type: text/plain
File size: 17502 byte(s)
Commit of Blackjack simulation project.
1 dashley 11 //----------------------------------------------------------------------------------------------------
2     //$Header: /home/dashley/cvsrep/e3ft_gpl01/e3ft_gpl01/dtaipubs/cron/2010/blackjack_201010/source/bjcceval/sha512.c,v 1.9 2012/03/30 00:20:15 dashley Exp $
3     //----------------------------------------------------------------------------------------------------
4     //Copyright (C) 2012, David T. Ashley.
5     //
6     //This file is part of BJCCEVAL, a program that evaluates by simulation
7     //the best basic strategy, card-counting, and other playing strategies
8     //for several variants of the game of Blackjack.
9     //
10     //BJCCEVAL is free software: you can redistribute it and/or modify
11     //it under the terms of the GNU General Public License as published by
12     //the Free Software Foundation, either version 3 of the License, or
13     //(at your option) any later version.
14     //
15     //BJCCEVAL is distributed in the hope that it will be useful,
16     //but WITHOUT ANY WARRANTY; without even the implied warranty of
17     //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18     //GNU General Public License for more details.
19     //
20     //You should have received a copy of the GNU General Public License
21     //along with this program. If not, see <http://www.gnu.org/licenses/>.
22     //(A copy of the GNU General Public License, Version 3 is provided in
23     //the file "COPYING" distributed with BJCCEVAL.)
24     //
25     //David T. Ashley can be contacted at DASHLEY@GMAIL.COM and/or at
26     //P.O. Box 918, Marshall MI 49068.
27     //----------------------------------------------------------------------------------------------------
28     #define MODULE_SHA512
29    
30     #include <stdlib.h>
31     #include <string.h>
32    
33     #include "cassert.h"
34     #include "charfunc.h"
35     #include "sha512.h"
36    
37     //This is a right rotation macro for efficiency. This
38     //macro rotates a 64-bit quantity x right (cyclically) by
39     //n bits. Nomenclature from FIPS 180-3.
40     #define SHA512_FUNC_ROTR(x, n) (((x) >> (n)) | ((x) << (64-(n))))
41    
42     //This is a right shift macro for efficiency. This
43     //macro shifts a 64-bit quantity x right by
44     //n bits. Nomenclature from FIPS 180-3.
45     #define SHA512_FUNC_SHR(x, n) ((x) >> (n))
46    
47     //These functions come directly from FIPS 180-3.
48     #define SHA512_FUNC_CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
49     #define SHA512_FUNC_MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
50     #define SHA512_FUNC_SIGMABIG_0(x) (SHA512_FUNC_ROTR(x, 28) ^ SHA512_FUNC_ROTR(x, 34) ^ SHA512_FUNC_ROTR(x, 39))
51     #define SHA512_FUNC_SIGMABIG_1(x) (SHA512_FUNC_ROTR(x, 14) ^ SHA512_FUNC_ROTR(x, 18) ^ SHA512_FUNC_ROTR(x, 41))
52     #define SHA512_FUNC_SIGMASMALL_0(x) (SHA512_FUNC_ROTR(x, 1) ^ SHA512_FUNC_ROTR(x, 8) ^ SHA512_FUNC_SHR(x, 7))
53     #define SHA512_FUNC_SIGMASMALL_1(x) (SHA512_FUNC_ROTR(x, 19) ^ SHA512_FUNC_ROTR(x, 61) ^ SHA512_FUNC_SHR(x, 6))
54    
55    
56     //Constants, from FIPS 180-3.
57     const unsigned long long SHA512_K[80] =
58     {
59     0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
60     0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
61     0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
62     0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
63     0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
64     0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
65     0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
66     0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
67     0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
68     0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
69     0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
70     0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
71     0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
72     0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
73     0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
74     0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
75     0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
76     0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
77     0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
78     0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
79     0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
80     0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
81     0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
82     0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
83     0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
84     0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
85     0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
86     0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
87     0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
88     0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
89     0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
90     0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
91     0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
92     0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
93     0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
94     0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
95     0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
96     0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
97     0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
98     0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
99     };
100    
101    
102     //Initializes an SHA-512 state structure in preparation for adding data.
103     //
104     void SHA512_Sha512StateStructOpen(struct SHA512_Sha512StateStruct *arg)
105     {
106     memset(arg, 0, sizeof(struct SHA512_Sha512StateStruct));
107     //Set everything to zero--processed bitcount set to zero.
108    
109     //This assignment comes directly from FIPS 180-3.
110     arg->H0 = 0x6a09e667f3bcc908ULL;
111     arg->H1 = 0xbb67ae8584caa73bULL;
112     arg->H2 = 0x3c6ef372fe94f82bULL;
113     arg->H3 = 0xa54ff53a5f1d36f1ULL;
114     arg->H4 = 0x510e527fade682d1ULL;
115     arg->H5 = 0x9b05688c2b3e6c1fULL;
116     arg->H6 = 0x1f83d9abfb41bd6bULL;
117     arg->H7 = 0x5be0cd19137e2179ULL;
118     }
119    
120    
121    
122     //Copies the byte buffer to the word buffer within the state block.
123     //This is done in a way which hides big-endian/little-endian concerns.
124     //
125     static void SHA512_CopyBytesToWords(struct SHA512_Sha512StateStruct *arg)
126     {
127     unsigned int i;
128    
129     #ifdef P_CASSERT
130     CASSERT_Assert(arg != NULL, __FILE__, __LINE__);
131     #endif
132    
133     //Copy the buffer contents into the words. We need to be careful
134     //to do this correctly, because of big-endian/little-endian concerns.
135     //From FIPS 180-3 (alluded to, not really stated), the message is
136     //loaded in from M[0] down to M[15]. Additionally, per the other
137     //conventions in the document, the first byte is uppermost in each
138     //word.
139     for (i=0; i<16; i++)
140     {
141    
142     #ifdef P_CASSERT
143     CASSERT_Assert((i * 8 + 7) < 128, __FILE__, __LINE__);
144     #endif
145    
146     arg->M[i] = (((unsigned long long)(arg->buf[i*8+0])) << 56)
147     +
148     (((unsigned long long)(arg->buf[i*8+1])) << 48)
149     +
150     (((unsigned long long)(arg->buf[i*8+2])) << 40)
151     +
152     (((unsigned long long)(arg->buf[i*8+3])) << 32)
153     +
154     (((unsigned long long)(arg->buf[i*8+4])) << 24)
155     +
156     (((unsigned long long)(arg->buf[i*8+5])) << 16)
157     +
158     (((unsigned long long)(arg->buf[i*8+6])) << 8)
159     +
160     (((unsigned long long)(arg->buf[i*8+7])));
161     }
162     }
163    
164    
165     //Copies the buffer of words into a string buffer of string length 128, and also places
166     //the zero terminator, which means that the string supplied by the caller must be of size
167     //129 or larger.
168     //
169     static void SHA512_CopyWordsToStringBuffer(struct SHA512_Sha512ResultStruct *arg)
170     {
171     unsigned int i, j;
172     unsigned char *puc;
173     unsigned long long woi;
174    
175     #ifdef P_CASSERT
176     CASSERT_Assert(arg != NULL, __FILE__, __LINE__);
177     #endif
178    
179     //Copy the buffer contents into the words. We need to be careful
180     //to do this correctly, because of big-endian/little-endian concerns.
181     //From FIPS 180-3 (alluded to, not really stated), the message is
182     //loaded in from M[0] down to M[15]. Additionally, per the other
183     //conventions in the document, the first byte is uppermost in each
184     //word.
185     for (i=0; i<8; i++)
186     {
187     woi = arg->sha512_words[i];
188    
189     //Form a pointer to the buffer location of interest. We work
190     //backwards.
191     puc = (unsigned char *)(arg->sha512_chars) + (i * 16) + 15;
192    
193     //Fill in the buffer.
194     for (j=0; j<16; j++)
195     {
196     *puc = (unsigned char)CHARFUNC_nibble_to_lc_hex_digit((int)(woi & 0xF));
197     woi >>= 4;
198     puc--;
199     }
200     }
201    
202     //Place the zero string terminator.
203     arg->sha512_chars[128] = 0;
204     }
205    
206    
207    
208     //Do the SHA-512 rounds as specified by FIPS 180-3.
209     //
210     static void SHA512_DoSha512Rounds(struct SHA512_Sha512StateStruct *arg)
211     {
212     int i;
213     //Iteration variable.
214     unsigned long long T1, T2;
215     //Temporary variables. Nomenclature is from FIPS 180-3.
216     unsigned long long M[16];
217     //Buffer of message block to avoid repeated dereferences.
218     unsigned long long H[8];
219     //Buffer of hash state to avoid repeated dereferences.
220     unsigned long long W[80];
221     //Working variable. Nomenclature directly from FIPS 180-3.
222     unsigned long long a, b, c, d, e, f, g, h;
223     //Nomenclature above directly from FIPS 180-3.
224    
225     #ifdef P_CASSERT
226     CASSERT_Assert(arg != NULL, __FILE__, __LINE__);
227     #endif
228    
229     //Copy bytes into words.
230     SHA512_CopyBytesToWords(arg);
231    
232     //Copy out the message buffer for speed. This should avoid repeated
233     //dereferences.
234     M[ 0] = arg->M[ 0];
235     M[ 1] = arg->M[ 1];
236     M[ 2] = arg->M[ 2];
237     M[ 3] = arg->M[ 3];
238     M[ 4] = arg->M[ 4];
239     M[ 5] = arg->M[ 5];
240     M[ 6] = arg->M[ 6];
241     M[ 7] = arg->M[ 7];
242     M[ 8] = arg->M[ 8];
243     M[ 9] = arg->M[ 9];
244     M[10] = arg->M[10];
245     M[11] = arg->M[11];
246     M[12] = arg->M[12];
247     M[13] = arg->M[13];
248     M[14] = arg->M[14];
249     M[15] = arg->M[15];
250    
251     //Copy out the hash state for speed. This should avoid repeated dereferences.
252     H[0] = arg->H0;
253     H[1] = arg->H1;
254     H[2] = arg->H2;
255     H[3] = arg->H3;
256     H[4] = arg->H4;
257     H[5] = arg->H5;
258     H[6] = arg->H6;
259     H[7] = arg->H7;
260    
261     //Prepare the message schedule. The nomenclature comes directly from FIPS 180-3.
262     W[ 0] = M[ 0];
263     W[ 1] = M[ 1];
264     W[ 2] = M[ 2];
265     W[ 3] = M[ 3];
266     W[ 4] = M[ 4];
267     W[ 5] = M[ 5];
268     W[ 6] = M[ 6];
269     W[ 7] = M[ 7];
270     W[ 8] = M[ 8];
271     W[ 9] = M[ 9];
272     W[10] = M[10];
273     W[11] = M[11];
274     W[12] = M[12];
275     W[13] = M[13];
276     W[14] = M[14];
277     W[15] = M[15];
278    
279     for (i=16; i<80; i++)
280     {
281     W[i] = SHA512_FUNC_SIGMASMALL_1(W[i-2])
282     + W[i-7]
283     + SHA512_FUNC_SIGMASMALL_0(W[i-15])
284     + W[i-16];
285     }
286    
287     //Initialize the 8 working variables as specified in FIPS 180-3.
288     a = H[0];
289     b = H[1];
290     c = H[2];
291     d = H[3];
292     e = H[4];
293     f = H[5];
294     g = H[6];
295     h = H[7];
296    
297     //Perform the rounds as specified in FIPS 180-3. Nomenclature below comes from
298     //FIPS 180-3.
299     for (i=0; i<80; i++)
300     {
301     T1 = h
302     + SHA512_FUNC_SIGMABIG_1(e)
303     + SHA512_FUNC_CH(e, f, g)
304     + SHA512_K[i]
305     + W[i];
306     //
307     T2 = SHA512_FUNC_SIGMABIG_0(a)
308     + SHA512_FUNC_MAJ(a, b, c);
309     //
310     h = g;
311     //
312     g = f;
313     //
314     f = e;
315     //
316     e = d + T1;
317     //
318     d = c;
319     //
320     c = b;
321     //
322     b = a;
323     //
324     a = T1 + T2;
325     }
326    
327     //Compute the next hash value. The nomenclature comes from FIPS 180-3.
328     H[0] = a + H[0];
329     H[1] = b + H[1];
330     H[2] = c + H[2];
331     H[3] = d + H[3];
332     H[4] = e + H[4];
333     H[5] = f + H[5];
334     H[6] = g + H[6];
335     H[7] = h + H[7];
336    
337     //Place the local variables back in the structure. This the only state that
338     //gets preserved between the operation of doing the rounds.
339     arg->H0 = H[0];
340     arg->H1 = H[1];
341     arg->H2 = H[2];
342     arg->H3 = H[3];
343     arg->H4 = H[4];
344     arg->H5 = H[5];
345     arg->H6 = H[6];
346     arg->H7 = H[7];
347     }
348    
349    
350     //Adds a block of data to the SHA-512 structure. Zero length is allowed.
351     //
352     void SHA512_Sha512StateStructAddData(struct SHA512_Sha512StateStruct *arg,
353     void *pointer_in,
354     unsigned len)
355     {
356     unsigned int low_32;
357     unsigned int byte_offset;
358     unsigned char *data;
359    
360     #ifdef P_CASSERT
361     CASSERT_Assert((len == 0) || (arg != NULL), __FILE__, __LINE__);
362     CASSERT_Assert(pointer_in != NULL, __FILE__, __LINE__);
363     #endif
364    
365     data = (unsigned char *)pointer_in;
366     //It is easier to do it this way, rather than cast all the time.
367    
368     low_32 = (unsigned int)arg->bit_count;
369     //Copy off the least significant bits. Easier to do once. We only
370     //need the 32 least significant because the block size is 0 modulo 1024.
371    
372     byte_offset = low_32 >> 3;
373     //This gives our byte offset, up to 500+Mb or so.
374    
375     while (len--)
376     {
377     //We process rounds AFTER a byte is added to the buffer. So
378     //it is always safe to add a byte first.
379     arg->buf[byte_offset & 0x7F] = *data;
380    
381     //Nothing to do unless this was the final byte of the buffer.
382     if ((byte_offset & 0x7F) == 127)
383     {
384     SHA512_DoSha512Rounds(arg);
385     }
386    
387     //Increment.
388     data++;
389     byte_offset++;
390     arg->bit_count += 8;
391     }
392     }
393    
394    
395     //Closes the SHA-512 structure and places the SHA-512 result into the result structure.
396     //After this operation, state is destroyed and no further data may be added.
397     //
398     void SHA512_Sha512StateStructClose(struct SHA512_Sha512StateStruct *state,
399     struct SHA512_Sha512ResultStruct *result)
400     {
401     unsigned long long msglen;
402     //Used to hold message length before we pad the message.
403     unsigned char c80 = 0x80;
404     //Used to append the "1" per FIPS 180-3.
405     unsigned char c00 = 0x00;
406     //Used to add 0's per FIPS 180-3.
407     unsigned char length_buf[16];
408     //Buffer used to form the message length and append it to the message per FIPS 180-3.
409    
410     //Be sure the input pointers aren't obviously invalid.
411     #ifdef P_CASSERT
412     CASSERT_Assert(state != NULL, __FILE__, __LINE__);
413     CASSERT_Assert(result != NULL, __FILE__, __LINE__);
414     #endif
415    
416     //Snapshot the message length. We'll be changing it when we pad the message.
417     msglen = state->bit_count;
418    
419     //Add the required "1" to the end of the message, per FIPS 180-3. Because
420     //this software module only allows the addition of bytes (not bits), adding the
421     //"1" will always involve adding the byte 0x80.
422     SHA512_Sha512StateStructAddData(state, &c80, 1);
423    
424     //Add enough 0's to the message so that we have exactly room for 16 bytes (128 bits)
425     //of length information at the end of the message.
426     while ((state->bit_count & 0x3FF) != 896)
427     SHA512_Sha512StateStructAddData(state, &c00, 1);
428    
429     //Calculate the length as a series of bytes.
430     length_buf[ 0] = 0;
431     length_buf[ 1] = 0;
432     length_buf[ 2] = 0;
433     length_buf[ 3] = 0;
434     length_buf[ 4] = 0;
435     length_buf[ 5] = 0;
436     length_buf[ 6] = 0;
437     length_buf[ 7] = 0;
438     length_buf[ 8] = (unsigned char)((msglen >> 56) & 0xFF);
439     length_buf[ 9] = (unsigned char)((msglen >> 48) & 0xFF);
440     length_buf[10] = (unsigned char)((msglen >> 40) & 0xFF);
441     length_buf[11] = (unsigned char)((msglen >> 32) & 0xFF);
442     length_buf[12] = (unsigned char)((msglen >> 24) & 0xFF);
443     length_buf[13] = (unsigned char)((msglen >> 16) & 0xFF);
444     length_buf[14] = (unsigned char)((msglen >> 8) & 0xFF);
445     length_buf[15] = (unsigned char)((msglen) & 0xFF);
446    
447     //Add the length to the message. This should work out to generate the
448     //final manipulation round.
449     SHA512_Sha512StateStructAddData(state, length_buf, 16);
450    
451     //Copy the words from the state vector to the result vector.
452     result->sha512_words[0] = state->H0;
453     result->sha512_words[1] = state->H1;
454     result->sha512_words[2] = state->H2;
455     result->sha512_words[3] = state->H3;
456     result->sha512_words[4] = state->H4;
457     result->sha512_words[5] = state->H5;
458     result->sha512_words[6] = state->H6;
459     result->sha512_words[7] = state->H7;
460    
461     //Form a string from the hash vector.
462     SHA512_CopyWordsToStringBuffer(result);
463    
464     //Destroy the state, which may contain sensitive information.
465     memset(state, 0, sizeof(struct SHA512_Sha512StateStruct));
466     }
467    
468    
469     const char *SHA512_Vcinfo_C(void)
470     {
471     return("$Revision: 1.9 $");
472     }
473    
474    
475     const char *SHA512_Vcinfo_H(void)
476     {
477     return(SHA512_VCINFO_H);
478     }
479    
480    
481     //----------------------------------------------------------------------------------------------------
482     //$Log: sha512.c,v $
483     //Revision 1.9 2012/03/30 00:20:15 dashley
484     //Edits.
485     //
486     //Revision 1.8 2012/03/29 23:44:01 dashley
487     //Edits.
488     //
489     //Revision 1.7 2012/03/26 02:17:49 dashley
490     //Edits.
491     //
492     //Revision 1.6 2012/03/26 02:11:07 dashley
493     //Edits.
494     //
495     //Revision 1.5 2012/03/15 23:38:08 dashley
496     //License text enhanced.
497     //
498     //Revision 1.4 2012/03/14 02:44:49 dashley
499     //Edits.
500     //
501     //Revision 1.3 2012/03/14 01:57:52 dashley
502     //Edits.
503     //----------------------------------------------------------------------------------------------------
504     // End of $RCSfile: sha512.c,v $.
505     //----------------------------------------------------------------------------------------------------
506    

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25