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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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

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

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

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

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

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

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

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

78
}  // namespace Botan_CLI
79

80
#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