Botan  1.10.9
lubyrack.cpp
Go to the documentation of this file.
1 /*
2 * Luby-Rackoff
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/lubyrack.h>
9 #include <botan/internal/xor_buf.h>
10 
11 namespace Botan {
12 
13 /*
14 * Luby-Rackoff Encryption
15 */
16 void LubyRackoff::encrypt_n(const byte in[], byte out[], size_t blocks) const
17  {
18  const size_t len = hash->output_length();
19 
20  SecureVector<byte> buffer_vec(len);
21  byte* buffer = &buffer_vec[0];
22 
23  for(size_t i = 0; i != blocks; ++i)
24  {
25  hash->update(K1);
26  hash->update(in, len);
27  hash->final(buffer);
28  xor_buf(out + len, in + len, buffer, len);
29 
30  hash->update(K2);
31  hash->update(out + len, len);
32  hash->final(buffer);
33  xor_buf(out, in, buffer, len);
34 
35  hash->update(K1);
36  hash->update(out, len);
37  hash->final(buffer);
38  xor_buf(out + len, buffer, len);
39 
40  hash->update(K2);
41  hash->update(out + len, len);
42  hash->final(buffer);
43  xor_buf(out, buffer, len);
44 
45  in += 2 * len;
46  out += 2 * len;
47  }
48  }
49 
50 /*
51 * Luby-Rackoff Decryption
52 */
53 void LubyRackoff::decrypt_n(const byte in[], byte out[], size_t blocks) const
54  {
55  const size_t len = hash->output_length();
56 
57  SecureVector<byte> buffer_vec(len);
58  byte* buffer = &buffer_vec[0];
59 
60  for(size_t i = 0; i != blocks; ++i)
61  {
62  hash->update(K2);
63  hash->update(in + len, len);
64  hash->final(buffer);
65  xor_buf(out, in, buffer, len);
66 
67  hash->update(K1);
68  hash->update(out, len);
69  hash->final(buffer);
70  xor_buf(out + len, in + len, buffer, len);
71 
72  hash->update(K2);
73  hash->update(out + len, len);
74  hash->final(buffer);
75  xor_buf(out, buffer, len);
76 
77  hash->update(K1);
78  hash->update(out, len);
79  hash->final(buffer);
80  xor_buf(out + len, buffer, len);
81 
82  in += 2 * len;
83  out += 2 * len;
84  }
85  }
86 
87 /*
88 * Luby-Rackoff Key Schedule
89 */
90 void LubyRackoff::key_schedule(const byte key[], size_t length)
91  {
92  K1.resize(length / 2);
93  K2.resize(length / 2);
94  copy_mem(&K1[0], key , length / 2);
95  copy_mem(&K2[0], key + length / 2, length / 2);
96  }
97 
98 /*
99 * Clear memory of sensitive data
100 */
102  {
103  zeroise(K1);
104  zeroise(K2);
105  hash->clear();
106  }
107 
108 /*
109 * Return a clone of this object
110 */
112  {
113  return new LubyRackoff(hash->clone());
114  }
115 
116 /*
117 * Return the name of this type
118 */
119 std::string LubyRackoff::name() const
120  {
121  return "Luby-Rackoff(" + hash->name() + ")";
122  }
123 
124 /*
125 * Luby-Rackoff Constructor
126 */
128  {
129  }
130 
131 }
LubyRackoff(HashFunction *hash)
Definition: lubyrack.cpp:127
void resize(size_t n)
Definition: secmem.h:211
virtual void clear()=0
virtual HashFunction * clone() const =0
unsigned char byte
Definition: types.h:22
void update(const byte in[], size_t length)
Definition: buf_comp.h:33
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
virtual std::string name() const =0
void final(byte out[])
Definition: buf_comp.h:80
void encrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: lubyrack.cpp:16
BlockCipher * clone() const
Definition: lubyrack.cpp:111
void decrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: lubyrack.cpp:53
std::string name() const
Definition: lubyrack.cpp:119
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
virtual size_t output_length() const =0