I am puting up a C code for the Multiplication of block (Alogrithm 1) in the GCM SP-800-38D document here. Page 11-12.
Having completed the code, I want to see if there are any way I can test the code. You can find attached below the code I have put up. Note that instead of the 128 bit block, I used a 24 bit block just for testing purposed. I will appreciate any suggestions where necessary.
void BLK_MUL (u8 *val_1,u8 *val_2, u8 *out_val)
{
u8 xdata R_val = 0xE1;
u8 xdata Z_val[3],V_val[3];
u8 mask_b = 0x80;
u16 i; u8 j;
bit rnd;
for(j=0;j<3;j++,++val_2)
{
Z_val[j]=0x00;
V_val[j]=*val_2;
}
for(i=0;i<24;i++)
{
if (*val_1 & mask_b)
{
for(j=0;j<3;j++)
Z_val[j]^=V_val[j];
}
if (!(V_val[2] & 0x01))
{//if LSB of V_val is 0
for(j=0;j<3;j++)
{ //V_val = rightshift(V_val)
if (j!=0)
if (V_val[2-j] & 0x01)
V_val[3-j] |= 0x80;
V_val[2-j]>>=1;
}
}
else
{//if LSB of V_val is 1
for(j=0;j<3;j++)
{//V_val = rightshift(V_val)
if (j!=0)
if (V_val[2-j] & 0x01)
V_val[3-j] |= 0x80;
V_val[2-j]>>=1;
}
V_val[0]^=R_val; //V_val = rightshift(V_val) ^ R
}
if(mask_b & 0x01) { val_1++; rnd=1;}
mask_b >>= 1;
if (rnd) { mask_b=0x80; rnd=0; }
}
STR_CPY(out_val,Z_val,3);
return ;
}
void main()
{
code unsigned char val_1[3] ={ 0x2b,0x7e,0x15 };
code unsigned char val_2[3] ={ 0x39,0x25,0x84 };
unsigned char out[3];
BLK_MUL (val_1,val_2,out);
return;
}
if (!(V_val[2] & 0x01))
clause and in itselse
clause there are identical for-loops. Is there a good reason for this? – Scruple