Skip to content

Commit

Permalink
Cleanup usage of CryptoPP
Browse files Browse the repository at this point in the history
  • Loading branch information
netheril96 committed Jul 8, 2017
1 parent 756bd88 commit 2596467
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 31 deletions.
30 changes: 7 additions & 23 deletions sources/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ Json::Value generate_config(unsigned int version,

byte iv[CONFIG_IV_LENGTH];
byte mac[CONFIG_MAC_LENGTH];
CryptoPP::OS_GenerateRandomBlock(false, iv, array_length(iv));
generate_random(iv, array_length(iv));

CryptoPP::GCM<CryptoPP::AES>::Encryption encryptor;
encryptor.SetKeyWithIV(key_to_encrypt.data(), key_to_encrypt.size(), iv, array_length(iv));
Expand Down Expand Up @@ -458,7 +458,7 @@ void CommandBase::write_config(StreamBase* stream,
unsigned rounds)
{
key_type salt;
CryptoPP::OS_GenerateRandomBlock(false, salt.data(), salt.size());
generate_random(salt.data(), salt.size());
auto str = generate_config(config.version,
pbdkf_algorithm,
config.master_key,
Expand Down Expand Up @@ -584,13 +584,7 @@ class CreateCommand : public CommonCommandBase
auto config_stream
= open_config_stream(get_real_config_path(), O_WRONLY | O_CREAT | O_EXCL);
DEFER(if (std::uncaught_exception()) {
try
{
OSService::get_default().remove_file(get_real_config_path());
}
catch (...)
{
}
OSService::get_default().remove_file(get_real_config_path());
});
write_config(config_stream.get(),
pbkdf.getValue(),
Expand Down Expand Up @@ -658,21 +652,13 @@ class ChangePasswordCommand : public CommonCommandBase
{
auto original_path = get_real_config_path();
byte buffer[16];
CryptoPP::OS_GenerateRandomBlock(false, buffer, array_length(buffer));
generate_random(buffer, array_length(buffer));
auto tmp_path = original_path + hexify(buffer, array_length(buffer));
auto stream = OSService::get_default().open_file_stream(original_path, O_RDONLY, 0644);
auto config = read_config(stream.get(), old_password.data(), old_password.size());
stream = OSService::get_default().open_file_stream(
tmp_path, O_WRONLY | O_CREAT | O_EXCL, 0644);
DEFER(if (std::uncaught_exception()) {
try
{
OSService::get_default().remove_file(tmp_path);
}
catch (...)
{
}
});
DEFER(if (std::uncaught_exception()) { OSService::get_default().remove_file(tmp_path); });
write_config(stream.get(),
pbkdf.getValue(),
config,
Expand Down Expand Up @@ -747,8 +733,7 @@ class MountCommand : public CommonCommandBase
{
password.resize(pass.getValue().size());
memcpy(password.data(), pass.getValue().data(), password.size());
CryptoPP::OS_GenerateRandomBlock(
false, reinterpret_cast<byte*>(&pass.getValue()[0]), pass.getValue().size());
generate_random(reinterpret_cast<byte*>(&pass.getValue()[0]), pass.getValue().size());
}
else
{
Expand Down Expand Up @@ -1008,8 +993,7 @@ class FixCommand : public CommonCommandBase
config.version);
return 3;
}
CryptoPP::OS_GenerateRandomBlock(
false, password.data(), password.size()); // Erase user input
generate_random(password.data(), password.size()); // Erase user input

operations::MountOptions fsopt;
fsopt.root = std::make_shared<OSService>(data_dir.getValue());
Expand Down
5 changes: 2 additions & 3 deletions sources/common_platform.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "crypto.h"
#include "myutils.h"
#include "platform.h"

#include <cryptopp/osrng.h>

namespace securefs
{
const OSService& OSService::get_default()
Expand All @@ -14,7 +13,7 @@ const OSService& OSService::get_default()
std::string OSService::temp_name(StringRef prefix, StringRef suffix)
{
byte random[16];
CryptoPP::OS_GenerateRandomBlock(false, random, array_length(random));
generate_random(random, array_length(random));
std::string result;
result.reserve(prefix.size() + 32 + suffix.size());
result.append(prefix.data(), prefix.size());
Expand Down
3 changes: 2 additions & 1 deletion test/test_files.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "catch.hpp"
#include "crypto.h"
#include "exceptions.h"
#include "file_table.h"
#include "files.h"
Expand All @@ -17,7 +18,7 @@ TEST_CASE("File table")

key_type master_key(0x48);
id_type null_id, file_id;
CryptoPP::OS_GenerateRandomBlock(false, file_id.data(), file_id.size());
generate_random(file_id.data(), file_id.size());
const char* xattr_name = "com.apple.FinderInfo...";
const securefs::PODArray<char, 32> xattr_value(0x11);

Expand Down
8 changes: 4 additions & 4 deletions test/test_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "case_fold.h"
#include "catch.hpp"
#include "crypto.h"
#include "myutils.h"
#include "platform.h"

#include <cryptopp/base32.h>
#include <cryptopp/osrng.h>

TEST_CASE("Test endian")
{
Expand Down Expand Up @@ -36,7 +36,7 @@ TEST_CASE("Test string")
TEST_CASE("Test conversion of hex")
{
securefs::id_type id;
CryptoPP::OS_GenerateRandomBlock(false, id.data(), id.size());
securefs::generate_random(id.data(), id.size());
auto hex = securefs::hexify(id);
securefs::id_type id_copy;
securefs::parse_hex(hex, id_copy.data(), id_copy.size());
Expand Down Expand Up @@ -70,7 +70,7 @@ TEST_CASE("our base32")
if (i > 0)
{
input.resize(i, 0);
CryptoPP::OS_GenerateRandomBlock(false, (byte*)input.data(), i);
securefs::generate_random((byte*)input.data(), i);
}
securefs::base32_encode((const byte*)input.data(), i, output);
CAPTURE(output);
Expand All @@ -89,7 +89,7 @@ TEST_CASE("our base32 against CryptoPP")
if (i > 0)
{
input.resize(i, 0);
CryptoPP::OS_GenerateRandomBlock(false, (byte*)input.data(), i);
securefs::generate_random((byte*)input.data(), i);
}
securefs::base32_encode((const byte*)input.data(), i, output);

Expand Down

0 comments on commit 2596467

Please sign in to comment.