Advertisement
Guest User

Rabbit 2

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