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

randombit / botan / 6163190478

12 Sep 2023 06:03PM UTC coverage: 91.726% (+0.02%) from 91.707%
6163190478

push

github

web-flow
Merge pull request #3680 from huven/kuznyechik

Add Kuznyechik block cipher

78777 of 85883 relevant lines covered (91.73%)

8562448.47 hits per line

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

94.12
/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
}  // namespace
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

57
   // Generate the keys and IV
58

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

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

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

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

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

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

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

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

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

99
   if(ciphertext.size() < CRYPTOBOX_HEADER_LEN) {
36✔
100
      throw Decoding_Error("Invalid CryptoBox input");
×
101
   }
102

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

110
   const uint8_t* pbkdf_salt = &ciphertext[VERSION_CODE_LEN];
36✔
111
   const uint8_t* box_mac = &ciphertext[VERSION_CODE_LEN + PBKDF_SALT_LEN];
36✔
112

113
   auto pbkdf_fam = PasswordHashFamily::create_or_throw("PBKDF2(HMAC(SHA-512))");
54✔
114
   auto pbkdf = pbkdf_fam->from_params(PBKDF_ITERATIONS);
36✔
115

116
   secure_vector<uint8_t> master_key(CIPHER_KEY_LEN + MAC_KEY_LEN + CIPHER_IV_LEN);
54✔
117

118
   pbkdf->derive_key(
36✔
119
      master_key.data(), master_key.size(), passphrase.data(), passphrase.size(), pbkdf_salt, PBKDF_SALT_LEN);
120

121
   const uint8_t* mk = master_key.data();
36✔
122
   const uint8_t* cipher_key = mk;
36✔
123
   const uint8_t* mac_key = mk + CIPHER_KEY_LEN;
36✔
124
   const uint8_t* iv = mk + CIPHER_KEY_LEN + MAC_KEY_LEN;
36✔
125

126
   // Now authenticate and decrypt
127
   std::unique_ptr<MessageAuthenticationCode> hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
54✔
128
   hmac->set_key(mac_key, MAC_KEY_LEN);
36✔
129

130
   if(ciphertext.size() > CRYPTOBOX_HEADER_LEN) {
36✔
131
      hmac->update(&ciphertext[CRYPTOBOX_HEADER_LEN], ciphertext.size() - CRYPTOBOX_HEADER_LEN);
34✔
132
   }
133
   secure_vector<uint8_t> computed_mac = hmac->final();
36✔
134

135
   if(!constant_time_compare(computed_mac.data(), box_mac, MAC_OUTPUT_LEN)) {
36✔
136
      throw Decoding_Error("CryptoBox integrity failure");
18✔
137
   }
138

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

144
   ciphertext.erase(ciphertext.begin(), ciphertext.begin() + CRYPTOBOX_HEADER_LEN);
18✔
145
   return ciphertext;
18✔
146
}
234✔
147

148
BOTAN_DIAGNOSTIC_PUSH
149
BOTAN_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS
150

151
secure_vector<uint8_t> decrypt_bin(std::string_view input, std::string_view passphrase) {
18✔
152
   return decrypt_bin(cast_char_ptr_to_uint8(input.data()), input.size(), passphrase);
18✔
153
}
154

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

158
   return std::string(cast_uint8_ptr_to_char(&bin[0]), bin.size());
×
159
}
×
160

161
std::string decrypt(std::string_view input, std::string_view passphrase) {
18✔
162
   return decrypt(cast_char_ptr_to_uint8(input.data()), input.size(), passphrase);
18✔
163
}
164

165
BOTAN_DIAGNOSTIC_POP
166

167
}  // 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

© 2025 Coveralls, Inc