Ejemplo n.º 1
0
int main()
{ /* test driver */
    int i,j,n,nb,nk;
    BYTE y,x,m;
    char key[32];
    char block[32];

#ifndef STATICTABLES
    rijndaelGentables();
#endif

    for (i=0;i<32;i++) key[i]=0;
    key[0]=1;
    for (i=0;i<32;i++) block[i]=i;

    for (nb=4;nb<=8;nb+=2)
        for (nk=4;nk<=8;nk+=2)
    {  
        printf("\nBlock Size= %d bits, Key Size= %d bits\n",nb*32,nk*32);
        rijndaelKeySched(nb,nk,key);
        printf("Plain=   ");
        for (i=0;i<nb*4;i++) printf("%2x",block[i]);
        printf("\n");
        rijndaelEncrypt(block);
        printf("Encrypt= ");
        for (i=0;i<nb*4;i++) printf("%2x",(unsigned char)block[i]);
        printf("\n");
        rijndaelDecrypt(block);
        printf("Decrypt= ");
        for (i=0;i<nb*4;i++) printf("%2x",block[i]);
        printf("\n");
    }
    return 0;
}
Ejemplo n.º 2
0
int makeKey(BYTE direction)
{
  char i;
	
  if ((direction == DIR_ENCRYPT) || (direction == DIR_DECRYPT)) {
    keyInst.direction = direction;
  } else {
    return BAD_KEY_DIR;
  }

  keyInst.keyLen = KEY_LENGTH;


  /*** ->TRIALOG ***/
  /* keyMaterial given in bytes */

  /* initialize key schedule: */ 
  for(i = 0; i < KEY_LENGTH/8; i++) {
    k[i % 4][i / 4] = (word8) keyMaterial[i];
  }	
  /*** TRIALOG ***/

  rijndaelKeySched ();
	
  return TRUE;
}
Ejemplo n.º 3
0
char *generate_answer(stralloc *answer, uint32 uid, 
		      char *lip, uint16 lport, char *rip, uint16 rport)
{
  char *problem = "ok";
  char *x;
  char buf[5];
  stralloc out = {0};
  stralloc tmp = {0};
  stralloc key = {0};

  /* get key from enviroment */
  x = env_get("KEY");
  if (!x)
    {
      problem = "$KEY not set";
      strerr_warn1("didentd warning: $KEY not set using 'snakeoilkey'", NULL);
      x = "snakeoilkey";
    }
  
  /* initialize rijndael with $KEY */
  stralloc_copys(&key, x);
  txtparse(&key);
  pad(&key, 32);
  rijndaelKeySched(6, 8, key.s);
    
  /* build answer */
  stralloc_cats(answer, " : USERID : OTHER : ");

  uint32_pack(buf, uid); stralloc_catb(&tmp, buf, 4);
  uint16_pack(buf, lport); stralloc_catb(&tmp, buf, 2);
  uint16_pack(buf, rport); stralloc_catb(&tmp, buf, 2);
  uint32_pack(buf, time(NULL)); stralloc_catb(&tmp, buf, 4);
  stralloc_catb(&tmp, lip, 4);
  stralloc_catb(&tmp, rip, 8);

  /* encrypt last part of answer with rijndael */
  rijndaelEncrypt(tmp.s);
  
  stralloc_readyplus(&out, 32);
  base64encode(out.s, tmp.s, 24);
  
  stralloc_catb(answer, out.s, 32); 
  stralloc_cats(answer, "\r\n");
  stralloc_0(answer);
  
  return problem;
}
Ejemplo n.º 4
0
int main() {
  word8 rk[MAXROUNDS+1][4][MAXBC];
  word8 k[4][MAXKC];
  word8 a[4][MAXBC];
  word8 a0[4][MAXBC];
  roundkey rkk;
  xword32 k1_32[MAXKC];
  xword32 a1_32[MAXBC];
  char* k1 = (char *)k1_32;
  char* a1 = (char *)a1_32;

  int seed = time(0);
  int BC, KC, i, j, d, total=0;

  /* for each combination of key size / block size, check encryption
     and decryption of a random block */

  printf("Random seed: %d\n", seed);

  srand(seed);

  for (KC=4; KC<=8; KC+=2)
    for (BC=4; BC<=8; BC+=2) {

      /* generate random key and block */
      for (i=0; i<4; i++)
	for (j=0; j<BC; j++)
	  a0[i][j] = a[i][j] = a1[j*4+i] = (word8) rand();
	  
      for (i=0; i<4; i++)
	for (j=0; j<KC; j++)
	  k[i][j] = k1[j*4+i] = (word8) rand();

      /* generate round keys */
      rijndaelKeySched (k, KC*32, BC*32, rk);
      xrijndaelKeySched (k1_32, KC*32, BC*32, &rkk);
      
      /* encrypt */
      rijndaelEncrypt (a, KC*32, BC*32, rk);
      xrijndaelEncrypt (a1_32, &rkk);

      /* test difference */
      d = 0;
      for (i=0; i<4; i++)
	for (j=0; j<BC; j++)
	  if (a[i][j] != (word8) a1[j*4+i]) {
	    printf("BC=%d, KC=%d, ", BC, KC);
	    printf("Encryption: difference a[i][j]=%d, a1[j*4+i]=%d\n", a[i][j], a1[j*4+i]);
	    d++; total++;
	  }
      if (d) printf("Encryption: %d differences\n", d);

      /* decrypt */
      rijndaelDecrypt (a, KC*32, BC*32, rk);
      xrijndaelDecrypt (a1_32, &rkk);

      /* test difference */
      d = 0;
      for (i=0; i<4; i++)
	for (j=0; j<BC; j++)
	  if (a[i][j] != (word8) a1[j*4+i]) {
	    printf("BC=%d, KC=%d, ", BC, KC);
	    printf("Decryption: difference a[i][j]=%d, a1[j*4+i]=%d\n", a[i][j], a1[j*4+i]);
	    d++; total++;
	  }
      if (d) printf("Decryption: %d differences\n", d);

      /* test difference to original */
      d = 0;
      for (i=0; i<4; i++)
	for (j=0; j<BC; j++)
	  if (a0[i][j] != (word8) a1[j*4+i]) {
	    printf("BC=%d, KC=%d, ", BC, KC);
	    printf("Inverse difference a0[i][j]=%d, a1[j*4+i]=%d\n", a0[i][j], a1[j*4+i]);
	    d++; total++;
	  }
      if (d) printf("Inverse: %d differences\n", d);
    }         
  printf("Total: %d differences\n", total);

  if (total) {
    printf("The optimized Rijndael implementation does not agree with the reference implementation.\n");
    return 1;
  }

  printf("The optimized Rijndael implementation agrees with the reference implementation.\n");
  return 0;
}
Ejemplo n.º 5
0
int     aesMakeKey(MV_U8 *expandedKey, MV_U8 *keyMaterial, int keyLen, int blockLen)
{
    MV_U8   W[MAXROUNDS+1][4][MAXBC];
	MV_U8   k[4][MAXKC];
    MV_U8   j;
	int     i, rounds, KC;
	
	if (expandedKey == NULL) 
    {
		return AES_BAD_KEY_INSTANCE;
	}

	if (!((keyLen == 128) || (keyLen == 192) || (keyLen == 256))) 
    { 
		return AES_BAD_KEY_MAT;
	}

	if (keyMaterial == NULL) 
    {
		return AES_BAD_KEY_MAT;
	}

	/* initialize key schedule: */ 
 	for(i=0; i<keyLen/8; i++) 
    {
		j = keyMaterial[i];
		k[i % 4][i / 4] = j; 
	}	
	
	rijndaelKeySched (k, keyLen, blockLen, W);
#ifdef MV_AES_DEBUG
    {
        MV_U8*  pW = &W[0][0][0];
        int     x;

        mvOsPrintf("Expended Key: size = %d\n", sizeof(W));
        for(i=0; i<sizeof(W); i++)
        {
            mvOsPrintf("%02x ", pW[i]);
        }
        for(i=0; i<MAXROUNDS+1; i++)
        {
            mvOsPrintf("\n Round #%02d: ", i);
            for(x=0; x<MAXBC; x++)
            {
                mvOsPrintf("%02x%02x%02x%02x ", 
                    W[i][0][x], W[i][1][x], W[i][2][x], W[i][3][x]);
            }
            mvOsPrintf("\n");
        }
    }
#endif /* MV_AES_DEBUG */
  	switch (keyLen) 
    {
	    case 128: 
            rounds = 10;
            KC = 4; 
            break;
	    case 192: 
            rounds = 12;
            KC = 6; 
            break;
	    case 256: 
            rounds = 14;
            KC = 8; 
            break;
	    default : 
            return (-1);
	}

    for(i=0; i<MAXBC; i++)
    {
        for(j=0; j<4; j++)
        {
            expandedKey[i*4+j] = W[rounds][j][i];
        }
    }
    for(; i<KC; i++)
    {
        for(j=0; j<4; j++)
        {
            expandedKey[i*4+j] = W[rounds-1][j][i+MAXBC-KC];
        }
    }

	
	return 0;
}