Botan  1.11.4
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  secure_vector<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  secure_vector<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.assign(key, key + (length / 2));
93  K2.assign(key + (length / 2), key + length);
94  }
95 
96 /*
97 * Clear memory of sensitive data
98 */
100  {
101  zap(K1);
102  zap(K2);
103  hash->clear();
104  }
105 
106 /*
107 * Return a clone of this object
108 */
110  {
111  return new LubyRackoff(hash->clone());
112  }
113 
114 /*
115 * Return the name of this type
116 */
117 std::string LubyRackoff::name() const
118  {
119  return "Luby-Rackoff(" + hash->name() + ")";
120  }
121 
122 /*
123 * Luby-Rackoff Constructor
124 */
126  {
127  }
128 
129 }