Ejemplo n.º 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
}   
Ejemplo n.º 2
0
/* calc observation matrix Q for given observer*/
void
findQ(void)
{
	vec1	alpha, beta, gamma, v, w;
	double	E[5][5];
	double	F[5][5];
	double	G[5][5];
	double	H[5][5];
	double	U[5][5];

	/* calc translation matrix F*/
	tran3(eye.x, eye.y, eye.z, F);

	/* calc rotation matrix G*/
	alpha = angle(-direct.x, -direct.y);
	rot3(3, alpha, G);

	/* calc rotation matrix H*/
	v = sqrt(direct.x*direct.x + direct.y*direct.y);
	beta = angle(-direct.z, v);
	rot3(2, beta, H);

	/* calc rotation matrix U*/
	w = sqrt(v*v + direct.z*direct.z);
	gamma = angle(-direct.x*w, direct.y*direct.z);
	rot3(3, -gamma, U);

	/* combine the transformations to find Q*/
	mult3(G, F, Q);
	mult3(H, Q, E);
	mult3(U, E, Q);
}
Ejemplo n.º 3
0
void WireCreator::collision(QVector3D point, QPair<QVector3D, QVector3D> box)
{
    auto collidedPoint= point;
    //qDebug()<<point<<"  "<<collidedPoint;
    collidedPoint.setX(0);


    auto axis = QVector3D::crossProduct(collidedPoint, QVector3D(0.0f, 1.0f, 0.0f));
    float angle= calcMinimumAngle(collidedPoint, box);
    angle=qRadiansToDegrees(angle);
    //qDebug()<<"angle: "<<angle;
    //qDebug()<<"axis: "<<axis;
    QQuaternion quat= QQuaternion::fromAxisAndAngle(axis, angle);
    QMatrix3x3 rot3= quat.toRotationMatrix();
    QMatrix4x4 result;
    for(int i=0;i<3;i++){
        for(int j=0; j<3; j++){
            if(i<3 && j<3)result(i,j)=rot3(i,j);
        }
    }
    //currPath=applyMatrix(currPath, result);
      //qDebug()<<"collision handled";
    emit collisionHandled();


}
Ejemplo n.º 4
0
Vector3f Arm::F(VectorXf theta) {
	Vector3f rot1(theta(0), theta(1), theta(2));
	Vector3f rot2(theta(3), theta(4), theta(5));
	Vector3f rot3(theta(6), theta(7), theta(8));
	Vector3f rot4(theta(9), theta(10), theta(11));
    Matrix4f R1; R1 = rodrigues(rot1);
    Matrix4f R2; R2 = rodrigues(rot2);
    Matrix4f R3; R3 = rodrigues(rot3);
    Matrix4f R4; R4 = rodrigues(rot4);

    Matrix4f T1; T1 = list_joints[0]->transformation();
    Matrix4f T2; T2 = list_joints[1]->transformation();
    Matrix4f T3; T3 = list_joints[2]->transformation();
    Matrix4f T4; T4 = list_joints[3]->transformation();

    Vector4f identity(0.0, 0.0, 0.0, 1.0);
    Vector4f result;

    result = R1 * T1 * R2 * T2 * R3 * T3 * R4 * T4 * identity;

    Vector3f ret(result(0), result(1), result(2));
    // cout << "Theta:" << endl << theta;
    // cout << "ret: " << endl << ret << endl;=
    return ret;
}
Ejemplo n.º 5
0
void Client::Start() {
	working = true;
	state.ingame = true;

	g_Map()->Load("1234");
	auto skin = g_Resources()->skinTextures.end();
	for (int i = 0; i < MAX_PLAYERS; i++) {
		if (skin == g_Resources()->skinTextures.end())
			skin = g_Resources()->skinTextures.begin();
		g_World()->players[i] = new Player(i);
		Player *player = g_World()->players[i];
		player->nickname = g_World()->players[i]->skin = (*skin).first;
		player->pos = glm::vec3(rand() % 4080, rand() % 4080, 400.0);
		player->weapon = rand() % NUM_WEAPONS;
		player->emote = rand() % NUM_EMOTES;
		player->rot = rot3(rand() * M_PI / RAND_MAX * 2, rand() * M_PI / RAND_MAX * 2,
		                   rand() * M_PI / RAND_MAX * 2);
		skin++;
	}
	localPlayer = g_World()->players[0];
	// localPlayer->color = glm::vec4(0, 0, 0, 0.3f);

	depthMap =  new Image(g_ShaderShadow()->shadowMap);
	depthMap->size = glm::vec2(0.5f, 0.5f);
	g_UI()->screenLayout->Add(depthMap);
	fps = new Label("FPS: 60", FONT_BIG);
	fps->align = glm::uvec2(ALIGN_RIGHT, ALIGN_TOP);
	g_UI()->screenLayout->Add(fps);
}
Ejemplo n.º 6
0
QMatrix4x4 WireCreator::calcRotationMatrix(QVector3D axis, float angle)
{

    QQuaternion quat=QQuaternion::fromAxisAndAngle(axis, angle);
    QMatrix3x3 rot3= quat.toRotationMatrix();
    QMatrix4x4 result;
    for(int i=0;i<3;i++){
        for(int j=0; j<3; j++){
            if(i<3 && j<3)result(i,j)=rot3(i,j);
        }
    }
    return result;


}
Ejemplo n.º 7
0
int main()
{
  // Testing with ints

  // testing min_max
  int int_arr[] = {1, 7, 2, -4, 23, 42, 5 };
  int int_min = 0;
  int int_max = 0;

  min_max( int_arr, 7, int_min, int_max );
  
  if( ( int_min == -4 ) && ( int_max == 42 ) ) {
    printf( "int min_max succeeded!\n" );
  }
  else {
    printf( "int min_max failed (%d,%d)\n", int_min, int_max );
  }

  // testing swap2
  int int_a = 7;
  int int_b = 8;

  swap2( int_a, int_b );

  if( ( int_a == 8 ) && ( int_b == 7 ) ) {
    printf( "int swap2 succeeded!\n" );
  }
  else {
    printf( "int swap2 failed (%d,%d)\n", int_a, int_b );
  }

  // testing rot3			       
  int_a = 7;
  int_b = 8;
  int int_c = 9;
  
  rot3( int_a, int_b, int_c );

  if( ( int_a == 9 ) && ( int_b == 7 ) && ( int_c == 8 ) ) {
    printf( "int rot3 succeeded!\n" );
  }
  else {
    printf( "int rot3 failed (%d, %d, %d)\n", int_a, int_b, int_c );
  }
  
  return 0;
}
Ejemplo n.º 8
0
/*-----------------------------------------------------------
 * RijndaelKeySchedule
 *   Initialise the key schedule from a supplied key
 */
void RijndaelKeySchedule(u8 key[16])
{
    u32  t;
    u32  *ek=Ekey,      /* pointer to the expanded key   */
          *rc=rnd_con;  /* pointer to the round constant */

    Ekey[0] = u32_in(key     );
    Ekey[1] = u32_in(key +  4);
    Ekey[2] = u32_in(key +  8);
    Ekey[3] = u32_in(key + 12);

    while(ek < Ekey + 40) {
        t = rot3(ek[3]);
        ek[4] = ek[0] ^ ls_box(t) ^ *rc++;
        ek[5] = ek[1] ^ ek[4];
        ek[6] = ek[2] ^ ek[5];
        ek[7] = ek[3] ^ ek[6];
        ek += 4;
    }
}
Ejemplo n.º 9
0
UInt solve_cubic(double a, double b, double c, double* roots) {
	double p = b - a*a / 3;
	double q = c + (2 * a*a*a - 9 * a * b) / 27;
	if (p == 0 && q == 0) {
		roots[0] = -a / 3;
		return 1;
	}
	complex<double> u;
	if (q > 0) {
		u = curt(q/2 + sqrt(complex<double>(q*q / 4  +  p*p*p / 27)));
	} else {
		u = curt(q/2 - sqrt(complex<double>(q*q / 4  +  p*p*p / 27)));
	}
	// now for the complex part
	//              rot1(1,    0)
	complex<double> rot2(-0.5, sqrt(3.0) / 2);
	complex<double> rot3(-0.5, -sqrt(3.0) / 2);
	complex<double> x1 = p / (3.0 * u)         -  u        -  a / 3.0;
	complex<double> x2 = p / (3.0 * u * rot2)  -  u * rot2 -  a / 3.0;
	complex<double> x3 = p / (3.0 * u * rot3)  -  u * rot3 -  a / 3.0;
	// check if the solutions are real
	UInt count = 0;
	if (abs(x1.imag()) < 0.00001) {
		roots[count] = x1.real();
		count += 1;
	}
	if (abs(x2.imag()) < 0.00001) {
		roots[count] = x2.real();
		count += 1;
	}
	if (abs(x3.imag()) < 0.00001) {
		roots[count] = x3.real();
		count += 1;
	}
	return count;
}
Ejemplo n.º 10
0
__inline u32 byte_swap(const u32 x)
{
    return rot1(x) & 0x00ff00ff | rot3(x) & 0xff00ff00;
}
Ejemplo n.º 11
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(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];
}
Ejemplo n.º 12
0
/*******************************************************************************
* 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)]]);
  }
}
Ejemplo n.º 13
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
}   
Ejemplo n.º 14
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
}   
Ejemplo n.º 15
0
__INLINE uint32_t byte_swap(uint32_t x){
    return rot1(x) & 0x00ff00ff | rot3(x) & 0xff00ff00;
}