Sha2.hpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #ifndef ICL_CORE_CRYPT_SHA2_HPP_INCLUDED
24 #define ICL_CORE_CRYPT_SHA2_HPP_INCLUDED
25 
26 #include <sstream>
27 #include <iomanip>
28 #include <string.h>
29 
30 namespace icl_core {
31 namespace crypt {
32 
33 #define TEMPLATEM template <typename T, T t_h0, T t_h1, T t_h2, T t_h3, T t_h4, T t_h5, T t_h6, T t_h7, size_t t_len>
34 #define CLASSM Sha2<T, t_h0, t_h1, t_h2, t_h3, t_h4, t_h5, t_h6, t_h7, t_len>
35 #define IMPLM Sha2Impl<T, t_h0, t_h1, t_h2, t_h3, t_h4, t_h5, t_h6, t_h7, t_len>
36 
38 CLASSM::Sha2()
39  : IMPLM()
40 {
41 }
42 
44 CLASSM& CLASSM::process(const char *data)
45 {
46  for (; *data != 0; ++data)
47  {
48  if (m_buffer_fill == cMESSAGE_BLOCK_SIZE)
49  {
50  processBuffer();
51  m_buffer_fill = 0;
52  }
53  m_buffer[m_buffer_fill++] = *data;
54  ++m_message_size;
55  }
56  return *this;
57 }
58 
60 CLASSM& CLASSM::process(const void *data, size_t size)
61 {
62  const uint8_t *ptr = reinterpret_cast<const uint8_t *>(data);
63  size_t rest = size;
64 
65  // Fill the buffer completely as many times as possible.
66  while (rest >= cMESSAGE_BLOCK_SIZE-m_buffer_fill)
67  {
68  size_t amount = cMESSAGE_BLOCK_SIZE-m_buffer_fill;
69  ::memcpy(&m_buffer[m_buffer_fill], ptr, amount);
70  rest -= amount;
71  processBuffer();
72  ptr += amount;
73  m_message_size += amount;
74  m_buffer_fill = 0;
75  }
76  // Partially fill the buffer using the remaining data.
77  ::memcpy(&m_buffer[m_buffer_fill], ptr, rest);
78  m_message_size += rest;
79  m_buffer_fill += rest;
80  return *this;
81 }
82 
84 CLASSM& CLASSM::finalize()
85 {
86  finalizeBuffer(m_buffer_fill);
87  return *this;
88 }
89 
90 #define bswap64(i) ((i) >> 56 | \
91  (((i) >> 40) & 0x000000000000ff00ull) | \
92  (((i) >> 24) & 0x0000000000ff0000ull) | \
93  (((i) >> 8) & 0x00000000ff000000ull) | \
94  (((i) << 8) & 0x000000ff00000000ull) | \
95  (((i) << 24) & 0x0000ff0000000000ull) | \
96  (((i) << 40) & 0x00ff000000000000ull) | \
97  (i) << 56) \
98 
100 void CLASSM::finalizeBuffer(size_t size)
101 {
102  uint64_t message_size_bits = m_message_size*8;
103 
104  // Always pad a "1" bit (i.e., 0x80).
105  if (size < cMESSAGE_BLOCK_SIZE)
106  {
107  m_buffer[size++] = 0x80;
108  }
109  else
110  {
111  // Buffer is full, process first and then add the 0x80
112  processBuffer();
113  m_buffer[0] = 0x80;
114  size = 1;
115  }
116 
117  // Now pad to the padding position.
118  if (size <= cMESSAGE_PAD_POSITION)
119  {
120  for (size_t i = size; i < cMESSAGE_BLOCK_SIZE-8; ++i)
121  {
122  m_buffer[i] = 0;
123  }
124  m_buffer[cMESSAGE_BLOCK_SIZE-8] = uint8_t((message_size_bits >> 56) & 0xff);
125  m_buffer[cMESSAGE_BLOCK_SIZE-7] = uint8_t((message_size_bits >> 48) & 0xff);
126  m_buffer[cMESSAGE_BLOCK_SIZE-6] = uint8_t((message_size_bits >> 40) & 0xff);
127  m_buffer[cMESSAGE_BLOCK_SIZE-5] = uint8_t((message_size_bits >> 32) & 0xff);
128  m_buffer[cMESSAGE_BLOCK_SIZE-4] = uint8_t((message_size_bits >> 24) & 0xff);
129  m_buffer[cMESSAGE_BLOCK_SIZE-3] = uint8_t((message_size_bits >> 16) & 0xff);
130  m_buffer[cMESSAGE_BLOCK_SIZE-2] = uint8_t((message_size_bits >> 8) & 0xff);
131  m_buffer[cMESSAGE_BLOCK_SIZE-1] = uint8_t((message_size_bits ) & 0xff);
132  processBuffer();
133  }
134  else
135  {
136  // Pad buffer first and process. The message size goes into the
137  // next block.
138  for (size_t i = size; i < cMESSAGE_BLOCK_SIZE; ++i)
139  {
140  m_buffer[i] = 0;
141  }
142  processBuffer();
143 
144  // Pad the next block.
145  for (size_t i = 0; i < cMESSAGE_BLOCK_SIZE-8; ++i)
146  {
147  m_buffer[i] = 0;
148  }
149  m_buffer[cMESSAGE_BLOCK_SIZE-8] = uint8_t((message_size_bits >> 56) & 0xff);
150  m_buffer[cMESSAGE_BLOCK_SIZE-7] = uint8_t((message_size_bits >> 48) & 0xff);
151  m_buffer[cMESSAGE_BLOCK_SIZE-6] = uint8_t((message_size_bits >> 40) & 0xff);
152  m_buffer[cMESSAGE_BLOCK_SIZE-5] = uint8_t((message_size_bits >> 32) & 0xff);
153  m_buffer[cMESSAGE_BLOCK_SIZE-4] = uint8_t((message_size_bits >> 24) & 0xff);
154  m_buffer[cMESSAGE_BLOCK_SIZE-3] = uint8_t((message_size_bits >> 16) & 0xff);
155  m_buffer[cMESSAGE_BLOCK_SIZE-2] = uint8_t((message_size_bits >> 8) & 0xff);
156  m_buffer[cMESSAGE_BLOCK_SIZE-1] = uint8_t((message_size_bits ) & 0xff);
157  processBuffer();
158  }
159 }
160 
161 #undef bswap64
162 
163 #undef TEMPLATEM
164 #undef CLASSM
165 #undef IMPLM
166 
167 }
168 }
169 
170 #endif
void * memcpy(void *dest, void *src, size_t count)
Definition: os_mem.h:41
#define TEMPLATEM
Definition: Sha2.hpp:33
unsigned __int64 uint64_t
Definition: msvc_stdint.h:103
unsigned char uint8_t
Definition: msvc_stdint.h:91
#define IMPLM
Definition: Sha2.hpp:35
#define CLASSM
Definition: Sha2.hpp:34


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58