Botan 1.10.17

tiger.cpp
Go to the documentation of this file.
1 /*
2 * Tiger
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/tiger.h>
9 #include <botan/exceptn.h>
10 #include <botan/loadstor.h>
11 #include <botan/parsing.h>
12
13 namespace Botan {
14
15 namespace {
16
17 /*
18 * Tiger Mixing Function
19 */
20 inline void mix(MemoryRegion<u64bit>& X)
21 {
22 X[0] -= X[7] ^ 0xA5A5A5A5A5A5A5A5;
23 X[1] ^= X[0];
24 X[2] += X[1];
25 X[3] -= X[2] ^ ((~X[1]) << 19);
26 X[4] ^= X[3];
27 X[5] += X[4];
28 X[6] -= X[5] ^ ((~X[4]) >> 23);
29 X[7] ^= X[6];
30
31 X[0] += X[7];
32 X[1] -= X[0] ^ ((~X[7]) << 19);
33 X[2] ^= X[1];
34 X[3] += X[2];
35 X[4] -= X[3] ^ ((~X[2]) >> 23);
36 X[5] ^= X[4];
37 X[6] += X[5];
38 X[7] -= X[6] ^ 0x0123456789ABCDEF;
39 }
40
41 }
42
43 /*
44 * Tiger Compression Function
45 */
46 void Tiger::compress_n(const byte input[], size_t blocks)
47 {
48 u64bit A = digest[0], B = digest[1], C = digest[2];
49
50 for(size_t i = 0; i != blocks; ++i)
51 {
52 load_le(&X[0], input, X.size());
53
54 pass(A, B, C, X, 5); mix(X);
55 pass(C, A, B, X, 7); mix(X);
56 pass(B, C, A, X, 9);
57
58 for(size_t j = 3; j != passes; ++j)
59 {
60 mix(X);
61 pass(A, B, C, X, 9);
62 u64bit T = A; A = C; C = B; B = T;
63 }
64
65 A = (digest[0] ^= A);
66 B = digest[1] = B - digest[1];
67 C = (digest[2] += C);
68
69 input += hash_block_size();
70 }
71 }
72
73 /*
74 * Copy out the digest
75 */
76 void Tiger::copy_out(byte output[])
77 {
78 for(size_t i = 0; i != output_length(); ++i)
79 output[i] = get_byte(7 - (i % 8), digest[i/8]);
80 }
81
82 /*
83 * Tiger Pass
84 */
85 void Tiger::pass(u64bit& A, u64bit& B, u64bit& C,
86 const MemoryRegion<u64bit>& X,
87 byte mul)
88 {
89 C ^= X[0];
90 A -= SBOX1[get_byte(7, C)] ^ SBOX2[get_byte(5, C)] ^
91 SBOX3[get_byte(3, C)] ^ SBOX4[get_byte(1, C)];
92 B += SBOX1[get_byte(0, C)] ^ SBOX2[get_byte(2, C)] ^
93 SBOX3[get_byte(4, C)] ^ SBOX4[get_byte(6, C)];
94 B *= mul;
95
96 A ^= X[1];
97 B -= SBOX1[get_byte(7, A)] ^ SBOX2[get_byte(5, A)] ^
98 SBOX3[get_byte(3, A)] ^ SBOX4[get_byte(1, A)];
99 C += SBOX1[get_byte(0, A)] ^ SBOX2[get_byte(2, A)] ^
100 SBOX3[get_byte(4, A)] ^ SBOX4[get_byte(6, A)];
101 C *= mul;
102
103 B ^= X[2];
104 C -= SBOX1[get_byte(7, B)] ^ SBOX2[get_byte(5, B)] ^
105 SBOX3[get_byte(3, B)] ^ SBOX4[get_byte(1, B)];
106 A += SBOX1[get_byte(0, B)] ^ SBOX2[get_byte(2, B)] ^
107 SBOX3[get_byte(4, B)] ^ SBOX4[get_byte(6, B)];
108 A *= mul;
109
110 C ^= X[3];
111 A -= SBOX1[get_byte(7, C)] ^ SBOX2[get_byte(5, C)] ^
112 SBOX3[get_byte(3, C)] ^ SBOX4[get_byte(1, C)];
113 B += SBOX1[get_byte(0, C)] ^ SBOX2[get_byte(2, C)] ^
114 SBOX3[get_byte(4, C)] ^ SBOX4[get_byte(6, C)];
115 B *= mul;
116
117 A ^= X[4];
118 B -= SBOX1[get_byte(7, A)] ^ SBOX2[get_byte(5, A)] ^
119 SBOX3[get_byte(3, A)] ^ SBOX4[get_byte(1, A)];
120 C += SBOX1[get_byte(0, A)] ^ SBOX2[get_byte(2, A)] ^
121 SBOX3[get_byte(4, A)] ^ SBOX4[get_byte(6, A)];
122 C *= mul;
123
124 B ^= X[5];
125 C -= SBOX1[get_byte(7, B)] ^ SBOX2[get_byte(5, B)] ^
126 SBOX3[get_byte(3, B)] ^ SBOX4[get_byte(1, B)];
127 A += SBOX1[get_byte(0, B)] ^ SBOX2[get_byte(2, B)] ^
128 SBOX3[get_byte(4, B)] ^ SBOX4[get_byte(6, B)];
129 A *= mul;
130
131 C ^= X[6];
132 A -= SBOX1[get_byte(7, C)] ^ SBOX2[get_byte(5, C)] ^
133 SBOX3[get_byte(3, C)] ^ SBOX4[get_byte(1, C)];
134 B += SBOX1[get_byte(0, C)] ^ SBOX2[get_byte(2, C)] ^
135 SBOX3[get_byte(4, C)] ^ SBOX4[get_byte(6, C)];
136 B *= mul;
137
138 A ^= X[7];
139 B -= SBOX1[get_byte(7, A)] ^ SBOX2[get_byte(5, A)] ^
140 SBOX3[get_byte(3, A)] ^ SBOX4[get_byte(1, A)];
141 C += SBOX1[get_byte(0, A)] ^ SBOX2[get_byte(2, A)] ^
142 SBOX3[get_byte(4, A)] ^ SBOX4[get_byte(6, A)];
143 C *= mul;
144 }
145
146 /*
147 * Clear memory of sensitive data
148 */
149 void Tiger::clear()
150 {
151 MDx_HashFunction::clear();
152 zeroise(X);
153 digest[0] = 0x0123456789ABCDEF;
154 digest[1] = 0xFEDCBA9876543210;
155 digest[2] = 0xF096A5B4C3B2E187;
156 }
157
158 /*
159 * Return the name of this type
160 */
161 std::string Tiger::name() const
162 {
163 return "Tiger(" + to_string(output_length()) + "," + to_string(passes) + ")";
164 }
165
166 /*
167 * Tiger Constructor
168 */
169 Tiger::Tiger(size_t hash_len, size_t passes) :
170 MDx_HashFunction(64, false, false),
171 X(8),
172 digest(3),
173 hash_len(hash_len),
174 passes(passes)
175 {
176 if(output_length() != 16 && output_length() != 20 && output_length() != 24)
177 throw Invalid_Argument("Tiger: Illegal hash output size: " +
178 to_string(output_length()));
179
180 if(passes < 3)
181 throw Invalid_Argument("Tiger: Invalid number of passes: "
182 + to_string(passes));
183 clear();
184 }
185
186 }
Botan::load_le
T load_le(const byte in[], size_t off)
Definition: loadstor.h:116
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::Tiger::output_length
size_t output_length() const
Definition: tiger.h:22
Botan::get_byte
byte get_byte(size_t byte_num, T input)
Definition: get_byte.h:21
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::u64bit
unsigned long long u64bit
Definition: types.h:49
Botan::Tiger::name
std::string name() const
Definition: tiger.cpp:161
Botan::Tiger::Tiger
Tiger(size_t out_size=24, size_t passes=3)
Definition: tiger.cpp:169
Botan::MDx_HashFunction
Definition: mdx_hash.h:18
Botan
Definition: algo_base.h:14
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
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::to_string
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
Botan::Tiger::clear
void clear()
Definition: tiger.cpp:149
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428