/// Read the source slit sizes from a config file string /// @param line :: line (string) from the config file void EQSANSLoad::readSourceSlitSize(const std::string &line) { boost::regex re_key("wheel", boost::regex::icase); if (boost::regex_search(line, re_key)) { boost::regex re_sig("([1-8]) wheel[ ]*([1-3])[ \\t]*=[ \\t]*(\\w+)"); boost::smatch posVec; if (boost::regex_search(line, posVec, re_sig)) { if (posVec.size() == 4) { std::string num_str = posVec[1]; int slit_number = 0; Poco::NumberParser::tryParse(num_str, slit_number); slit_number--; num_str = posVec[2]; int wheel_number = 0; Poco::NumberParser::tryParse(num_str, wheel_number); wheel_number--; num_str = posVec[3]; boost::regex re_size("\\w*?([0-9]+)mm"); int slit_size = 0; if (boost::regex_search(num_str, posVec, re_size)) { if (posVec.size() == 2) { num_str = posVec[1]; Poco::NumberParser::tryParse(num_str, slit_size); } } m_slit_positions[wheel_number][slit_number] = slit_size; } } } }
/// Read rectangular masks from a config file string /// @param line :: line (string) from the config file void EQSANSLoad::readRectangularMasks(const std::string& line) { // Looking for rectangular mask // Rectangular mask = 7, 0; 7, 255 Poco::RegularExpression re_key("rectangular mask", Poco::RegularExpression::RE_CASELESS); Poco::RegularExpression re_key_alt("elliptical mask", Poco::RegularExpression::RE_CASELESS); Poco::RegularExpression::Match match; if (re_key.match(line, 0, match) || re_key_alt.match(line, 0, match)) { Poco::RegularExpression re_sig("=[ ]*([0-9]+)[ ]*[ ,][ ]*([0-9]+)[ ]*[ ;,][ ]*([0-9]+)[ ]*[ ,][ ]*([0-9]+)"); if (re_sig.match(line, 0, match)) { Poco::RegularExpression::MatchVec posVec; re_sig.match(line, 0, posVec); if (posVec.size()==5) { for (int i=0; i<4; i++) { std::string num_str = line.substr(posVec[i+1].offset, posVec[i+1].length); m_mask_as_string = m_mask_as_string + " " + num_str; } m_mask_as_string += ","; } } } }
/// Read the moderator position from a config file string /// @param line :: line (string) from the config file void EQSANSLoad::readModeratorPosition(const std::string &line) { boost::regex re_key("sample location", boost::regex::icase); if (boost::regex_search(line, re_key)) { boost::regex re_sig("=[ ]*([0-9]+)"); boost::smatch posVec; if (boost::regex_search(line, posVec, re_sig)) { if (posVec.size() == 2) { std::string num_str = posVec[1]; Poco::NumberParser::tryParseFloat(num_str, m_moderator_position); m_moderator_position = -m_moderator_position / 1000.0; } } } }
/// Read the beam center from a config file string /// @param line :: line (string) from the config file void EQSANSLoad::readBeamCenter(const std::string &line) { boost::regex re_key("spectrum center", boost::regex::icase); if (boost::regex_search(line, re_key)) { boost::regex re_sig("=[ ]*([0-9]+.[0-9]*)[ ]*[ ,][ ]*([0-9]+.[0-9]+)"); boost::smatch posVec; if (boost::regex_search(line, posVec, re_sig)) { if (posVec.size() == 3) { std::string num_str = posVec[1]; Poco::NumberParser::tryParseFloat(num_str, m_center_x); num_str = posVec[2]; Poco::NumberParser::tryParseFloat(num_str, m_center_y); } } } }
/// Read the TOF cuts from a config file string /// @param line :: line (string) from the config file void EQSANSLoad::readTOFcuts(const std::string &line) { boost::regex re_key("tof edge discard", boost::regex::icase); if (boost::regex_search(line, re_key)) { boost::regex re_sig("=[ ]*([0-9]+)[ ]*[ ,][ ]*([0-9]+)"); boost::smatch posVec; if (boost::regex_search(line, posVec, re_sig)) { if (posVec.size() == 3) { std::string num_str = posVec[1]; Poco::NumberParser::tryParseFloat(num_str, m_low_TOF_cut); num_str = posVec[2]; Poco::NumberParser::tryParseFloat(num_str, m_high_TOF_cut); } } } }
/// Read the moderator position from a config file string /// @param line :: line (string) from the config file void EQSANSLoad::readModeratorPosition(const std::string& line) { Poco::RegularExpression re_key("sample location", Poco::RegularExpression::RE_CASELESS); Poco::RegularExpression::Match match; if (re_key.match(line, 0, match)) { Poco::RegularExpression re_sig("=[ ]*([0-9]+)"); if (re_sig.match(line, 0, match)) { Poco::RegularExpression::MatchVec posVec; re_sig.match(line, 0, posVec); if (posVec.size()==2) { std::string num_str = line.substr(posVec[1].offset, posVec[1].length); Poco::NumberParser::tryParseFloat(num_str, m_moderator_position); m_moderator_position = -m_moderator_position/1000.0; } } } }
/// Read rectangular masks from a config file string /// @param line :: line (string) from the config file void EQSANSLoad::readRectangularMasks(const std::string &line) { // Looking for rectangular mask // Rectangular mask = 7, 0; 7, 255 boost::regex re_key("rectangular mask", boost::regex::icase); boost::regex re_key_alt("elliptical mask", boost::regex::icase); if (boost::regex_search(line, re_key) || boost::regex_search(line, re_key_alt)) { boost::regex re_sig("=[ ]*([0-9]+)[ ]*[ ,][ ]*([0-9]+)[ ]*[ ;,][ " "]*([0-9]+)[ ]*[ ,][ ]*([0-9]+)"); boost::smatch posVec; if (boost::regex_search(line, posVec, re_sig)) { if (posVec.size() == 5) { for (int i = 0; i < 4; i++) { std::string num_str = posVec[i + 1]; m_mask_as_string = m_mask_as_string + " " + num_str; } m_mask_as_string += ","; } } } }
/// Read the beam center from a config file string /// @param line :: line (string) from the config file void EQSANSLoad::readBeamCenter(const std::string& line) { Poco::RegularExpression re_key("spectrum center", Poco::RegularExpression::RE_CASELESS); Poco::RegularExpression::Match match; if (re_key.match(line, 0, match)) { Poco::RegularExpression re_sig("=[ ]*([0-9]+.[0-9]*)[ ]*[ ,][ ]*([0-9]+.[0-9]+)"); if (re_sig.match(line, 0, match)) { Poco::RegularExpression::MatchVec posVec; re_sig.match(line, 0, posVec); if (posVec.size()==3) { std::string num_str = line.substr(posVec[1].offset, posVec[1].length); Poco::NumberParser::tryParseFloat(num_str, m_center_x); num_str = line.substr(posVec[2].offset, posVec[2].length); Poco::NumberParser::tryParseFloat(num_str, m_center_y); } } } }
/// Read the TOF cuts from a config file string /// @param line :: line (string) from the config file void EQSANSLoad::readTOFcuts(const std::string& line) { Poco::RegularExpression re_key("tof edge discard", Poco::RegularExpression::RE_CASELESS); Poco::RegularExpression::Match match; if (re_key.match(line, 0, match)) { Poco::RegularExpression re_sig("=[ ]*([0-9]+)[ ]*[ ,][ ]*([0-9]+)"); if (re_sig.match(line, 0, match)) { Poco::RegularExpression::MatchVec posVec; re_sig.match(line, 0, posVec); if (posVec.size()==3) { std::string num_str = line.substr(posVec[1].offset, posVec[1].length); Poco::NumberParser::tryParseFloat(num_str, m_low_TOF_cut); num_str = line.substr(posVec[2].offset, posVec[2].length); Poco::NumberParser::tryParseFloat(num_str, m_high_TOF_cut); } } } }
/// Read the source slit sizes from a config file string /// @param line :: line (string) from the config file void EQSANSLoad::readSourceSlitSize(const std::string& line) { Poco::RegularExpression re_key("wheel", Poco::RegularExpression::RE_CASELESS); Poco::RegularExpression::Match match; if (re_key.match(line, 0, match)) { Poco::RegularExpression re_sig("([1-8]) wheel[ ]*([1-3])[ \\t]*=[ \\t]*(\\w+)"); if (re_sig.match(line, 0, match)) { Poco::RegularExpression::MatchVec posVec; re_sig.match(line, 0, posVec); if (posVec.size()==2) { std::string num_str = line.substr(posVec[1].offset, posVec[1].length); int slit_number = 0; Poco::NumberParser::tryParse(num_str, slit_number); slit_number--; num_str = line.substr(posVec[2].offset, posVec[2].length); int wheel_number = 0; Poco::NumberParser::tryParse(num_str, wheel_number); wheel_number--; num_str = line.substr(posVec[3].offset, posVec[3].length); Poco::RegularExpression re_size("\\w*?([0-9]+)mm"); int slit_size = 0; re_size.match(num_str, 0, posVec); if (posVec.size()==2) { num_str = line.substr(posVec[1].offset, posVec[1].length); Poco::NumberParser::tryParse(num_str, slit_size); } m_slit_positions[wheel_number][slit_number] = slit_size; } } } }
int main(int argc, char **argv) { key_data key; int inc = 10; int num_trials = 1; // Initialize the microblaze platform... init_platform(); // Set up the attack incremement if (inc > 0) { inc = 1 << inc; } printf("\nStudy increment: %d", inc); // Display the algorithm. printf("\nUsing algorithm \"%s\"", alg_name()); printf("\nAttacking key \"%s\"", argv[1]); // Display the number of trials... if (num_trials != 1) { printf("\nNumber of trials: %d", num_trials); } /* else if (out_file) printf("\nOutputting to file: %s", out_file); else if (in_file) printf("\nReading from file: %s", in_file); */ #ifdef NONE_AES printf("\nAttacking NONE_AES_CORE implementation"); #elif defined(SMALL_AES) printf("\nAttacking SMALL_AES_CORE implementation"); #else printf("\nAttacking traditional AES_CORE implementation"); #endif #ifdef DECRYPT_MODE printf("\nDECRYPT MODE"); #else printf("\nENCRYPT MODE"); #endif cache_evict_init(); int * results = malloc(num_trials * sizeof(int)); timing_pair * buffer = malloc(BUF_SIZE * sizeof(timing_pair)); timing_data * data = malloc(sizeof(timing_data)); if (data == 0x0) { printf("Data couldn't allocate.\n"); return; } if (buffer == 0x0) { printf("Buffer couldn't allocate.\n"); return; } FILE * in; //if (!USE_RANDOM_KEY) { printf("Initializing random key...\n"); re_key(&key, argv[1]); } fflush(stdout); /* if (out_file) { output_timings(out_file, buffer, inc, &key); return 1; } if (in_file) { char open_type = 'r'; in = fopen(in_file, &open_type); if (in == NULL) { printf("\nCould not open file: %s", in_file); return -1; } } */ int round, i; for (round = 1; round <= num_trials; round++) { printf("\nBeginning Attack #%d\n", round); init_data(data); //if (USE_RANDOM_KEY) // reseed with a random key re_key(&key, "/dev/random"); long long num_studies = 0; double clip_time = 0; int success = 0; long long max = MAX_STUDY; while (num_studies < max && !success) { printf("num_studies = %d\n", num_studies); int offset = num_studies % BUF_SIZE; if (inc > BUF_SIZE) { int read = 0; int num_read = BUF_SIZE; while (read < inc) { //printf("read = %d\n", read); generate_samples(buffer, &key); printf("buffer address %x:\n", buffer); printf("data address = %x\n", data); clip_time = calculate_clip_time(buffer, num_read); for (i = 0; i < BUF_SIZE && read < inc; i++) { //printf("i = %d\n", i); if (buffer[offset + i].time < clip_time) record_timing(data, &buffer[offset + i]); read++; } } } else { printf("inc <= BUF_SIZE\n"); if (offset == 0) { int num_read = BUF_SIZE; //if (!in_file) generate_samples(buffer, &key); printf("generate_samples done\n"); /*else { num_read = fread(buffer, sizeof(timing_pair), BUF_SIZE, in); if (num_read < BUF_SIZE) max = num_studies + num_read; printf("\nRead in %d samples ", num_read); }*/ clip_time = calculate_clip_time(buffer, num_read); printf("clip_time done\n"); } for (i = 0; i < inc; i++) printf("i (inc) = %d\n", i); if (buffer[offset + i].time < clip_time) record_timing(data, &buffer[offset + i]); } num_studies += inc; printf("\nchecking data!\n"); if (check_data(data, &key)) { printf( "\nKey recovered after studying %lld samples (< 2^%d)\n", num_studies, bound(num_studies)); success = 1; } else printf("\nNo success after studying %d samples", num_studies); } if (!success) printf("Attack failed after %d encryptions", num_studies); else { int j = round - 1; while (j > 0 && results[j - 1] > num_studies) { results[j] = results[j - 1]; j--; } results[j] = num_studies; printf("\nResults: "); int total = 0; for (j = 0; j < round; j++) { printf(" %d ", results[j]); total += results[j]; } printf("\nMin: %d", results[0]); printf("\nMax: %d", results[round - 1]); printf("\nMed: %d", results[round / 2]); printf("\nMean: %.2f", ((double) total) / round); } } // Clean up and terminate... cleanup_platform(); }