Advertisement
saleks28

hash_function1

Jan 10th, 2021
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. #include "hash_function.h"
  2.  
  3.  
  4. extern uint32_t sourceSize;
  5.  
  6. CryptoSystem::CryptoSystem( const std::vector<uint8_t>& S, const uint32_t& key ) :
  7.      S_(S), feistelKey_(key)
  8. {
  9.      
  10. }
  11.  
  12. uint32_t CryptoSystem::FormBlock( const std::vector<uint8_t>& bytes )
  13. {
  14.      uint32_t tempBlock = 0;
  15.      for ( size_t i = 0; i < 4; ++i )
  16.      {
  17.           tempBlock += bytes[i];
  18.           if (i != 3)
  19.           {
  20.                tempBlock <<= 8;
  21.           }
  22.      }
  23.      return tempBlock;
  24. }
  25.  
  26. std::vector<uint8_t> CryptoSystem::DeformBlock(const uint32_t& block )
  27. {
  28.      std::vector<uint8_t> deformedBlock =
  29.      {
  30.           static_cast<uint8_t>(( block >> 24) & 0xFF ),
  31.           static_cast<uint8_t>((block >> 16) & 0xFF),
  32.           static_cast<uint8_t>((block >> 8) & 0xFF),
  33.           static_cast<uint8_t>(block  & 0xFF)
  34.  
  35.      };
  36.      return deformedBlock;
  37. }
  38.  
  39. uint32_t CryptoSystem::EncryptBlock( const uint32_t& block, uint32_t key )
  40. {
  41.      uint32_t result = 0;
  42.      // Раундовые ключи
  43.      const uint16_t theta1 =  key >> 16;
  44.      const uint16_t theta2 = ( key << 16 ) >> 16;
  45.      // Ключ отбеливания
  46.      const uint32_t thetaWhitening = ( ( theta1 ^ theta2 ) << 16 ) | ( theta1 ^ theta2 );
  47.      
  48.      // 4 Раунда
  49.      result = FeistelRound( block, theta1 );
  50.      result = FeistelRound( result, theta2 );
  51.      result = FeistelRound( result, theta1 );
  52.      result = FeistelRound( result, theta2 );
  53.      // Тау
  54.      result = ( result << 16 ) | ( result  >> 16);
  55.      // Отбеливание
  56.      result ^= thetaWhitening;
  57.      // Хэш Fx ^ X
  58.      result ^= block;
  59.      
  60.      return result;
  61. }
  62.  
  63. uint32_t CryptoSystem::SP(const uint16_t& x, const uint16_t& roundKey)
  64. {
  65.      uint16_t res = x ^ roundKey;
  66.      uint16_t sBlocks[4] = { 0, 0, 0, 0 };
  67.      // Делим на блоки от 1-m, 2m-3m и т.д
  68.      sBlocks[0] = res >> 12;
  69.      sBlocks[1] = res << 4;
  70.      sBlocks[1] >>= 12;
  71.      sBlocks[2] = res << 8;
  72.      sBlocks[2] >>= 12;
  73.      sBlocks[3] = res << 12;
  74.      sBlocks[3] >>= 12;
  75.      // S
  76.      for ( size_t i = 0; i < 4; ++i )
  77.      {
  78.           sBlocks[i] = S_[sBlocks[i]];
  79.      }
  80.      // P
  81.      uint16_t P0 = sBlocks[3] | ( sBlocks[2] << 4 )
  82.           | ( sBlocks[1] << 8 ) | ( sBlocks[0] << 12);
  83.      uint16_t num1 = 0;
  84.      res = 0;
  85.      for ( int i = 0; i < 16; ++i )
  86.      {
  87.           num1 = P0 << i;
  88.           num1 >>= 15;
  89.           res += num1 * static_cast<uint16_t>(pow(2, 15 - ((13 * i + 1) % 16)));
  90.      }
  91.  
  92.      return res;
  93. }
  94.  
  95. uint32_t CryptoSystem::FeistelRound( const uint32_t& block, const uint16_t& roundKey )
  96. {
  97.      uint32_t x1 = block >> 16;
  98.      uint32_t x2 = block << 16;
  99.      x2 >>= 16;
  100.  
  101.      uint16_t funcRes = SP( x2, roundKey );
  102.      uint32_t result = ( x2 << 16 ) | ( x1 ^ funcRes );
  103.  
  104.      return result;
  105. }
  106.  
  107. uint32_t CryptoSystem::EncryptText( std::vector<uint8_t> sourceText )
  108. {
  109.      // Вектор, хранящий значение шифрования
  110.      std::vector<uint8_t> encryptedText;
  111.      while ( sourceText.size() % 4 != 0 )
  112.      {
  113.           sourceText.push_back(0);
  114.      }
  115.      // Считывание и шифрования 1 блока
  116.      std::vector<uint8_t> tempBlock(4);
  117.      uint32_t sourceBlock = 0;
  118.      uint32_t resultBlock = 0;
  119.      uint32_t key = feistelKey_;
  120.      for ( size_t i = 0; i < sourceText.size(); i+=4 )
  121.      {
  122.           // 4 байта открытого текста
  123.           tempBlock[0] = sourceText[i];
  124.           tempBlock[1] = sourceText[i + 1];
  125.           tempBlock[2] = sourceText[i + 2];
  126.           tempBlock[3] = sourceText[i + 3];
  127.  
  128.           // Упакуем в uint32_t
  129.           sourceBlock = FormBlock( tempBlock );
  130.           // Шифруем
  131.           resultBlock = EncryptBlock( sourceBlock, key ^ sourceBlock );
  132.           // Записываем результат
  133.           std::vector<uint8_t> deformedBlock = DeformBlock( resultBlock );
  134.           key = resultBlock;
  135.      }
  136.      sourceBlock = sourceSize;
  137.      resultBlock = EncryptBlock( sourceBlock, key ^ sourceBlock );
  138.      return resultBlock;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement