示例#1
0
void RunEncryptionKeySchedule(uint8_t *key, uint8_t *roundKeys)
{
	int32_t i;
	uint32_t j, k;
	uint32_t A, B; 
	uint32_t L[c];

	uint32_t *RoundKeys = (uint32_t *)roundKeys;

	
	/* Initialize L, then S, then mix key into S */
	L[c - 1] = 0;
	for(i = KEY_SIZE - 1; i != -1; i--) 
	{
		L[i / u] = (L[i / u] << 8) + key[i];
	}

	RoundKeys[0] = P;
	for(i = 1; i < t; i++) 
	{
		RoundKeys[i] = RoundKeys[i - 1] + Q;
	}

	A = B = i = j = 0;
	for(k = 0; k < 3 * t; k++) 
	{
		A = RoundKeys[i] = RC5_ROTL(RoundKeys[i] + (A + B), 3);
		B = L[j] = RC5_ROTL(L[j] + (A + B), (A + B));

		i = (i + 1) % t;
		j = (j + 1) % c;
	}
}
示例#2
0
// Encrypt data block
// Parameters: 
// pt - input data
// ct - encrypt data
void RC5Simple::RC5_EncryptBlock(RC5_TWORD *pt, RC5_TWORD *ct)
{ 
  RC5_TWORD i; 
  RC5_TWORD a=pt[0]+rc5_s[0];
  RC5_TWORD b=pt[1]+rc5_s[1];

  for(i=1; i<=RC5_R; i++)
    { 
      a = RC5_ROTL(a^b, b)+rc5_s[2*i]; 
      b = RC5_ROTL(b^a, a)+rc5_s[2*i+1]; 
    }

  ct[0] = a; 
  ct[1] = b;  
} 
示例#3
0
// Setup secret key
// Parameters: 
// key - secret input key[RC5_B] 
void RC5Simple::RC5_Setup(unsigned char *key)
{
   RC5_LOG(( "RC5_Setup, set key to: " ));
   for(int i=0; i<RC5_B; i++)
    RC5_LOG(( "%.2X", key[i] ));
   RC5_LOG(( "\n" ));


   int i, j, k;
   RC5_TWORD u=RC5_W/8, a, b, l[RC5_C]; 

   // Initialize l[], then rc5_s[], then mix key into rc5_s[]
   for(i=RC5_B-1, l[RC5_C-1]=0; i!=-1; i--)
    l[i/u] = (l[i/u]<<8)+key[i];

   RC5_LOG(( "RC5_Setup, l[]: " ));
   for(int i=0; i<RC5_C; i++)
    RC5_LOG(( "%.2X", l[i] ));
   RC5_LOG(( "\n" ));


   for(rc5_s[0]=rc5_p,i=1; i<RC5_T; i++) 
    rc5_s[i] = rc5_s[i-1]+rc5_q;

   RC5_LOG(( "RC5_Setup, rc5_s[]: " ));
   for(int i=0; i<RC5_T; i++)
    RC5_LOG(( "%.2X", rc5_s[i] ));
   RC5_LOG(( "\n" ));


   // 3*t > 3*c
   for(a=b=i=j=k=0; k<3*RC5_T; k++, i=(i+1)%RC5_T, j=(j+1)%RC5_C)   
     { 
       a = rc5_s[i] = RC5_ROTL(rc5_s[i]+(a+b),3);  
       b = l[j] = RC5_ROTL(l[j]+(a+b),(a+b)); 
     } 

   RC5_LOG(( "RC5_Setup, mix rc5_s[]: " ));
   for(int i=0; i<RC5_T; i++)
    RC5_LOG(( "%.2X", rc5_s[i] ));
   RC5_LOG(( "\n" ));
}
示例#4
0
void Encrypt(uint8_t *block, uint8_t *roundKeys)
{
	uint32_t *Block = (uint32_t *)block;
	uint32_t *RoundKeys = (uint32_t *)roundKeys;
	

	Block[0] = Block[0] + READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[0]);
	Block[1] = Block[1] + READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[1]);

	
	/* Round 1 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[2]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[3]);


	/* Round 2 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[4]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[5]); 


	/* Round 3 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[6]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[7]); 


	/* Round 4 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[8]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[9]); 


	/* Round 5 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[10]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[11]); 


	/* Round 6 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[12]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[13]); 


	/* Round 7 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[14]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[15]); 


	/* Round 8 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[16]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[17]); 


	/* Round 9 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[18]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[19]); 


	/* Round 10 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[20]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[21]); 


	/* Round 11 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[22]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[23]); 


	/* Round 12 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[24]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[25]); 


	/* Round 13 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[26]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[27]); 


	/* Round 14 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[28]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[29]); 


	/* Round 15 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[30]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[31]); 


	/* Round 16 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[32]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[33]); 


	/* Round 17 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[34]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[35]); 


	/* Round 18 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[36]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[37]); 


	/* Round 19 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[38]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[39]); 


	/* Round 20 */
	Block[0] = RC5_ROTL(Block[0] ^ Block[1], Block[1]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[40]); 
	Block[1] = RC5_ROTL(Block[1] ^ Block[0], Block[0]) + 
						READ_ROUND_KEY_DOUBLE_WORD(RoundKeys[41]);
}