<commit_before>/*
* (C) 2014,2015 <NAME>
* (C) 2016 <NAME>, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_DLIES) && defined(BOTAN_HAS_DIFFIE_HELLMAN)
   #include "test_pubkey.h"
   #include <botan/dlies.h>
   #include <botan/dh.h>
#endif

namespace Botan_Tests {

namespace {

#if defined(BOTAN_HAS_DLIES) && defined(BOTAN_HAS_DIFFIE_HELLMAN)

class DLIES_KAT_Tests final : public Text_Based_Test
   {
   public:
      DLIES_KAT_Tests()
         : Text_Based_Test(
              "pubkey/dlies.vec",
              "Kdf,Mac,MacKeyLen,IV,Group,X1,X2,Msg,Ciphertext") {}

      Test::Result run_one_test(const std::string& cipher_algo, const VarMap& vars) override
         {
         const Botan::BigInt x1 = get_req_bn(vars, "X1");
         const Botan::BigInt x2 = get_req_bn(vars, "X2");

         const std::vector<uint8_t> input    = get_req_bin(vars, "Msg");
         const std::vector<uint8_t> expected = get_req_bin(vars, "Ciphertext");

         const std::string kdf_algo = get_req_str(vars, "Kdf");
         const std::string mac_algo = get_req_str(vars, "Mac");
         const size_t mac_key_len = get_req_sz(vars, "MacKeyLen");
         const std::string group_name = get_req_str(vars, "Group");

         const std::vector<uint8_t> iv = get_opt_bin(vars, "IV");

         Test::Result result("DLIES " + cipher_algo);

         std::unique_ptr<Botan::KDF> kdf(Botan::KDF::create(kdf_algo));
         if(!kdf)
            {
            result.test_note("Skipping due to missing KDF:  " + kdf_algo);
            return result;
            }

         std::unique_ptr<Botan::MAC> mac(Botan::MAC::create(mac_algo));
         if(!mac)
            {
            result.test_note("Skipping due to missing MAC:  " + mac_algo);
            return result;
            }

         std::unique_ptr<Botan::Cipher_Mode> enc;
         std::unique_ptr<Botan::Cipher_Mode> dec;
         size_t cipher_key_len = 0;

         if(cipher_algo != "XOR")
            {
            enc.reset(Botan::get_cipher_mode(cipher_algo, Botan::ENCRYPTION));
            dec.reset(Botan::get_cipher_mode(cipher_algo, Botan::DECRYPTION));

            cipher_key_len = enc->key_spec().maximum_keylength();
            }

         Botan::DL_Group domain(group_name);

         Botan::DH_PrivateKey from(Test::rng(), domain, x1);
         Botan::DH_PrivateKey to(Test::rng(), domain, x2);

         Botan::DLIES_Encryptor encryptor(from, Test::rng(), kdf->clone(), enc.release(), cipher_key_len, mac->clone(),
                                          mac_key_len);
         Botan::DLIES_Decryptor decryptor(to, Test::rng(), kdf.release(), dec.release(), cipher_key_len, mac.release(),<commit_msg>Avoid crash in DLIES tests if AEADs disabled<commit_after>/*
* (C) 2014,2015 <NAME>
* (C) 2016 <NAME>, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_DLIES) && defined(BOTAN_HAS_DIFFIE_HELLMAN)
   #include "test_pubkey.h"
   #include <botan/dlies.h>
   #include <botan/dh.h>
#endif

namespace Botan_Tests {

namespace {

#if defined(BOTAN_HAS_DLIES) && defined(BOTAN_HAS_DIFFIE_HELLMAN)

class DLIES_KAT_Tests final : public Text_Based_Test
   {
   public:
      DLIES_KAT_Tests()
         : Text_Based_Test(
              "pubkey/dlies.vec",
              "Kdf,Mac,MacKeyLen,IV,Group,X1,X2,Msg,Ciphertext") {}

      Test::Result run_one_test(const std::string& cipher_algo, const VarMap& vars) override
         {
         const Botan::BigInt x1 = get_req_bn(vars, "X1");
         const Botan::BigInt x2 = get_req_bn(vars, "X2");

         const std::vector<uint8_t> input    = get_req_bin(vars, "Msg");
         const std::vector<uint8_t> expected = get_req_bin(vars, "Ciphertext");

         const std::string kdf_algo = get_req_str(vars, "Kdf");
         const std::string mac_algo = get_req_str(vars, "Mac");
         const size_t mac_key_len = get_req_sz(vars, "MacKeyLen");
         const std::string group_name = get_req_str(vars, "Group");

         const std::vector<uint8_t> iv = get_opt_bin(vars, "IV");

         Test::Result result("DLIES " + cipher_algo);

         std::unique_ptr<Botan::KDF> kdf(Botan::KDF::create(kdf_algo));
         if(!kdf)
            {
            result.test_note("Skipping due to missing KDF:  " + kdf_algo);
            return result;
            }

         std::unique_ptr<Botan::MAC> mac(Botan::MAC::create(mac_algo));
         if(!mac)
            {
            result.test_note("Skipping due to missing MAC:  " + mac_algo);
            return result;
            }

         std::unique_ptr<Botan::Cipher_Mode> enc;
         std::unique_ptr<Botan::Cipher_Mode> dec;
         size_t cipher_key_len = 0;

         if(cipher_algo != "XOR")
            {
            enc.reset(Botan::get_cipher_mode(cipher_algo, Botan::ENCRYPTION));
            dec.reset(Botan::get_cipher_mode(cipher_algo, Botan::DECRYPTION));

            if(!enc || !dec)
               {
               result.test_note("Skipping due to missing cipher:  " + mac_algo);
               return result;
               }

            cipher_key_len = enc->key_spec().maximum_keylength();
            }

         Botan::DL_Group domain(group_name);

         Botan::DH_PrivateKey from(Test::rng(), domain, x1);
         Botan::DH_PrivateKey to(Test::rng(), domain, x2);

         Botan::DLIES_Encryptor encryptor(from, Test::rng(), kdf->clone(), enc.release(), cipher_key_len, mac->clone(),
                                          mac_key_len);
         Botan::DLIES_Decryptor decryptor(to, Test::rng(), kdf.release(), dec.release(), cipher_key_len, mac.release(),
{'fraction_non_alphanumeric': 0.13369262865090403, 'fraction_numerical': 0.008344923504867872, 'mean_word_length': 2.6973007712082264, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2099391', 'n_tokens_mistral': 2077, 'n_tokens_neox': 1973, 'n_words': 348}