Advertisement
Guest User

Rabbit 3 (nur CPP)

a guest
Jul 30th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.47 KB | None | 0 0
  1. /* NUR Rabbit CPP
  2. */
  3.  
  4. #include "rabbit.h"
  5. const uint32_t a[8] = {0x4D34D34D, 0xD34D34D3, 0x34D34D34, 0x4D34D34D, 0xD34D34D3, 0x34D34D34, 0x4D34D34D, 0xD34D34D3};
  6.  
  7. rabbit::rabbit(){mCounterCarryBit = 0;}
  8.  
  9. rabbit::rabbit(uint16_t key[8]){
  10.     setKey(key);
  11.     mCounterCarryBit = 0;
  12. }
  13.  
  14. rabbit::~rabbit(){}
  15.  
  16. //Bit wise rotation (<<<)
  17. uint32_t bwr(uint8_t steps, uint32_t in){
  18.     uint32_t tmp;
  19.     tmp = in >> (32-steps);
  20.     return (in << steps) + tmp;
  21. }
  22.  
  23. void rabbit::setKey(uint16_t sKey[8]){
  24.     //Set state variable (state 0-7)
  25.     for(uint_fast8_t j = 0; j < 8; j++){
  26.         //For j even, state (j) = key(j+1 mod 8) (concatenated with) key (j)
  27.         if((j%2) == 0){
  28.             mState[j] = sKey[(j+1)%8];
  29.             mState[j] = (mState[j] << 16) + sKey[j];
  30.         //For j odd, state(j) = key (j+5 mod 8) (concatenated with) key (j+4 mod 8)
  31.         }else{
  32.             mState[j] = sKey[(j+5) %8];
  33.             mState[j] = (mState[j] << 16) + sKey[(j+4)%8];
  34.         }
  35.     }
  36.  
  37.     //Set counter variable (counter 0-7)
  38.     for(uint_fast8_t j = 0; j < 8; j++){
  39.         //For j even, counter (j) = key(j+4 mod 8) (concatenated with) key (j+5 mod8)
  40.         if(j%2 == 0){
  41.             mCounter[j] = sKey[(j+4)%8];
  42.             mCounter[j] = (mCounter[j] << 16) + sKey[(j+5)%8];
  43.         //For j odd, counter(j) = key (j+1 mod 8) (concatenated with) key (j)
  44.         }else{
  45.             mCounter[j] = sKey[(j+1) %8];
  46.             mCounter[j] = (mCounter[j] << 16) + sKey[j];
  47.         }
  48.     }
  49.     //To diminish correlations between bits in the key and bits in the internal state variables it iterates 4 times
  50.     for(uint_fast8_t i = 0; i < 4; i++)
  51.         nextState();
  52.     //re-initialized to prevent key recoverty
  53.     for(uint_fast8_t j = 0; j < 8; j++){
  54.         mCounter[j] = mCounter[j] ^ mState[(j+4)%8];
  55.     }
  56. }
  57.  
  58. //Get the counter carry bit
  59. uint8_t rabbit::getCCB(uint8_t number){
  60.     static uint8_t lastCCB;
  61.     if(number == 0){
  62.         lastCCB = mCounterCarryBit;
  63.         //If counter(0) + a (j)0 + carry (old 7) >= 2^32 AND j = 0 set carry 1 else 0
  64.         uint64_t tmp = mCounter[0]+a[0]+lastCCB;
  65.         if(tmp >= 0xFFFFFFFF){
  66.             lastCCB = 1;
  67.         }else{
  68.             lastCCB = 0;
  69.         }
  70.         return lastCCB;
  71.     }else{
  72.         //If counter (j) + a(j) + carry (new j-1) >= 2^32 AND j > 0 set carry 1 else 0
  73.         uint64_t tmp = mCounter[number]+a[number]+lastCCB;
  74.         if(tmp >= 0xFFFFFFFF){
  75.             lastCCB = 1;
  76.         }else{
  77.             lastCCB = 0;
  78.         }
  79.     }
  80.     return lastCCB;
  81. }
  82.  
  83. void rabbit::incrementCounter(){
  84.     uint64_t calc;
  85.     //Set incremented counter variables
  86.     //counter (new 0) = counter (old 0) +a(0) + old carry bit (7) mod 2^32
  87.     calc = (mCounter[0] + a[0] + mCounterCarryBit) % 0xFFFFFFFF;
  88.     mCounter[0] = static_cast<uint32_t>(calc);
  89.     //for counter (j>0): counter(new j) = counter (old j) + a(j) + new carry bit (j-1) mod 2^32
  90.     for(uint_fast8_t j = 1; j< 8;j++){
  91.         calc = (mCounter[j] + a[j] + getCCB(j-1)) % 0xFFFFFFFF;
  92.         mCounter[j] = static_cast<uint32_t>(calc);
  93.     }
  94.     mCounterCarryBit = getCCB(7);
  95. }
  96.  
  97. void rabbit::nextState(){
  98.     incrementCounter();
  99.     uint32_t temp[8];
  100.     uint64_t calc; //For calculating more then 32Bit
  101.     //temp(j) = (state(j) + counter(j))^2 XOR ((state(j) + counter(j))^2 >> 32) mod 2^32
  102.     for(uint_fast8_t j = 0; j < 8; j++){
  103.         uint64_t a = (mState[j] + mCounter[j]);
  104.         a = a*a;
  105.         uint64_t b = (mState[j] + mCounter[j]);
  106.         b = (b * b )>>32;
  107.         b = (a^b)%0xFFFFFFFF;
  108.         temp[j] = b;
  109.     }
  110.  
  111.     //Set state variables
  112.     calc = (temp[0] + bwr(16, temp[7]) + bwr(16, temp[6])) % 0xFFFFFFFF;
  113.     mState[0] = static_cast<uint32_t>(calc);
  114.     calc = (temp[1] + bwr(8, temp[0]) + temp[7]) % 0xFFFFFFFF;
  115.     mState[1] = static_cast<uint32_t>(calc);
  116.     calc = (temp[2] + bwr(16, temp[1]) + bwr(16, temp[0])) % 0xFFFFFFFF;
  117.     mState[2] = static_cast<uint32_t>(calc);
  118.     calc = (temp[3] + bwr(8, temp[2]) + temp[1]) % 0xFFFFFFFF;
  119.     mState[3] = static_cast<uint32_t>(calc);
  120.     calc = (temp[4] + bwr(16, temp[3]) + bwr(16, temp[2])) % 0xFFFFFFFF;
  121.     mState[4] = static_cast<uint32_t>(calc);
  122.     calc = (temp[5] + bwr(8, temp[4]) + temp[3]) % 0xFFFFFFFF;
  123.     mState[5] = static_cast<uint32_t>(calc);
  124.     calc = (temp[6] + bwr(16, temp[5]) + bwr(16, temp[4])) % 0xFFFFFFFF;
  125.     mState[6] = static_cast<uint32_t>(calc);
  126.     calc = (temp[7] + bwr(8, temp[6]) + temp[5]) % 0xFFFFFFFF;
  127.     mState[7] = static_cast<uint32_t>(calc);
  128. }
  129.  
  130. void rabbit::getKeystream(){
  131.     mKeystream[0] = static_cast<uint16_t>((mState[0]) ^ (mState[5]>>16));
  132.     mKeystream[1] = static_cast<uint16_t>((mState[0]>>16) ^ mState[3]);
  133.     mKeystream[2] = static_cast<uint16_t>(mState[2] ^ (mState[7]>>16));
  134.     mKeystream[3] = static_cast<uint16_t>((mState[2]>>16) ^ mState[5]);
  135.     mKeystream[4] = static_cast<uint16_t>(mState[4] ^ (mState[1]>>16));
  136.     mKeystream[5] = static_cast<uint16_t>((mState[4]>>16) ^ mState[7]);
  137.     mKeystream[6] = static_cast<uint16_t>(mState[6] ^ (mState[3]>>16));
  138.     mKeystream[7] = static_cast<uint16_t>((mState[6]>>16) ^ mState[1]);
  139. }
  140.  
  141. void rabbit::encrypt(uint16_t in[8], uint16_t out[8]){
  142.     getKeystream();
  143.     for(uint_fast8_t i = 0; i < 8; i++){
  144.         out[i] = in[i] ^ mKeystream[i];
  145.     }
  146.     nextState();
  147. }
  148.  
  149. void rabbit::decrypt(uint16_t in[8], uint16_t out[8]){
  150.     getKeystream();
  151.     for(uint_fast8_t i = 0; i < 8; i++){
  152.         out[i] = in[i] ^ mKeystream[i];
  153.     }
  154.     nextState();
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement