Botan 1.10.17

sha160.cpp
Go to the documentation of this file.
1 /*
2 * SHA-160
3 * (C) 1999-2008,2011 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/sha160.h>
9 #include <botan/loadstor.h>
10 #include <botan/rotate.h>
11
12 namespace Botan {
13
14 namespace SHA1_F {
15
16 namespace {
17
18 /*
19 * SHA-160 F1 Function
20 */
21 inline void F1(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg)
22 {
23 E += (D ^ (B & (C ^ D))) + msg + 0x5A827999 + rotate_left(A, 5);
24 B = rotate_left(B, 30);
25 }
26
27 /*
28 * SHA-160 F2 Function
29 */
30 inline void F2(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg)
31 {
32 E += (B ^ C ^ D) + msg + 0x6ED9EBA1 + rotate_left(A, 5);
33 B = rotate_left(B, 30);
34 }
35
36 /*
37 * SHA-160 F3 Function
38 */
39 inline void F3(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg)
40 {
41 E += ((B & C) | ((B | C) & D)) + msg + 0x8F1BBCDC + rotate_left(A, 5);
42 B = rotate_left(B, 30);
43 }
44
45 /*
46 * SHA-160 F4 Function
47 */
48 inline void F4(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg)
49 {
50 E += (B ^ C ^ D) + msg + 0xCA62C1D6 + rotate_left(A, 5);
51 B = rotate_left(B, 30);
52 }
53
54 }
55
56 }
57
58 /*
59 * SHA-160 Compression Function
60 */
61 void SHA_160::compress_n(const byte input[], size_t blocks)
62 {
63 using namespace SHA1_F;
64
65 u32bit A = digest[0], B = digest[1], C = digest[2],
66 D = digest[3], E = digest[4];
67
68 for(size_t i = 0; i != blocks; ++i)
69 {
70 load_be(&W[0], input, 16);
71
72 for(size_t j = 16; j != 80; j += 8)
73 {
74 W[j ] = rotate_left((W[j-3] ^ W[j-8] ^ W[j-14] ^ W[j-16]), 1);
75 W[j+1] = rotate_left((W[j-2] ^ W[j-7] ^ W[j-13] ^ W[j-15]), 1);
76 W[j+2] = rotate_left((W[j-1] ^ W[j-6] ^ W[j-12] ^ W[j-14]), 1);
77 W[j+3] = rotate_left((W[j ] ^ W[j-5] ^ W[j-11] ^ W[j-13]), 1);
78 W[j+4] = rotate_left((W[j+1] ^ W[j-4] ^ W[j-10] ^ W[j-12]), 1);
79 W[j+5] = rotate_left((W[j+2] ^ W[j-3] ^ W[j- 9] ^ W[j-11]), 1);
80 W[j+6] = rotate_left((W[j+3] ^ W[j-2] ^ W[j- 8] ^ W[j-10]), 1);
81 W[j+7] = rotate_left((W[j+4] ^ W[j-1] ^ W[j- 7] ^ W[j- 9]), 1);
82 }
83
84 F1(A, B, C, D, E, W[ 0]); F1(E, A, B, C, D, W[ 1]);
85 F1(D, E, A, B, C, W[ 2]); F1(C, D, E, A, B, W[ 3]);
86 F1(B, C, D, E, A, W[ 4]); F1(A, B, C, D, E, W[ 5]);
87 F1(E, A, B, C, D, W[ 6]); F1(D, E, A, B, C, W[ 7]);
88 F1(C, D, E, A, B, W[ 8]); F1(B, C, D, E, A, W[ 9]);
89 F1(A, B, C, D, E, W[10]); F1(E, A, B, C, D, W[11]);
90 F1(D, E, A, B, C, W[12]); F1(C, D, E, A, B, W[13]);
91 F1(B, C, D, E, A, W[14]); F1(A, B, C, D, E, W[15]);
92 F1(E, A, B, C, D, W[16]); F1(D, E, A, B, C, W[17]);
93 F1(C, D, E, A, B, W[18]); F1(B, C, D, E, A, W[19]);
94
95 F2(A, B, C, D, E, W[20]); F2(E, A, B, C, D, W[21]);
96 F2(D, E, A, B, C, W[22]); F2(C, D, E, A, B, W[23]);
97 F2(B, C, D, E, A, W[24]); F2(A, B, C, D, E, W[25]);
98 F2(E, A, B, C, D, W[26]); F2(D, E, A, B, C, W[27]);
99 F2(C, D, E, A, B, W[28]); F2(B, C, D, E, A, W[29]);
100 F2(A, B, C, D, E, W[30]); F2(E, A, B, C, D, W[31]);
101 F2(D, E, A, B, C, W[32]); F2(C, D, E, A, B, W[33]);
102 F2(B, C, D, E, A, W[34]); F2(A, B, C, D, E, W[35]);
103 F2(E, A, B, C, D, W[36]); F2(D, E, A, B, C, W[37]);
104 F2(C, D, E, A, B, W[38]); F2(B, C, D, E, A, W[39]);
105
106 F3(A, B, C, D, E, W[40]); F3(E, A, B, C, D, W[41]);
107 F3(D, E, A, B, C, W[42]); F3(C, D, E, A, B, W[43]);
108 F3(B, C, D, E, A, W[44]); F3(A, B, C, D, E, W[45]);
109 F3(E, A, B, C, D, W[46]); F3(D, E, A, B, C, W[47]);
110 F3(C, D, E, A, B, W[48]); F3(B, C, D, E, A, W[49]);
111 F3(A, B, C, D, E, W[50]); F3(E, A, B, C, D, W[51]);
112 F3(D, E, A, B, C, W[52]); F3(C, D, E, A, B, W[53]);
113 F3(B, C, D, E, A, W[54]); F3(A, B, C, D, E, W[55]);
114 F3(E, A, B, C, D, W[56]); F3(D, E, A, B, C, W[57]);
115 F3(C, D, E, A, B, W[58]); F3(B, C, D, E, A, W[59]);
116
117 F4(A, B, C, D, E, W[60]); F4(E, A, B, C, D, W[61]);
118 F4(D, E, A, B, C, W[62]); F4(C, D, E, A, B, W[63]);
119 F4(B, C, D, E, A, W[64]); F4(A, B, C, D, E, W[65]);
120 F4(E, A, B, C, D, W[66]); F4(D, E, A, B, C, W[67]);
121 F4(C, D, E, A, B, W[68]); F4(B, C, D, E, A, W[69]);
122 F4(A, B, C, D, E, W[70]); F4(E, A, B, C, D, W[71]);
123 F4(D, E, A, B, C, W[72]); F4(C, D, E, A, B, W[73]);
124 F4(B, C, D, E, A, W[74]); F4(A, B, C, D, E, W[75]);
125 F4(E, A, B, C, D, W[76]); F4(D, E, A, B, C, W[77]);
126 F4(C, D, E, A, B, W[78]); F4(B, C, D, E, A, W[79]);
127
128 A = (digest[0] += A);
129 B = (digest[1] += B);
130 C = (digest[2] += C);
131 D = (digest[3] += D);
132 E = (digest[4] += E);
133
134 input += hash_block_size();
135 }
136 }
137
138 /*
139 * Copy out the digest
140 */
141 void SHA_160::copy_out(byte output[])
142 {
143 for(size_t i = 0; i != output_length(); i += 4)
144 store_be(digest[i/4], output + i);
145 }
146
147 /*
148 * Clear memory of sensitive data
149 */
150 void SHA_160::clear()
151 {
152 MDx_HashFunction::clear();
153 zeroise(W);
154 digest[0] = 0x67452301;
155 digest[1] = 0xEFCDAB89;
156 digest[2] = 0x98BADCFE;
157 digest[3] = 0x10325476;
158 digest[4] = 0xC3D2E1F0;
159 }
160
161 }
Botan::load_be
T load_be(const byte in[], size_t off)
Definition: loadstor.h:100
Botan::SHA_160::copy_out
void copy_out(byte[])
Definition: sha160.cpp:141
Botan::rotate_left
T rotate_left(T input, size_t rot)
Definition: rotate.h:21
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::SHA_160::W
SecureVector< u32bit > W
Definition: sha160.h:55
Botan::HAS_160_F::F2
void F2(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:29
Botan::HAS_160_F::F3
void F3(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:39
Botan::SHA_160::clear
void clear()
Definition: sha160.cpp:150
Botan::SHA_160::output_length
size_t output_length() const
Definition: sha160.h:22
Botan::SHA_160::compress_n
void compress_n(const byte[], size_t blocks)
Definition: sha160.cpp:61
Botan::SHA_160::digest
SecureVector< u32bit > digest
Definition: sha160.h:50
Botan
Definition: algo_base.h:14
Botan::HAS_160_F::F4
void F4(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:49
Botan::store_be
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:412
Botan::MDx_HashFunction::clear
void clear()
Definition: mdx_hash.cpp:32
Botan::MDx_HashFunction::hash_block_size
size_t hash_block_size() const
Definition: mdx_hash.h:32
Botan::HAS_160_F::F1
void F1(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:19
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428
Botan::u32bit
unsigned int u32bit
Definition: types.h:32