-5

How to implement s-box for DES in c language? i want to use array form creating the lookup table as the substitution table. and then call the function

the code i used is:-

#include<stdio.h>
#include<stdint.h>
int main()
{
uint32_t x,y,a,b,c,p,q;
int mask1=1;
int mask2=32;
printf("enter any no.:",x);
scanf("%d",&x);
y=x&mask1;
printf("y=%d\n",y);
a=x&mask2;
printf("a=%d\n",a);
b=a>>4;
printf("b=%d\n",b);
c=y|b;
printf("row no :%d\n",c);
int mask3=0x1E;
p=x&mask3;
printf("p=%d\n",p);
q=p>>1;
printf("column no: %d\n",q);
static const uint32_t s[4][16] =
{
  14,  0,  4, 15, 13,  7,  1,  4,  2, 14, 15,  2, 11, 13,  8,  1,
   3, 10, 10,  6,  6, 12, 12, 11,  5,  9,  9,  5,  0,  3,  7,  8,
   4, 15,  1, 12, 14,  8,  8,  2, 13,  4,  6,  9,  2,  1, 11,  7,
  15,  5, 12, 11,  9,  3,  7, 14,  3, 10, 10,  0,  5,  6,  0, 13};
int i,j;
{
printf("SBox subsequent value[%d][%d]=%d\n",c,q,s[c][q]);
}
return(0);
}
)

now i want to shorten the whole process by one line and call the function by reference. Please help.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
codecrazy
  • 9
  • 1
  • 2
  • 1
    `How to implement` --> ans: 1. write code. 2. compile. 3. remove compilation errors. 4. run 5. crash. 6. analyze and debug. 7. Final trial. Done. !! – Sourav Ghosh Jan 06 '15 at 19:23
  • @shekharsuman A block of unformated code... Seriously, you're asking for help but can't be bothered to use proper English instead of txtspk, formatting or capitalization (i). And your question is very poor quality to boot. Read [ask] before asking another question. – dandan78 Jan 06 '15 at 19:52
  • @dandan78-well,SORRy to draw your attention,but,the OP of this question is not I myself. I was just trying to help as much as I could by letting his/her question get opened! – Am_I_Helpful Jan 06 '15 at 19:59
  • And while we're at it the example S Box array values don't belong to DES for the row and column addresses c and q which are computed per the DES standard. Each row contains more than one of the same value, something not provided in the standard. Experimenting a bit shows the s[][] array to already be in x order, meaning declaring the array with the name s1[64] will yield the correct answer for s1[x], incidentally nullifying the need for a function in the first place or all the printf calls. The proof is a twenty something line program generating your s table from the original. –  Jan 08 '15 at 03:46

1 Answers1

0

You can convert your program into a function:

#include <stdint.h>
#define BIT(x)  ((1 << x))
int sbox(int x) {
    static const uint32_t s[4][16] = {
      14,  0,  4, 15, 13,  7,  1,  4,  2, 14, 15,  2, 11, 13,  8,  1,
       3, 10, 10,  6,  6, 12, 12, 11,  5,  9,  9,  5,  0,  3,  7,  8,
       4, 15,  1, 12, 14,  8,  8,  2, 13,  4,  6,  9,  2,  1, 11,  7,
      15,  5, 12, 11,  9,  3,  7, 14,  3, 10, 10,  0,  5,  6,  0, 13
    };
    return (s[ (( x & BIT(5))?2:0) | ( x & BIT(0) ) ]   // c
            [(x >> 1) & 0xf]);                          // q
}

But there's a flaw in the program, in that the indexing mechanism doesn't match the table organization.

I can take the original sbox1 code:

#include <stdint.h>
static const uint32_t sbox1[64] = {
    14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
     0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
     4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
    15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13   
};
#define BIT(x)  (1 << x)
#include <stdio.h>

int main() {
    int i;
    printf("    static const uint32_t s[64] = {\n");
    for (i = 0; i < 64; i++) {
        if ((i & 15 ) == 0) 
            printf("       ");
        printf ("%3d%c",
            sbox1[(( i & BIT(5))?32:0) | ( i & BIT(0)?16:0 ) |
              ((i >> 1) & 0xf)], 
              (i < 63)?',':' ');
        if ((i & 15) == 15)
            printf("\n");
    }
    printf("    };\n");
    return (0);
}

And produce your table:

cc sbox1.c -o sbox1
sbox1

static const uint32_t s[64] = {  
    14,  0,  4, 15, 13,  7,  1,  4,  2, 14, 15,  2, 11, 13,  8,  1,  
     3, 10, 10,  6,  6, 12, 12, 11,  5,  9,  9,  5,  0,  3,  7,  8,  
     4, 15,  1, 12, 14,  8,  8,  2, 13,  4,  6,  9,  2,  1, 11,  7,  
    15,  5, 12, 11,  9,  3,  7, 14,  3, 10, 10,  0,  5,  6,  0, 13  
};  

The give away here is that your table rows have duplicate values on them, something that doesn't occur in the DES standard.

So the little program sbox1 also demonstrates that the your sbox has been linearly indexed, meaning all I'd need to do in a program is s[x] to return the correct output, already indexed linearly.

And that says, assuming you make unique tables for the eight boxes, that it's not worth writing a function to call just for a straight table look up.