• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.1
/src/cli/cipher.cpp
1
/*
2
* (C) 2015,2017 Simon Warta (Kullo GmbH)
3
* (C) 2020 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "cli.h"
9

10
#if defined(BOTAN_HAS_CIPHER_MODES)
11

12
   #include <botan/cipher_mode.h>
13
   #include <botan/hex.h>
14
   #include <sstream>
15

16
   #if defined(BOTAN_HAS_AEAD_MODES)
17
      #include <botan/aead.h>
18
   #endif
19

20
namespace Botan_CLI {
21

22
class Cipher final : public Command {
23
   public:
24
      Cipher() : Command("cipher --cipher=AES-256/GCM --decrypt --key= --nonce= --ad= --buf-size=4096 input-file") {}
12,546✔
25

26
      std::string group() const override { return "crypto"; }
1✔
27

28
      std::string description() const override { return "Encrypt or decrypt with a symmetric cipher"; }
1✔
29

30
      void go() override {
6,272✔
31
         const std::string cipher_algo = get_arg_or("cipher", "");
12,544✔
32
         const std::string key_hex = get_arg("key");
6,272✔
33
         const std::string nonce_hex = get_arg("nonce");
6,272✔
34
         const std::string ad_hex = get_arg_or("ad", "");
12,544✔
35
         const std::string input_file = get_arg_or("input-file", "-");
12,544✔
36
         const size_t buf_size = get_arg_sz("buf-size");
6,272✔
37

38
         const Botan::SymmetricKey key(key_hex);
6,272✔
39
         const Botan::InitializationVector nonce(nonce_hex);
6,272✔
40
         const std::vector<uint8_t> ad = Botan::hex_decode(ad_hex);
6,272✔
41

42
         auto direction = flag_set("decrypt") ? Botan::Cipher_Dir::Decryption : Botan::Cipher_Dir::Encryption;
9,408✔
43

44
         auto cipher = Botan::Cipher_Mode::create(cipher_algo, direction);
6,272✔
45
         if(!cipher)
6,272✔
46
            throw CLI_Error_Unsupported("Cipher algorithm '" + cipher_algo + "' not available");
×
47

48
         // Set key
49
         cipher->set_key(key);
6,272✔
50

51
         // Set associated data
52
         if(!ad.empty()) {
6,272✔
53
   #if defined(BOTAN_HAS_AEAD_MODES)
54
            if(Botan::AEAD_Mode* aead = dynamic_cast<Botan::AEAD_Mode*>(cipher.get())) {
3,456✔
55
               aead->set_associated_data(ad);
3,456✔
56
            } else
57
   #endif
58
            {
59
               throw CLI_Usage_Error("Cannot specify associated data with non-AEAD mode");
×
60
            }
61
         }
62

63
         // Set nonce
64
         cipher->start(nonce.bits_of());
6,272✔
65

66
         const std::vector<uint8_t> input = this->slurp_file(input_file, buf_size);
6,272✔
67

68
         Botan::secure_vector<uint8_t> buf(input.begin(), input.end());
6,272✔
69
         cipher->finish(buf);
6,272✔
70

71
         write_output(buf);
6,272✔
72
      }
56,393✔
73
};
74

75
BOTAN_REGISTER_COMMAND("cipher", Cipher);
6,273✔
76

77
}
78

79
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc