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

randombit / botan / 26931553895

02 Jun 2026 07:58PM UTC coverage: 91.672% (+2.3%) from 89.389%
26931553895

push

github

web-flow
Merge pull request #5639 from randombit/jack/sm4-hwaes-ks

Add hwaes hook for SM4 key schedule

113254 of 123543 relevant lines covered (91.67%)

10598982.68 hits per line

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

97.18
/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/exceptn.h>
13
#include <botan/mac.h>
14
#include <botan/pem.h>
15
#include <botan/pwdhash.h>
16
#include <botan/rng.h>
17
#include <botan/internal/ct_utils.h>
18
#include <botan/internal/int_utils.h>
19
#include <botan/internal/loadstor.h>
20
#include <botan/internal/mem_utils.h>
21

22
namespace Botan::CryptoBox {
23

24
namespace {
25

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

32
const size_t VERSION_CODE_LEN = 4;
33
const size_t CIPHER_KEY_LEN = 32;
34
const size_t CIPHER_IV_LEN = 16;
35
const size_t MAC_KEY_LEN = 32;
36
const size_t MAC_OUTPUT_LEN = 20;
37
const size_t PBKDF_SALT_LEN = 10;
38
const size_t PBKDF_ITERATIONS = 8 * 1024;
39

40
const size_t CRYPTOBOX_HEADER_LEN = VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN;
41

42
}  // namespace
43

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

61
   // Generate the keys and IV
62

63
   auto pbkdf_fam = PasswordHashFamily::create_or_throw("PBKDF2(HMAC(SHA-512))");
18✔
64
   auto pbkdf = pbkdf_fam->from_params(PBKDF_ITERATIONS);
18✔
65

66
   secure_vector<uint8_t> master_key(CIPHER_KEY_LEN + MAC_KEY_LEN + CIPHER_IV_LEN);
18✔
67

68
   pbkdf->derive_key(master_key.data(),
18✔
69
                     master_key.size(),
70
                     passphrase.data(),
71
                     passphrase.size(),
72
                     &out_buf[VERSION_CODE_LEN],
18✔
73
                     PBKDF_SALT_LEN);
74

75
   const uint8_t* mk = master_key.data();
18✔
76
   const uint8_t* cipher_key = mk;
18✔
77
   const uint8_t* mac_key = mk + CIPHER_KEY_LEN;
18✔
78
   const uint8_t* iv = mk + CIPHER_KEY_LEN + MAC_KEY_LEN;
18✔
79

80
   // Now encrypt and authenticate
81
   auto ctr = Cipher_Mode::create_or_throw("Serpent/CTR-BE", Cipher_Dir::Encryption);
18✔
82
   ctr->set_key(cipher_key, CIPHER_KEY_LEN);
18✔
83
   ctr->start(iv, CIPHER_IV_LEN);
18✔
84
   ctr->finish(out_buf, CRYPTOBOX_HEADER_LEN);
18✔
85

86
   std::unique_ptr<MessageAuthenticationCode> hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
18✔
87
   hmac->set_key(mac_key, MAC_KEY_LEN);
18✔
88
   if(input_len > 0) {
18✔
89
      hmac->update(&out_buf[CRYPTOBOX_HEADER_LEN], input_len);
17✔
90
   }
91

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

96
   return PEM_Code::encode(out_buf, "BOTAN CRYPTOBOX MESSAGE");
18✔
97
}
126✔
98

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

103
   if(ciphertext.size() < CRYPTOBOX_HEADER_LEN) {
36✔
104
      throw Decoding_Error("Invalid CryptoBox input");
×
105
   }
106

107
   for(size_t i = 0; i != VERSION_CODE_LEN; ++i) {
180✔
108
      const uint32_t version = load_be<uint32_t>(ciphertext.data(), 0);
144✔
109
      if(version != CRYPTOBOX_VERSION_CODE) {
144✔
110
         throw Decoding_Error("Bad CryptoBox version");
×
111
      }
112
   }
113

114
   const uint8_t* pbkdf_salt = &ciphertext[VERSION_CODE_LEN];
36✔
115
   const uint8_t* box_mac = &ciphertext[VERSION_CODE_LEN + PBKDF_SALT_LEN];
36✔
116

117
   auto pbkdf_fam = PasswordHashFamily::create_or_throw("PBKDF2(HMAC(SHA-512))");
54✔
118
   auto pbkdf = pbkdf_fam->from_params(PBKDF_ITERATIONS);
36✔
119

120
   secure_vector<uint8_t> master_key(CIPHER_KEY_LEN + MAC_KEY_LEN + CIPHER_IV_LEN);
54✔
121

122
   pbkdf->derive_key(
36✔
123
      master_key.data(), master_key.size(), passphrase.data(), passphrase.size(), pbkdf_salt, PBKDF_SALT_LEN);
124

125
   const uint8_t* mk = master_key.data();
36✔
126
   const uint8_t* cipher_key = mk;
36✔
127
   const uint8_t* mac_key = mk + CIPHER_KEY_LEN;
36✔
128
   const uint8_t* iv = mk + CIPHER_KEY_LEN + MAC_KEY_LEN;
36✔
129

130
   // Now authenticate and decrypt
131
   std::unique_ptr<MessageAuthenticationCode> hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
54✔
132
   hmac->set_key(mac_key, MAC_KEY_LEN);
36✔
133

134
   if(ciphertext.size() > CRYPTOBOX_HEADER_LEN) {
36✔
135
      hmac->update(&ciphertext[CRYPTOBOX_HEADER_LEN], ciphertext.size() - CRYPTOBOX_HEADER_LEN);
34✔
136
   }
137
   secure_vector<uint8_t> computed_mac = hmac->final();
36✔
138

139
   if(!CT::is_equal(computed_mac.data(), box_mac, MAC_OUTPUT_LEN).as_bool()) {
36✔
140
      throw Decoding_Error("CryptoBox integrity failure");
18✔
141
   }
142

143
   auto ctr = Cipher_Mode::create_or_throw("Serpent/CTR-BE", Cipher_Dir::Decryption);
36✔
144
   ctr->set_key(cipher_key, CIPHER_KEY_LEN);
18✔
145
   ctr->start(iv, CIPHER_IV_LEN);
18✔
146
   ctr->finish(ciphertext, CRYPTOBOX_HEADER_LEN);
18✔
147

148
   ciphertext.erase(ciphertext.begin(), ciphertext.begin() + CRYPTOBOX_HEADER_LEN);
18✔
149
   return ciphertext;
18✔
150
}
234✔
151

152
BOTAN_DIAGNOSTIC_PUSH
153
BOTAN_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS
154

155
namespace {
156

157
secure_vector<uint8_t> decrypt_bin(std::span<const uint8_t> input, std::string_view passphrase) {
18✔
158
   return CryptoBox::decrypt_bin(input.data(), input.size(), passphrase);
18✔
159
}
160

161
std::string decrypt(std::span<const uint8_t> input, std::string_view passphrase) {
18✔
162
   return CryptoBox::decrypt(input.data(), input.size(), passphrase);
18✔
163
}
164

165
}  // namespace
166

167
secure_vector<uint8_t> decrypt_bin(std::string_view input, std::string_view passphrase) {
18✔
168
   return decrypt_bin(as_span_of_bytes(input), passphrase);
18✔
169
}
170

171
std::string decrypt(const uint8_t input[], size_t input_len, std::string_view passphrase) {
18✔
172
   return bytes_to_string(decrypt_bin(input, input_len, passphrase));
18✔
173
}
174

175
std::string decrypt(std::string_view input, std::string_view passphrase) {
18✔
176
   return decrypt(as_span_of_bytes(input), passphrase);
18✔
177
}
178

179
BOTAN_DIAGNOSTIC_POP
180

181
}  // namespace Botan::CryptoBox
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