Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* NUR Rabbit CPP
- */
- #include "rabbit.h"
- const uint32_t a[8] = {0x4D34D34D, 0xD34D34D3, 0x34D34D34, 0x4D34D34D, 0xD34D34D3, 0x34D34D34, 0x4D34D34D, 0xD34D34D3};
- rabbit::rabbit(){mCounterCarryBit = 0;}
- rabbit::rabbit(uint16_t key[8]){
- setKey(key);
- mCounterCarryBit = 0;
- }
- rabbit::~rabbit(){}
- //Bit wise rotation (<<<)
- uint32_t bwr(uint8_t steps, uint32_t in){
- uint32_t tmp;
- tmp = in >> (32-steps);
- return (in << steps) + tmp;
- }
- void rabbit::setKey(uint16_t sKey[8]){
- //Set state variable (state 0-7)
- for(uint_fast8_t j = 0; j < 8; j++){
- //For j even, state (j) = key(j+1 mod 8) (concatenated with) key (j)
- if((j%2) == 0){
- mState[j] = sKey[(j+1)%8];
- mState[j] = (mState[j] << 16) + sKey[j];
- //For j odd, state(j) = key (j+5 mod 8) (concatenated with) key (j+4 mod 8)
- }else{
- mState[j] = sKey[(j+5) %8];
- mState[j] = (mState[j] << 16) + sKey[(j+4)%8];
- }
- }
- //Set counter variable (counter 0-7)
- for(uint_fast8_t j = 0; j < 8; j++){
- //For j even, counter (j) = key(j+4 mod 8) (concatenated with) key (j+5 mod8)
- if(j%2 == 0){
- mCounter[j] = sKey[(j+4)%8];
- mCounter[j] = (mCounter[j] << 16) + sKey[(j+5)%8];
- //For j odd, counter(j) = key (j+1 mod 8) (concatenated with) key (j)
- }else{
- mCounter[j] = sKey[(j+1) %8];
- mCounter[j] = (mCounter[j] << 16) + sKey[j];
- }
- }
- //To diminish correlations between bits in the key and bits in the internal state variables it iterates 4 times
- for(uint_fast8_t i = 0; i < 4; i++)
- nextState();
- //re-initialized to prevent key recoverty
- for(uint_fast8_t j = 0; j < 8; j++){
- mCounter[j] = mCounter[j] ^ mState[(j+4)%8];
- }
- }
- //Get the counter carry bit
- uint8_t rabbit::getCCB(uint8_t number){
- static uint8_t lastCCB;
- if(number == 0){
- lastCCB = mCounterCarryBit;
- //If counter(0) + a (j)0 + carry (old 7) >= 2^32 AND j = 0 set carry 1 else 0
- uint64_t tmp = mCounter[0]+a[0]+lastCCB;
- if(tmp >= 0xFFFFFFFF){
- lastCCB = 1;
- }else{
- lastCCB = 0;
- }
- return lastCCB;
- }else{
- //If counter (j) + a(j) + carry (new j-1) >= 2^32 AND j > 0 set carry 1 else 0
- uint64_t tmp = mCounter[number]+a[number]+lastCCB;
- if(tmp >= 0xFFFFFFFF){
- lastCCB = 1;
- }else{
- lastCCB = 0;
- }
- }
- return lastCCB;
- }
- void rabbit::incrementCounter(){
- uint64_t calc;
- //Set incremented counter variables
- //counter (new 0) = counter (old 0) +a(0) + old carry bit (7) mod 2^32
- calc = (mCounter[0] + a[0] + mCounterCarryBit) % 0xFFFFFFFF;
- mCounter[0] = static_cast<uint32_t>(calc);
- //for counter (j>0): counter(new j) = counter (old j) + a(j) + new carry bit (j-1) mod 2^32
- for(uint_fast8_t j = 1; j< 8;j++){
- calc = (mCounter[j] + a[j] + getCCB(j-1)) % 0xFFFFFFFF;
- mCounter[j] = static_cast<uint32_t>(calc);
- }
- mCounterCarryBit = getCCB(7);
- }
- void rabbit::nextState(){
- incrementCounter();
- uint32_t temp[8];
- uint64_t calc; //For calculating more then 32Bit
- //temp(j) = (state(j) + counter(j))^2 XOR ((state(j) + counter(j))^2 >> 32) mod 2^32
- for(uint_fast8_t j = 0; j < 8; j++){
- uint64_t a = (mState[j] + mCounter[j]);
- a = a*a;
- uint64_t b = (mState[j] + mCounter[j]);
- b = (b * b )>>32;
- b = (a^b)%0xFFFFFFFF;
- temp[j] = b;
- }
- //Set state variables
- calc = (temp[0] + bwr(16, temp[7]) + bwr(16, temp[6])) % 0xFFFFFFFF;
- mState[0] = static_cast<uint32_t>(calc);
- calc = (temp[1] + bwr(8, temp[0]) + temp[7]) % 0xFFFFFFFF;
- mState[1] = static_cast<uint32_t>(calc);
- calc = (temp[2] + bwr(16, temp[1]) + bwr(16, temp[0])) % 0xFFFFFFFF;
- mState[2] = static_cast<uint32_t>(calc);
- calc = (temp[3] + bwr(8, temp[2]) + temp[1]) % 0xFFFFFFFF;
- mState[3] = static_cast<uint32_t>(calc);
- calc = (temp[4] + bwr(16, temp[3]) + bwr(16, temp[2])) % 0xFFFFFFFF;
- mState[4] = static_cast<uint32_t>(calc);
- calc = (temp[5] + bwr(8, temp[4]) + temp[3]) % 0xFFFFFFFF;
- mState[5] = static_cast<uint32_t>(calc);
- calc = (temp[6] + bwr(16, temp[5]) + bwr(16, temp[4])) % 0xFFFFFFFF;
- mState[6] = static_cast<uint32_t>(calc);
- calc = (temp[7] + bwr(8, temp[6]) + temp[5]) % 0xFFFFFFFF;
- mState[7] = static_cast<uint32_t>(calc);
- }
- void rabbit::getKeystream(){
- mKeystream[0] = static_cast<uint16_t>((mState[0]) ^ (mState[5]>>16));
- mKeystream[1] = static_cast<uint16_t>((mState[0]>>16) ^ mState[3]);
- mKeystream[2] = static_cast<uint16_t>(mState[2] ^ (mState[7]>>16));
- mKeystream[3] = static_cast<uint16_t>((mState[2]>>16) ^ mState[5]);
- mKeystream[4] = static_cast<uint16_t>(mState[4] ^ (mState[1]>>16));
- mKeystream[5] = static_cast<uint16_t>((mState[4]>>16) ^ mState[7]);
- mKeystream[6] = static_cast<uint16_t>(mState[6] ^ (mState[3]>>16));
- mKeystream[7] = static_cast<uint16_t>((mState[6]>>16) ^ mState[1]);
- }
- void rabbit::encrypt(uint16_t in[8], uint16_t out[8]){
- getKeystream();
- for(uint_fast8_t i = 0; i < 8; i++){
- out[i] = in[i] ^ mKeystream[i];
- }
- nextState();
- }
- void rabbit::decrypt(uint16_t in[8], uint16_t out[8]){
- getKeystream();
- for(uint_fast8_t i = 0; i < 8; i++){
- out[i] = in[i] ^ mKeystream[i];
- }
- nextState();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement