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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/*
* DLIES
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#include <botan/dlies.h>
#include <botan/internal/xor_buf.h>
namespace Botan {
/*
* DLIES_Encryptor Constructor
*/
DLIES_Encryptor::DLIES_Encryptor(const PK_Key_Agreement_Key& key,
KDF* kdf_obj,
MessageAuthenticationCode* mac_obj,
u32bit mac_kl) :
ka(key, "Raw"),
kdf(kdf_obj),
mac(mac_obj),
mac_keylen(mac_kl)
{
my_key = key.public_value();
}
DLIES_Encryptor::~DLIES_Encryptor()
{
delete kdf;
delete mac;
}
/*
* DLIES Encryption
*/
SecureVector<byte> DLIES_Encryptor::enc(const byte in[], u32bit length,
RandomNumberGenerator&) const
{
if(length > maximum_input_size())
throw Invalid_Argument("DLIES: Plaintext too large");
if(other_key.empty())
throw Invalid_State("DLIES: The other key was never set");
SecureVector<byte> out(my_key.size() + length + mac->OUTPUT_LENGTH);
out.copy(&my_key[0], my_key.size());
out.copy(my_key.size(), in, length);
SecureVector<byte> vz = my_key;
vz.append(ka.derive_key(0, other_key).bits_of());
const u32bit K_LENGTH = length + mac_keylen;
OctetString K = kdf->derive_key(K_LENGTH, vz);
if(K.length() != K_LENGTH)
throw Encoding_Error("DLIES: KDF did not provide sufficient output");
byte* C = &out[my_key.size()];
xor_buf(C, K.begin() + mac_keylen, length);
mac->set_key(K.begin(), mac_keylen);
mac->update(C, length);
for(u32bit j = 0; j != 8; ++j)
mac->update(0);
mac->final(C + length);
return out;
}
/*
* Set the other parties public key
*/
void DLIES_Encryptor::set_other_key(const MemoryRegion<byte>& ok)
{
other_key = ok;
}
/*
* Return the max size, in bytes, of a message
*/
u32bit DLIES_Encryptor::maximum_input_size() const
{
return 32;
}
/*
* DLIES_Decryptor Constructor
*/
DLIES_Decryptor::DLIES_Decryptor(const PK_Key_Agreement_Key& key,
KDF* kdf_obj,
MessageAuthenticationCode* mac_obj,
u32bit mac_kl) :
ka(key, "Raw"),
kdf(kdf_obj),
mac(mac_obj),
mac_keylen(mac_kl)
{
my_key = key.public_value();
}
DLIES_Decryptor::~DLIES_Decryptor()
{
delete kdf;
delete mac;
}
/*
* DLIES Decryption
*/
SecureVector<byte> DLIES_Decryptor::dec(const byte msg[], u32bit length) const
{
if(length < my_key.size() + mac->OUTPUT_LENGTH)
throw Decoding_Error("DLIES decryption: ciphertext is too short");
const u32bit CIPHER_LEN = length - my_key.size() - mac->OUTPUT_LENGTH;
SecureVector<byte> v(msg, my_key.size());
SecureVector<byte> C(msg + my_key.size(), CIPHER_LEN);
SecureVector<byte> T(msg + my_key.size() + CIPHER_LEN, mac->OUTPUT_LENGTH);
SecureVector<byte> vz(msg, my_key.size());
vz.append(ka.derive_key(0, v).bits_of());
const u32bit K_LENGTH = C.size() + mac_keylen;
OctetString K = kdf->derive_key(K_LENGTH, vz);
if(K.length() != K_LENGTH)
throw Encoding_Error("DLIES: KDF did not provide sufficient output");
mac->set_key(K.begin(), mac_keylen);
mac->update(C);
for(u32bit j = 0; j != 8; ++j)
mac->update(0);
SecureVector<byte> T2 = mac->final();
if(T != T2)
throw Decoding_Error("DLIES: message authentication failed");
xor_buf(C, K.begin() + mac_keylen, C.size());
return C;
}
}
|