Botan: Botan::CryptoBox Namespace Reference

Botan 1.10.17

Functions
Botan::CryptoBox Namespace Reference

## Functions
std::string | decrypt (const byte input[], size_t input_len, const std::string &passphrase)
std::string | decrypt (const std::string &input, const std::string &passphrase)
std::string | encrypt (const byte 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]

BOTAN_DLL std::string Botan::CryptoBox::decrypt | ( | const byte | 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 99 of file cryptobox.cpp.

References Botan::OctetString::begin(), Botan::PEM_Code::decode_check_label(), Botan::DECRYPTION, Botan::PKCS5_PBKDF2::derive_key(), Botan::get_byte(), Botan::get_cipher(), Botan::Pipe::process_msg(), Botan::Pipe::read(), Botan::Pipe::read_all_as_string(), Botan::same_mem(), and Botan::MemoryRegion< T >::size().

Referenced by decrypt().

101 {
102 DataSource_Memory input_src(input, input_len);
103 SecureVector<byte> ciphertext =
104 PEM_Code::decode_check_label(input_src,
105 "BOTAN CRYPTOBOX MESSAGE");
106
107 if(ciphertext.size() < (VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN))
108 throw Decoding_Error("Invalid CryptoBox input");
109
110 for(size_t i = 0; i != VERSION_CODE_LEN; ++i)
111 if(ciphertext[i] != get_byte(i, CRYPTOBOX_VERSION_CODE))
112 throw Decoding_Error("Bad CryptoBox version");
113
114 const byte* pbkdf_salt = &ciphertext[VERSION_CODE_LEN];
115
116 PKCS5_PBKDF2 pbkdf(new HMAC(new 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 byte* mk = master_key.begin();
126
127 SymmetricKey cipher_key(&mk[0], CIPHER_KEY_LEN);
128 SymmetricKey mac_key(&mk[CIPHER_KEY_LEN], MAC_KEY_LEN);
129 InitializationVector iv(&mk[CIPHER_KEY_LEN + MAC_KEY_LEN], CIPHER_IV_LEN);
130
131 Pipe pipe(new Fork(
132 get_cipher("Serpent/CTR-BE", cipher_key, iv, DECRYPTION),
133 new MAC_Filter(new HMAC(new SHA_512),
134 mac_key, MAC_OUTPUT_LEN)));
135
136 const size_t ciphertext_offset =
137 VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN;
138
139 pipe.process_msg(&ciphertext[ciphertext_offset],
140 ciphertext.size() - ciphertext_offset);
141
142 byte computed_mac[MAC_OUTPUT_LEN];
143 pipe.read(computed_mac, MAC_OUTPUT_LEN, 1);
144
145 if(!same_mem(computed_mac,
146 &ciphertext[VERSION_CODE_LEN + PBKDF_SALT_LEN],
147 MAC_OUTPUT_LEN))
148 throw Decoding_Error("CryptoBox integrity failure");
149
150 return pipe.read_all_as_string(0);
151 }
Botan::same_mem
bool same_mem(const T *p1, const T *p2, size_t n)
Definition: mem_ops.h:57
Botan::get_byte
byte get_byte(size_t byte_num, T input)
Definition: get_byte.h:21
Botan::DECRYPTION
Definition: sym_algo.h:87
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::SymmetricKey
OctetString SymmetricKey
Definition: symkey.h:147
Botan::get_cipher
Keyed_Filter * get_cipher(const std::string &algo_spec, Cipher_Dir direction)
Definition: lookup.cpp:124
Botan::PEM_Code::decode_check_label
SecureVector< byte > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:42
Botan::InitializationVector
OctetString InitializationVector
Definition: symkey.h:152

## ◆ decrypt() [2/2]

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

References decrypt().

155 {
156 return decrypt(reinterpret_cast<const byte*>(&input[0]),
157 input.size(),
158 passphrase);
159 }
Botan::CryptoBox::decrypt
std::string decrypt(const std::string &input, const std::string &passphrase)
Definition: cryptobox.cpp:153

## ◆ encrypt()

BOTAN_DLL std::string Botan::CryptoBox::encrypt | ( | const byte | 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.

46 {
47 SecureVector<byte> pbkdf_salt(PBKDF_SALT_LEN);
48 rng.randomize(&pbkdf_salt[0], pbkdf_salt.size());
49
50 PKCS5_PBKDF2 pbkdf(new HMAC(new SHA_512));
51
52 OctetString master_key = pbkdf.derive_key(
53 PBKDF_OUTPUT_LEN,
54 passphrase,
55 &pbkdf_salt[0],
56 pbkdf_salt.size(),
57 PBKDF_ITERATIONS);
58
59 const byte* mk = master_key.begin();
60
61 SymmetricKey cipher_key(&mk[0], CIPHER_KEY_LEN);
62 SymmetricKey mac_key(&mk[CIPHER_KEY_LEN], MAC_KEY_LEN);
63 InitializationVector iv(&mk[CIPHER_KEY_LEN + MAC_KEY_LEN], CIPHER_IV_LEN);
64
65 Pipe pipe(get_cipher("Serpent/CTR-BE", cipher_key, iv, ENCRYPTION),
66 new Fork(
67 0,
68 new MAC_Filter(new HMAC(new SHA_512),
69 mac_key, MAC_OUTPUT_LEN)));
70
71 pipe.process_msg(input, input_len);
72
73 /*
74 Output format is:
75 version # (4 bytes)
76 salt (10 bytes)
77 mac (20 bytes)
78 ciphertext
79 */
80 const size_t ciphertext_len = pipe.remaining(0);
81
82 SecureVector<byte> out_buf(VERSION_CODE_LEN +
83 PBKDF_SALT_LEN +
84 MAC_OUTPUT_LEN +
85 ciphertext_len);
86
87 for(size_t i = 0; i != VERSION_CODE_LEN; ++i)
88 out_buf[i] = get_byte(i, CRYPTOBOX_VERSION_CODE);
89
90 copy_mem(&out_buf[VERSION_CODE_LEN], &pbkdf_salt[0], PBKDF_SALT_LEN);
91
92 pipe.read(&out_buf[VERSION_CODE_LEN + PBKDF_SALT_LEN], MAC_OUTPUT_LEN, 1);
93 pipe.read(&out_buf[VERSION_CODE_LEN + PBKDF_SALT_LEN + MAC_OUTPUT_LEN],
94 ciphertext_len, 0);
95
96 return PEM_Code::encode(out_buf, "BOTAN CRYPTOBOX MESSAGE");
97 }
Botan::ENCRYPTION
Definition: sym_algo.h:87
Botan::get_byte
byte get_byte(size_t byte_num, T input)
Definition: get_byte.h:21
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::SymmetricKey
OctetString SymmetricKey
Definition: symkey.h:147
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
Botan::PEM_Code::encode
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:19
Botan::get_cipher
Keyed_Filter * get_cipher(const std::string &algo_spec, Cipher_Dir direction)
Definition: lookup.cpp:124
Botan::InitializationVector
OctetString InitializationVector
Definition: symkey.h:152