1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/*************************************************
* Luby-Rackoff Source File *
* (C) 1999-2006 The Botan Project *
*************************************************/
#include <botan/lubyrack.h>
#include <botan/lookup.h>
#include <botan/bit_ops.h>
namespace Botan {
/*************************************************
* Luby-Rackoff Encryption *
*************************************************/
void LubyRackoff::enc(const byte in[], byte out[]) const
{
const u32bit OUTPUT_LENGTH = hash->OUTPUT_LENGTH;
SecureVector<byte> buffer(OUTPUT_LENGTH);
hash->update(K1);
hash->update(in, OUTPUT_LENGTH);
hash->final(buffer);
xor_buf(out + OUTPUT_LENGTH, in + OUTPUT_LENGTH, buffer, OUTPUT_LENGTH);
hash->update(K2);
hash->update(out + OUTPUT_LENGTH, OUTPUT_LENGTH);
hash->final(buffer);
xor_buf(out, in, buffer, OUTPUT_LENGTH);
hash->update(K1);
hash->update(out, OUTPUT_LENGTH);
hash->final(buffer);
xor_buf(out + OUTPUT_LENGTH, buffer, OUTPUT_LENGTH);
hash->update(K2);
hash->update(out + OUTPUT_LENGTH, OUTPUT_LENGTH);
hash->final(buffer);
xor_buf(out, buffer, OUTPUT_LENGTH);
}
/*************************************************
* Luby-Rackoff Decryption *
*************************************************/
void LubyRackoff::dec(const byte in[], byte out[]) const
{
const u32bit OUTPUT_LENGTH = hash->OUTPUT_LENGTH;
SecureVector<byte> buffer(OUTPUT_LENGTH);
hash->update(K2);
hash->update(in + OUTPUT_LENGTH, OUTPUT_LENGTH);
hash->final(buffer);
xor_buf(out, in, buffer, OUTPUT_LENGTH);
hash->update(K1);
hash->update(out, OUTPUT_LENGTH);
hash->final(buffer);
xor_buf(out + OUTPUT_LENGTH, in + OUTPUT_LENGTH, buffer, OUTPUT_LENGTH);
hash->update(K2);
hash->update(out + OUTPUT_LENGTH, OUTPUT_LENGTH);
hash->final(buffer);
xor_buf(out, buffer, OUTPUT_LENGTH);
hash->update(K1);
hash->update(out, OUTPUT_LENGTH);
hash->final(buffer);
xor_buf(out + OUTPUT_LENGTH, buffer, OUTPUT_LENGTH);
}
/*************************************************
* Luby-Rackoff Key Schedule *
*************************************************/
void LubyRackoff::key(const byte key[], u32bit length)
{
K1.set(key, length / 2);
K2.set(key + length / 2, length / 2);
}
/*************************************************
* Clear memory of sensitive data *
*************************************************/
void LubyRackoff::clear() throw()
{
K1.clear();
K2.clear();
hash->clear();
}
/*************************************************
* Return a clone of this object *
*************************************************/
BlockCipher* LubyRackoff::clone() const
{
return new LubyRackoff(hash->name());
}
/*************************************************
* Return the name of this type *
*************************************************/
std::string LubyRackoff::name() const
{
return "Luby-Rackoff(" + hash->name() + ")";
}
/*************************************************
* Luby-Rackoff Constructor *
*************************************************/
LubyRackoff::LubyRackoff(const std::string& hash_name) :
BlockCipher(2*output_length_of(hash_name), 2, 32, 2),
hash(get_hash(hash_name))
{
}
}
|