示例#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
文件: cg2.c 项目: vivekn/blitz8086
int code_2op_reg_imm(char *mnem,char * sreg1,int imm){
	int reg1 = getval(sreg1);
	int codesize = 0;
	//printf("\n%s:%d\n%s:%d",sreg1,reg1,reg2);
	if(!(reg1+1)){if(VERBOSE)
		printf("\nInstruction Result:%d",codesize);
		return 0;}
	buffer.s = 0;
	buffer.w = reg1>7?1:0;
	buffer.mod = 3;
	int t_rmcode = _rmcode(mnem);
	if(t_rmcode==-2)
		{
		if(reg1==0||reg1==4||reg1==8) {
			buffer.opcode = 011;			
			if(buffer.w){
				int mat[2];
				encode(mat,imm);
				putc(mat[0],fp);
				putc(mat[1],fp);
				codesize += 2;
			}
			else {
				putc(imm,fp);codesize++;}
			if(byte1(buffer)) {
				putc(byte1(buffer),fp);
				codesize++;}
		}
		if(VERBOSE)
		printf("\nInstruction Result:%d",codesize);
	return codesize;	
		}

	buffer.reg =reg1;
	int t_opcode = _opcode(mnem);
	if (t_opcode==-1||t_rmcode==-1)
		{if(VERBOSE)
		printf("\nInstruction Result:%d",codesize);return 0;} //insert some error handling code here
	buffer.opcode = t_opcode;
	buffer.rm = t_rmcode;
	if(byte1(buffer)) { 
		putc(byte1(buffer),fp);
		codesize++;
}	
	if(byte2(buffer)) {
		putc(byte2(buffer),fp);
		codesize++;}
	if(buffer.w){
			int mat[2];
			encode(mat,imm);
			putc(mat[0],fp);
			putc(mat[1],fp);codesize+=2;
		}
		else{
			putc(imm,fp);codesize++;}
	_clean();
	if(VERBOSE)
		printf("\nInstruction Result:%d",codesize);
	return codesize;
}
示例#3
0
文件: cg2.c 项目: vivekn/blitz8086
int code_2op(char *mnem,char * sreg1,char * sreg2){
	int reg1 = getval(sreg1);
	int reg2 = getval(sreg2);
	int codesize = 0;	
	if(!((reg1+1)&&(reg2+1)))
		{if(VERBOSE)
		printf("\nInstruction Result:%d",codesize);
		return 0;}
	buffer.s = 0;
	buffer.w = reg1>7?1:0;
	buffer.mod = 3;
	buffer.rm = reg2;
	buffer.reg =reg1;
	int t_opcode = _opcode(mnem);
	if (t_opcode==-1){
	if(VERBOSE)
		printf("\nInstruction Result:%d",codesize);	return 0; }//insert some error handling code here
	buffer.opcode = t_opcode;
	if(byte1(buffer)) {
		putc(byte1(buffer),fp);codesize++;}	
	if(byte2(buffer)) {
		putc(byte2(buffer),fp);codesize++;}
	_clean();
	if(VERBOSE)
		printf("\nInstruction Result:%d",codesize);
	return codesize;
}
示例#4
0
文件: cg2.c 项目: vivekn/blitz8086
int code_2op_reg_mem(char *mnem,char * sreg1,char *ev)	{
	int reg1 = getval(sreg1);
	int codesize = 0;
	if(!(reg1+1)){
		return 0;if(VERBOSE)
		printf("\nInstruction Result:%d",codesize);}	
	buffer.w=1;
	buffer.s = 0;
	buffer.mod = 0;
	buffer.rm = (*(ev+1)=='B'||*(ev+1)=='b')?6:7;	
	buffer.reg =reg1;
	int xt = extr(ev);
		if (!xt){
			int tmp[2];
			encode(tmp,xt);
			buffer.dispa=tmp[0];
			buffer.dispb=tmp[1];
		}
	int t_opcode = _opcode(mnem);
	if (t_opcode==-1)
		return 0; //insert some error handling code here
	buffer.opcode = t_opcode;
	if(byte1(buffer)) {
		putc(byte1(buffer),fp);	codesize++;}
	if(byte2(buffer)) {
		putc(byte2(buffer),fp);codesize++;}
	if(buffer.rm==7)	{
		putc(buffer.dispa,fp);
		putc(buffer.dispb,fp);
		codesize+=2;
	}
	_clean();
}
示例#5
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;
  }
}
示例#6
0
String Uri::Private::undoHex(const String &uri) const
{
    String res;
    size_t i = 0;
    while (i < uri.size()) {
        const Char currChar = uri[i];
        if (currChar == '%') {
            Char newChar;
            String byte1(uri[i + 1]);
            byte1 += uri[i + 2];
            const iuint32 byte1Val = strtoul(byte1.data(), 0, 16);
            if (!(byte1Val & 0x80)) {
                newChar.c = byte1Val;
                i += 3;
            } else if (!(byte1Val & 0x20)) {
                String byte2(uri[i + 4]);
                byte2 += uri[i + 5];
                const iuint32 byte2Val = strtoul(byte2.data(), 0, 16);
                newChar.c |= (byte1Val << 8);
                newChar.c |= byte2Val;
                i += 6;
            } else if (!(byte1Val & 0x10)) {
                String byte2(uri[i + 4]);
                byte2 += uri[i + 5];
                String byte3(uri[i + 7]);
                byte3 += uri[i + 8];
                const iuint32 byte2Val = strtoul(byte2.data(), 0, 16);
                const iuint32 byte3Val = strtoul(byte3.data(), 0, 16);
                newChar.c |= (byte1Val << 16);
                newChar.c |= (byte2Val << 8);
                newChar.c |= byte3Val;
                i += 9;
            } else if (!(byte1Val & 0x8)) {
                String byte2(uri[i + 4]);
                byte2 += uri[i + 5];
                String byte3(uri[i + 7]);
                byte3 += uri[i + 8];
                String byte4(uri[i + 10]);
                byte4 += uri[i + 11];
                const iuint32 byte2Val = strtoul(byte2.data(), 0, 16);
                const iuint32 byte3Val = strtoul(byte3.data(), 0, 16);
                const iuint32 byte4Val = strtoul(byte4.data(), 0, 16);
                newChar.c |= (byte1Val << 24);
                newChar.c |= (byte2Val << 16);
                newChar.c |= (byte3Val << 8);
                newChar.c |= byte4Val;
                i += 12;
            }
            res += newChar;
        } else {
            res += currChar;
            ++i;
        }
    }
    return res;
}
示例#7
0
文件: cg2.c 项目: vivekn/blitz8086
int code_1op_mem(char *mnem,char *ev) {
	int codesize=0;	
	buffer.mod=00;
	buffer.rm = (*(ev+1)=='B'||*(ev+1)=='b')?6:7;	
	
	if(!strcmp(mnem,"POP")||!(strcmp(mnem,"pop"))){
		buffer.reg = 0;
		buffer.opc = 0xaf;
		int xt = extr(ev);
		if (!xt){
			int tmp[2];
			encode(tmp,xt);
			buffer.dispa=tmp[0];
			buffer.dispb=tmp[1];
		}}
	if(!strcmp(mnem,"PUSH")||!(strcmp(mnem,"push"))){
		buffer.reg = 6;
		buffer.opc = 0xff;
			int xt = extr(ev);
		if (!xt){
			int tmp[2];
			encode(tmp,xt);
			buffer.dispa=tmp[0];
			buffer.dispb=tmp[1];
		}
	}
	if(buffer.opc){ 
		putc(byte1(buffer),fp);codesize++;}
	if(byte2(buffer)) {
		putc(byte2(buffer),fp);codesize++;}
	if(buffer.rm==7)	{
		putc(buffer.dispa,fp);
		putc(buffer.dispb,fp);
		codesize++;
	}
	_clean();

	if(VERBOSE)
		printf("\nInstruction Result:%d",codesize);
	return codesize;
}
示例#8
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
}      
示例#9
0
QN  tex_font_metric_rep::mid (QN c) { return (QN) byte1 (exten [rem (c)]); }
示例#10
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;
	}
      }
    }
示例#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(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];
}
示例#12
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)]]);
  }
}
示例#13
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;
  }
}	
示例#14
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;
}
示例#15
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;	
}
示例#16
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
}   
示例#17
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
}