• 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

95.59
/src/lib/misc/cryptobox/cryptobox.cpp
1
/*
2
* Cryptobox Message Routines
3
* (C) 2009 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/cryptobox.h>
9

10
#include <botan/cipher_mode.h>
11
#include <botan/data_src.h>
12
#include <botan/mac.h>
13
#include <botan/mem_ops.h>
14
#include <botan/pem.h>
15
#include <botan/pwdhash.h>
16
#include <botan/rng.h>
17
#include <botan/internal/loadstor.h>
18

19
namespace Botan::CryptoBox {
20

21
namespace {
22

23
/*
24
First 24 bits of SHA-256("Botan Cryptobox"), followed by 8 0 bits
25
for later use as flags, etc if needed
26
*/
27
const uint32_t CRYPTOBOX_VERSION_CODE = 0xEFC22400;
28

29
const size_t VERSION_CODE_LEN = 4;
30
const size_t CIPHER_KEY_LEN = 32;
31
const size_t CIPHER_IV_LEN = 16;
32
const size_t MAC_KEY_LEN = 32;
33
const size_t MAC_OUTPUT_LEN = 20;
34
const size_t PBKDF_SALT_LEN = 10;
35
const size_t PBKDF_ITERATIONS = 8 * 1024;
36

37
const size_t CRYPTOBOX_HEADER_LEN = VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN;
38

39
}
40

41
std::string encrypt(const uint8_t input[], size_t input_len, std::string_view passphrase, RandomNumberGenerator& rng) {
18✔
42
   /*
43
   Output format is:
44
      version # (4 bytes)
45
      salt (10 bytes)
46
      mac (20 bytes)
47
      ciphertext
48
   */
49
   secure_vector<uint8_t> out_buf(CRYPTOBOX_HEADER_LEN + input_len);
18✔
50
   store_be(CRYPTOBOX_VERSION_CODE, out_buf.data());
18✔
51
   rng.randomize(&out_buf[VERSION_CODE_LEN], PBKDF_SALT_LEN);
18✔
52
   // space left for MAC here
53
   if(input_len > 0)
18✔
54
      copy_mem(&out_buf[CRYPTOBOX_HEADER_LEN], input, input_len);
17✔
55

56
   // Generate the keys and IV
57

58
   auto pbkdf_fam = PasswordHashFamily::create_or_throw("PBKDF2(HMAC(SHA-512))");
18✔
59
   auto pbkdf = pbkdf_fam->from_params(PBKDF_ITERATIONS);
18✔
60

61
   secure_vector<uint8_t> master_key(CIPHER_KEY_LEN + MAC_KEY_LEN + CIPHER_IV_LEN);
18✔
62

63
   pbkdf->derive_key(master_key.data(),
18✔
64
                     master_key.size(),
65
                     passphrase.data(),
66
                     passphrase.size(),
67
                     &out_buf[VERSION_CODE_LEN],
18✔
68
                     PBKDF_SALT_LEN);
69

70
   const uint8_t* mk = master_key.data();
18✔
71
   const uint8_t* cipher_key = mk;
18✔
72
   const uint8_t* mac_key = mk + CIPHER_KEY_LEN;
18✔
73
   const uint8_t* iv = mk + CIPHER_KEY_LEN + MAC_KEY_LEN;
18✔
74

75
   // Now encrypt and authenticate
76
   auto ctr = Cipher_Mode::create_or_throw("Serpent/CTR-BE", Cipher_Dir::Encryption);
18✔
77
   ctr->set_key(cipher_key, CIPHER_KEY_LEN);
18✔
78
   ctr->start(iv, CIPHER_IV_LEN);
18✔
79
   ctr->finish(out_buf, CRYPTOBOX_HEADER_LEN);
18✔
80

81
   std::unique_ptr<MessageAuthenticationCode> hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
18✔
82
   hmac->set_key(mac_key, MAC_KEY_LEN);
18✔
83
   if(input_len > 0)
18✔
84
      hmac->update(&out_buf[CRYPTOBOX_HEADER_LEN], input_len);
17✔
85

86
   // Can't write directly because of MAC truncation
87
   secure_vector<uint8_t> mac = hmac->final();
18✔
88
   copy_mem(&out_buf[VERSION_CODE_LEN + PBKDF_SALT_LEN], mac.data(), MAC_OUTPUT_LEN);
18✔
89

90
   return PEM_Code::encode(out_buf, "BOTAN CRYPTOBOX MESSAGE");
18✔
91
}
126✔
92

93
secure_vector<uint8_t> decrypt_bin(const uint8_t input[], size_t input_len, std::string_view passphrase) {
36✔
94
   DataSource_Memory input_src(input, input_len);
36✔
95
   secure_vector<uint8_t> ciphertext = PEM_Code::decode_check_label(input_src, "BOTAN CRYPTOBOX MESSAGE");
54✔
96

97
   if(ciphertext.size() < CRYPTOBOX_HEADER_LEN)
36✔
98
      throw Decoding_Error("Invalid CryptoBox input");
×
99

100
   for(size_t i = 0; i != VERSION_CODE_LEN; ++i) {
160✔
101
      uint32_t version = load_be<uint32_t>(ciphertext.data(), 0);
129✔
102
      if(version != CRYPTOBOX_VERSION_CODE)
129✔
103
         throw Decoding_Error("Bad CryptoBox version");
5✔
104
   }
105

106
   const uint8_t* pbkdf_salt = &ciphertext[VERSION_CODE_LEN];
31✔
107
   const uint8_t* box_mac = &ciphertext[VERSION_CODE_LEN + PBKDF_SALT_LEN];
31✔
108

109
   auto pbkdf_fam = PasswordHashFamily::create_or_throw("PBKDF2(HMAC(SHA-512))");
49✔
110
   auto pbkdf = pbkdf_fam->from_params(PBKDF_ITERATIONS);
31✔
111

112
   secure_vector<uint8_t> master_key(CIPHER_KEY_LEN + MAC_KEY_LEN + CIPHER_IV_LEN);
44✔
113

114
   pbkdf->derive_key(
31✔
115
      master_key.data(), master_key.size(), passphrase.data(), passphrase.size(), pbkdf_salt, PBKDF_SALT_LEN);
116

117
   const uint8_t* mk = master_key.data();
31✔
118
   const uint8_t* cipher_key = mk;
31✔
119
   const uint8_t* mac_key = mk + CIPHER_KEY_LEN;
31✔
120
   const uint8_t* iv = mk + CIPHER_KEY_LEN + MAC_KEY_LEN;
31✔
121

122
   // Now authenticate and decrypt
123
   std::unique_ptr<MessageAuthenticationCode> hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
44✔
124
   hmac->set_key(mac_key, MAC_KEY_LEN);
31✔
125

126
   if(ciphertext.size() > CRYPTOBOX_HEADER_LEN) {
31✔
127
      hmac->update(&ciphertext[CRYPTOBOX_HEADER_LEN], ciphertext.size() - CRYPTOBOX_HEADER_LEN);
29✔
128
   }
129
   secure_vector<uint8_t> computed_mac = hmac->final();
31✔
130

131
   if(!constant_time_compare(computed_mac.data(), box_mac, MAC_OUTPUT_LEN))
31✔
132
      throw Decoding_Error("CryptoBox integrity failure");
13✔
133

134
   auto ctr = Cipher_Mode::create_or_throw("Serpent/CTR-BE", Cipher_Dir::Decryption);
31✔
135
   ctr->set_key(cipher_key, CIPHER_KEY_LEN);
18✔
136
   ctr->start(iv, CIPHER_IV_LEN);
18✔
137
   ctr->finish(ciphertext, CRYPTOBOX_HEADER_LEN);
18✔
138

139
   ciphertext.erase(ciphertext.begin(), ciphertext.begin() + CRYPTOBOX_HEADER_LEN);
18✔
140
   return ciphertext;
18✔
141
}
209✔
142

143
BOTAN_DIAGNOSTIC_PUSH
144
BOTAN_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS
145

146
secure_vector<uint8_t> decrypt_bin(std::string_view input, std::string_view passphrase) {
18✔
147
   return decrypt_bin(cast_char_ptr_to_uint8(input.data()), input.size(), passphrase);
18✔
148
}
149

150
std::string decrypt(const uint8_t input[], size_t input_len, std::string_view passphrase) {
18✔
151
   const secure_vector<uint8_t> bin = decrypt_bin(input, input_len, passphrase);
18✔
152

153
   return std::string(cast_uint8_ptr_to_char(&bin[0]), bin.size());
×
154
}
×
155

156
std::string decrypt(std::string_view input, std::string_view passphrase) {
18✔
157
   return decrypt(cast_char_ptr_to_uint8(input.data()), input.size(), passphrase);
18✔
158
}
159

160
BOTAN_DIAGNOSTIC_POP
161

162
}
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

© 2026 Coveralls, Inc