Botan  2.4.0
Crypto and TLS for C++11
Functions
Botan::CryptoBox Namespace Reference

Functions

std::string decrypt (const uint8_t input[], size_t input_len, const std::string &passphrase)
 
std::string decrypt (const std::string &input, const std::string &passphrase)
 
secure_vector< uint8_t > decrypt_bin (const uint8_t input[], size_t input_len, const std::string &passphrase)
 
secure_vector< uint8_t > decrypt_bin (const std::string &input, const std::string &passphrase)
 
std::string encrypt (const uint8_t input[], size_t input_len, const std::string &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,
const std::string &  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 162 of file cryptobox.cpp.

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

Referenced by Botan::PK_Decryptor::decrypt(), decrypt(), Botan::PK_KEM_Decryptor::decrypt(), Botan::Camellia_128::decrypt_n(), Botan::Camellia_192::decrypt_n(), Botan::Camellia_256::decrypt_n(), Botan::OCB_Decryption::finish(), Botan::OCB_Decryption::minimum_final_size(), Botan::OCB_Decryption::process(), and Botan::TLS::Session::Session().

164  {
165  const secure_vector<uint8_t> bin = decrypt_bin(input, input_len, passphrase);
166 
167  return std::string(cast_uint8_ptr_to_char(&bin[0]),
168  bin.size());
169  }
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:125
secure_vector< uint8_t > decrypt_bin(const std::string &input, const std::string &passphrase)
Definition: cryptobox.cpp:154

◆ decrypt() [2/2]

std::string Botan::CryptoBox::decrypt ( const std::string &  input,
const std::string &  passphrase 
)

Decrypt a message encrypted with CryptoBox::encrypt

Parameters
inputthe input data
passphrasethe passphrase used to encrypt the message

Definition at line 171 of file cryptobox.cpp.

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

173  {
174  return decrypt(cast_char_ptr_to_uint8(input.data()),
175  input.size(), passphrase);
176  }
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:120
std::string decrypt(const std::string &input, const std::string &passphrase)
Definition: cryptobox.cpp:171

◆ decrypt_bin() [1/2]

secure_vector< uint8_t > Botan::CryptoBox::decrypt_bin ( const uint8_t  input[],
size_t  input_len,
const std::string &  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 98 of file cryptobox.cpp.

References Botan::OctetString::begin(), Botan::constant_time_compare(), Botan::PBKDF::create_or_throw(), Botan::MessageAuthenticationCode::create_or_throw(), Botan::PEM_Code::decode_check_label(), Botan::DECRYPTION, Botan::get_byte(), and Botan::get_cipher_mode().

Referenced by decrypt(), and decrypt_bin().

100  {
101  DataSource_Memory input_src(input, input_len);
102  secure_vector<uint8_t> ciphertext =
104  "BOTAN CRYPTOBOX MESSAGE");
105 
106  if(ciphertext.size() < CRYPTOBOX_HEADER_LEN)
107  throw Decoding_Error("Invalid CryptoBox input");
108 
109  for(size_t i = 0; i != VERSION_CODE_LEN; ++i)
110  if(ciphertext[i] != get_byte(i, CRYPTOBOX_VERSION_CODE))
111  throw Decoding_Error("Bad CryptoBox version");
112 
113  const uint8_t* pbkdf_salt = &ciphertext[VERSION_CODE_LEN];
114  const uint8_t* box_mac = &ciphertext[VERSION_CODE_LEN + PBKDF_SALT_LEN];
115 
116  std::unique_ptr<PBKDF> pbkdf(PBKDF::create_or_throw("PBKDF2(HMAC(SHA-512))"));
117 
118  OctetString master_key = pbkdf->derive_key(
119  PBKDF_OUTPUT_LEN,
120  passphrase,
121  pbkdf_salt,
122  PBKDF_SALT_LEN,
123  PBKDF_ITERATIONS);
124 
125  const uint8_t* mk = master_key.begin();
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 =
132  MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
133  hmac->set_key(mac_key, MAC_KEY_LEN);
134 
135  if(ciphertext.size() > CRYPTOBOX_HEADER_LEN)
136  {
137  hmac->update(&ciphertext[CRYPTOBOX_HEADER_LEN],
138  ciphertext.size() - CRYPTOBOX_HEADER_LEN);
139  }
140  secure_vector<uint8_t> computed_mac = hmac->final();
141 
142  if(!constant_time_compare(computed_mac.data(), box_mac, MAC_OUTPUT_LEN))
143  throw Decoding_Error("CryptoBox integrity failure");
144 
145  std::unique_ptr<Cipher_Mode> ctr(get_cipher_mode("Serpent/CTR-BE", DECRYPTION));
146  ctr->set_key(cipher_key, CIPHER_KEY_LEN);
147  ctr->start(iv, CIPHER_IV_LEN);
148  ctr->finish(ciphertext, CRYPTOBOX_HEADER_LEN);
149 
150  ciphertext.erase(ciphertext.begin(), ciphertext.begin() + CRYPTOBOX_HEADER_LEN);
151  return ciphertext;
152  }
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
Definition: mem_ops.cpp:44
secure_vector< uint8_t > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:54
Cipher_Mode * get_cipher_mode(const std::string &algo, Cipher_Dir direction, const std::string &provider)
Definition: cipher_mode.cpp:40
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:39

◆ decrypt_bin() [2/2]

secure_vector< uint8_t > Botan::CryptoBox::decrypt_bin ( const std::string &  input,
const std::string &  passphrase 
)

Decrypt a message encrypted with CryptoBox::encrypt

Parameters
inputthe input data
passphrasethe passphrase used to encrypt the message

Definition at line 154 of file cryptobox.cpp.

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

156  {
157  return decrypt_bin(cast_char_ptr_to_uint8(input.data()),
158  input.size(),
159  passphrase);
160  }
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:120
secure_vector< uint8_t > decrypt_bin(const std::string &input, const std::string &passphrase)
Definition: cryptobox.cpp:154

◆ encrypt()

std::string Botan::CryptoBox::encrypt ( const uint8_t  input[],
size_t  input_len,
const std::string &  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
rnga ref to a random number generator, such as AutoSeeded_RNG

Definition at line 43 of file cryptobox.cpp.

References Botan::OctetString::begin(), Botan::copy_mem(), Botan::PBKDF::create_or_throw(), Botan::MessageAuthenticationCode::create_or_throw(), Botan::PEM_Code::encode(), Botan::ENCRYPTION, Botan::get_byte(), Botan::get_cipher_mode(), and Botan::RandomNumberGenerator::randomize().

Referenced by Botan::PK_KEM_Encryptor::encrypt(), Botan::Camellia_128::encrypt_n(), Botan::Camellia_192::encrypt_n(), Botan::Camellia_256::encrypt_n(), Botan::OCB_Encryption::finish(), Botan::OCB_Encryption::minimum_final_size(), Botan::OCB_Encryption::process(), and Botan::TLS::Session::Session().

46  {
47  /*
48  Output format is:
49  version # (4 bytes)
50  salt (10 bytes)
51  mac (20 bytes)
52  ciphertext
53  */
54  secure_vector<uint8_t> out_buf(CRYPTOBOX_HEADER_LEN + input_len);
55  for(size_t i = 0; i != VERSION_CODE_LEN; ++i)
56  out_buf[i] = get_byte(i, CRYPTOBOX_VERSION_CODE);
57  rng.randomize(&out_buf[VERSION_CODE_LEN], PBKDF_SALT_LEN);
58  // space left for MAC here
59  if(input_len > 0)
60  copy_mem(&out_buf[CRYPTOBOX_HEADER_LEN], input, input_len);
61 
62  // Generate the keys and IV
63 
64  std::unique_ptr<PBKDF> pbkdf(PBKDF::create_or_throw("PBKDF2(HMAC(SHA-512))"));
65 
66  OctetString master_key = pbkdf->derive_key(
67  CIPHER_KEY_LEN + MAC_KEY_LEN + CIPHER_IV_LEN,
68  passphrase,
69  &out_buf[VERSION_CODE_LEN],
70  PBKDF_SALT_LEN,
71  PBKDF_ITERATIONS);
72 
73  const uint8_t* mk = master_key.begin();
74  const uint8_t* cipher_key = mk;
75  const uint8_t* mac_key = mk + CIPHER_KEY_LEN;
76  const uint8_t* iv = mk + CIPHER_KEY_LEN + MAC_KEY_LEN;
77 
78  // Now encrypt and authenticate
79  std::unique_ptr<Cipher_Mode> ctr(get_cipher_mode("Serpent/CTR-BE", ENCRYPTION));
80  ctr->set_key(cipher_key, CIPHER_KEY_LEN);
81  ctr->start(iv, CIPHER_IV_LEN);
82  ctr->finish(out_buf, CRYPTOBOX_HEADER_LEN);
83 
84  std::unique_ptr<MessageAuthenticationCode> hmac =
85  MessageAuthenticationCode::create_or_throw("HMAC(SHA-512)");
86  hmac->set_key(mac_key, MAC_KEY_LEN);
87  if(input_len > 0)
88  hmac->update(&out_buf[CRYPTOBOX_HEADER_LEN], input_len);
89 
90  // Can't write directly because of MAC truncation
91  secure_vector<uint8_t> mac = hmac->final();
92  copy_mem(&out_buf[VERSION_CODE_LEN + PBKDF_SALT_LEN], mac.data(), MAC_OUTPUT_LEN);
93 
94  return PEM_Code::encode(out_buf, "BOTAN CRYPTOBOX MESSAGE");
95  }
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:97
Cipher_Mode * get_cipher_mode(const std::string &algo, Cipher_Dir direction, const std::string &provider)
Definition: cipher_mode.cpp:40
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:39