Version: SMASH-2.0
smash::sha256::Context Class Reference

#include <sha256.h>

A SHA256 context.

Definition at line 28 of file sha256.h.

Collaboration diagram for smash::sha256::Context:
[legend]

Public Member Functions

 Context ()
 Construct a SHA256 context. More...
 
void reset ()
 Reset the SHA256 context. More...
 
void update (uint8_t const *buffer, size_t buffer_size)
 Add data to the SHA256 context. More...
 
void update (const std::string &buffer)
 Add data to the SHA256 context. More...
 
Hash finalize ()
 Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit hash). More...
 

Private Member Functions

void transform_function (uint8_t const *buffer)
 Compress 512-bits. More...
 

Private Attributes

uint64_t length_
 Length of the SHA256 hash. More...
 
uint32_t state_ [8]
 State of the SHA256 hash. More...
 
size_t curlen_
 Current length of the SHA256 hash. More...
 
uint8_t buf_ [64]
 Buffer of the SHA256 hash. More...
 

Constructor & Destructor Documentation

◆ Context()

smash::sha256::Context::Context ( )
inline

Construct a SHA256 context.

Definition at line 46 of file sha256.h.

46 { reset(); }
Here is the call graph for this function:

Member Function Documentation

◆ transform_function()

void smash::sha256::Context::transform_function ( uint8_t const *  buffer)
private

Compress 512-bits.

Definition at line 95 of file sha256.cc.

95  {
96  uint32_t S[8];
97  uint32_t W[64];
98  uint32_t t0;
99  uint32_t t1;
100  uint32_t t;
101  int i;
102 
103  // Copy state into S
104  for (i = 0; i < 8; i++) {
105  S[i] = state_[i];
106  }
107 
108  // Copy the state into 512-bits into W[0..15]
109  for (i = 0; i < 16; i++) {
110  LOAD32H(W[i], buffer + (4 * i));
111  }
112 
113  // Fill W[16..63]
114  for (i = 16; i < 64; i++) {
115  W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
116  }
117 
118  // Compress
119  for (i = 0; i < 64; i++) {
120  Sha256Round(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i);
121  t = S[7];
122  S[7] = S[6];
123  S[6] = S[5];
124  S[5] = S[4];
125  S[4] = S[3];
126  S[3] = S[2];
127  S[2] = S[1];
128  S[1] = S[0];
129  S[0] = t;
130  }
131 
132  // Feedback
133  for (i = 0; i < 8; i++) {
134  state_[i] += S[i];
135  }
136 }
Here is the caller graph for this function:

◆ reset()

void smash::sha256::Context::reset ( )

Reset the SHA256 context.

Definition at line 140 of file sha256.cc.

140  {
141  curlen_ = 0;
142  length_ = 0;
143  state_[0] = 0x6A09E667UL;
144  state_[1] = 0xBB67AE85UL;
145  state_[2] = 0x3C6EF372UL;
146  state_[3] = 0xA54FF53AUL;
147  state_[4] = 0x510E527FUL;
148  state_[5] = 0x9B05688CUL;
149  state_[6] = 0x1F83D9ABUL;
150  state_[7] = 0x5BE0CD19UL;
151 }
Here is the caller graph for this function:

◆ update() [1/2]

void smash::sha256::Context::update ( uint8_t const *  buffer,
size_t  buffer_size 
)

Add data to the SHA256 context.

This will process the data and update the internal state of the context. Keep on calling this function until all the data has been added. Then call finalize to calculate the hash.

Definition at line 153 of file sha256.cc.

153  {
154  size_t n;
155 
156  if (curlen_ > sizeof(buf_)) {
157  return;
158  }
159 
160  while (buffer_size > 0) {
161  if (curlen_ == 0 && buffer_size >= BLOCK_SIZE) {
162  transform_function(buffer);
163  length_ += BLOCK_SIZE * 8;
164  buffer += BLOCK_SIZE;
165  buffer_size -= BLOCK_SIZE;
166  } else {
167  n = MIN(buffer_size, (BLOCK_SIZE - curlen_));
168  std::memcpy(buf_ + curlen_, buffer, n);
169  curlen_ += n;
170  buffer += n;
171  buffer_size -= n;
172  if (curlen_ == BLOCK_SIZE) {
174  length_ += 8 * BLOCK_SIZE;
175  curlen_ = 0;
176  }
177  }
178  }
179 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ update() [2/2]

void smash::sha256::Context::update ( const std::string &  buffer)

Add data to the SHA256 context.

This will process the data and update the internal state of the context. Keep on calling this function until all the data has been added. Then call finalize to calculate the hash.

Definition at line 181 of file sha256.cc.

181  {
182  update(reinterpret_cast<const uint8_t*>(buffer.c_str()), buffer.size());
183 }
Here is the call graph for this function:

◆ finalize()

Hash smash::sha256::Context::finalize ( )

Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit hash).

After calling this, reset must be used to reuse the context.

Definition at line 185 of file sha256.cc.

185  {
186  Hash digest{{}};
187  if (curlen_ >= sizeof(buf_)) {
188  return digest;
189  }
190 
191  // Increase the length of the message
192  length_ += curlen_ * 8;
193 
194  // Append the '1' bit
195  buf_[curlen_++] = 0x80;
196 
197  // if the length is currently above 56 bytes we append zeros
198  // then compress. Then we can fall back to padding zeros and length
199  // encoding like normal.
200  if (curlen_ > 56) {
201  while (curlen_ < 64) {
202  buf_[curlen_++] = 0;
203  }
205  curlen_ = 0;
206  }
207 
208  // Pad up to 56 bytes of zeroes
209  while (curlen_ < 56) {
210  buf_[curlen_++] = 0;
211  }
212 
213  // Store length
214  STORE64H(length_, buf_ + 56);
216 
217  // Copy output
218  for (int i = 0; i < 8; i++) {
219  STORE32H(state_[i], digest.data() + (4 * i));
220  }
221  return digest;
222 }
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ length_

uint64_t smash::sha256::Context::length_
private

Length of the SHA256 hash.

Definition at line 31 of file sha256.h.

◆ state_

uint32_t smash::sha256::Context::state_[8]
private

State of the SHA256 hash.

Definition at line 33 of file sha256.h.

◆ curlen_

size_t smash::sha256::Context::curlen_
private

Current length of the SHA256 hash.

Definition at line 35 of file sha256.h.

◆ buf_

uint8_t smash::sha256::Context::buf_[64]
private

Buffer of the SHA256 hash.

Definition at line 37 of file sha256.h.


The documentation for this class was generated from the following files:
smash::sha256::Context::state_
uint32_t state_[8]
State of the SHA256 hash.
Definition: sha256.h:33
smash::sha256::Context::update
void update(uint8_t const *buffer, size_t buffer_size)
Add data to the SHA256 context.
Definition: sha256.cc:153
smash::sha256::Context::transform_function
void transform_function(uint8_t const *buffer)
Compress 512-bits.
Definition: sha256.cc:95
smash::sha256::Context::length_
uint64_t length_
Length of the SHA256 hash.
Definition: sha256.h:31
Gamma1
#define Gamma1(x)
Definition: sha256.cc:59
STORE64H
#define STORE64H(x, y)
Definition: sha256.cc:40
Sha256Round
#define Sha256Round(a, b, c, d, e, f, g, h, i)
Definition: sha256.cc:61
smash::sha256::Context::buf_
uint8_t buf_[64]
Buffer of the SHA256 hash.
Definition: sha256.h:37
LOAD32H
#define LOAD32H(x, y)
Definition: sha256.cc:32
smash::sha256::Context::reset
void reset()
Reset the SHA256 context.
Definition: sha256.cc:140
STORE32H
#define STORE32H(x, y)
Definition: sha256.cc:24
MIN
#define MIN(x, y)
Definition: sha256.cc:22
smash::sha256::Hash
std::array< uint8_t, HASH_SIZE > Hash
A SHA256 hash.
Definition: sha256.h:25
Gamma0
#define Gamma0(x)
Definition: sha256.cc:58
smash::sha256::BLOCK_SIZE
constexpr size_t BLOCK_SIZE
Definition: sha256.cc:88
S
#define S(x, n)
Definition: sha256.cc:54
smash::pdg::n
constexpr int n
Neutron.
Definition: pdgcode_constants.h:30
smash::sha256::Context::curlen_
size_t curlen_
Current length of the SHA256 hash.
Definition: sha256.h:35