Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Namespace List
- Namespace Members

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

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

  input | the input data
  input_len | the length of input in bytes
  passphrase | the 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(), and Botan::Camellia_256::decrypt_n().

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 }
Botan::cast_uint8_ptr_to_char
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:194
Botan::CryptoBox::decrypt_bin
secure_vector< uint8_t > decrypt_bin(const std::string &input, const std::string &passphrase)
Definition: cryptobox.cpp:154

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

Decrypt a message encrypted with CryptoBox::encrypt

Parameters

  input | the input data
  passphrase | the 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 }
Botan::cast_char_ptr_to_uint8
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:189
Botan::CryptoBox::decrypt
std::string decrypt(const std::string &input, const std::string &passphrase)
Definition: cryptobox.cpp:171

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

  input | the input data
  input_len | the length of input in bytes
  passphrase | the 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::Cipher_Mode::create_or_throw(), Botan::PEM_Code::decode_check_label(), Botan::DECRYPTION, and Botan::get_byte().

Referenced by decrypt(), and decrypt_bin().

100 {
101 DataSource_Memory input_src(input, input_len);
102 secure_vector<uint8_t> ciphertext =
103 PEM_Code::decode_check_label(input_src,
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(Cipher_Mode::create_or_throw("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 }
Botan::get_byte
constexpr uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:41
Botan::constant_time_compare
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
Definition: mem_ops.h:82
Botan::PEM_Code::decode_check_label
secure_vector< uint8_t > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:54
Botan::DECRYPTION
Definition: cipher_mode.h:23

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

Decrypt a message encrypted with CryptoBox::encrypt

Parameters

  input | the input data
  passphrase | the 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 }
Botan::cast_char_ptr_to_uint8
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:189
Botan::CryptoBox::decrypt_bin
secure_vector< uint8_t > decrypt_bin(const std::string &input, const std::string &passphrase)
Definition: cryptobox.cpp:154

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

  input | the input data
  input_len | the length of input in bytes
  passphrase | the passphrase used to encrypt the message
  rng | a 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::Cipher_Mode::create_or_throw(), Botan::PEM_Code::encode(), Botan::ENCRYPTION, Botan::get_byte(), and Botan::RandomNumberGenerator::randomize().

Referenced by Botan::PK_KEM_Encryptor::encrypt(), Botan::Camellia_128::encrypt_n(), Botan::Camellia_192::encrypt_n(), and Botan::Camellia_256::encrypt_n().

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 = Cipher_Mode::create_or_throw("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 }
Botan::get_byte
constexpr uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:41
Botan::PEM_Code::encode
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:132
Botan::ENCRYPTION
Definition: cipher_mode.h:23