Skip to content

Commit

Permalink
Added list of known models from http://reveng.sourceforge.net to spee…
Browse files Browse the repository at this point in the history
…d some tests. Might add options to allow probing of initial or final xor.

Added properties and methods to allow reversed input/ouput as default.
  • Loading branch information
martynp committed Sep 25, 2016
1 parent efbf819 commit c0700e6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
54 changes: 39 additions & 15 deletions bf_crc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,17 @@ bool bf_crc::brute_force(uint32_t search_poly_start, uint32_t search_poly_end, s
uint32_t init = 0;

// Probe reflected input
for(int probe_reflected_input = 0;
probe_reflected_input <= bool_to_int(probe_reflected_input_);
int probe_rin_start = probe_reflected_input_ ? 0 : bool_to_int(reflected_input_);
int probe_rin_end = probe_reflected_input_ ? 1 : bool_to_int(reflected_input_);
for(int probe_reflected_input = probe_rin_start;
probe_reflected_input <= probe_rin_end;
probe_reflected_input++) {

// Probe reflected output
for(int probe_reflected_output = 0;
probe_reflected_output <= bool_to_int(probe_reflected_output_);
int probe_rout_start = probe_reflected_output_ ? 0 : bool_to_int(reflected_output_);
int probe_rout_end = probe_reflected_output_ ? 1 : bool_to_int(reflected_output_);
for(int probe_reflected_output = probe_rout_start;
probe_reflected_output <= probe_rout_end;
probe_reflected_output++) {

// Check all possible polynomials
Expand Down Expand Up @@ -257,7 +261,6 @@ bool bf_crc::brute_force(uint32_t search_poly_start, uint32_t search_poly_end, s
if(probe_final_xor_ || (crc_counter % 0x80 == 0))
{
print_stats();
//std::cout << std::endl << std::hex << poly << std::dec << "\r";
}

mymutex.unlock();
Expand Down Expand Up @@ -288,8 +291,7 @@ int bf_crc::do_brute_force(int num_threads, std::vector<test_vector_t> test_vect
// Start a thread pool
ThreadPool<boost::function0<void> > pool;

if (verbose_)
{
if (verbose_) {
// Show the current settings
print_settings();

Expand All @@ -311,32 +313,46 @@ int bf_crc::do_brute_force(int num_threads, std::vector<test_vector_t> test_vect
crc_model_match_.clear();

// TODO: Search all known CRC combinations first
if (verbose_) {
std::cout << std::endl;
std::cout << "Testing Known CRC's for Length " << crc_width_ << std::endl;
std::cout << "---------------------------------" << std::endl;
std::cout << std::endl << std::flush;
}

assert(known_models.size() >= crc_width_ - 1); // Check there are enough known models...
for (size_t model = 0; model < known_models[crc_width_].size(); model++)
{

bf_crc *known_bf = new bf_crc( crc_width_,
known_models[crc_width_][model].polynomial,
probe_final_xor_,
final_xor_,
probe_initial_,
initial_,
probe_reflected_input_,
probe_reflected_output_);
false,
known_models[crc_width_][model].final_xor,
false,
known_models[crc_width_][model].initial,
false,
false);
known_bf->set_reflected_input(known_models[crc_width_][model].reflected_input);
known_bf->set_reflected_output(known_models[crc_width_][model].reflected_output);

known_bf->set_quiet(true); // Turn off all output
known_bf->brute_force(known_models[crc_width_][model].polynomial,known_models[crc_width_][model].polynomial, test_vectors);

for (size_t found = 0; found < known_bf->crc_model_match().size(); found++) {
crc_model_t result = known_bf->crc_model_match()[found];

if (std::find(crc_model_match_.begin(), crc_model_match_.end(), result) != crc_model_match_.end()) {
std::cout << "Duplicate" << std::endl;
// Nothing to do if model exists already
show_hit(known_bf->crc_model_match()[found]);
} else {
crc_model_match_.push_back(known_bf->crc_model_match()[found]);
std::cout << "Found" << std::endl;
if (verbose_)
show_hit(known_bf->crc_model_match()[found]);
}
}

delete known_bf;

}


Expand All @@ -351,6 +367,14 @@ int bf_crc::do_brute_force(int num_threads, std::vector<test_vector_t> test_vect
// Handle low polynomial count
if (poly_step == 0) poly_step = 1;

if (verbose_) {
std::cout << std::endl;
std::cout << "Starting brute forcer over selected threads" << std::endl;
std::cout << "-------------------------------------------" << std::endl;
std::cout << std::endl << std::flush;
}


for(int thread_number = 0; thread_number < num_threads; thread_number++) {

uint32_t search_end = 0;
Expand Down
8 changes: 8 additions & 0 deletions bf_crc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class bf_crc {
crc_model_match_.clear();
verbose_ = false;
quiet_ = false;
reflected_input_ = false;
reflected_output_ = false;


// Polulate known models from http://reveng.sourceforge.net
Expand Down Expand Up @@ -404,7 +406,9 @@ class bf_crc {
bool probe_initial_;
uint32_t initial_;
bool probe_reflected_input_;
bool reflected_input_;
bool probe_reflected_output_;
bool reflected_output_;

uint64_t test_vector_count_;
bool verbose_;
Expand Down Expand Up @@ -448,8 +452,12 @@ class bf_crc {
uint32_t initial() const { return initial_; }
void set_probe_reflected_input(bool var) { probe_reflected_input_ = var; update_test_vector_count(); }
bool probe_reflected_input() const { return probe_reflected_input_; }
void set_reflected_input(bool var) { reflected_input_ = var; }
bool relfected_input() const { return reflected_input_; }
void set_probe_reflected_output(bool var) { probe_reflected_output_ = var; update_test_vector_count(); }
bool probe_reflected_output() const { return probe_reflected_output_; }
void set_reflected_output(bool var) { reflected_output_ = var; }
bool reflected_output() const { return reflected_output_; }

uint64_t test_vector_count() const { return test_vector_count_; }
void set_verbose(bool var) { verbose_ = var; }
Expand Down
2 changes: 2 additions & 0 deletions tests/test_bruteforce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ BOOST_AUTO_TEST_CASE(crcThirtyTwo)
false, // Probe Reflected Input?
false); // Probe Reflected Output?
crc_bruteforce->set_verbose(true);
crc_bruteforce->set_polynomial_start(0x00000000);
crc_bruteforce->set_polynomial_end(0x10000000);
crc_bruteforce->do_brute_force(4, test_vectors);

// Get results
Expand Down

0 comments on commit c0700e6

Please sign in to comment.