Botan  2.6.0
Crypto and TLS for C++11
sha160.cpp
Go to the documentation of this file.
1 /*
2 * SHA-160
3 * (C) 1999-2008,2011 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/sha160.h>
9 #include <botan/cpuid.h>
10 
11 namespace Botan {
12 
13 std::unique_ptr<HashFunction> SHA_160::copy_state() const
14  {
15  return std::unique_ptr<HashFunction>(new SHA_160(*this));
16  }
17 
18 namespace SHA1_F {
19 
20 namespace {
21 
22 /*
23 * SHA-160 F1 Function
24 */
25 inline void F1(uint32_t A, uint32_t& B, uint32_t C, uint32_t D, uint32_t& E, uint32_t msg)
26  {
27  E += (D ^ (B & (C ^ D))) + msg + 0x5A827999 + rotl<5>(A);
28  B = rotl<30>(B);
29  }
30 
31 /*
32 * SHA-160 F2 Function
33 */
34 inline void F2(uint32_t A, uint32_t& B, uint32_t C, uint32_t D, uint32_t& E, uint32_t msg)
35  {
36  E += (B ^ C ^ D) + msg + 0x6ED9EBA1 + rotl<5>(A);
37  B = rotl<30>(B);
38  }
39 
40 /*
41 * SHA-160 F3 Function
42 */
43 inline void F3(uint32_t A, uint32_t& B, uint32_t C, uint32_t D, uint32_t& E, uint32_t msg)
44  {
45  E += ((B & C) | ((B | C) & D)) + msg + 0x8F1BBCDC + rotl<5>(A);
46  B = rotl<30>(B);
47  }
48 
49 /*
50 * SHA-160 F4 Function
51 */
52 inline void F4(uint32_t A, uint32_t& B, uint32_t C, uint32_t D, uint32_t& E, uint32_t msg)
53  {
54  E += (B ^ C ^ D) + msg + 0xCA62C1D6 + rotl<5>(A);
55  B = rotl<30>(B);
56  }
57 
58 }
59 
60 }
61 
62 /*
63 * SHA-160 Compression Function
64 */
65 void SHA_160::compress_n(const uint8_t input[], size_t blocks)
66  {
67  using namespace SHA1_F;
68 
69 #if defined(BOTAN_HAS_SHA1_X86_SHA_NI)
70  if(CPUID::has_intel_sha())
71  {
72  return sha1_compress_x86(m_digest, input, blocks);
73  }
74 #endif
75 
76 #if defined(BOTAN_HAS_SHA1_ARMV8)
77  if(CPUID::has_arm_sha1())
78  {
79  return sha1_armv8_compress_n(m_digest, input, blocks);
80  }
81 #endif
82 
83 #if defined(BOTAN_HAS_SHA1_SSE2)
84  if(CPUID::has_sse2())
85  {
86  return sse2_compress_n(m_digest, input, blocks);
87  }
88 
89 #endif
90 
91  uint32_t A = m_digest[0], B = m_digest[1], C = m_digest[2],
92  D = m_digest[3], E = m_digest[4];
93 
94  m_W.resize(80);
95 
96  for(size_t i = 0; i != blocks; ++i)
97  {
98  load_be(m_W.data(), input, 16);
99 
100  for(size_t j = 16; j != 80; j += 8)
101  {
102  m_W[j ] = rotl<1>(m_W[j-3] ^ m_W[j-8] ^ m_W[j-14] ^ m_W[j-16]);
103  m_W[j+1] = rotl<1>(m_W[j-2] ^ m_W[j-7] ^ m_W[j-13] ^ m_W[j-15]);
104  m_W[j+2] = rotl<1>(m_W[j-1] ^ m_W[j-6] ^ m_W[j-12] ^ m_W[j-14]);
105  m_W[j+3] = rotl<1>(m_W[j ] ^ m_W[j-5] ^ m_W[j-11] ^ m_W[j-13]);
106  m_W[j+4] = rotl<1>(m_W[j+1] ^ m_W[j-4] ^ m_W[j-10] ^ m_W[j-12]);
107  m_W[j+5] = rotl<1>(m_W[j+2] ^ m_W[j-3] ^ m_W[j- 9] ^ m_W[j-11]);
108  m_W[j+6] = rotl<1>(m_W[j+3] ^ m_W[j-2] ^ m_W[j- 8] ^ m_W[j-10]);
109  m_W[j+7] = rotl<1>(m_W[j+4] ^ m_W[j-1] ^ m_W[j- 7] ^ m_W[j- 9]);
110  }
111 
112  F1(A, B, C, D, E, m_W[ 0]); F1(E, A, B, C, D, m_W[ 1]);
113  F1(D, E, A, B, C, m_W[ 2]); F1(C, D, E, A, B, m_W[ 3]);
114  F1(B, C, D, E, A, m_W[ 4]); F1(A, B, C, D, E, m_W[ 5]);
115  F1(E, A, B, C, D, m_W[ 6]); F1(D, E, A, B, C, m_W[ 7]);
116  F1(C, D, E, A, B, m_W[ 8]); F1(B, C, D, E, A, m_W[ 9]);
117  F1(A, B, C, D, E, m_W[10]); F1(E, A, B, C, D, m_W[11]);
118  F1(D, E, A, B, C, m_W[12]); F1(C, D, E, A, B, m_W[13]);
119  F1(B, C, D, E, A, m_W[14]); F1(A, B, C, D, E, m_W[15]);
120  F1(E, A, B, C, D, m_W[16]); F1(D, E, A, B, C, m_W[17]);
121  F1(C, D, E, A, B, m_W[18]); F1(B, C, D, E, A, m_W[19]);
122 
123  F2(A, B, C, D, E, m_W[20]); F2(E, A, B, C, D, m_W[21]);
124  F2(D, E, A, B, C, m_W[22]); F2(C, D, E, A, B, m_W[23]);
125  F2(B, C, D, E, A, m_W[24]); F2(A, B, C, D, E, m_W[25]);
126  F2(E, A, B, C, D, m_W[26]); F2(D, E, A, B, C, m_W[27]);
127  F2(C, D, E, A, B, m_W[28]); F2(B, C, D, E, A, m_W[29]);
128  F2(A, B, C, D, E, m_W[30]); F2(E, A, B, C, D, m_W[31]);
129  F2(D, E, A, B, C, m_W[32]); F2(C, D, E, A, B, m_W[33]);
130  F2(B, C, D, E, A, m_W[34]); F2(A, B, C, D, E, m_W[35]);
131  F2(E, A, B, C, D, m_W[36]); F2(D, E, A, B, C, m_W[37]);
132  F2(C, D, E, A, B, m_W[38]); F2(B, C, D, E, A, m_W[39]);
133 
134  F3(A, B, C, D, E, m_W[40]); F3(E, A, B, C, D, m_W[41]);
135  F3(D, E, A, B, C, m_W[42]); F3(C, D, E, A, B, m_W[43]);
136  F3(B, C, D, E, A, m_W[44]); F3(A, B, C, D, E, m_W[45]);
137  F3(E, A, B, C, D, m_W[46]); F3(D, E, A, B, C, m_W[47]);
138  F3(C, D, E, A, B, m_W[48]); F3(B, C, D, E, A, m_W[49]);
139  F3(A, B, C, D, E, m_W[50]); F3(E, A, B, C, D, m_W[51]);
140  F3(D, E, A, B, C, m_W[52]); F3(C, D, E, A, B, m_W[53]);
141  F3(B, C, D, E, A, m_W[54]); F3(A, B, C, D, E, m_W[55]);
142  F3(E, A, B, C, D, m_W[56]); F3(D, E, A, B, C, m_W[57]);
143  F3(C, D, E, A, B, m_W[58]); F3(B, C, D, E, A, m_W[59]);
144 
145  F4(A, B, C, D, E, m_W[60]); F4(E, A, B, C, D, m_W[61]);
146  F4(D, E, A, B, C, m_W[62]); F4(C, D, E, A, B, m_W[63]);
147  F4(B, C, D, E, A, m_W[64]); F4(A, B, C, D, E, m_W[65]);
148  F4(E, A, B, C, D, m_W[66]); F4(D, E, A, B, C, m_W[67]);
149  F4(C, D, E, A, B, m_W[68]); F4(B, C, D, E, A, m_W[69]);
150  F4(A, B, C, D, E, m_W[70]); F4(E, A, B, C, D, m_W[71]);
151  F4(D, E, A, B, C, m_W[72]); F4(C, D, E, A, B, m_W[73]);
152  F4(B, C, D, E, A, m_W[74]); F4(A, B, C, D, E, m_W[75]);
153  F4(E, A, B, C, D, m_W[76]); F4(D, E, A, B, C, m_W[77]);
154  F4(C, D, E, A, B, m_W[78]); F4(B, C, D, E, A, m_W[79]);
155 
156  A = (m_digest[0] += A);
157  B = (m_digest[1] += B);
158  C = (m_digest[2] += C);
159  D = (m_digest[3] += D);
160  E = (m_digest[4] += E);
161 
162  input += hash_block_size();
163  }
164  }
165 
166 /*
167 * Copy out the digest
168 */
169 void SHA_160::copy_out(uint8_t output[])
170  {
171  copy_out_vec_be(output, output_length(), m_digest);
172  }
173 
174 /*
175 * Clear memory of sensitive data
176 */
178  {
180  zeroise(m_W);
181  m_digest[0] = 0x67452301;
182  m_digest[1] = 0xEFCDAB89;
183  m_digest[2] = 0x98BADCFE;
184  m_digest[3] = 0x10325476;
185  m_digest[4] = 0xC3D2E1F0;
186  }
187 
188 }
void copy_out_vec_be(uint8_t out[], size_t out_bytes, const std::vector< T, Alloc > &in)
Definition: loadstor.h:669
void clear() override
Definition: mdx_hash.cpp:33
std::unique_ptr< HashFunction > copy_state() const override
Definition: sha160.cpp:13
void clear() override
Definition: sha160.cpp:177
size_t output_length() const override
Definition: sha160.h:22
size_t hash_block_size() const override final
Definition: mdx_hash.h:32
T load_be(const uint8_t in[], size_t off)
Definition: loadstor.h:105
Definition: alg_id.cpp:13
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183