Links und Funktionen
Sprachumschaltung

Navigationspfad
You are here: Home / Teaching / Summer 2017 / Cryptography / Exercises / exercise6_3.c


Inhaltsbereich

exercise6_3.c

C source code icon exercise6_3.c — C source code, 1 KB (1003 bytes)

File contents

#include <stdio.h>
#include <stdint.h>

const uint8_t sbox[16] = {
  0xe, 0x4, 0x0, 0x1, 0x2, 0xf, 0x8, 0xb, 0x3, 0xa, 0x6, 0xc, 0x5, 0x9, 0xd, 0x7
};

/********************************************************************************
 * Computing the difference statistics of the S-Box
 ********************************************************************************/

int sbox_diffstat[16][16];

void init_sbox_diffstat() {
  for (int i = 0; i < 16; i++) {
    for (int j = 0; j < 16; j++) {
      sbox_diffstat[i][j] = 0;
    }
  }

  for (uint8_t x1 = 0; x1 < 16; x1++) {
    for (uint8_t x2 = 0; x2 < 16; x2++) {
      int dx = x1 ^ x2;
      int dy = sbox[x1] ^ sbox[x2];
      sbox_diffstat[dx][dy]++;
    }
  }
}


int main() {

  init_sbox_diffstat();

  printf("Difference statistics of S-Box (inputs in rows, outputs in columns)\n");
  for (int i = 0; i < 16; i++) {
    for (int j = 0; j < 16; j++) {
      printf("%2i ", sbox_diffstat[i][j]);
    }
    printf("\n");
  }
  printf("\n");

}

Document Actions


Funktionsleiste