Пример #1
0
void VerifyChecksumTask::do_task(){
	{ Glib::Mutex::Lock lock(mutex); _status = RUNNING; }
	Botan::Pipe *pipe = new Botan::Pipe(
		new Botan::Hash_Filter(_type.c_str()),new Botan::Hex_Encoder
	);
	Glib::RefPtr<Gio::FileInputStream> file_stream;
	Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(_filename);
	try{
		file_stream = file->read(); // This raises a Gio::Error if not found
		pipe->start_msg();
		unsigned long readsofar = 0;
		unsigned long loop_read = 0;
		unsigned long fileSize = (file->query_info())->get_size();
		Botan::SecureVector<Botan::byte> buffer(fileSize/200 > DEFAULT_BUFFER_SIZE ? fileSize/200 : DEFAULT_BUFFER_SIZE);
		while((loop_read = file_stream->read(buffer.begin(),buffer.size()))){
			if(_status == STOPPED){ break; }
			pipe->write(buffer,loop_read);
			readsofar += loop_read;
			/*{
				Glib::Mutex::Lock lock(mutex);*/
				_progress = int((((float)readsofar/(float)fileSize))*100);
			/*}*/
			signal_recursion();
		}
		pipe->end_msg();
		std::string result = Utils::uppercase(pipe->read_all_as_string());
		#ifdef DEBUG
			if(_status != STOPPED){
				std::cout<<"\tFilename: "<< _filename << std::endl;
				std::cout<<"\tChecksum: "<< _cksum << std::endl;
				std::cout<<"\t  Result: "<< result <<std::endl;
			}
		#endif
		if(result.compare(_cksum) == 0){
			_result = MATCH;
		} else { _result = NO_MATCH; }
		file_stream->close();
	} catch (const Gio::Error& er){
		#ifdef DEBUG
			std::cerr << "[DEBUG] "<< __FILE__ << " line " << __LINE__ 
                      << " on file " << _filename << ": " << er.what() 
                      << std::endl;
		#endif
		_result = NOT_FOUND;
		
	} catch (std::exception &ex) {
		#ifdef DEBUG
			std::cerr << "Exception " << ex.what() << std::endl;
		#endif		
	}
	delete pipe;
	if(file_stream){ file_stream->close(); }
	_status = STOPPED;
	signal_finished.emit();
}
Пример #2
0
char* digestPIN(CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen) {
  // We do not use any salt
  Botan::Pipe *digestPIN = new Botan::Pipe(new Botan::Hash_Filter(new Botan::SHA_256), new Botan::Hex_Encoder);
  digestPIN->start_msg();
  digestPIN->write((Botan::byte*)pPin, (Botan::u32bit)ulPinLen);
  digestPIN->write((Botan::byte*)pPin, (Botan::u32bit)ulPinLen);
  digestPIN->write((Botan::byte*)pPin, (Botan::u32bit)ulPinLen);
  digestPIN->end_msg();

  // Get the digested PIN
  Botan::SecureVector<Botan::byte> pinVector = digestPIN->read_all();
  int size = pinVector.size();
  char *tmpPIN = (char *)malloc(size + 1);
  if(tmpPIN != NULL_PTR) {
    tmpPIN[size] = '\0';
    memcpy(tmpPIN, pinVector.begin(), size);
  }
  delete digestPIN;

  return tmpPIN;
}
Пример #3
0
bool failed_test(const std::string& algo,
                 std::vector<std::string> params,
                 bool is_extension, bool exp_pass,
                 std::string& last_missing,
                 Botan::RandomNumberGenerator& rng)
   {
#if !EXTRA_TESTS
   if(!exp_pass) return true;
#endif

   std::map<std::string, std::string> vars;
   vars["input"] = params[0];
   vars["output"] = params[1];

   if(params.size() > 2)
      vars["key"] = params[2];

   if(params.size() > 3)
      vars["iv"] = params[3];

   std::map<std::string, bool> results =
      algorithm_kat(algo, vars, global_state().algorithm_factory());

   if(results.size())
      {
      for(std::map<std::string, bool>::const_iterator i = results.begin();
          i != results.end(); ++i)
         {
         if(i->second == false)
            {
            std::cout << algo << " test with provider "
                      << i->first << " failed\n";
            return true;
            }
         }

      return false; // OK
      }

   const std::string in = params[0];
   const std::string expected = params[1];

   params.erase(params.begin());
   params.erase(params.begin());

   if(in.size() % 2 == 1)
      {
      std::cout << "Can't have an odd sized hex string!" << std::endl;
      return true;
      }

   Botan::Pipe pipe;

   try {
      Botan::Filter* test = lookup(algo, params);
      if(test == 0 && is_extension) return !exp_pass;
      if(test == 0)
         {
         if(algo != last_missing)
            {
            std::cout << "WARNING: \"" + algo + "\" is not a known "
                      << "algorithm name." << std::endl;
            last_missing = algo;
            }
         return 0;
         }

      pipe.reset();
      pipe.append(test);
      pipe.append(new Botan::Hex_Encoder);

      Botan::SecureVector<byte> data = Botan::hex_decode(in);
      const byte* data_ptr = &data[0];

      // this can help catch errors with buffering, etc
      size_t len = data.size();
      pipe.start_msg();
      while(len)
         {
         u32bit how_much = random_word(rng, len);
         pipe.write(data_ptr, how_much);
         data_ptr += how_much;
         len -= how_much;
         }
      pipe.end_msg();
      }
   catch(Botan::Algorithm_Not_Found& e)
      {
      std::cout << "Algorithm not found: " << e.what() << std::endl;
      return false;
      }
   catch(Botan::Exception& e)
      {
      if(exp_pass || DEBUG)
         std::cout << "Exception caught: " << e.what() << std::endl;
      return true;
      }
   catch(std::exception& e)
      {
      if(exp_pass || DEBUG)
         std::cout << "Standard library exception caught: "
                   << e.what() << std::endl;
      return true;
      }
   catch(...)
      {
      if(exp_pass || DEBUG)
         std::cout << "Unknown exception caught." << std::endl;
      return true;
      }

   std::string output;

   if(pipe.remaining())
      {
      /* Test peeking at an offset in Pipe/SecureQueue */
      size_t offset = random_word(rng, pipe.remaining() - 1);
      size_t length = random_word(rng, pipe.remaining() - offset);

      Botan::SecureVector<byte> peekbuf(length);
      pipe.peek(&peekbuf[0], peekbuf.size(), offset);

      output = pipe.read_all_as_string();

      bool OK = true;

      for(size_t j = offset; j != offset+length; j++)
         if(static_cast<byte>(output[j]) != peekbuf[j-offset])
            OK = false;

      if(!OK)
         throw Botan::Self_Test_Failure("Peek testing failed in validate.cpp");
      }

   if(output == expected && !exp_pass)
      {
      std::cout << "FAILED: " << expected << " == " << std::endl
                << "        " << output << std::endl;
      return false;
      }

   if(output != expected && exp_pass)
      {
      std::cout << "\nFAILED: " << expected << " != " << std::endl
                << "        " << output << std::endl;
      return true;
      }

   if(output != expected && !exp_pass) return true;

   return false;
   }