Botan 2.1.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)
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

BOTAN_DLL 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 102 of file cryptobox.cpp.

References Botan::OctetString::begin(), BOTAN_ASSERT_EQUAL, Botan::PEM_Code::decode_check_label(), Botan::DECRYPTION, Botan::PBKDF::derive_key(), Botan::get_byte(), Botan::get_cipher(), Botan::Pipe::process_msg(), Botan::Pipe::read(), Botan::Pipe::read_all_as_string(), and Botan::same_mem().

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().

104 {
105 DataSource_Memory input_src(input, input_len);
106 secure_vector<uint8_t> ciphertext =
107 PEM_Code::decode_check_label(input_src,
108 "BOTAN CRYPTOBOX MESSAGE");
109
110 if(ciphertext.size() < (VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN))
111 throw Decoding_Error("Invalid CryptoBox input");
112
113 for(size_t i = 0; i != VERSION_CODE_LEN; ++i)
114 if(ciphertext[i] != get_byte(i, CRYPTOBOX_VERSION_CODE))
115 throw Decoding_Error("Bad CryptoBox version");
116
117 const uint8_t* pbkdf_salt = &ciphertext[VERSION_CODE_LEN];
118
119 PKCS5_PBKDF2 pbkdf(new HMAC(new SHA_512));
120
121 OctetString master_key = pbkdf.derive_key(
122 PBKDF_OUTPUT_LEN,
123 passphrase,
124 pbkdf_salt,
125 PBKDF_SALT_LEN,
126 PBKDF_ITERATIONS);
127
128 const uint8_t* mk = master_key.begin();
129
130 SymmetricKey cipher_key(mk, CIPHER_KEY_LEN);
131 SymmetricKey mac_key(&mk[CIPHER_KEY_LEN], MAC_KEY_LEN);
132 InitializationVector iv(&mk[CIPHER_KEY_LEN + MAC_KEY_LEN], CIPHER_IV_LEN);
133
134 Pipe pipe(new Fork(
135 get_cipher("Serpent/CTR-BE", cipher_key, iv, DECRYPTION),
136 new MAC_Filter(new HMAC(new SHA_512),
137 mac_key, MAC_OUTPUT_LEN)));
138
139 const size_t ciphertext_offset =
140 VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN;
141
142 pipe.process_msg(&ciphertext[ciphertext_offset],
143 ciphertext.size() - ciphertext_offset);
144
145 uint8_t computed_mac[MAC_OUTPUT_LEN];
146 BOTAN_ASSERT_EQUAL(MAC_OUTPUT_LEN, pipe.read(computed_mac, MAC_OUTPUT_LEN, 1), "MAC size");
147
148 if(!same_mem(computed_mac,
149 &ciphertext[VERSION_CODE_LEN + PBKDF_SALT_LEN],
150 MAC_OUTPUT_LEN))
151 throw Decoding_Error("CryptoBox integrity failure");
152
153 return pipe.read_all_as_string(0);
154 }
Botan::InitializationVector
OctetString InitializationVector
Definition: symkey.h:141
Botan::same_mem
bool same_mem(const T *p1, const T *p2, size_t n)
Definition: mem_ops.h:98
Botan::DECRYPTION
Definition: cipher_mode.h:212
BOTAN_ASSERT_EQUAL
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:53
Botan::SymmetricKey
OctetString SymmetricKey
Definition: symkey.h:136
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::get_byte
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:47
Botan::get_cipher
Keyed_Filter * get_cipher(const std::string &algo_spec, Cipher_Dir direction)
Definition: key_filt.cpp:12

BOTAN_DLL 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 156 of file cryptobox.cpp.

References decrypt().

158 {
159 return decrypt(reinterpret_cast<const uint8_t*>(input.data()),
160 input.size(),
161 passphrase);
162 }
Botan::CryptoBox::decrypt
std::string decrypt(const std::string &input, const std::string &passphrase)
Definition: cryptobox.cpp:156

BOTAN_DLL 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 42 of file cryptobox.cpp.

References Botan::OctetString::begin(), BOTAN_ASSERT_EQUAL, Botan::copy_mem(), Botan::PBKDF::derive_key(), Botan::PEM_Code::encode(), Botan::ENCRYPTION, Botan::get_byte(), Botan::get_cipher(), Botan::Pipe::process_msg(), Botan::RandomNumberGenerator::randomize(), Botan::Pipe::read(), and Botan::Pipe::remaining().

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

45 {
46 secure_vector<uint8_t> pbkdf_salt(PBKDF_SALT_LEN);
47 rng.randomize(pbkdf_salt.data(), pbkdf_salt.size());
48
49 PKCS5_PBKDF2 pbkdf(new HMAC(new SHA_512));
50
51 OctetString master_key = pbkdf.derive_key(
52 PBKDF_OUTPUT_LEN,
53 passphrase,
54 pbkdf_salt.data(),
55 pbkdf_salt.size(),
56 PBKDF_ITERATIONS);
57
58 const uint8_t* mk = master_key.begin();
59
60 SymmetricKey cipher_key(mk, CIPHER_KEY_LEN);
61 SymmetricKey mac_key(&mk[CIPHER_KEY_LEN], MAC_KEY_LEN);
62 InitializationVector iv(&mk[CIPHER_KEY_LEN + MAC_KEY_LEN], CIPHER_IV_LEN);
63
64 Pipe pipe(get_cipher("Serpent/CTR-BE", cipher_key, iv, ENCRYPTION),
65 new Fork(
66 nullptr,
67 new MAC_Filter(new HMAC(new SHA_512),
68 mac_key, MAC_OUTPUT_LEN)));
69
70 pipe.process_msg(input, input_len);
71
72 /*
73 Output format is:
74 version # (4 bytes)
75 salt (10 bytes)
76 mac (20 bytes)
77 ciphertext
78 */
79 const size_t ciphertext_len = pipe.remaining(0);
80
81 std::vector<uint8_t> out_buf(VERSION_CODE_LEN +
82 PBKDF_SALT_LEN +
83 MAC_OUTPUT_LEN +
84 ciphertext_len);
85
86 for(size_t i = 0; i != VERSION_CODE_LEN; ++i)
87 out_buf[i] = get_byte(i, CRYPTOBOX_VERSION_CODE);
88
89 copy_mem(&out_buf[VERSION_CODE_LEN], pbkdf_salt.data(), PBKDF_SALT_LEN);
90
91 BOTAN_ASSERT_EQUAL(
92 pipe.read(&out_buf[VERSION_CODE_LEN + PBKDF_SALT_LEN], MAC_OUTPUT_LEN, 1),
93 MAC_OUTPUT_LEN, "MAC output");
94 BOTAN_ASSERT_EQUAL(
95 pipe.read(&out_buf[VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN],
96 ciphertext_len, 0),
97 ciphertext_len, "Ciphertext size");
98
99 return PEM_Code::encode(out_buf, "BOTAN CRYPTOBOX MESSAGE");
100 }
Botan::InitializationVector
OctetString InitializationVector
Definition: symkey.h:141
Botan::ENCRYPTION
Definition: cipher_mode.h:212
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_ASSERT_EQUAL
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:53
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
Botan::SymmetricKey
OctetString SymmetricKey
Definition: symkey.h:136
Botan::get_byte
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:47
Botan::get_cipher
Keyed_Filter * get_cipher(const std::string &algo_spec, Cipher_Dir direction)
Definition: key_filt.cpp:12