monotone

monotone Mtn Source Tree

Root/botan/dlies.cpp

1/*************************************************
2* DLIES Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/dlies.h>
7#include <botan/lookup.h>
8#include <botan/look_pk.h>
9#include <botan/bit_ops.h>
10#include <memory>
11
12namespace Botan {
13
14/*************************************************
15* DLIES_Encryptor Constructor *
16*************************************************/
17DLIES_Encryptor::DLIES_Encryptor(const PK_Key_Agreement_Key& k,
18 const std::string& kdf,
19 const std::string& mac, u32bit mk_len) :
20 key(k), kdf_algo(kdf), mac_algo(mac), MAC_KEYLEN(mk_len)
21 {
22 }
23
24/*************************************************
25* DLIES Encryption *
26*************************************************/
27SecureVector<byte> DLIES_Encryptor::enc(const byte in[], u32bit length) const
28 {
29 if(length > maximum_input_size())
30 throw Invalid_Argument("DLIES: Plaintext too large");
31 if(other_key.is_empty())
32 throw Invalid_State("DLIES: The other key was never set");
33
34 std::auto_ptr<KDF> kdf(get_kdf(kdf_algo));
35 std::auto_ptr<MessageAuthenticationCode> mac(get_mac(mac_algo));
36
37 MemoryVector<byte> v = key.public_value();
38
39 SecureVector<byte> out(v.size() + length + mac->OUTPUT_LENGTH);
40 out.copy(v, v.size());
41 out.copy(v.size(), in, length);
42
43 SecureVector<byte> vz(v, key.derive_key(other_key, other_key.size()));
44
45 const u32bit K_LENGTH = length + MAC_KEYLEN;
46 OctetString K = kdf->derive_key(K_LENGTH, vz, vz.size());
47 if(K.length() != K_LENGTH)
48 throw Encoding_Error("DLIES: KDF did not provide sufficient output");
49 byte* C = out + v.size();
50
51 xor_buf(C, K.begin() + MAC_KEYLEN, length);
52 mac->set_key(K.begin(), MAC_KEYLEN);
53
54 mac->update(C, length);
55 for(u32bit j = 0; j != 8; ++j)
56 mac->update(0);
57
58 mac->final(C + length);
59
60 return out;
61 }
62
63/*************************************************
64* Set the other parties public key *
65*************************************************/
66void DLIES_Encryptor::set_other_key(const MemoryRegion<byte>& ok)
67 {
68 other_key = ok;
69 }
70
71/*************************************************
72* Return the max size, in bytes, of a message *
73*************************************************/
74u32bit DLIES_Encryptor::maximum_input_size() const
75 {
76 return 32;
77 }
78
79/*************************************************
80* DLIES_Decryptor Constructor *
81*************************************************/
82DLIES_Decryptor::DLIES_Decryptor(const PK_Key_Agreement_Key& k,
83 const std::string& kdf,
84 const std::string& mac, u32bit mk_len) :
85 key(k), kdf_algo(kdf), mac_algo(mac),
86 MAC_KEYLEN(mk_len), PUBLIC_LEN(key.public_value().size())
87 {
88 }
89
90/*************************************************
91* DLIES Decryption *
92*************************************************/
93SecureVector<byte> DLIES_Decryptor::dec(const byte msg[], u32bit length) const
94 {
95 std::auto_ptr<MessageAuthenticationCode> mac(get_mac(mac_algo));
96
97 if(length < PUBLIC_LEN + mac->OUTPUT_LENGTH)
98 throw Decoding_Error("DLIES decryption: ciphertext is too short");
99
100 std::auto_ptr<KDF> kdf(get_kdf(kdf_algo));
101
102 const u32bit CIPHER_LEN = length - PUBLIC_LEN - mac->OUTPUT_LENGTH;
103
104 SecureVector<byte> v(msg, PUBLIC_LEN);
105 SecureVector<byte> C(msg + PUBLIC_LEN, CIPHER_LEN);
106 SecureVector<byte> T(msg + PUBLIC_LEN + CIPHER_LEN, mac->OUTPUT_LENGTH);
107
108 SecureVector<byte> vz(v, key.derive_key(v, v.size()));
109
110 const u32bit K_LENGTH = C.size() + MAC_KEYLEN;
111 OctetString K = kdf->derive_key(K_LENGTH, vz, vz.size());
112 if(K.length() != K_LENGTH)
113 throw Encoding_Error("DLIES: KDF did not provide sufficient output");
114
115 mac->set_key(K.begin(), MAC_KEYLEN);
116 mac->update(C);
117 for(u32bit j = 0; j != 8; ++j)
118 mac->update(0);
119 SecureVector<byte> T2 = mac->final();
120 if(T != T2)
121 throw Integrity_Failure("DLIES: message authentication failed");
122
123 xor_buf(C, K.begin() + MAC_KEYLEN, C.size());
124
125 return C;
126 }
127
128}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status