0% found this document useful (0 votes)
20 views10 pages

New Text Document

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

New Text Document

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <stdio.

h>

#include <stdint.h>

/*************************************
* PRIVATE VARIABLES *
*************************************/

static const unsigned char SBoxP[16] = {


//0 1 2 3 4 5 6 7 8 9 A B C
D E F
0x03, 0x0F, 0x0E, 0x00, 0x05, 0x04, 0x0B, 0x0C, 0x0D, 0x0A, 0x09, 0x06,
0x07, 0x08, 0x02, 0x01
};
static const unsigned char SBoxQ[16] = {
//0 1 2 3 4 5 6 7 8 9 A B C
D E F
0x09, 0x0E, 0x05, 0x06, 0x0A, 0x02, 0x03, 0x0C, 0x0F, 0x00, 0x04, 0x0D,
0x07, 0x0B, 0x01, 0x08
};

/*************************************
* FUNCTION DECLARATIONS *
*************************************/

//static void KeyScheduleFG1(uint8_t * ,uint8_t *);

static void KeyScheduleFG2(uint8_t * ,uint8_t *);

static void f_fUNCTIONFG1(uint8_t * );

//void RunEncryptionKeySchedule1(uint8_t *, uint8_t *);

//void Encrypt1(uint8_t *, uint8_t *);

void Encrypt2(uint8_t *, uint8_t *);

//void Decrypt1(uint8_t *, uint8_t *);


void Decrypt2(uint8_t *, uint8_t *);

/*************************************
* TEST VECTORS *
*************************************/

// State in the form {10,32,54,76,98,ba,dc,fe} for nibble ordering in array

//static unsigned char Key80[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00};

uint8_t State1[8] = {0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0xe9};

//uint8_t State1[8] = {0xa1, 0xb2, 0xc3, 0xd4, 0x4e, 0xff, 0x78, 0x11};
uint8_t Key808[8] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80};

//uint8_t State1[8] = {0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0xe9};
//uint8_t Key808[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

//uint8_t State1[8] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//uint8_t Key808[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

//uint8_t State1[8] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80};
//uint8_t Key808[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

//uint8_t State1[8] = {0x01, 0x0f, 0x0a, 0x0b, 0x0e, 0x0c, 0x0d, 0x09};
//uint8_t Key808[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};

//uint8_t State1[8] ={0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};

//000A4A6DE8DB6667
//925BEDEAD4E631EB
//0909717b5d3bcfa8
//uint8_t State1[8] ={0x00, 0x0a, 0x4a, 0x6d, 0xe8, 0xdb, 0x66, 0x67};
//uint8_t Key808[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
//uint8_t Key808[10] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x10};

//uint8_t Key808[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00};

//uint8_t Key808[10] = {0x33, 0x33, 0x33, 0x33, 0x99, 0x99, 0x99, 0x99, 0x99,
0x99};

/*************************************
* PRIMARY FUNCTIONS *
*************************************/

//static unsigned char a,aa,bb,a1,b1;


//static unsigned char b,P1,Q1,P2,Q2,p11,p22,p33,QQ1,QQ2,PP1,PP2;
//
//static unsigned char c,PPP1,PPP2,QQQ1,QQQ2;
//static unsigned char d;
/**
* The complete PRINCE forward encryption on the 64-bit state performed through
nibble calculations
*/

/**
* Main method
*/
int main() {

unsigned char KR[8];

printf("\n ========================= \n");


unsigned char i;
printf("\n plain text \n");
for (i = 0; i < 8; i++)
{
printf("%x ", State1[i] );
//
}
printf("\n");

printf("\n key text \n");


for (i = 0; i < 8; i++)
{
printf("%x ", Key808[i] );
//
}
printf("\n");

//static unsigned char S1,S2,S3,S4;

KeyScheduleFG2(Key808,KR);

printf("\n");

printf("\n key cipher \n");


for (i = 0; i < 8; i++)
{
printf("%x ", KR[i] );
//
}

printf("\n========jjjjjjjjjjj=============\n");

//Encrypt1(State1,KR);

Encrypt2(State1,KR);

Decrypt2(State1,KR);

printf("\n ------------------\n ");

return 0;
}

/*************************************
* HELPER FUNCTIONS *
*************************************/

/**
* Send the state through a substitution layer nibble-by-nibble

*/

static void KeyScheduleFG2(uint8_t * Key,uint8_t * KeyRR) {


uint8_t i;

uint8_t XX[2];
uint8_t XR[8];

for (i=0;i<8;i+=2)
{

//READ_SBOX_BYTE(P[Key[i] >> 4]);


//READ_SBOX_BYTE(Q[Key[i] & 0x0F]);
//READ_SBOX_BYTE(P[Key[i+1] >> 4]);
//READ_SBOX_BYTE(Q[Key[i+1] & 0x0F]);

XX[0]=(SBoxP[Key[i] >> 4] );
XX[1]= SBoxQ[Key[i] & 0x0F];
XX[2]= (SBoxP[Key[i+1] >> 4] );
XX[3]= SBoxQ[Key[i+1] & 0x0F];

f_fUNCTIONFG1(XX);

KeyRR[i]=((XX[0] << 4) | XX[1]) ;


KeyRR[i+1]= ((XX[2]<< 4) | XX[3]) ;

XR[0]=~(KeyRR[0]^KeyRR[2]);
XR[1]=~(KeyRR[1]^KeyRR[3]);

XR[2]=(XR[0]^KeyRR[2]);
XR[3]=(XR[1]^KeyRR[3]);

XR[4]=~(KeyRR[4]^XR[2]);
XR[5]=~(KeyRR[5]^XR[3]);

XR[6]=XR[4]^KeyRR[6];
XR[7]=XR[5]^KeyRR[7];

for (i = 0; i < 8; i++)


KeyRR[i]=XR[i];

//printf("\n ===end key 1unit 8= ========================= \n");


//KeyRR[10]= KeyRR[0]^ KeyRR[2]^ KeyRR[4]^ KeyRR[6]^ KeyRR[8];
//KeyRR[11]= KeyRR[1]^ KeyRR[3]^ KeyRR[5]^ KeyRR[7]^ KeyRR[9];

printf("\n key shedule000000000000 \n");


for (i = 0; i < 8; i++)
printf("%x ",KeyRR[i]);
}

static void f_fUNCTIONFG1(uint8_t * XX) {

// uint8_t S1,S2,S3,S4;
uint8_t SS1,SS2,SS3,SS4;
//printf("\n ========================= \n");

//========================
// S1=XX[0];
// S2=XX[1];
// S3=XX[2];
// S4=XX[3];

SS1=(XX[0] & 0x0C) | ((XX[1] & 0x0C) >> 2);


SS2= ((XX[0] & 0x03 )<< 2)|((XX[2] & 0x0C ) >> 2);
SS3=((XX[1] &0x03)<<2) | ((XX[3] & 0x0C)>>2); // shift left <<
SS4= ((XX[2] & 0x03) << 2 )| (XX[3] & 0x03 );

//===================================

XX[0]=SBoxQ[SS1];
XX[1]=SBoxP[SS2];
XX[2]=SBoxQ[SS3];
XX[3]=SBoxP[SS4];
//-----------------------------

SS1=(XX[0] & 0x0C) | ((XX[1] & 0x0C) >> 2);


SS2= ((XX[0] & 0x03 )<< 2)|((XX[2] & 0x0C ) >> 2);
SS3=((XX[1]& 0x03)<<2) | ((XX[3] & 0x0C)>>2); // shift left <<
SS4= ((XX[2] & 0x03) << 2 )| (XX[3] & 0x03 );

//==================

XX[0]=SBoxP[SS1];
XX[1]=SBoxQ[SS2];
XX[2]=SBoxP[SS3];
XX[3]=SBoxQ[SS4];

// XX[0]=S1;
// XX[1]=S2;
// XX[2]=S3;
// XX[3]=S4;

void Decrypt2(uint8_t * block, uint8_t *rK)

{
uint8_t i,ss,j,N=8,round=0,R=5,mi,mc,Ob1;
uint8_t XX[4];
uint8_t ciphertxt[8];
uint8_t Left1[4],Right1[4],Ogject1[4];

for (i = 0; i < N; i++)


ciphertxt[i]=block[i];

mi=N/2;

for (round = 0; round <R; round++)


{//==============

for (i = 0; i <mi; i++)


{
Left1[i]=ciphertxt[i];
Right1[i]=ciphertxt[mi+i];
}

mc=0;
for (i = 0; i <mi; i++)
{
ciphertxt[mc]=Right1[i];
mc++;
ciphertxt[mc]=Left1[i];
mc++;
}

for (j = 0; j <N; j++)


{
ciphertxt[j]=~(ciphertxt[j] ^ rK[j]);
}

//=======round===
}

for (j = 0; j <N; j++)

{
block[j]= ciphertxt[j] ;
}
//=====================

printf("\n Befor pliantext after function 1 \n");


for (i = 0; i < N; i++)
printf("%x ",block[i]);
printf("\n \n");
//=====================

XX[0]=(SBoxP[block[0] >> 4] );
XX[1]= SBoxQ[block[0] & 0x0F];

XX[2]= (SBoxP[block[1] >> 4] );


XX[3]= SBoxQ[block[1] & 0x0F];
f_fUNCTIONFG1(XX);
ciphertxt[0]= ((XX[0] << 4) | XX[1]) ;
ciphertxt[1]= ((XX[2]<< 4) | XX[3]) ;

XX[0]=(SBoxP[block[6] >> 4] );
XX[1]= SBoxQ[block[6] & 0x0F];
XX[2]= (SBoxP[block[7] >> 4] );
XX[3]= SBoxQ[block[7] & 0x0F];
f_fUNCTIONFG1(XX);

ciphertxt[6]= ((XX[0] << 4) | XX[1]) ;


ciphertxt[7]= ((XX[2]<< 4) | XX[3]) ;

//=====================
ciphertxt[2]= ((ciphertxt[2])^ block[6]) ;
ciphertxt[3]= ((ciphertxt[3])^ block[7]) ;

ciphertxt[4]= ((ciphertxt[4])^ block[0]) ;


ciphertxt[5]= ((ciphertxt[5])^ block[1]) ;

//=====================

for (j = 0; j <N; j++)


{
block[j]=ciphertxt[j];

mi=N/2;
mc=mi;
for (i = 0; i <mi; i++)
{
Ob1=block[i];
block[i]=block[mc];
block[mc]=Ob1;
mc++;
}

// for (i = 0; i <N; i+=2)


// {
//
// XX[0]=(SBoxP[block[i] >> 4] );
// XX[1]= SBoxQ[block[i] & 0x0F];
// XX[2]= (SBoxP[block[i+1] >> 4] );
// XX[3]= SBoxQ[block[i+1] & 0x0F];
//
// f_fUNCTIONFG1(XX);
//
// ciphertxt[i]= ((XX[0] << 4) | XX[1]) ;
// ciphertxt[i+1]= ((XX[2]<< 4) | XX[3]) ;
// }

//====================

//============================

printf("\n pliantext after dec \n");


for (i = 0; i < N; i++)
printf("%x ",block[i]);

//=============================

void Encrypt2(uint8_t *block, uint8_t *rK)


{
uint8_t i,ss,i1,j,N=8,round=0,R=5,mi,mc,Ob1;
uint8_t XX[4];
uint8_t ciphertxt[8];

mi=N/2;
mc=mi;

for (i = 0; i <mi; i++)


{
Ob1=block[i];
block[i]=block[mc];
block[mc]=Ob1;
mc++;
}

//=====================
XX[0]=(SBoxP[block[0] >> 4] );
XX[1]= SBoxQ[block[0] & 0x0F];
XX[2]= (SBoxP[block[1] >> 4] );
XX[3]= SBoxQ[block[1] & 0x0F];

f_fUNCTIONFG1(XX);

ciphertxt[0]= ((XX[0] << 4) | XX[1]) ;


ciphertxt[1]= ((XX[2]<< 4) | XX[3]) ;
XX[0]=(SBoxP[block[6] >> 4] );
XX[1]= SBoxQ[block[6] & 0x0F];
XX[2]= (SBoxP[block[7] >> 4] );
XX[3]= SBoxQ[block[7] & 0x0F];

f_fUNCTIONFG1(XX);

ciphertxt[6]= ((XX[0] << 4) | XX[1]) ;


ciphertxt[7]= ((XX[2]<< 4) | XX[3]) ;

ciphertxt[2]= ((ciphertxt[6])^ block[2]) ;


ciphertxt[3]= ((ciphertxt[7])^ block[3]) ;

ciphertxt[4]= ((ciphertxt[0])^ block[4]) ;


ciphertxt[5]= ((ciphertxt[1])^ block[5]) ;

// for (i = 0; i <N; i+=2)


// {
//
// XX[0]=(SBoxP[block[i] >> 4] );
// XX[1]= SBoxQ[block[i] & 0x0F];
// XX[2]= (SBoxP[block[i+1] >> 4] );
// XX[3]= SBoxQ[block[i+1] & 0x0F];
//
// f_fUNCTIONFG1(XX);
//
// ciphertxt[i]= ((XX[0] << 4) | XX[1]) ;
// ciphertxt[i+1]= ((XX[2]<< 4) | XX[3]) ;
// }

for (j = 0; j <N; j++)


{
block[j]=ciphertxt[j];

}
//====================

// printf("\n blok after sawp \n ");


// for (i = 0; i < 8; i++)
// printf("%x ",block[i]);
//
// printf("\n end end sawp \n ");

for (round = 0; round <R; round++)


{

for (j = 0; j <N; j++)


{
block[j]=~(block[j] ^ rK[j]);
}

// printf("\n blok after Exor \n ");


// for (i = 0; i < N; i++)
// printf("%x ",block[i]);
// printf("\n end end Exor \n ");
i=0;
i1=1;
mi=N/2;

for (j = 0; j <mi; j++)


{

ciphertxt[j]=block[i];

ciphertxt[j+mi]=block[i1];

//printf("%x ", ciphertxt[i] );


i=i+2;
i1=i1+2;
}

for (j = 0; j <N; j++)


{
block[j]=ciphertxt[j];

// printf("\n blok after Odd and Even \n ");


// for (i = 0; i < N; i++)
// printf("%x ",block[i]);
// printf("\n end end Odd and Even \n ");

mi=N/2;
mc=mi;
for (i = 0; i <mi; i++)
{
Ob1=block[i];
block[i]=block[mc];
block[mc]=Ob1;
mc++;
}

// printf("\n blok after last sawp \n ");


// for (i = 0; i < N; i++)
// printf("%x ",block[i]);
// printf("\n end end last sawp \n ");

}// end looop

printf("\n Laste cipher \n ");


for (i = 0; i < N; i++)
printf("%x ",block[i]);

printf("\n end cipher \n ");

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy