Beispiel #1
0
/* Generate current and previous connection id for ip */
static void udp_make_connectionid( uint32_t connid[2], const ot_ip6 remoteip, int age ) {
  uint32_t plain[4], crypt[4];
  int i;
  if( g_now_minutes + 60 > g_hour_of_the_key ) {
    g_hour_of_the_key = g_now_minutes;
    g_key_of_the_hour[1] = g_key_of_the_hour[0];
    g_key_of_the_hour[0] = random();
  }

  memcpy( plain, remoteip, sizeof( plain ) );
  for( i=0; i<4; ++i ) plain[i] ^= g_key_of_the_hour[age];
  rijndaelEncrypt128( g_rijndael_round_key, (uint8_t*)remoteip, (uint8_t*)crypt );
  connid[0] = crypt[0] ^ crypt[1];
  connid[1] = crypt[2] ^ crypt[3];
}
Beispiel #2
0
int     aesBlockEncrypt128(MV_U8 mode, MV_U8 *IV, MV_U8 *expandedKey, int  keyLen, 
                        MV_U32 *plain, int numBlocks, MV_U32 *cipher)
{
	int     i, j, t;
	MV_U8   block[4][MAXBC];
    int     rounds;
    char    *input, *outBuffer;

    input = (char*)plain;
    outBuffer = (char*)cipher;

        /* check parameter consistency: */
    if( (expandedKey == NULL) || ((keyLen != 128) && (keyLen != 192) && (keyLen != 256))) 
    {
        return AES_BAD_KEY_MAT;
    }
    if ((mode != MODE_ECB && mode != MODE_CBC))
    {
        return AES_BAD_CIPHER_STATE;
    }

	switch (keyLen) 
    {
	    case 128: rounds = 10; break;
	    case 192: rounds = 12; break;
	    case 256: rounds = 14; break;
	    default : return (-3); /* this cannot happen */
	}

	
	switch (mode) 
    {
	    case MODE_ECB: 
		    for (i = 0; i < numBlocks; i++) 
            {
			    for (j = 0; j < 4; j++) 
                {
				    for(t = 0; t < 4; t++)
				        /* parse input stream into rectangular array */
					    block[t][j] = input[16*i+4*j+t] & 0xFF;
			    }                           
			    rijndaelEncrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
			    for (j = 0; j < 4; j++) 
                {           
				    /* parse rectangular array into output ciphertext bytes */
				    for(t = 0; t < 4; t++)
                        outBuffer[16*i+4*j+t] = (MV_U8) block[t][j];

			    }
		    }
		    break;
		
	    case MODE_CBC:
		    for (j = 0; j < 4; j++) 
            {
			    for(t = 0; t < 4; t++)
			    /* parse initial value into rectangular array */
					block[t][j] = IV[t+4*j] & 0xFF;
			}
		    for (i = 0; i < numBlocks; i++) 
            {
			    for (j = 0; j < 4; j++) 
                {
				    for(t = 0; t < 4; t++)
				        /* parse input stream into rectangular array and exor with 
				        IV or the previous ciphertext */
					    block[t][j] ^= input[16*i+4*j+t] & 0xFF;
			    }
			    rijndaelEncrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
			    for (j = 0; j < 4; j++) 
                {
				    /* parse rectangular array into output ciphertext bytes */
				    for(t = 0; t < 4; t++)
					    outBuffer[16*i+4*j+t] = (MV_U8) block[t][j];
			    }
		    }
		    break;
	
	    default: return AES_BAD_CIPHER_STATE;
	}
	
	return 0;
}