Advertisement
Guest User

Rabbit

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