RabbitRabbit is a Light Weight Stream Cipher and was written by Martin Boesgaard, Mette Vesterager, Thomas Christensen and Erik Zenner. It creates a key stream from an 128-bit key and a 64-bit initialization vector [paper]. Test vectorsThe keys, IVs and outputs are presented byte-wise in hexadecimal format. The leftmost byte is the one to be put on the lowest address. ================================================================================ Test 1: Key setup and encryption/decryption/prng key1 = [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00] out1 = [02 F7 4A 1C 26 45 6B F5 EC D6 A5 36 F0 54 57 B1 A7 8A C6 89 47 6C 69 7B 39 0C 9C C5 15 D8 E8 88 EF 9A 69 71 8B 82 49 A1 A7 3C 5A 6E 5B 90 45 95] ================================================================================ Test 2: Key setup and encryption/decryption/prng key2 = [C2 1F CF 38 81 CD 5E E8 62 8A CC B0 A9 89 0D F8] out2 = [3D 02 E0 C7 30 55 91 12 B4 73 B7 90 DE E0 18 DF CD 6D 73 0C E5 4E 19 F0 C3 5E C4 79 0E B6 C7 4A 9F B4 92 E1 B5 40 36 3A E3 83 C0 1F 9F A2 26 1A] ================================================================================ Test 3: Key setup and encryption/decryption/prng key3 = [1D 27 2C 6A 2D 8E 3D FC AC 14 05 6B 78 D6 33 A0] out3 = [A3 A9 7A BB 80 39 38 20 B7 E5 0C 4A BB 53 82 3D C4 42 37 99 C2 EF C9 FF B3 A4 12 5F 1F 4C 99 A8 97 C0 73 3F F1 F1 8D 25 6A 59 E2 BA AB C1 F4 F1] ================================================================================ Test 4: Key setup, iv setup and encryption/decryption/prng key4 = [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00] iv4 = [00 00 00 00 00 00 00 00] out4 = [ED B7 05 67 37 5D CD 7C D8 95 54 F8 5E 27 A7 C6 8D 4A DC 70 32 29 8F 7B D4 EF F5 04 AC A6 29 5F 66 8F BF 47 8A DB 2B E5 1E 6C DE 29 2B 82 DE 2A] ================================================================================ Test 5: Key setup, iv setup and encryption/decryption/prng key5 = [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00] iv5 = [59 7E 26 C1 75 F5 73 C3] out5 = [6D 7D 01 22 92 CC DC E0 E2 12 00 58 B9 4E CD 1F 2E 6F 93 ED FF 99 24 7B 01 25 21 D1 10 4E 5F A7 A7 9B 02 12 D0 BD 56 23 39 38 E7 93 C3 12 C1 EB] ================================================================================ Test 6: Key setup, iv setup and encryption/decryption/prng key6 = [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00] iv6 = [27 17 F4 D2 1A 56 EB A6] out6 = [4D 10 51 A1 23 AF B6 70 BF 8D 85 05 C8 D8 5A 44 03 5B C3 AC C6 67 AE AE 5B 2C F4 47 79 F2 C8 96 CB 51 15 F0 34 F0 3D 31 17 1C A7 5F 89 FC CB 9F] ================================================================================ /******************************************************************************/ /* File name: rabbit.c */ /*----------------------------------------------------------------------------*/ /* Rabbit C source code in ECRYPT format */ /*----------------------------------------------------------------------------*/ /* Copyright (C) Cryptico A/S. All rights reserved. */ /* */ /* YOU SHOULD CAREFULLY READ THIS LEGAL NOTICE BEFORE USING THIS SOFTWARE. */ /* */ /* This software is developed by Cryptico A/S and/or its suppliers. */ /* All title and intellectual property rights in and to the software, */ /* including but not limited to patent rights and copyrights, are owned by */ /* Cryptico A/S and/or its suppliers. */ /* */ /* The software may be used solely for non-commercial purposes */ /* without the prior written consent of Cryptico A/S. For further */ /* information on licensing terms and conditions please contact Cryptico A/S */ /* at [email protected] */ /* */ /* Cryptico, CryptiCore, the Cryptico logo and "Re-thinking encryption" are */ /* either trademarks or registered trademarks of Cryptico A/S. */ /* */ /* Cryptico A/S shall not in any way be liable for any use of this software. */ /* The software is provided "as is" without any express or implied warranty. */ /* */ /******************************************************************************/ #include "ecrypt-sync.h" #include "ecrypt-portable.h" #include <stdio.h> #include <string.h> /* -------------------------------------------------------------------------- */ /* Square a 32-bit unsigned integer to obtain the 64-bit result and return */ /* the upper 32 bits XOR the lower 32 bits */ static u32 RABBIT_g_func(u32 x) { /* Temporary variables */ u32 a, b, h, l; /* Construct high and low argument for squaring */ a = x&0xFFFF; b = x>>16; /* Calculate high and low result of squaring */ h = (((U32V(a*a)>>17) + U32V(a*b))>>15) + b*b; l = x*x; /* Return high XOR low */ return U32V(h^l); } /* -------------------------------------------------------------------------- */ int hex2data(unsigned char *data, const unsigned char *hexstring, unsigned int len) { unsigned const char *pos = hexstring; char *endptr; size_t count = 0; if ((hexstring[0] == '\0') || (strlen(hexstring) % 2)) { //hexstring contains no data //or hexstring has an odd length return -1; } for(count = 0; count < len; count++) { char buf[5] = {'0', 'x', pos[0], pos[1], 0}; data[count] = strtol(buf, &endptr, 0); pos += 2 * sizeof(char); if (endptr[0] != '\0') { //non-hexadecimal character encountered return -1; } } return 0; } void perform_test (u8 *key, u8* iv, int iv_length_in_bits ) { ECRYPT_ctx ctx; /* Keystream generator context */ u8 keystream[16]; /* Array to contain generated keystream bytes */ int i; /* Counting variable */ /* Load key */ ECRYPT_keysetup (&ctx, key, 128, iv_length_in_bits); /* Load IV */ if (iv_length_in_bits>1) ECRYPT_ivsetup (&ctx, iv); /* Generate keystream */ ECRYPT_keystream_bytes (&ctx, keystream, 16); /* Display the key */ printf ("Key ="); for (i=0; i<16; i++) printf ("%02x", key[i]); printf ("\n"); /* Display the IV */ printf ("IV ="); for (i=0; i<(iv_length_in_bits+7)/8; i++) printf ("%02x", iv[i]); printf ("\n"); /* Display the derived keytream */ printf ("Keystream ="); for (i=0; i<16; i++) printf ("%02x", keystream[i]); printf ("\n"); printf ("\n"); } /* Calculate the next internal state */ static void RABBIT_next_state(RABBIT_ctx *p_instance) { /* Temporary variables */ u32 g[8], c_old[8], i; /* Save old counter values */ for (i=0; i<8; i++) c_old[i] = p_instance->c[i]; /* Calculate new counter values */ p_instance->c[0] = U32V(p_instance->c[0] + 0x4D34D34D + p_instance->carry); p_instance->c[1] = U32V(p_instance->c[1] + 0xD34D34D3 + (p_instance->c[0] < c_old[0])); p_instance->c[2] = U32V(p_instance->c[2] + 0x34D34D34 + (p_instance->c[1] < c_old[1])); p_instance->c[3] = U32V(p_instance->c[3] + 0x4D34D34D + (p_instance->c[2] < c_old[2])); p_instance->c[4] = U32V(p_instance->c[4] + 0xD34D34D3 + (p_instance->c[3] < c_old[3])); p_instance->c[5] = U32V(p_instance->c[5] + 0x34D34D34 + (p_instance->c[4] < c_old[4])); p_instance->c[6] = U32V(p_instance->c[6] + 0x4D34D34D + (p_instance->c[5] < c_old[5])); p_instance->c[7] = U32V(p_instance->c[7] + 0xD34D34D3 + (p_instance->c[6] < c_old[6])); p_instance->carry = (p_instance->c[7] < c_old[7]); /* Calculate the g-values */ for (i=0;i<8;i++) g[i] = RABBIT_g_func(U32V(p_instance->x[i] + p_instance->c[i])); /* Calculate new state values */ p_instance->x[0] = U32V(g[0] + ROTL32(g[7],16) + ROTL32(g[6], 16)); p_instance->x[1] = U32V(g[1] + ROTL32(g[0], 8) + g[7]); p_instance->x[2] = U32V(g[2] + ROTL32(g[1],16) + ROTL32(g[0], 16)); p_instance->x[3] = U32V(g[3] + ROTL32(g[2], 8) + g[1]); p_instance->x[4] = U32V(g[4] + ROTL32(g[3],16) + ROTL32(g[2], 16)); p_instance->x[5] = U32V(g[5] + ROTL32(g[4], 8) + g[3]); p_instance->x[6] = U32V(g[6] + ROTL32(g[5],16) + ROTL32(g[4], 16)); p_instance->x[7] = U32V(g[7] + ROTL32(g[6], 8) + g[5]); } /* ------------------------------------------------------------------------- */ /* No initialization is needed for Rabbit */ void ECRYPT_init(void) { return; } /* ------------------------------------------------------------------------- */ /* Key setup */ void ECRYPT_keysetup(ECRYPT_ctx* ctx, const u8* key, u32 keysize, u32 ivsize) { /* Temporary variables */ u32 k0, k1, k2, k3, i; /* Generate four subkeys */ k0 = U8TO32_LITTLE(key+ 0); k1 = U8TO32_LITTLE(key+ 4); k2 = U8TO32_LITTLE(key+ 8); k3 = U8TO32_LITTLE(key+12); /* Generate initial state variables */ ctx->master_ctx.x[0] = k0; ctx->master_ctx.x[2] = k1; ctx->master_ctx.x[4] = k2; ctx->master_ctx.x[6] = k3; ctx->master_ctx.x[1] = U32V(k3<<16) | (k2>>16); ctx->master_ctx.x[3] = U32V(k0<<16) | (k3>>16); ctx->master_ctx.x[5] = U32V(k1<<16) | (k0>>16); ctx->master_ctx.x[7] = U32V(k2<<16) | (k1>>16); /* Generate initial counter values */ ctx->master_ctx.c[0] = ROTL32(k2, 16); ctx->master_ctx.c[2] = ROTL32(k3, 16); ctx->master_ctx.c[4] = ROTL32(k0, 16); ctx->master_ctx.c[6] = ROTL32(k1, 16); ctx->master_ctx.c[1] = (k0&0xFFFF0000) | (k1&0xFFFF); ctx->master_ctx.c[3] = (k1&0xFFFF0000) | (k2&0xFFFF); ctx->master_ctx.c[5] = (k2&0xFFFF0000) | (k3&0xFFFF); ctx->master_ctx.c[7] = (k3&0xFFFF0000) | (k0&0xFFFF); /* Clear carry bit */ ctx->master_ctx.carry = 0; /* Iterate the system four times */ for (i=0; i<4; i++) RABBIT_next_state(&(ctx->master_ctx)); /* Modify the counters */ for (i=0; i<8; i++) ctx->master_ctx.c[i] ^= ctx->master_ctx.x[(i+4)&0x7]; /* Copy master instance to work instance */ for (i=0; i<8; i++) { ctx->work_ctx.x[i] = ctx->master_ctx.x[i]; ctx->work_ctx.c[i] = ctx->master_ctx.c[i]; } ctx->work_ctx.carry = ctx->master_ctx.carry; } /* ------------------------------------------------------------------------- */ /* IV setup */ void ECRYPT_ivsetup(ECRYPT_ctx* ctx, const u8* iv) { /* Temporary variables */ u32 i0, i1, i2, i3, i; /* Generate four subvectors */ i0 = U8TO32_LITTLE(iv+0); i2 = U8TO32_LITTLE(iv+4); i1 = (i0>>16) | (i2&0xFFFF0000); i3 = (i2<<16) | (i0&0x0000FFFF); /* Modify counter values */ ctx->work_ctx.c[0] = ctx->master_ctx.c[0] ^ i0; ctx->work_ctx.c[1] = ctx->master_ctx.c[1] ^ i1; ctx->work_ctx.c[2] = ctx->master_ctx.c[2] ^ i2; ctx->work_ctx.c[3] = ctx->master_ctx.c[3] ^ i3; ctx->work_ctx.c[4] = ctx->master_ctx.c[4] ^ i0; ctx->work_ctx.c[5] = ctx->master_ctx.c[5] ^ i1; ctx->work_ctx.c[6] = ctx->master_ctx.c[6] ^ i2; ctx->work_ctx.c[7] = ctx->master_ctx.c[7] ^ i3; /* Copy state variables */ for (i=0; i<8; i++) ctx->work_ctx.x[i] = ctx->master_ctx.x[i]; ctx->work_ctx.carry = ctx->master_ctx.carry; /* Iterate the system four times */ for (i=0; i<4; i++) RABBIT_next_state(&(ctx->work_ctx)); } /* ------------------------------------------------------------------------- */ /* Encrypt/decrypt a message of any size */ void ECRYPT_process_bytes(int action, ECRYPT_ctx* ctx, const u8* input, u8* output, u32 msglen) { /* Temporary variables */ u32 i; u8 buffer[16]; /* Encrypt/decrypt all full blocks */ while (msglen >= 16) { /* Iterate the system */ RABBIT_next_state(&(ctx->work_ctx)); /* Encrypt/decrypt 16 bytes of data */ *(u32*)(output+ 0) = *(u32*)(input+ 0) ^ U32TO32_LITTLE(ctx->work_ctx.x[0] ^ (ctx->work_ctx.x[5]>>16) ^ U32V(ctx->work_ctx.x[3]<<16)); *(u32*)(output+ 4) = *(u32*)(input+ 4) ^ U32TO32_LITTLE(ctx->work_ctx.x[2] ^ (ctx->work_ctx.x[7]>>16) ^ U32V(ctx->work_ctx.x[5]<<16)); *(u32*)(output+ 8) = *(u32*)(input+ 8) ^ U32TO32_LITTLE(ctx->work_ctx.x[4] ^ (ctx->work_ctx.x[1]>>16) ^ U32V(ctx->work_ctx.x[7]<<16)); *(u32*)(output+12) = *(u32*)(input+12) ^ U32TO32_LITTLE(ctx->work_ctx.x[6] ^ (ctx->work_ctx.x[3]>>16) ^ U32V(ctx->work_ctx.x[1]<<16)); /* Increment pointers and decrement length */ input += 16; output += 16; msglen -= 16; } /* Encrypt/decrypt remaining data */ if (msglen) { /* Iterate the system */ RABBIT_next_state(&(ctx->work_ctx)); /* Generate 16 bytes of pseudo-random data */ *(u32*)(buffer+ 0) = U32TO32_LITTLE(ctx->work_ctx.x[0] ^ (ctx->work_ctx.x[5]>>16) ^ U32V(ctx->work_ctx.x[3]<<16)); *(u32*)(buffer+ 4) = U32TO32_LITTLE(ctx->work_ctx.x[2] ^ (ctx->work_ctx.x[7]>>16) ^ U32V(ctx->work_ctx.x[5]<<16)); *(u32*)(buffer+ 8) = U32TO32_LITTLE(ctx->work_ctx.x[4] ^ (ctx->work_ctx.x[1]>>16) ^ U32V(ctx->work_ctx.x[7]<<16)); *(u32*)(buffer+12) = U32TO32_LITTLE(ctx->work_ctx.x[6] ^ (ctx->work_ctx.x[3]>>16) ^ U32V(ctx->work_ctx.x[1]<<16)); /* Encrypt/decrypt the data */ for (i=0; i<msglen; i++) output[i] = input[i] ^ buffer[i]; } } /* ------------------------------------------------------------------------- */ /* Generate keystream */ void ECRYPT_keystream_bytes(ECRYPT_ctx* ctx, u8* keystream, u32 length) { /* Temporary variables */ u32 i; u8 buffer[16]; /* Generate all full blocks */ while (length >= 16) { /* Iterate the system */ RABBIT_next_state(&(ctx->work_ctx)); /* Generate 16 bytes of pseudo-random data */ *(u32*)(keystream+ 0) = U32TO32_LITTLE(ctx->work_ctx.x[0] ^ (ctx->work_ctx.x[5]>>16) ^ U32V(ctx->work_ctx.x[3]<<16)); *(u32*)(keystream+ 4) = U32TO32_LITTLE(ctx->work_ctx.x[2] ^ (ctx->work_ctx.x[7]>>16) ^ U32V(ctx->work_ctx.x[5]<<16)); *(u32*)(keystream+ 8) = U32TO32_LITTLE(ctx->work_ctx.x[4] ^ (ctx->work_ctx.x[1]>>16) ^ U32V(ctx->work_ctx.x[7]<<16)); *(u32*)(keystream+12) = U32TO32_LITTLE(ctx->work_ctx.x[6] ^ (ctx->work_ctx.x[3]>>16) ^ U32V(ctx->work_ctx.x[1]<<16)); /* Increment pointers and decrement length */ keystream += 16; length -= 16; } /* Generate remaining pseudo-random data */ if (length) { /* Iterate the system */ RABBIT_next_state(&(ctx->work_ctx)); /* Generate 16 bytes of pseudo-random data */ *(u32*)(buffer+ 0) = U32TO32_LITTLE(ctx->work_ctx.x[0] ^ (ctx->work_ctx.x[5]>>16) ^ U32V(ctx->work_ctx.x[3]<<16)); *(u32*)(buffer+ 4) = U32TO32_LITTLE(ctx->work_ctx.x[2] ^ (ctx->work_ctx.x[7]>>16) ^ U32V(ctx->work_ctx.x[5]<<16)); *(u32*)(buffer+ 8) = U32TO32_LITTLE(ctx->work_ctx.x[4] ^ (ctx->work_ctx.x[1]>>16) ^ U32V(ctx->work_ctx.x[7]<<16)); *(u32*)(buffer+12) = U32TO32_LITTLE(ctx->work_ctx.x[6] ^ (ctx->work_ctx.x[3]>>16) ^ U32V(ctx->work_ctx.x[1]<<16)); /* Copy remaining data */ for (i=0; i<length; i++) keystream[i] = buffer[i]; } } /* ------------------------------------------------------------------------- */ /* Encrypt/decrypt a number of full blocks */ void ECRYPT_process_blocks(int action, ECRYPT_ctx* ctx, const u8* input, u8* output, u32 blocks) { /* Temporary variables */ u32 i; for (i=0; i<blocks; i++) { /* Iterate the system */ RABBIT_next_state(&(ctx->work_ctx)); /* Encrypt/decrypt 16 bytes of data */ *(u32*)(output+ 0) = *(u32*)(input+ 0) ^ U32TO32_LITTLE(ctx->work_ctx.x[0] ^ (ctx->work_ctx.x[5]>>16) ^ U32V(ctx->work_ctx.x[3]<<16)); *(u32*)(output+ 4) = *(u32*)(input+ 4) ^ U32TO32_LITTLE(ctx->work_ctx.x[2] ^ (ctx->work_ctx.x[7]>>16) ^ U32V(ctx->work_ctx.x[5]<<16)); *(u32*)(output+ 8) = *(u32*)(input+ 8) ^ U32TO32_LITTLE(ctx->work_ctx.x[4] ^ (ctx->work_ctx.x[1]>>16) ^ U32V(ctx->work_ctx.x[7]<<16)); *(u32*)(output+12) = *(u32*)(input+12) ^ U32TO32_LITTLE(ctx->work_ctx.x[6] ^ (ctx->work_ctx.x[3]>>16) ^ U32V(ctx->work_ctx.x[1]<<16)); /* Increment pointers to input and output data */ input += 16; output += 16; } } /* ------------------------------------------------------------------------- */ int main(int argc, char **argv) { char str1[]="00000000000000000000000000000000"; char str2[]="0000000000000000"; char str3[]=""; u8 key_1[16] = {0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; u8 iv_1[16] = {0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; /* Initialise the algorithm */ ECRYPT_init (); if (argc>1) { strcpy(str1, argv[1]); } if (argc>2) { strcpy(str2, argv[2]); } puts("Rabbit"); // hex2data(key_1, str1,strlen(str1)); strcpy(str3,str2); if (strlen(str2)>1) hex2data(iv_1, str2,strlen(str2)); /* Generate the test data */ if (strlen(str3)==16) perform_test (key_1, iv_1,4*strlen(str3) ); else { u8* test; perform_test (key_1, test,0); } } Code |