51 store_be(CRYPTOBOX_VERSION_CODE, out_buf.data());
52 rng.
randomize(&out_buf[VERSION_CODE_LEN], PBKDF_SALT_LEN);
55 copy_mem(&out_buf[CRYPTOBOX_HEADER_LEN], input, input_len);
61 auto pbkdf = pbkdf_fam->from_params(PBKDF_ITERATIONS);
65 pbkdf->derive_key(master_key.data(),
69 &out_buf[VERSION_CODE_LEN],
72 const uint8_t* mk = master_key.data();
73 const uint8_t* cipher_key = mk;
74 const uint8_t* mac_key = mk + CIPHER_KEY_LEN;
75 const uint8_t* iv = mk + CIPHER_KEY_LEN + MAC_KEY_LEN;
79 ctr->set_key(cipher_key, CIPHER_KEY_LEN);
80 ctr->start(iv, CIPHER_IV_LEN);
81 ctr->finish(out_buf, CRYPTOBOX_HEADER_LEN);
84 hmac->set_key(mac_key, MAC_KEY_LEN);
86 hmac->update(&out_buf[CRYPTOBOX_HEADER_LEN], input_len);
91 copy_mem(&out_buf[VERSION_CODE_LEN + PBKDF_SALT_LEN], mac.data(), MAC_OUTPUT_LEN);
100 if(ciphertext.size() < CRYPTOBOX_HEADER_LEN) {
104 for(
size_t i = 0; i != VERSION_CODE_LEN; ++i) {
106 if(version != CRYPTOBOX_VERSION_CODE) {
111 const uint8_t* pbkdf_salt = &ciphertext[VERSION_CODE_LEN];
112 const uint8_t* box_mac = &ciphertext[VERSION_CODE_LEN + PBKDF_SALT_LEN];
115 auto pbkdf = pbkdf_fam->from_params(PBKDF_ITERATIONS);
120 master_key.data(), master_key.size(), passphrase.data(), passphrase.size(), pbkdf_salt, PBKDF_SALT_LEN);
122 const uint8_t* mk = master_key.data();
123 const uint8_t* cipher_key = mk;
124 const uint8_t* mac_key = mk + CIPHER_KEY_LEN;
125 const uint8_t* iv = mk + CIPHER_KEY_LEN + MAC_KEY_LEN;
129 hmac->set_key(mac_key, MAC_KEY_LEN);
131 if(ciphertext.size() > CRYPTOBOX_HEADER_LEN) {
132 hmac->update(&ciphertext[CRYPTOBOX_HEADER_LEN], ciphertext.size() - CRYPTOBOX_HEADER_LEN);
136 if(!
CT::is_equal(computed_mac.data(), box_mac, MAC_OUTPUT_LEN).as_bool()) {
141 ctr->set_key(cipher_key, CIPHER_KEY_LEN);
142 ctr->start(iv, CIPHER_IV_LEN);
143 ctr->finish(ciphertext, CRYPTOBOX_HEADER_LEN);
145 ciphertext.erase(ciphertext.begin(), ciphertext.begin() + CRYPTOBOX_HEADER_LEN);