Botan 1.10.17
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
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.

101 {
102 DataSource_Memory input_src(input, input_len);
103 SecureVector<byte> ciphertext =
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 }
Fork(Filter *, Filter *, Filter *=0, Filter *=0)
Definition basefilt.cpp:51
HMAC(HashFunction *hash)
Definition hmac.cpp:87
MAC_Filter(MessageAuthenticationCode *mac_obj, size_t out_len=0)
Definition filters.h:172
size_t size() const
Definition secmem.h:29
const byte * begin() const
Definition symkey.h:35
SecureVector< byte > decode_check_label(DataSource &source, const std::string &label_want)
Definition pem.cpp:42
OctetString SymmetricKey
Definition symkey.h:147
byte get_byte(size_t byte_num, T input)
Definition get_byte.h:21
Keyed_Filter * get_cipher(const std::string &algo_spec, Cipher_Dir direction)
Definition lookup.cpp:124
OctetString InitializationVector
Definition symkey.h:152
bool same_mem(const T *p1, const T *p2, size_t n)
Definition mem_ops.h:57
@ DECRYPTION
Definition sym_algo.h:87
Decoding_Error(const std::string &name)
Definition exceptn.h:140

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

Referenced by decrypt(), and decrypt().

◆ 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
inputthe input data
passphrasethe passphrase used to encrypt the message

Definition at line 153 of file cryptobox.cpp.

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

References decrypt().

◆ 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
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.

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 }
virtual void randomize(byte output[], size_t length)=0
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition pem.cpp:19
void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:22
@ ENCRYPTION
Definition sym_algo.h:87

References Botan::OctetString::begin(), Botan::copy_mem(), Botan::PKCS5_PBKDF2::derive_key(), Botan::PEM_Code::encode(), encrypt(), Botan::ENCRYPTION, Botan::Fork::Fork(), Botan::get_byte(), Botan::get_cipher(), Botan::HMAC::HMAC(), Botan::MAC_Filter::MAC_Filter(), Botan::Pipe::process_msg(), Botan::RandomNumberGenerator::randomize(), Botan::Pipe::read(), Botan::Pipe::remaining(), and Botan::MemoryRegion< T >::size().

Referenced by encrypt().