Botan 3.13.0
Crypto and TLS for C&
cryptobox.cpp
Go to the documentation of this file.
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
23
24namespace {
25
26/*
27First 24 bits of SHA-256("Botan Cryptobox"), followed by 8 0 bits
28for later use as flags, etc if needed
29*/
30const uint32_t CRYPTOBOX_VERSION_CODE = 0xEFC22400;
31
32const size_t VERSION_CODE_LEN = 4;
33const size_t CIPHER_KEY_LEN = 32;
34const size_t CIPHER_IV_LEN = 16;
35const size_t MAC_KEY_LEN = 32;
36const size_t MAC_OUTPUT_LEN = 20;
37const size_t PBKDF_SALT_LEN = 10;
38const size_t PBKDF_ITERATIONS = 8 * 1024;
39
40const size_t CRYPTOBOX_HEADER_LEN = VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN;
41
42} // namespace
43
44std::string encrypt(const uint8_t input[], size_t input_len, std::string_view passphrase, RandomNumberGenerator& rng) {
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");
53 secure_vector<uint8_t> out_buf(out_len);
54 store_be(CRYPTOBOX_VERSION_CODE, out_buf.data());
55 rng.randomize(&out_buf[VERSION_CODE_LEN], PBKDF_SALT_LEN);
56 // space left for MAC here
57 if(input_len > 0) {
58 copy_mem(&out_buf[CRYPTOBOX_HEADER_LEN], input, input_len);
59 }
60
61 // Generate the keys and IV
62
63 auto pbkdf_fam = PasswordHashFamily::create_or_throw("PBKDF2(HMAC(SHA-512))");
64 auto pbkdf = pbkdf_fam->from_params(PBKDF_ITERATIONS);
65
66 secure_vector<uint8_t> master_key(CIPHER_KEY_LEN + MAC_KEY_LEN + CIPHER_IV_LEN);
67
68 pbkdf->derive_key(master_key.data(),
69 master_key.size(),
70 passphrase.data(),
71 passphrase.size(),
72 &out_buf[VERSION_CODE_LEN],
73 PBKDF_SALT_LEN);
74
75 const uint8_t* mk = master_key.data();
76 const uint8_t* cipher_key = mk;
77 const uint8_t* mac_key = mk + CIPHER_KEY_LEN;
78 const uint8_t* iv = mk + CIPHER_KEY_LEN + MAC_KEY_LEN;
79
80 // Now encrypt and authenticate
81 auto ctr = Cipher_Mode::create_or_throw("Serpent/CTR-BE", Cipher_Dir::Encryption);
82 ctr->set_key(cipher_key, CIPHER_KEY_LEN);
83 ctr->start(iv, CIPHER_IV_LEN);
84 ctr->finish(out_buf, CRYPTOBOX_HEADER_LEN);
85
86 std::unique_ptr<MessageAuthenticationCode> hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
87 hmac->set_key(mac_key, MAC_KEY_LEN);
88 if(input_len > 0) {
89 hmac->update(&out_buf[CRYPTOBOX_HEADER_LEN], input_len);
90 }
91
92 // Can't write directly because of MAC truncation
93 secure_vector<uint8_t> mac = hmac->final();
94 copy_mem(&out_buf[VERSION_CODE_LEN + PBKDF_SALT_LEN], mac.data(), MAC_OUTPUT_LEN);
95
96 return PEM_Code::encode(out_buf, "BOTAN CRYPTOBOX MESSAGE");
97}
98
99secure_vector<uint8_t> decrypt_bin(const uint8_t input[], size_t input_len, std::string_view passphrase) {
100 DataSource_Memory input_src(input, input_len);
101 secure_vector<uint8_t> ciphertext = PEM_Code::decode_check_label(input_src, "BOTAN CRYPTOBOX MESSAGE");
102
103 if(ciphertext.size() < CRYPTOBOX_HEADER_LEN) {
104 throw Decoding_Error("Invalid CryptoBox input");
105 }
106
107 for(size_t i = 0; i != VERSION_CODE_LEN; ++i) {
108 const uint32_t version = load_be<uint32_t>(ciphertext.data(), 0);
109 if(version != CRYPTOBOX_VERSION_CODE) {
110 throw Decoding_Error("Bad CryptoBox version");
111 }
112 }
113
114 const uint8_t* pbkdf_salt = &ciphertext[VERSION_CODE_LEN];
115 const uint8_t* box_mac = &ciphertext[VERSION_CODE_LEN + PBKDF_SALT_LEN];
116
117 auto pbkdf_fam = PasswordHashFamily::create_or_throw("PBKDF2(HMAC(SHA-512))");
118 auto pbkdf = pbkdf_fam->from_params(PBKDF_ITERATIONS);
119
120 secure_vector<uint8_t> master_key(CIPHER_KEY_LEN + MAC_KEY_LEN + CIPHER_IV_LEN);
121
122 pbkdf->derive_key(
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();
126 const uint8_t* cipher_key = mk;
127 const uint8_t* mac_key = mk + CIPHER_KEY_LEN;
128 const uint8_t* iv = mk + CIPHER_KEY_LEN + MAC_KEY_LEN;
129
130 // Now authenticate and decrypt
131 std::unique_ptr<MessageAuthenticationCode> hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
132 hmac->set_key(mac_key, MAC_KEY_LEN);
133
134 if(ciphertext.size() > CRYPTOBOX_HEADER_LEN) {
135 hmac->update(&ciphertext[CRYPTOBOX_HEADER_LEN], ciphertext.size() - CRYPTOBOX_HEADER_LEN);
136 }
137 secure_vector<uint8_t> computed_mac = hmac->final();
138
139 if(!CT::is_equal(computed_mac.data(), box_mac, MAC_OUTPUT_LEN).as_bool()) {
140 throw Decoding_Error("CryptoBox integrity failure");
141 }
142
143 auto ctr = Cipher_Mode::create_or_throw("Serpent/CTR-BE", Cipher_Dir::Decryption);
144 ctr->set_key(cipher_key, CIPHER_KEY_LEN);
145 ctr->start(iv, CIPHER_IV_LEN);
146 ctr->finish(ciphertext, CRYPTOBOX_HEADER_LEN);
147
148 ciphertext.erase(ciphertext.begin(), ciphertext.begin() + CRYPTOBOX_HEADER_LEN);
149 return ciphertext;
150}
151
154
155namespace {
156
157secure_vector<uint8_t> decrypt_bin(std::span<const uint8_t> input, std::string_view passphrase) {
158 return CryptoBox::decrypt_bin(input.data(), input.size(), passphrase);
159}
160
161std::string decrypt(std::span<const uint8_t> input, std::string_view passphrase) {
162 return CryptoBox::decrypt(input.data(), input.size(), passphrase);
163}
164
165} // namespace
166
167secure_vector<uint8_t> decrypt_bin(std::string_view input, std::string_view passphrase) {
168 return decrypt_bin(as_span_of_bytes(input), passphrase);
169}
170
171std::string decrypt(const uint8_t input[], size_t input_len, std::string_view passphrase) {
172 return bytes_to_string(decrypt_bin(input, input_len, passphrase));
173}
174
175std::string decrypt(std::string_view input, std::string_view passphrase) {
176 return decrypt(as_span_of_bytes(input), passphrase);
177}
178
180
181} // namespace Botan::CryptoBox
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:128
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:125
#define BOTAN_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS
Definition api.h:126
static std::unique_ptr< Cipher_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:149
static std::unique_ptr< PasswordHashFamily > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition pwdhash.cpp:123
void randomize(std::span< uint8_t > output)
Definition rng.h:86
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:798
secure_vector< uint8_t > decrypt_bin(const uint8_t input[], size_t input_len, std::string_view passphrase)
Definition cryptobox.cpp:99
std::string encrypt(const uint8_t input[], size_t input_len, std::string_view passphrase, RandomNumberGenerator &rng)
Definition cryptobox.cpp:44
std::string decrypt(const uint8_t input[], size_t input_len, std::string_view passphrase)
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:39
secure_vector< uint8_t > decode_check_label(DataSource &source, std::string_view label_want)
Definition pem.cpp:49
constexpr T add_or_throw(T a, T b, std::string_view msg)
Definition int_utils.h:66
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:59
std::string bytes_to_string(std::span< const uint8_t > bytes)
Definition mem_utils.h:76
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504