Botan 1.10.17

wid_wake.cpp
Go to the documentation of this file.
1 /*
2 * WiderWake
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/wid_wake.h>
9 #include <botan/loadstor.h>
10 #include <botan/internal/xor_buf.h>
11
12 namespace Botan {
13
14 /*
15 * Combine cipher stream with message
16 */
17 void WiderWake_41_BE::cipher(const byte in[], byte out[], size_t length)
18 {
19 while(length >= buffer.size() - position)
20 {
21 xor_buf(out, in, &buffer[position], buffer.size() - position);
22 length -= (buffer.size() - position);
23 in += (buffer.size() - position);
24 out += (buffer.size() - position);
25 generate(buffer.size());
26 }
27 xor_buf(out, in, &buffer[position], length);
28 position += length;
29 }
30
31 /*
32 * Generate cipher stream
33 */
34 void WiderWake_41_BE::generate(size_t length)
35 {
36 u32bit R0 = state[0], R1 = state[1],
37 R2 = state[2], R3 = state[3],
38 R4 = state[4];
39
40 for(size_t i = 0; i != length; i += 8)
41 {
42 u32bit R0a;
43
44 store_be(R3, &buffer[i]);
45
46 R0a = R4 + R3; R3 += R2; R2 += R1; R1 += R0;
47 R0a = (R0a >> 8) ^ T[(R0a & 0xFF)];
48 R1 = (R1 >> 8) ^ T[(R1 & 0xFF)];
49 R2 = (R2 >> 8) ^ T[(R2 & 0xFF)];
50 R3 = (R3 >> 8) ^ T[(R3 & 0xFF)];
51 R4 = R0; R0 = R0a;
52
53 store_be(R3, &buffer[i + 4]);
54
55 R0a = R4 + R3; R3 += R2; R2 += R1; R1 += R0;
56 R0a = (R0a >> 8) ^ T[(R0a & 0xFF)];
57 R1 = (R1 >> 8) ^ T[(R1 & 0xFF)];
58 R2 = (R2 >> 8) ^ T[(R2 & 0xFF)];
59 R3 = (R3 >> 8) ^ T[(R3 & 0xFF)];
60 R4 = R0; R0 = R0a;
61 }
62
63 state[0] = R0;
64 state[1] = R1;
65 state[2] = R2;
66 state[3] = R3;
67 state[4] = R4;
68
69 position = 0;
70 }
71
72 /*
73 * WiderWake Key Schedule
74 */
75 void WiderWake_41_BE::key_schedule(const byte key[], size_t)
76 {
77 for(size_t i = 0; i != 4; ++i)
78 t_key[i] = load_be<u32bit>(key, i);
79
80 static const u32bit MAGIC[8] = {
81 0x726A8F3B, 0xE69A3B5C, 0xD3C71FE5, 0xAB3C73D2,
82 0x4D3A8EB3, 0x0396D6E8, 0x3D4C2F7A, 0x9EE27CF3 };
83
84 for(size_t i = 0; i != 4; ++i)
85 T[i] = t_key[i];
86
87 for(size_t i = 4; i != 256; ++i)
88 {
89 u32bit X = T[i-1] + T[i-4];
90 T[i] = (X >> 3) ^ MAGIC[X % 8];
91 }
92
93 for(size_t i = 0; i != 23; ++i)
94 T[i] += T[i+89];
95
96 u32bit X = T[33];
97 u32bit Z = (T[59] | 0x01000001) & 0xFF7FFFFF;
98 for(size_t i = 0; i != 256; ++i)
99 {
100 X = (X & 0xFF7FFFFF) + Z;
101 T[i] = (T[i] & 0x00FFFFFF) ^ X;
102 }
103
104 X = (T[X & 0xFF] ^ X) & 0xFF;
105 Z = T[0];
106 T[0] = T[X];
107 for(size_t i = 1; i != 256; ++i)
108 {
109 T[X] = T[i];
110 X = (T[i ^ X] ^ X) & 0xFF;
111 T[i] = T[X];
112 }
113 T[X] = Z;
114
115 position = 0;
116
117 const byte ZEROS[8] = { 0 };
118 set_iv(ZEROS, sizeof(ZEROS));
119 }
120
121 /*
122 * Resynchronization
123 */
124 void WiderWake_41_BE::set_iv(const byte iv[], size_t length)
125 {
126 if(!valid_iv_length(length))
127 throw Invalid_IV_Length(name(), length);
128
129 for(size_t i = 0; i != 4; ++i)
130 state[i] = t_key[i];
131
132 state[4] = load_be<u32bit>(iv, 0);
133 state[0] ^= state[4];
134 state[2] ^= load_be<u32bit>(iv, 1);
135
136 generate(8*4);
137 generate(buffer.size());
138 }
139
140 /*
141 * Clear memory of sensitive data
142 */
143 void WiderWake_41_BE::clear()
144 {
145 position = 0;
146 zeroise(t_key);
147 zeroise(state);
148 zeroise(T);
149 zeroise(buffer);
150 }
151
152 }
Botan::WiderWake_41_BE::name
std::string name() const
Definition: wid_wake.h:36
Botan::WiderWake_41_BE::clear
void clear()
Definition: wid_wake.cpp:143
Botan::Invalid_IV_Length
Definition: exceptn.h:78
R0
#define R0
Definition: asm_x86_64.h:51
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::WiderWake_41_BE::cipher
void cipher(const byte[], byte[], size_t)
Definition: wid_wake.cpp:17
Botan::WiderWake_41_BE::set_iv
void set_iv(const byte[], size_t)
Definition: wid_wake.cpp:124
R2
#define R2
Definition: asm_x86_64.h:53
Botan
Definition: algo_base.h:14
R1
#define R1
Definition: asm_x86_64.h:52
Botan::load_be< u32bit >
u32bit load_be< u32bit >(const byte in[], size_t off)
Definition: loadstor.h:166
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::WiderWake_41_BE::valid_iv_length
bool valid_iv_length(size_t iv_len) const
Definition: wid_wake.h:27
Botan::store_be
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:412
R4
#define R4
Definition: asm_x86_64.h:57
R3
#define R3
Definition: asm_x86_64.h:55
Botan::xor_buf
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428
Botan::u32bit
unsigned int u32bit
Definition: types.h:32