SHA256.cpp
Go to the documentation of this file.
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include <cstring>
10 #include <cassert>
11 #include "SHA256.h"
12 
19 #define LOAD32H(x, y) \
20  { x = ((ulong32)((y)[0] & 255)<<24) | \
21  ((ulong32)((y)[1] & 255)<<16) | \
22  ((ulong32)((y)[2] & 255)<<8) | \
23  ((ulong32)((y)[3] & 255)); }
24 
25 #define STORE32H(x, y) \
26  { (y)[0] = (ulong8)(((x)>>24)&255); (y)[1] = (ulong8)(((x)>>16)&255); \
27  (y)[2] = (ulong8)(((x)>>8)&255); (y)[3] = (ulong8)((x)&255); }
28 
29 #define STORE64H(x, y) \
30  { (y)[0] = (ulong8)(((x)>>56)&255); (y)[1] = (ulong8)(((x)>>48)&255); \
31  (y)[2] = (ulong8)(((x)>>40)&255); (y)[3] = (ulong8)(((x)>>32)&255); \
32  (y)[4] = (ulong8)(((x)>>24)&255); (y)[5] = (ulong8)(((x)>>16)&255); \
33  (y)[6] = (ulong8)(((x)>>8)&255); (y)[7] = (ulong8)((x)&255); }
34 
35 #define XMEMCPY memcpy
36 
37 #define XMEMCMP memcmp
38 
39 #ifndef MIN
40 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
41 #endif
42 
43 
44 #define RORc(x, y) ( ((((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((y)&31)) | ((ulong32)(x)<<(ulong32)(32-((y)&31)))) & 0xFFFFFFFFUL)
45 
46 /* Various logical functions */
47 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
48 #define Maj(x,y,z) (((x | y) & z) | (x & y))
49 #define S(x, n) RORc((x),(n))
50 #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
51 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
52 #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
53 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
54 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
55 
56 static int sha256_compress(hash_state * md, const unsigned char *buf)
57 {
58  ulong32 S[8], W[64], t0, t1;
59  int i;
60 
61  /* copy state into S */
62  for (i = 0; i < 8; i++) {
63  S[i] = md->sha256.state[i];
64  }
65 
66  /* copy the state into 512-bits into W[0..15] */
67  for (i = 0; i < 16; i++) {
68  LOAD32H(W[i], buf + (4*i));
69  }
70 
71  /* fill W[16..63] */
72  for (i = 16; i < 64; i++) {
73  W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
74  }
75 
76  /* Compress */
77 #define RND(a,b,c,d,e,f,g,h,i,ki) \
78  t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
79  t1 = Sigma0(a) + Maj(a, b, c); \
80  d += t0; \
81  h = t0 + t1;
82 
83  RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
84  RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
85  RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
86  RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
87  RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
88  RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
89  RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
90  RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
91  RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
92  RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
93  RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
94  RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
95  RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
96  RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
97  RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
98  RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
99  RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
100  RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
101  RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
102  RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
103  RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
104  RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
105  RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
106  RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
107  RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
108  RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
109  RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
110  RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
111  RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
112  RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
113  RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
114  RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
115  RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
116  RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
117  RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
118  RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
119  RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
120  RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
121  RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
122  RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
123  RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
124  RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
125  RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
126  RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
127  RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
128  RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
129  RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
130  RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
131  RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
132  RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
133  RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
134  RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
135  RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
136  RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
137  RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
138  RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
139  RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
140  RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
141  RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
142  RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
143  RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
144  RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
145  RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
146  RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
147 
148 #undef RND
149 
150  /* feedback */
151  for (i = 0; i < 8; i++) {
152  md->sha256.state[i] = md->sha256.state[i] + S[i];
153  }
154  return CRYPT_OK;
155 }
156 
157 
164 {
165  assert(md != NULL);
166 
167  md->sha256.curlen = 0;
168  md->sha256.length = 0;
169  md->sha256.state[0] = 0x6A09E667UL;
170  md->sha256.state[1] = 0xBB67AE85UL;
171  md->sha256.state[2] = 0x3C6EF372UL;
172  md->sha256.state[3] = 0xA54FF53AUL;
173  md->sha256.state[4] = 0x510E527FUL;
174  md->sha256.state[5] = 0x9B05688CUL;
175  md->sha256.state[6] = 0x1F83D9ABUL;
176  md->sha256.state[7] = 0x5BE0CD19UL;
177  return CRYPT_OK;
178 }
179 
195 int sha256_process(hash_state* md, const ulong8* in, ulong32 inlen)
196 {
197  ulong32 n;
198  int err;
199  if (NULL == in || NULL == md)
200  {
201  return CRYPT_INVALID_ARG;
202  }
203  if (md-> sha256 .curlen > sizeof(md-> sha256 .buf))
204  {
205  return CRYPT_INVALID_ARG;
206  }
207  if ((md-> sha256 .length + inlen) < md-> sha256 .length)
208  {
209  return CRYPT_HASH_OVERFLOW;
210  }
211  while (inlen > 0)
212  {
213  if (md-> sha256 .curlen == 0 && inlen >= 64)
214  {
215  if ((err = sha256_compress(md, (ulong8*)in)) != CRYPT_OK)
216  {
217  return err;
218  }
219  md-> sha256 .length += 64 * 8;
220  in += 64;
221  inlen -= 64;
222  }
223  else
224  {
225  n = MIN(inlen, (64 - md-> sha256 .curlen));
226  XMEMCPY(md-> sha256 .buf + md-> sha256.curlen, in, (size_t)n);
227  md-> sha256 .curlen += n;
228  in += n;
229  inlen -= n;
230  if (md-> sha256 .curlen == 64)
231  {
232  if ((err = sha256_compress(md, md-> sha256 .buf)) != CRYPT_OK)
233  {
234  return err;
235  }
236  md-> sha256 .length += 8 * 64;
237  md-> sha256 .curlen = 0;
238  }
239  }
240  }
241  return CRYPT_OK;
242 }
249 int sha256_done(hash_state * md, unsigned char *out)
250 {
251  int i;
252 
253  assert(md != NULL);
254  assert(out != NULL);
255 
256  if (md->sha256.curlen >= sizeof(md->sha256.buf)) {
257  return CRYPT_INVALID_ARG;
258  }
259 
260 
261  /* increase the length of the message */
262  md->sha256.length += md->sha256.curlen * 8;
263 
264  /* append the '1' bit */
265  md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80;
266 
267  /* if the length is currently above 56 bytes we append zeros
268  * then compress. Then we can fall back to padding zeros and length
269  * encoding like normal.
270  */
271  if (md->sha256.curlen > 56) {
272  while (md->sha256.curlen < 64) {
273  md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
274  }
275  sha256_compress(md, md->sha256.buf);
276  md->sha256.curlen = 0;
277  }
278 
279  /* pad upto 56 bytes of zeroes */
280  while (md->sha256.curlen < 56) {
281  md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
282  }
283 
284  /* store length */
285  STORE64H(md->sha256.length, md->sha256.buf+56);
286  sha256_compress(md, md->sha256.buf);
287 
288  /* copy output */
289  for (i = 0; i < 8; i++) {
290  STORE32H(md->sha256.state[i], out+(4*i));
291  }
292  return CRYPT_OK;
293 }
assert
Definition: mpl/assert.hpp:79
sha256_compress
static int sha256_compress(hash_state *md, const unsigned char *buf)
Definition: SHA256.cpp:56
Hash_state
Definition: SHA256.h:33
Gamma0
#define Gamma0(x)
Definition: SHA256.cpp:53
S
#define S(x, n)
Definition: SHA256.cpp:49
LOAD32H
#define LOAD32H(x, y)
Definition: SHA256.cpp:19
sha256_state::state
ulong32 state[8]
Definition: SHA256.h:20
STORE64H
#define STORE64H(x, y)
Definition: SHA256.cpp:29
sha256_process
int sha256_process(hash_state *md, const ulong8 *in, ulong32 inlen)
Definition: SHA256.cpp:195
ulong32
uint32_t ulong32
Definition: SHA256.h:15
sha256_init
int sha256_init(hash_state *md)
Definition: SHA256.cpp:163
SHA256.h
XMEMCPY
#define XMEMCPY
Definition: SHA256.cpp:35
sha256_state::length
ulong64 length
Definition: SHA256.h:19
ulong8
uint8_t ulong8
Definition: SHA256.h:14
STORE32H
#define STORE32H(x, y)
Definition: SHA256.cpp:25
sha256_state::curlen
ulong32 curlen
Definition: SHA256.h:20
MIN
#define MIN(x, y)
Definition: SHA256.cpp:40
sha256_state::buf
unsigned char buf[64]
Definition: SHA256.h:21
boost::iterators::i
D const & i
Definition: iterator_facade.hpp:956
Hash_state::sha256
struct sha256_state sha256
Definition: SHA256.h:35
CRYPT_HASH_OVERFLOW
@ CRYPT_HASH_OVERFLOW
Definition: SHA256.h:30
Gamma1
#define Gamma1(x)
Definition: SHA256.cpp:54
sha256_done
int sha256_done(hash_state *md, unsigned char *out)
Definition: SHA256.cpp:249
CRYPT_OK
@ CRYPT_OK
Definition: SHA256.h:27
CRYPT_INVALID_ARG
@ CRYPT_INVALID_ARG
Definition: SHA256.h:29
RND
#define RND(a, b, c, d, e, f, g, h, i, ki)


sick_visionary_ros
Author(s): SICK AG TechSupport 3D Snapshot
autogenerated on Thu Feb 8 2024 03:48:29