RINASim  October 2016
Documentation of framework for OMNeT++
SHA256.cc
Go to the documentation of this file.
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2014-2016 Brno University of Technology, PRISTINE project
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 
23 #include <SHA256.h>
24 
26 
27 
28 }
29 
31 
32 }
33 
34 const unsigned int SHA256::sha256_k[64] = //UL = uint32
35  {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
36  0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
37  0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
38  0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
39  0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
40  0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
41  0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
42  0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
43  0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
44  0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
45  0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
46  0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
47  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
48  0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
49  0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
50  0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
51 
52 void SHA256::transform(const unsigned char *message, unsigned int block_nb)
53 {
54  uint32 w[64];
55  uint32 wv[8];
56  uint32 t1, t2;
57  const unsigned char *sub_block;
58  int i;
59  int j;
60  for (i = 0; i < (int) block_nb; i++) {
61  sub_block = message + (i << 6);
62  for (j = 0; j < 16; j++) {
63  SHA2_PACK32(&sub_block[j << 2], &w[j]);
64  }
65  for (j = 16; j < 64; j++) {
66  w[j] = SHA256_F4(w[j - 2]) + w[j - 7] + SHA256_F3(w[j - 15]) + w[j - 16];
67  }
68  for (j = 0; j < 8; j++) {
69  wv[j] = m_h[j];
70  }
71  for (j = 0; j < 64; j++) {
72  t1 = wv[7] + SHA256_F2(wv[4]) + SHA2_CH(wv[4], wv[5], wv[6])
73  + sha256_k[j] + w[j];
74  t2 = SHA256_F1(wv[0]) + SHA2_MAJ(wv[0], wv[1], wv[2]);
75  wv[7] = wv[6];
76  wv[6] = wv[5];
77  wv[5] = wv[4];
78  wv[4] = wv[3] + t1;
79  wv[3] = wv[2];
80  wv[2] = wv[1];
81  wv[1] = wv[0];
82  wv[0] = t1 + t2;
83  }
84  for (j = 0; j < 8; j++) {
85  m_h[j] += wv[j];
86  }
87  }
88 }
89 
91 {
92  m_h[0] = 0x6a09e667;
93  m_h[1] = 0xbb67ae85;
94  m_h[2] = 0x3c6ef372;
95  m_h[3] = 0xa54ff53a;
96  m_h[4] = 0x510e527f;
97  m_h[5] = 0x9b05688c;
98  m_h[6] = 0x1f83d9ab;
99  m_h[7] = 0x5be0cd19;
100  m_len = 0;
101  m_tot_len = 0;
102 }
103 
104 void SHA256::update(const unsigned char *message, unsigned int len)
105 {
106  unsigned int block_nb;
107  unsigned int new_len, rem_len, tmp_len;
108  const unsigned char *shifted_message;
109  tmp_len = SHA224_256_BLOCK_SIZE - m_len;
110  rem_len = len < tmp_len ? len : tmp_len;
111  memcpy(&m_block[m_len], message, rem_len);
112  if (m_len + len < SHA224_256_BLOCK_SIZE) {
113  m_len += len;
114  return;
115  }
116  new_len = len - rem_len;
117  block_nb = new_len / SHA224_256_BLOCK_SIZE;
118  shifted_message = message + rem_len;
119  transform(m_block, 1);
120  transform(shifted_message, block_nb);
121  rem_len = new_len % SHA224_256_BLOCK_SIZE;
122  memcpy(m_block, &shifted_message[block_nb << 6], rem_len);
123  m_len = rem_len;
124  m_tot_len += (block_nb + 1) << 6;
125 }
126 
127 void SHA256::final(unsigned char *digest)
128 {
129  unsigned int block_nb;
130  unsigned int pm_len;
131  unsigned int len_b;
132  int i;
133  block_nb = (1 + ((SHA224_256_BLOCK_SIZE - 9)
135  len_b = (m_tot_len + m_len) << 3;
136  pm_len = block_nb << 6;
137  memset(m_block + m_len, 0, pm_len - m_len);
138  m_block[m_len] = 0x80;
139  SHA2_UNPACK32(len_b, m_block + pm_len - 4);
140  transform(m_block, block_nb);
141  for (i = 0 ; i < 8; i++) {
142  SHA2_UNPACK32(m_h[i], &digest[i << 2]);
143  }
144 }
145 
146 std::string sha256(std::string input)
147 {
148  unsigned char digest[SHA256::DIGEST_SIZE];
149  memset(digest,0,SHA256::DIGEST_SIZE);
150 
151  SHA256 ctx = SHA256();
152  ctx.init();
153  ctx.update( (unsigned char*)input.c_str(), input.length());
154  ctx.final(digest);
155 
156  char buf[2*SHA256::DIGEST_SIZE+1];
157  buf[2*SHA256::DIGEST_SIZE] = 0;
158  for (unsigned int i = 0; i < SHA256::DIGEST_SIZE; i++)
159  sprintf(buf+i*2, "%02x", digest[i]);
160  return std::string(buf);
161 }
#define SHA2_PACK32(str, x)
Definition: SHA256.h:76
Definition: SHA256.h:30
#define SHA2_MAJ(x, y, z)
Definition: SHA256.h:64
static const unsigned int DIGEST_SIZE
Definition: SHA256.h:46
void init()
Definition: SHA256.cc:90
unsigned char m_block[2 *SHA224_256_BLOCK_SIZE]
Definition: SHA256.h:52
#define SHA2_CH(x, y, z)
Definition: SHA256.h:63
void transform(const unsigned char *message, unsigned int block_nb)
Definition: SHA256.cc:52
void final(unsigned char *digest)
Definition: SHA256.cc:127
static const unsigned int SHA224_256_BLOCK_SIZE
Definition: SHA256.h:41
uint32 m_h[8]
Definition: SHA256.h:53
static const uint32 sha256_k[]
Definition: SHA256.h:40
std::string sha256(std::string input)
Definition: SHA256.cc:146
#define SHA256_F3(x)
Definition: SHA256.h:67
unsigned int m_tot_len
Definition: SHA256.h:50
unsigned int m_len
Definition: SHA256.h:51
unsigned int uint32
Definition: SHA256.h:37
SHA256()
Definition: SHA256.cc:25
#define SHA256_F4(x)
Definition: SHA256.h:68
#define SHA256_F2(x)
Definition: SHA256.h:66
#define SHA256_F1(x)
Definition: SHA256.h:65
#define SHA2_UNPACK32(x, str)
Definition: SHA256.h:69
void update(const unsigned char *message, unsigned int len)
Definition: SHA256.cc:104
virtual ~SHA256()
Definition: SHA256.cc:30