예제 #1
1
/*******************************************************************************  
* Function Name  : AES_expand_deckey  
* Description    : According to key computes the expanded key (expkey) for AES128   
*                  decryption.  
* Input          : key: user key (128 bits / 16 bytes)
* Output         : expkey: expanded key (320 bits / 40 bytes)
* Return         : None  
*******************************************************************************/   
void AES_expand_deckey(ot_u32* key, ot_u32* expkey) {
#if (AES_USEHW)
    platform_expand_deckey(key, expkey);

#elif (AES_USEFAST)
    register ot_u32* local_pointer;// = expkey;   
    register int i; // = 0;   
    register ot_u32 copy0;   
    register ot_u32 copy1;   
    register ot_u32 copy2;   
    register ot_u32 copy3;   
   
    AES_expand_enckey(key,expkey);   
    
    local_pointer       = expkey;
    local_pointer[0]    = key[0];   
    local_pointer[1]    = key[1];   
    local_pointer[2]    = key[2];   
    local_pointer[3]    = key[3];   
   
    for (i = 1; i <10; i++) {   
        local_pointer  += 4;   
        copy0           = local_pointer[0];    
        copy1           = local_pointer[1];   
        copy2           = local_pointer[2];   
        copy3           = local_pointer[3];   
        
        local_pointer[0] =      dec_table[Sbox[byte0(copy0)]]  ^   
                           rot1(dec_table[Sbox[byte1(copy0)]]) ^   
                           rot2(dec_table[Sbox[byte2(copy0)]]) ^   
                           rot3(dec_table[Sbox[byte3(copy0)]]);   
        
        local_pointer[1] =      dec_table[Sbox[byte0(copy1)]]  ^   
                           rot1(dec_table[Sbox[byte1(copy1)]]) ^   
                           rot2(dec_table[Sbox[byte2(copy1)]]) ^   
                           rot3(dec_table[Sbox[byte3(copy1)]]);   
        
        local_pointer[2] =      dec_table[Sbox[byte0(copy2)]]  ^   
                           rot1(dec_table[Sbox[byte1(copy2)]]) ^   
                           rot2(dec_table[Sbox[byte2(copy2)]]) ^   
                           rot3(dec_table[Sbox[byte3(copy2)]]);   
        
        local_pointer[3] =      dec_table[Sbox[byte0(copy3)]]  ^   
                           rot1(dec_table[Sbox[byte1(copy3)]]) ^   
                           rot2(dec_table[Sbox[byte2(copy3)]]) ^   
                           rot3(dec_table[Sbox[byte3(copy3)]]);   
  }
  
#elif (AES_USELITE == ENABLED)
    AES_expand_enckey(key, expkey);
#endif
}   
예제 #2
0
파일: aes128.c 프로젝트: david-kooi/eval
/*******************************************************************************
* Function Name  : AES_keyschedule_enc
* Description    : According to key, computes the expanded key (expkey) for AES128 
*                  encryption.
* Input          : key: user key
* Output         : expkey: expanded key
* Return         : None
*******************************************************************************/
void AES_keyschedule_enc(u32* key, u32* expkey)	
{
  register u32* local_pointer = expkey;
  register int i = 0;
  register u32 copy0;
  register u32 copy1;
  register u32 copy2;
  register u32 copy3;
  
  copy0 = key[0]; 
  copy1 = key[1];
  copy2 = key[2];
  copy3 = key[3];
  
  local_pointer[0] = copy0;
  local_pointer[1] = copy1;
  local_pointer[2] = copy2;
  local_pointer[3] = copy3;
  
  for (;i < 10;)
  {
    copy0 ^= WORD8_TO_WORD32( Sbox[byte1(copy3)],
                             Sbox[byte2(copy3)],
                             Sbox[byte3(copy3)],
                             Sbox[byte0(copy3)]) ^ rcon[i++];
    copy1 ^= copy0;
    copy2 ^= copy1;
    copy3 ^= copy2;
    local_pointer += 4;
    local_pointer[0] = copy0;
    local_pointer[1] = copy1;
    local_pointer[2] = copy2;
    local_pointer[3] = copy3;
  }
}
예제 #3
0
/*******************************************************************************  
* Function Name  : AES_expand_enckey  
* Description    : According to key computes the expanded key exp for AES128   
*                  encryption.  
* Input          : key: user key (128 bits / 16 bytes)
* Output         : expkey: expanded key (352 bits / 44 bytes)
* Return         : None  
*******************************************************************************/   
void AES_expand_enckey(ot_u32* key, ot_u32* expkey) {
#if (AES_USEHW)
    platform_expand_enckey(key, expkey);

#elif  ((AES_USEFAST == ENABLED) || (AES_USELITE == ENABLED))
    register ot_u32* local_pointer = expkey;   
    register int i = 0;   
    register ot_u32 copy0;   
    register ot_u32 copy1;   
    register ot_u32 copy2;   
    register ot_u32 copy3;   
       
    copy0 = key[0];    
    copy1 = key[1];   
    copy2 = key[2];   
    copy3 = key[3];   
    local_pointer[0] = copy0;   
    local_pointer[1] = copy1;   
    local_pointer[2] = copy2;   
    local_pointer[3] = copy3;
    
    for (;i <10;) {   
        copy0 ^= WORD8_TO_WORD32( Sbox[byte1(copy3)],   
                                  Sbox[byte2(copy3)],   
                                  Sbox[byte3(copy3)],   
                                  Sbox[byte0(copy3)]) ^ rcon[i++];   
        copy1 ^= copy0;   
        copy2 ^= copy1;   
        copy3 ^= copy2;   
        local_pointer += 4;   
        local_pointer[0] = copy0;   
        local_pointer[1] = copy1;   
        local_pointer[2] = copy2;   
        local_pointer[3] = copy3;   
    }
#endif
}      
예제 #4
0
QN  tex_font_metric_rep::top (QN c) { return (QN) byte0 (exten [rem (c)]); }
예제 #5
0
int tex_font_metric_rep::w (QN c) {
  if ((c<bc) || (c>ec)) return 0;
  return width [byte0 (char_info[c-bc])]; }
예제 #6
0
void
tex_font_metric_rep::execute (int* s, int n, int* buf, int* ker, int& m) {
  STACK_NEW_ARRAY (stack, int, m);
  int bp, sp=0, i;

  for (i=0; i<n; i++) stack[sp++]= s[n-1-i];
  sp--; bp= 0;

  while (sp>=0) {
    int cur_char= stack [sp]& 255;
    // cout << "Processing " << (char) cur_char << "\n";

    /***************** the ligature-kerning program ******************/
    if ((cur_char<bc) || (cur_char>ec)) sp--;
    else if ((tag (cur_char)==1) && (sp>0)) {
      register int next_char= stack [sp-1]& 255;
      register int pc= rem (cur_char);
      if (byte0 (lig_kern [pc]) > 128) pc= word1 (lig_kern [pc]);

      while (true) {
	register int instr= lig_kern [pc];

	//if (byte0 (instr) >= 128) { // halt
	//  // cout << "  Halt\n";
	//  ker [bp]  = 0;
	//  buf [bp++]= stack[sp--];
	//  break;
	//}

	if (byte1 (instr) != next_char) { // continue
	  // cout << "  " << (char) byte1 (instr) << " != " << (char) next_char
	  //      << " => pc := pc + " << (byte0 (instr)+1) << "\n";
	  int skip_byte = byte0(instr);
  	  if (skip_byte >= 128) { // current instruction is the final instruction
	    // cout << "  Halt\n";
	    ker [bp]  = 0;
	    buf [bp++]= stack[sp--];
	    break;
	  }
	  else {
	    pc += skip_byte+1;
	    continue;
	  }
	}

	// cout << "  " << (char) byte1 (instr) << " == "
	//      << (char) next_char << " => ";

	if (byte2 (instr) < 128) { // ligature
	  // cout << "Ligature ";
	  int code= byte2 (instr);
	  int a   = code>>2;
	  int b   = (code>>1)&1;
	  int c   = code&1;
	  // cout << "(" << a << "," << b << "," << c << ")\n";
	  if (b==0) sp--;
	  stack [sp++]= byte3 (instr);
	  if (c!=0) stack[sp++]= cur_char;
	  sp--;
	  while (a>0) {
	    ker [bp]  = 0;
	    buf [bp++]= stack [sp--];
	    a--;
	  }
	  break;
	}

	else { // kerning
	  // cout << "Kerning (" << kern  [word1x (instr)] << ")\n";
	  ker [bp]  = kern  [word1x (instr)];
	  buf [bp++]= stack [sp--];
	  break;
	}
      }
    }
예제 #7
0
파일: aes128.c 프로젝트: david-kooi/eval
/*******************************************************************************
* Function Name  : AES_decrypt
* Description    : Decrypts one block of 16 bytes
* Input          : - input_pointer: input block address
*                  - expkey: decryption key 
* Output         : output_pointer: output block address
* Return         : None
*******************************************************************************/
void AES_decrypt(u8* input_pointer, u32* output_pointer, u32* expkey)
{
  register u32 s0;
  register u32 s1;
  register u32 s2;
  register u32 s3;
  register u32 t0;
  register u32 t1;
  register u32 t2;
  register u32 t3;
  register int r = 10 >> 1;
  register u32* local_pointer = expkey + (40);
  
    //added ***
  register u32 copy0;
  register u32 copy1;
  register u32 copy2;
  register u32 copy3;
  
  copy0 = *input_pointer;
  copy0 =copy0<<8;
  copy0 |= *(input_pointer+1);
  copy0 =copy0<<8;
  copy0 |= *(input_pointer+2);
  copy0 =copy0<<8;
  copy0 |= *(input_pointer+3);
  
  copy1 = *(input_pointer+4);
  copy1 =copy1<<8;
  copy1 |= *(input_pointer+5);
  copy1 =copy1<<8;
  copy1 |= *(input_pointer+6);
  copy1 =copy1<<8;
  copy1 |= *(input_pointer+7);
  
  copy2 = *(input_pointer+8);
  copy2 =copy2<<8;
  copy2 |= *(input_pointer+9);
  copy2 =copy2<<8;
  copy2 |= *(input_pointer+10);
  copy2 =copy2<<8;
  copy2 |= *(input_pointer+11);
  
  copy3 = *(input_pointer+12);
  copy3 =copy3<<8;
  copy3 |= *(input_pointer+13);
  copy3 =copy3<<8;
  copy3 |= *(input_pointer+14);
  copy3 =copy3<<8;
  copy3 |= *(input_pointer+15);
  
  s0 = copy0^ local_pointer[0];
  s1 = copy1 ^ local_pointer[1];
  s2 = copy2 ^ local_pointer[2];
  s3 = copy3 ^ local_pointer[3];
  
  for (;;) 
  {
    local_pointer -= 8;
    
    t0 =      dec_table[byte0(s0)]  ^
      rot1(dec_table[byte1(s3)]) ^
        rot2(dec_table[byte2(s2)]) ^
          rot3(dec_table[byte3(s1)]) ^
            local_pointer[4];
    t1 =      dec_table[byte0(s1)]  ^
      rot1(dec_table[byte1(s0)]) ^
        rot2(dec_table[byte2(s3)]) ^
          rot3(dec_table[byte3(s2)]) ^
            local_pointer[5];
    t2 =      dec_table[byte0(s2)]  ^
      rot1(dec_table[byte1(s1)]) ^
        rot2(dec_table[byte2(s0)]) ^
          rot3(dec_table[byte3(s3)]) ^
            local_pointer[6];
    t3 =      dec_table[byte0(s3)]  ^
      rot1(dec_table[byte1(s2)]) ^
        rot2(dec_table[byte2(s1)]) ^
          rot3(dec_table[byte3(s0)]) ^
            local_pointer[7];
    
    if (--r == 0) 
    {
      break;
    }
    
    s0 =      dec_table[byte0(t0)]  ^
      rot1(dec_table[byte1(t3)]) ^
        rot2(dec_table[byte2(t2)]) ^
          rot3(dec_table[byte3(t1)]) ^
            local_pointer[0];
    s1 =      dec_table[byte0(t1)]  ^
      rot1(dec_table[byte1(t0)]) ^
        rot2(dec_table[byte2(t3)]) ^
          rot3(dec_table[byte3(t2)]) ^
            local_pointer[1];
    s2 =      dec_table[byte0(t2)]  ^
      rot1(dec_table[byte1(t1)]) ^
        rot2(dec_table[byte2(t0)]) ^
          rot3(dec_table[byte3(t3)]) ^
            local_pointer[2];
    s3 =      dec_table[byte0(t3)]  ^
      rot1(dec_table[byte1(t2)]) ^
        rot2(dec_table[byte2(t1)]) ^
          rot3(dec_table[byte3(t0)]) ^
            local_pointer[3];	   
  }
  
  output_pointer[0] = WORD8_TO_WORD32( InvSbox[byte0(t0)],
                                      InvSbox[byte1(t3)],
                                      InvSbox[byte2(t2)],
                                      InvSbox[byte3(t1)]) ^ local_pointer[0];
  output_pointer[1] = WORD8_TO_WORD32( InvSbox[byte0(t1)],
                                      InvSbox[byte1(t0)],
                                      InvSbox[byte2(t3)],
                                      InvSbox[byte3(t2)]) ^ local_pointer[1];
  output_pointer[2] = WORD8_TO_WORD32( InvSbox[byte0(t2)],
                                      InvSbox[byte1(t1)],
                                      InvSbox[byte2(t0)],
                                      InvSbox[byte3(t3)]) ^ local_pointer[2];
  output_pointer[3] = WORD8_TO_WORD32( InvSbox[byte0(t3)],
                                      InvSbox[byte1(t2)],
                                      InvSbox[byte2(t1)],
                                      InvSbox[byte3(t0)]) ^ local_pointer[3];
}
예제 #8
0
파일: aes128.c 프로젝트: david-kooi/eval
/*******************************************************************************
* Function Name  : AES_keyschedule_dec
* Description    : According to key computes the expanded key (expkey) for AES128 
*                  decryption.
* Input          : key: user key
* Output         : expkey: expanded key
* Return         : None
*******************************************************************************/
void AES_keyschedule_dec(u8* key, u32* expkey)
{
  register u32* local_pointer = expkey;
  register int i = 0;
  register u32 copy0;
  register u32 copy1;
  register u32 copy2;
  register u32 copy3;
  
  AES_keyschedule_enc(key,expkey);
  
  local_pointer[0] = *key;
  local_pointer[0] =local_pointer[0]<<8;
  local_pointer[0] |= *(key+1);
  local_pointer[0] =local_pointer[0]<<8;
  local_pointer[0] |= *(key+2);
  local_pointer[0] =local_pointer[0]<<8;
  local_pointer[0] |= *(key+3);
  
  local_pointer[1] = *(key+4);
  local_pointer[1] =local_pointer[1]<<8;
  local_pointer[1] |= *(key+5);
  local_pointer[1] =local_pointer[1]<<8;
  local_pointer[1] |= *(key+6);
  local_pointer[1] =local_pointer[1]<<8;
  local_pointer[1] |= *(key+7);
  
  local_pointer[2] = *(key+8);
  local_pointer[2] =local_pointer[2]<<8;
  local_pointer[2] |= *(key+9);
  local_pointer[2] =local_pointer[2]<<8;
  local_pointer[2] |= *(key+10);
  local_pointer[2] =local_pointer[2]<<8;
  local_pointer[2] |= *(key+11);
  
  local_pointer[3] = *(key+12);
  local_pointer[3] =local_pointer[3]<<8;
  local_pointer[3] |= *(key+13);
  local_pointer[3] =local_pointer[3]<<8;
  local_pointer[3] |= *(key+14);
  local_pointer[3] =local_pointer[3]<<8;
  local_pointer[3] |= *(key+15);
  
  
//  local_pointer[0] =  key[0];
//  local_pointer[1] =  key[1];
//  local_pointer[2] =  key[2];
//  local_pointer[3] =  key[3];
  
  for (i = 1; i <10; i++) 
  {
    local_pointer += 4;
    copy0 = local_pointer[0]; 
    copy1 = local_pointer[1];
    copy2 = local_pointer[2];
    copy3 = local_pointer[3];
    local_pointer[0] =      dec_table[Sbox[byte0(copy0)]]  ^
      rot1(dec_table[Sbox[byte1(copy0)]]) ^
        rot2(dec_table[Sbox[byte2(copy0)]]) ^
          rot3(dec_table[Sbox[byte3(copy0)]]);
    local_pointer[1] =      dec_table[Sbox[byte0(copy1)]]  ^
      rot1(dec_table[Sbox[byte1(copy1)]]) ^
        rot2(dec_table[Sbox[byte2(copy1)]]) ^
          rot3(dec_table[Sbox[byte3(copy1)]]);
    local_pointer[2] =      dec_table[Sbox[byte0(copy2)]]  ^
      rot1(dec_table[Sbox[byte1(copy2)]]) ^
        rot2(dec_table[Sbox[byte2(copy2)]]) ^
          rot3(dec_table[Sbox[byte3(copy2)]]);
    local_pointer[3] =      dec_table[Sbox[byte0(copy3)]]  ^
      rot1(dec_table[Sbox[byte1(copy3)]]) ^
        rot2(dec_table[Sbox[byte2(copy3)]]) ^
          rot3(dec_table[Sbox[byte3(copy3)]]);
  }
}
예제 #9
0
파일: aes128.c 프로젝트: david-kooi/eval
/*******************************************************************************
* Function Name  : AES_keyschedule_enc
* Description    : According to key computes the expanded key exp for AES128 
*                  encryption.
* Input          : key: user key
* Output         : expkey: expanded key
* Return         : None
*******************************************************************************/
void AES_keyschedule_enc(u8* key, u32* expkey)
{
  register u32* local_pointer = expkey;
  register int i = 0;
  register u32 copy0;
  register u32 copy1;
  register u32 copy2;
  register u32 copy3;
  
  
  copy0 = *key;
  copy0 =copy0<<8;
  copy0 |= *(key+1);
  copy0 =copy0<<8;
  copy0 |= *(key+2);
  copy0 =copy0<<8;
  copy0 |= *(key+3);
  
  copy1 = *(key+4);
  copy1 =copy1<<8;
  copy1 |= *(key+5);
  copy1 =copy1<<8;
  copy1 |= *(key+6);
  copy1 =copy1<<8;
  copy1 |= *(key+7);
  
  copy2 = *(key+8);
  copy2 =copy2<<8;
  copy2 |= *(key+9);
  copy2 =copy2<<8;
  copy2 |= *(key+10);
  copy2 =copy2<<8;
  copy2 |= *(key+11);
  
  copy3 = *(key+12);
  copy3 =copy3<<8;
  copy3 |= *(key+13);
  copy3 =copy3<<8;
  copy3 |= *(key+14);
  copy3 =copy3<<8;
  copy3 |= *(key+15);
  
  
  //  copy0 = key[0]; 
  //  copy1 = key[1];
  //  copy2 = key[2];
  //  copy3 = key[3];
  local_pointer[0] = copy0;
  local_pointer[1] = copy1;
  local_pointer[2] = copy2;
  local_pointer[3] = copy3;
  for (;i < 10;)
  {
    copy0 ^= WORD8_TO_WORD32( Sbox[byte1(copy3)],
                             Sbox[byte2(copy3)],
                             Sbox[byte3(copy3)],
                             Sbox[byte0(copy3)]) ^ rcon[i++];
    copy1 ^= copy0;
    copy2 ^= copy1;
    copy3 ^= copy2;
    local_pointer += 4;
    local_pointer[0] = copy0;
    local_pointer[1] = copy1;
    local_pointer[2] = copy2;
    local_pointer[3] = copy3;
  }
}	
예제 #10
0
파일: aes128.c 프로젝트: david-kooi/eval
/*******************************************************************************
* Function Name  : AES_encrypt
* Description    : Encrypts one block of 16 bytes
* Input          : - input_pointer: input block address
*                  - expkey: encryption key
* Output         : output_pointer
* Return         : None
*******************************************************************************/
void AES_encrypt(u32* input_pointer, u32* output_pointer, u32* expkey)
{
  register u32 t0;
  register u32 t1;
  register u32 t2;
  register u32 t3;  
  register u32 s0;
  register u32 s1;
  register u32 s2;
  register u32 s3;
  register int r = 10;/*Round counter */
  register u32* local_pointer = expkey;
  
  s0 = input_pointer[0] ^ local_pointer[0];
  s1 = input_pointer[1] ^ local_pointer[1];
  s2 = input_pointer[2] ^ local_pointer[2];
  s3 = input_pointer[3] ^ local_pointer[3];
  local_pointer += 4;
  /* ADD KEY before start round*/
  for (;;) 
  {
    t0 = WORD8_TO_WORD32( Sbox[byte0(s0)],
                         Sbox[byte1(s1)],
                         Sbox[byte2(s2)],
                         Sbox[byte3(s3)]);
    t1 = WORD8_TO_WORD32( Sbox[byte0(s1)],
                         Sbox[byte1(s2)],
                         Sbox[byte2(s3)],
                         Sbox[byte3(s0)]);
    t2 = WORD8_TO_WORD32( Sbox[byte0(s2)],
                         Sbox[byte1(s3)],
                         Sbox[byte2(s0)],
                         Sbox[byte3(s1)]);
    t3 = WORD8_TO_WORD32( Sbox[byte0(s3)],
                         Sbox[byte1(s0)],
                         Sbox[byte2(s1)],
                         Sbox[byte3(s2)]);
    /*End of SBOX + Shift ROW*/
    s0 = fwd_mcol(t0)^local_pointer[0];
    s1 = fwd_mcol(t1)^local_pointer[1];
    s2 = fwd_mcol(t2)^local_pointer[2];
    s3 = fwd_mcol(t3)^local_pointer[3];
    /*End of mix colomn*/
    
    local_pointer += 4; 
    if ( --r == 1)
    {
      break;
    }
    
  }/*End for(;;)*/
  
  /*Start Last round*/
  t0 = WORD8_TO_WORD32( Sbox[byte0(s0)],
                       Sbox[byte1(s1)],
                       Sbox[byte2(s2)],
                       Sbox[byte3(s3)]);
  t1 = WORD8_TO_WORD32( Sbox[byte0(s1)],
                       Sbox[byte1(s2)],
                       Sbox[byte2(s3)],
                       Sbox[byte3(s0)]);
  t2 = WORD8_TO_WORD32( Sbox[byte0(s2)],
                       Sbox[byte1(s3)],
                       Sbox[byte2(s0)],
                       Sbox[byte3(s1)]);
  t3 = WORD8_TO_WORD32( Sbox[byte0(s3)],
                       Sbox[byte1(s0)],
                       Sbox[byte2(s1)],
                       Sbox[byte3(s2)]);
  
  t0 ^= local_pointer[0];
  t1 ^= local_pointer[1];
  t2 ^= local_pointer[2];
  t3 ^= local_pointer[3];
  
  /*Store of the result of encryption*/
  output_pointer[0] = t0;
  output_pointer[1] = t1;
  output_pointer[2] = t2;
  output_pointer[3] = t3;
}
예제 #11
0
파일: aes128.c 프로젝트: david-kooi/eval
/*******************************************************************************
* Function Name  : AES_decrypt
* Description    : Decrypts one block of 16 bytes
* Input          : - input_pointer: input block address
*                  - expkey: decryption key 
* Output         : output_pointer: output block address
* Return         : None
*******************************************************************************/
void AES_decrypt(u32* input_pointer, u32* output_pointer, u32* expkey)
{  
  /* Register: ask to the compiler to maintain this variable in the 
  processor's registers and don't store it in RAM */
  register u32 t0; 
  register u32 t1;
  register u32 t2;
  register u32 t3;  
  register u32 s0;
  register u32 s1;
  register u32 s2;
  register u32 s3;
  register int r =  10; /* Count the round */ 
  register u32* local_pointer = expkey;
  u32 f2,f4,f8;
  
  s0 = input_pointer[0] ^ local_pointer[0];
  s1 = input_pointer[1] ^ local_pointer[1];
  s2 = input_pointer[2] ^ local_pointer[2];
  s3 = input_pointer[3] ^ local_pointer[3];
  
  /* First add key: before start the rounds */
  local_pointer += 4;
  
  for (;;) /* Start round */ 
  {
    t0 = WORD8_TO_WORD32( InvSbox[byte0(s0)],
                         InvSbox[byte1(s3)],
                         InvSbox[byte2(s2)],
                         InvSbox[byte3(s1)]) ^ local_pointer[4];
    t1 = WORD8_TO_WORD32( InvSbox[byte0(s1)],
                         InvSbox[byte1(s0)],
                         InvSbox[byte2(s3)],
                         InvSbox[byte3(s2)]) ^ local_pointer[5];
    t2 = WORD8_TO_WORD32( InvSbox[byte0(s2)],
                         InvSbox[byte1(s1)],
                         InvSbox[byte2(s0)],
                         InvSbox[byte3(s3)]) ^ local_pointer[6];
    t3 = WORD8_TO_WORD32( InvSbox[byte0(s3)],
                         InvSbox[byte1(s2)],
                         InvSbox[byte2(s1)],
                         InvSbox[byte3(s0)]) ^ local_pointer[7];
    /*End of InvSbox,  INVshiftRow,  add key*/
    s0=inv_mcol(t0);
    s1=inv_mcol(t1);
    s2=inv_mcol(t2);
    s3=inv_mcol(t3);
    /*End of INVMix column */		
    local_pointer += 4; /*Follow the key sheduler to choose the right round key*/
    
    if (--r == 1) 
    {
      break;
    }
    
  }/*End of round*/
  
  /*Start last round :is the only one different from the other*/
  t0 = WORD8_TO_WORD32( InvSbox[byte0(s0)],
                       InvSbox[byte1(s3)],
                       InvSbox[byte2(s2)],
                       InvSbox[byte3(s1)]) ^ local_pointer[4];
  t1 = WORD8_TO_WORD32( InvSbox[byte0(s1)],
                       InvSbox[byte1(s0)],
                       InvSbox[byte2(s3)],
                       InvSbox[byte3(s2)]) ^ local_pointer[5];
  t2 = WORD8_TO_WORD32( InvSbox[byte0(s2)],
                       InvSbox[byte1(s1)],
                       InvSbox[byte2(s0)],
                       InvSbox[byte3(s3)]) ^ local_pointer[6];
  t3 = WORD8_TO_WORD32( InvSbox[byte0(s3)],
                       InvSbox[byte1(s2)],
                       InvSbox[byte2(s1)],
                       InvSbox[byte3(s0)]) ^ local_pointer[7];
  output_pointer[0] = t0;
  output_pointer[1] = t1;
  output_pointer[2] = t2;
  output_pointer[3] = t3;	
}
예제 #12
0
/*******************************************************************************  
* Function Name  : AES_decrypt  
* Description    : Decrypts one block of 16 bytes  
* Input          : - input_pointer: input block address  
*                  - expkey: decryption key   
* Output         : output_pointer: output block address  
* Return         : None  
*******************************************************************************/   
void AES_decrypt(ot_u32* input_pointer, ot_u32* output_pointer, ot_u32* expkey) {
#if (AES_USEHW == ENABLED)

#elif (AES_USEFAST == ENABLED)   
  register ot_u32 s0;   
  register ot_u32 s1;   
  register ot_u32 s2;   
  register ot_u32 s3;   
  register ot_u32 t0;   
  register ot_u32 t1;   
  register ot_u32 t2;   
  register ot_u32 t3;   
  register int r = 10 >> 1;   
  register ot_u32* local_pointer = expkey + (40);   
   
  s0 = input_pointer[0] ^ local_pointer[0];   
  s1 = input_pointer[1] ^ local_pointer[1];   
  s2 = input_pointer[2] ^ local_pointer[2];   
  s3 = input_pointer[3] ^ local_pointer[3];   
   
  for (;;)    
  {   
    local_pointer -= 8;   
       
    t0 =      dec_table[byte0(s0)]  ^   
         rot1(dec_table[byte1(s3)]) ^   
         rot2(dec_table[byte2(s2)]) ^   
         rot3(dec_table[byte3(s1)]) ^   
         local_pointer[4];   
    t1 =      dec_table[byte0(s1)]  ^   
         rot1(dec_table[byte1(s0)]) ^   
         rot2(dec_table[byte2(s3)]) ^   
         rot3(dec_table[byte3(s2)]) ^   
         local_pointer[5];   
    t2 =      dec_table[byte0(s2)]  ^   
         rot1(dec_table[byte1(s1)]) ^   
         rot2(dec_table[byte2(s0)]) ^   
         rot3(dec_table[byte3(s3)]) ^   
         local_pointer[6];   
    t3 =      dec_table[byte0(s3)]  ^   
         rot1(dec_table[byte1(s2)]) ^   
         rot2(dec_table[byte2(s1)]) ^   
         rot3(dec_table[byte3(s0)]) ^   
         local_pointer[7];   
       
    if (--r == 0)    
    {   
      break;   
    }   
       
    s0 =      dec_table[byte0(t0)]  ^   
         rot1(dec_table[byte1(t3)]) ^   
         rot2(dec_table[byte2(t2)]) ^   
         rot3(dec_table[byte3(t1)]) ^   
         local_pointer[0];   
    s1 =      dec_table[byte0(t1)]  ^   
         rot1(dec_table[byte1(t0)]) ^   
         rot2(dec_table[byte2(t3)]) ^   
         rot3(dec_table[byte3(t2)]) ^   
         local_pointer[1];   
    s2 =      dec_table[byte0(t2)]  ^   
         rot1(dec_table[byte1(t1)]) ^   
         rot2(dec_table[byte2(t0)]) ^   
         rot3(dec_table[byte3(t3)]) ^   
         local_pointer[2];   
    s3 =      dec_table[byte0(t3)]  ^   
         rot1(dec_table[byte1(t2)]) ^   
         rot2(dec_table[byte2(t1)]) ^   
         rot3(dec_table[byte3(t0)]) ^   
         local_pointer[3];        
  }   
     
  output_pointer[0] = WORD8_TO_WORD32( InvSbox[byte0(t0)],   
                                       InvSbox[byte1(t3)],   
                                       InvSbox[byte2(t2)],   
                                       InvSbox[byte3(t1)]) ^ local_pointer[0];   
  output_pointer[1] = WORD8_TO_WORD32( InvSbox[byte0(t1)],   
                                       InvSbox[byte1(t0)],   
                                       InvSbox[byte2(t3)],   
                                       InvSbox[byte3(t2)]) ^ local_pointer[1];   
  output_pointer[2] = WORD8_TO_WORD32( InvSbox[byte0(t2)],   
                                       InvSbox[byte1(t1)],   
                                       InvSbox[byte2(t0)],   
                                       InvSbox[byte3(t3)]) ^ local_pointer[2];   
  output_pointer[3] = WORD8_TO_WORD32( InvSbox[byte0(t3)],   
                                       InvSbox[byte1(t2)],   
                                       InvSbox[byte2(t1)],   
                                       InvSbox[byte3(t0)]) ^ local_pointer[3];

#elif (AES_USELITE == ENABLED)
      /* Register: ask to the compiler to maintain this variable in the   
   processor's registers and don't store it in RAM */   
  register ot_u32 t0;    
  register ot_u32 t1;   
  register ot_u32 t2;   
  register ot_u32 t3;     
  register ot_u32 s0;   
  register ot_u32 s1;   
  register ot_u32 s2;   
  register ot_u32 s3;   
  register int r =  10; /* Count the round */    
  register ot_u32* local_pointer = expkey + 40;   
  ot_u32 f2,f4,f8;   
       
  s0 = input_pointer[0] ^ local_pointer[0];   
  s1 = input_pointer[1] ^ local_pointer[1];   
  s2 = input_pointer[2] ^ local_pointer[2];   
  s3 = input_pointer[3] ^ local_pointer[3];   
       
  /* First add key: before start the rounds */   
  local_pointer -= 8;   
       
  for (;;) /* Start round */    
  {   
    t0 = WORD8_TO_WORD32( InvSbox[byte0(s0)],   
                          InvSbox[byte1(s3)],   
                          InvSbox[byte2(s2)],   
                          InvSbox[byte3(s1)]) ^ local_pointer[4];   
    t1 = WORD8_TO_WORD32( InvSbox[byte0(s1)],   
                          InvSbox[byte1(s0)],   
                          InvSbox[byte2(s3)],   
                          InvSbox[byte3(s2)]) ^ local_pointer[5];   
    t2 = WORD8_TO_WORD32( InvSbox[byte0(s2)],   
                          InvSbox[byte1(s1)],   
                          InvSbox[byte2(s0)],   
                          InvSbox[byte3(s3)]) ^ local_pointer[6];   
    t3 = WORD8_TO_WORD32( InvSbox[byte0(s3)],   
                          InvSbox[byte1(s2)],   
                          InvSbox[byte2(s1)],   
                          InvSbox[byte3(s0)]) ^ local_pointer[7];   
    /*End of InvSbox,  INVshiftRow,  add key*/   
    s0=inv_mcol(t0);   
    s1=inv_mcol(t1);   
    s2=inv_mcol(t2);   
    s3=inv_mcol(t3);   
    /*End of INVMix column */          
    local_pointer -= 4; /*Follow the key sheduler to choose the right round key*/   
         
    if (--r == 1)    
    {   
      break;   
    }   
     
  }/*End of round*/   
     
  /*Start last round :is the only one different from the other*/   
  t0 = WORD8_TO_WORD32( InvSbox[byte0(s0)],   
                        InvSbox[byte1(s3)],   
                        InvSbox[byte2(s2)],   
                        InvSbox[byte3(s1)]) ^ local_pointer[4];   
  t1 = WORD8_TO_WORD32( InvSbox[byte0(s1)],   
                        InvSbox[byte1(s0)],   
                        InvSbox[byte2(s3)],   
                        InvSbox[byte3(s2)]) ^ local_pointer[5];   
  t2 = WORD8_TO_WORD32( InvSbox[byte0(s2)],   
                        InvSbox[byte1(s1)],   
                        InvSbox[byte2(s0)],   
                        InvSbox[byte3(s3)]) ^ local_pointer[6];   
  t3 = WORD8_TO_WORD32( InvSbox[byte0(s3)],   
                        InvSbox[byte1(s2)],   
                        InvSbox[byte2(s1)],   
                        InvSbox[byte3(s0)]) ^ local_pointer[7];   
  output_pointer[0] = t0;   
  output_pointer[1] = t1;   
  output_pointer[2] = t2;   
  output_pointer[3] = t3;
#endif
}   
예제 #13
0
/*******************************************************************************  
* Function Name  : AES_encrypt  
* Description    : Encrypts one block of 16 bytes  
* Input          : - input_pointer: input block address  
*                  - expkey: encryption key  
* Output         : output_pointer  
* Return         : None  
*******************************************************************************/   
void AES_encrypt(ot_u32* input_pointer, ot_u32* output_pointer, ot_u32* expkey) {
#if (AES_USEHW == ENABLED)

#elif (AES_USEFAST == ENABLED)
  register ot_u32 s0;   
  register ot_u32 s1;   
  register ot_u32 s2;   
  register ot_u32 s3;   
  register ot_u32 t0;   
  register ot_u32 t1;   
  register ot_u32 t2;   
  register ot_u32 t3;   
  register int r = 10 >> 1;   
  register ot_u32* local_pointer = expkey;   
     
  s0 = input_pointer[0] ^ local_pointer[0];   
  s1 = input_pointer[1] ^ local_pointer[1];   
  s2 = input_pointer[2] ^ local_pointer[2];   
  s3 = input_pointer[3] ^ local_pointer[3];   
     
  for (;;) {   
    t0 =      enc_table[byte0(s0)]  ^   
         rot1(enc_table[byte1(s1)]) ^   
         rot2(enc_table[byte2(s2)]) ^   
         rot3(enc_table[byte3(s3)]) ^   
         local_pointer[4];                                 
    t1 =      enc_table[byte0(s1)]  ^   
         rot1(enc_table[byte1(s2)]) ^   
         rot2(enc_table[byte2(s3)]) ^   
         rot3(enc_table[byte3(s0)]) ^   
         local_pointer[5];   
    t2 =      enc_table[byte0(s2)]  ^   
         rot1(enc_table[byte1(s3)]) ^   
         rot2(enc_table[byte2(s0)]) ^   
         rot3(enc_table[byte3(s1)]) ^   
         local_pointer[6];   
    t3 =      enc_table[byte0(s3)]  ^   
         rot1(enc_table[byte1(s0)]) ^   
         rot2(enc_table[byte2(s1)]) ^   
         rot3(enc_table[byte3(s2)]) ^   
         local_pointer[7];   
       
    local_pointer += 8;       
    if (--r == 0) {   
      break;   
    }   
       
    s0 =      enc_table[byte0(t0)]  ^   
         rot1(enc_table[byte1(t1)]) ^   
         rot2(enc_table[byte2(t2)]) ^   
         rot3(enc_table[byte3(t3)]) ^   
         local_pointer[0];   
    s1 =      enc_table[byte0(t1)]  ^   
         rot1(enc_table[byte1(t2)]) ^   
         rot2(enc_table[byte2(t3)]) ^   
         rot3(enc_table[byte3(t0)]) ^   
         local_pointer[1];   
    s2 =      enc_table[byte0(t2)]  ^   
         rot1(enc_table[byte1(t3)]) ^   
         rot2(enc_table[byte2(t0)]) ^   
         rot3(enc_table[byte3(t1)]) ^   
         local_pointer[2];   
    s3 =      enc_table[byte0(t3)]  ^   
         rot1(enc_table[byte1(t0)]) ^   
         rot2(enc_table[byte2(t1)]) ^   
         rot3(enc_table[byte3(t2)]) ^   
         local_pointer[3];   
  }   
     
  output_pointer[0] = WORD8_TO_WORD32( Sbox[byte0(t0)],   
                                       Sbox[byte1(t1)],   
                                       Sbox[byte2(t2)],   
                                       Sbox[byte3(t3)]) ^ local_pointer[0];   
  output_pointer[1] = WORD8_TO_WORD32( Sbox[byte0(t1)],   
                                       Sbox[byte1(t2)],   
                                       Sbox[byte2(t3)],   
                                       Sbox[byte3(t0)]) ^ local_pointer[1];   
  output_pointer[2] = WORD8_TO_WORD32( Sbox[byte0(t2)],   
                                       Sbox[byte1(t3)],   
                                       Sbox[byte2(t0)],   
                                       Sbox[byte3(t1)]) ^ local_pointer[2];   
  output_pointer[3] = WORD8_TO_WORD32( Sbox[byte0(t3)],   
                                       Sbox[byte1(t0)],   
                                       Sbox[byte2(t1)],   
                                       Sbox[byte3(t2)]) ^ local_pointer[3];

#elif (AES_USELITE == ENABLED)
    register ot_u32 t0;   
    register ot_u32 t1;   
    register ot_u32 t2;   
    register ot_u32 t3;     
    register ot_u32 s0;   
    register ot_u32 s1;   
    register ot_u32 s2;   
    register ot_u32 s3;   
    register int r = 10;    // Round counter
    register ot_u32* local_pointer = expkey;   
   
    s0 = input_pointer[0] ^ local_pointer[0];   
    s1 = input_pointer[1] ^ local_pointer[1];   
    s2 = input_pointer[2] ^ local_pointer[2];   
    s3 = input_pointer[3] ^ local_pointer[3];   
    local_pointer += 4;  
   
  // ADD KEY before start round
  for (;;)    
  {   
    t0 = WORD8_TO_WORD32( Sbox[byte0(s0)],   
                          Sbox[byte1(s1)],   
                          Sbox[byte2(s2)],   
                          Sbox[byte3(s3)]);   
    t1 = WORD8_TO_WORD32( Sbox[byte0(s1)],   
                          Sbox[byte1(s2)],   
                          Sbox[byte2(s3)],   
                          Sbox[byte3(s0)]);   
    t2 = WORD8_TO_WORD32( Sbox[byte0(s2)],   
                          Sbox[byte1(s3)],   
                          Sbox[byte2(s0)],   
                          Sbox[byte3(s1)]);   
    t3 = WORD8_TO_WORD32( Sbox[byte0(s3)],   
                          Sbox[byte1(s0)],   
                          Sbox[byte2(s1)],   
                          Sbox[byte3(s2)]);   
    /*End of SBOX + Shift ROW*/   
    s0 = fwd_mcol(t0)^local_pointer[0];   
    s1 = fwd_mcol(t1)^local_pointer[1];   
    s2 = fwd_mcol(t2)^local_pointer[2];   
    s3 = fwd_mcol(t3)^local_pointer[3];   
    /*End of mix colomn*/   
           
    local_pointer += 4;    
    if ( --r == 1)   
    {   
      break;   
    }   
     
  }/*End for(;;)*/   
       
  /*Start Last round*/   
  t0 = WORD8_TO_WORD32( Sbox[byte0(s0)],   
                        Sbox[byte1(s1)],   
                        Sbox[byte2(s2)],   
                        Sbox[byte3(s3)]);   
  t1 = WORD8_TO_WORD32( Sbox[byte0(s1)],   
                        Sbox[byte1(s2)],   
                        Sbox[byte2(s3)],   
                        Sbox[byte3(s0)]);   
  t2 = WORD8_TO_WORD32( Sbox[byte0(s2)],   
                        Sbox[byte1(s3)],   
                        Sbox[byte2(s0)],   
                        Sbox[byte3(s1)]);   
  t3 = WORD8_TO_WORD32( Sbox[byte0(s3)],   
                        Sbox[byte1(s0)],   
                        Sbox[byte2(s1)],   
                        Sbox[byte3(s2)]);   
               
  t0 ^= local_pointer[0];   
  t1 ^= local_pointer[1];   
  t2 ^= local_pointer[2];   
  t3 ^= local_pointer[3];   
       
  /*Store of the result of encryption*/   
  output_pointer[0] = t0;   
  output_pointer[1] = t1;   
  output_pointer[2] = t2;   
  output_pointer[3] = t3;
#endif
}