Botan 3.13.0
Crypto and TLS for C&
Botan::CryptoBox Namespace Reference

Namespaces

namespace  BOTAN_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS

Functions

std::string decrypt (const uint8_t input[], size_t input_len, std::string_view passphrase)
std::string decrypt (std::string_view input, std::string_view passphrase)
secure_vector< uint8_t > decrypt_bin (const uint8_t input[], size_t input_len, std::string_view passphrase)
secure_vector< uint8_t > decrypt_bin (std::string_view input, std::string_view passphrase)
std::string encrypt (const uint8_t input[], size_t input_len, std::string_view passphrase, RandomNumberGenerator &rng)

Detailed Description

This namespace holds various high-level crypto functions

Function Documentation

◆ decrypt() [1/2]

std::string Botan::CryptoBox::decrypt ( const uint8_t input[],
size_t input_len,
std::string_view passphrase )

Decrypt a message encrypted with CryptoBox::encrypt

Parameters
inputthe input data
input_lenthe length of input in bytes
passphrasethe passphrase used to encrypt the message

Definition at line 171 of file cryptobox.cpp.

171 {
172 return bytes_to_string(decrypt_bin(input, input_len, passphrase));
173}
secure_vector< uint8_t > decrypt_bin(const uint8_t input[], size_t input_len, std::string_view passphrase)
Definition cryptobox.cpp:99
std::string bytes_to_string(std::span< const uint8_t > bytes)
Definition mem_utils.h:76

References Botan::bytes_to_string(), and decrypt_bin().

Referenced by Botan::CryptoBox::BOTAN_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS::decrypt(), and decrypt().

◆ decrypt() [2/2]

std::string Botan::CryptoBox::decrypt ( std::string_view input,
std::string_view passphrase )

Decrypt a message encrypted with CryptoBox::encrypt

Parameters
inputthe input data
passphrasethe passphrase used to encrypt the message

Definition at line 175 of file cryptobox.cpp.

175 {
176 return decrypt(as_span_of_bytes(input), passphrase);
177}
std::string decrypt(const uint8_t input[], size_t input_len, std::string_view passphrase)
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:59

References Botan::as_span_of_bytes(), and decrypt().

◆ decrypt_bin() [1/2]

secure_vector< uint8_t > Botan::CryptoBox::decrypt_bin ( const uint8_t input[],
size_t input_len,
std::string_view passphrase )

Decrypt a message encrypted with CryptoBox::encrypt

Parameters
inputthe input data
input_lenthe length of input in bytes
passphrasethe passphrase used to encrypt the message

Definition at line 99 of file cryptobox.cpp.

99 {
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}
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
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:798
secure_vector< uint8_t > decode_check_label(DataSource &source, std::string_view label_want)
Definition pem.cpp:49
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504

References Botan::Cipher_Mode::create_or_throw(), Botan::MessageAuthenticationCode::create_or_throw(), Botan::PasswordHashFamily::create_or_throw(), Botan::PEM_Code::decode_check_label(), Botan::Decryption, Botan::CT::is_equal(), and Botan::load_be().

Referenced by decrypt(), Botan::CryptoBox::BOTAN_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS::decrypt_bin(), and decrypt_bin().

◆ decrypt_bin() [2/2]

secure_vector< uint8_t > Botan::CryptoBox::decrypt_bin ( std::string_view input,
std::string_view passphrase )

Decrypt a message encrypted with CryptoBox::encrypt

Parameters
inputthe input data
passphrasethe passphrase used to encrypt the message

Definition at line 167 of file cryptobox.cpp.

167 {
168 return decrypt_bin(as_span_of_bytes(input), passphrase);
169}

References Botan::as_span_of_bytes(), and decrypt_bin().

◆ encrypt()

std::string Botan::CryptoBox::encrypt ( const uint8_t input[],
size_t input_len,
std::string_view passphrase,
RandomNumberGenerator & rng )

Encrypt a message using a passphrase

Parameters
inputthe input data
input_lenthe length of input in bytes
passphrasethe passphrase used to encrypt the message
Random Number Generatorsa ref to a random number generator, such as AutoSeeded_RNG

Definition at line 44 of file cryptobox.cpp.

44 {
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}
void randomize(std::span< uint8_t > output)
Definition rng.h:86
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:39
constexpr T add_or_throw(T a, T b, std::string_view msg)
Definition int_utils.h:66
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745

References Botan::add_or_throw(), Botan::copy_mem(), Botan::Cipher_Mode::create_or_throw(), Botan::MessageAuthenticationCode::create_or_throw(), Botan::PasswordHashFamily::create_or_throw(), Botan::PEM_Code::encode(), Botan::Encryption, Botan::RandomNumberGenerator::randomize(), and Botan::store_be().