aboutsummaryrefslogtreecommitdiffstats
path: root/lib/pubkey/dlies
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pubkey/dlies')
-rw-r--r--lib/pubkey/dlies/dlies.cpp146
-rw-r--r--lib/pubkey/dlies/dlies.h71
-rw-r--r--lib/pubkey/dlies/info.txt7
3 files changed, 224 insertions, 0 deletions
diff --git a/lib/pubkey/dlies/dlies.cpp b/lib/pubkey/dlies/dlies.cpp
new file mode 100644
index 000000000..715b55a36
--- /dev/null
+++ b/lib/pubkey/dlies/dlies.cpp
@@ -0,0 +1,146 @@
+/*
+* 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,
+ size_t 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
+*/
+std::vector<byte> DLIES_Encryptor::enc(const byte in[], size_t 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");
+
+ secure_vector<byte> out(my_key.size() + length + mac->output_length());
+ buffer_insert(out, 0, my_key);
+ buffer_insert(out, my_key.size(), in, length);
+
+ secure_vector<byte> vz(my_key.begin(), my_key.end());
+ vz += ka.derive_key(0, other_key).bits_of();
+
+ const size_t 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(size_t j = 0; j != 8; ++j)
+ mac->update(0);
+
+ mac->final(C + length);
+
+ return unlock(out);
+ }
+
+/*
+* Set the other parties public key
+*/
+void DLIES_Encryptor::set_other_key(const std::vector<byte>& ok)
+ {
+ other_key = ok;
+ }
+
+/*
+* Return the max size, in bytes, of a message
+*/
+size_t 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,
+ size_t 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
+*/
+secure_vector<byte> DLIES_Decryptor::dec(const byte msg[], size_t length) const
+ {
+ if(length < my_key.size() + mac->output_length())
+ throw Decoding_Error("DLIES decryption: ciphertext is too short");
+
+ const size_t CIPHER_LEN = length - my_key.size() - mac->output_length();
+
+ std::vector<byte> v(msg, msg + my_key.size());
+
+ secure_vector<byte> C(msg + my_key.size(), msg + my_key.size() + CIPHER_LEN);
+
+ secure_vector<byte> T(msg + my_key.size() + CIPHER_LEN,
+ msg + my_key.size() + CIPHER_LEN + mac->output_length());
+
+ secure_vector<byte> vz(msg, msg + my_key.size());
+ vz += ka.derive_key(0, v).bits_of();
+
+ const size_t 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(size_t j = 0; j != 8; ++j)
+ mac->update(0);
+ secure_vector<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;
+ }
+
+}
diff --git a/lib/pubkey/dlies/dlies.h b/lib/pubkey/dlies/dlies.h
new file mode 100644
index 000000000..9739afeb2
--- /dev/null
+++ b/lib/pubkey/dlies/dlies.h
@@ -0,0 +1,71 @@
+/*
+* DLIES
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_DLIES_H__
+#define BOTAN_DLIES_H__
+
+#include <botan/pubkey.h>
+#include <botan/mac.h>
+#include <botan/kdf.h>
+
+namespace Botan {
+
+/**
+* DLIES Encryption
+*/
+class BOTAN_DLL DLIES_Encryptor : public PK_Encryptor
+ {
+ public:
+ DLIES_Encryptor(const PK_Key_Agreement_Key&,
+ KDF* kdf,
+ MessageAuthenticationCode* mac,
+ size_t mac_key_len = 20);
+
+ ~DLIES_Encryptor();
+
+ void set_other_key(const std::vector<byte>&);
+ private:
+ std::vector<byte> enc(const byte[], size_t,
+ RandomNumberGenerator&) const;
+
+ size_t maximum_input_size() const;
+
+ std::vector<byte> other_key, my_key;
+
+ PK_Key_Agreement ka;
+ KDF* kdf;
+ MessageAuthenticationCode* mac;
+ size_t mac_keylen;
+ };
+
+/**
+* DLIES Decryption
+*/
+class BOTAN_DLL DLIES_Decryptor : public PK_Decryptor
+ {
+ public:
+ DLIES_Decryptor(const PK_Key_Agreement_Key&,
+ KDF* kdf,
+ MessageAuthenticationCode* mac,
+ size_t mac_key_len = 20);
+
+ ~DLIES_Decryptor();
+
+ private:
+ secure_vector<byte> dec(const byte[], size_t) const;
+
+ std::vector<byte> my_key;
+
+ PK_Key_Agreement ka;
+ KDF* kdf;
+ MessageAuthenticationCode* mac;
+ size_t mac_keylen;
+ };
+
+}
+
+#endif
diff --git a/lib/pubkey/dlies/info.txt b/lib/pubkey/dlies/info.txt
new file mode 100644
index 000000000..b159cc546
--- /dev/null
+++ b/lib/pubkey/dlies/info.txt
@@ -0,0 +1,7 @@
+define DLIES 20131128
+
+<requires>
+kdf
+libstate
+mac
+</requires>