Created
September 1, 2019 19:25
-
-
Save HendrikSikke/02076ec12ee5644a7dcbe5b6e44f2d41 to your computer and use it in GitHub Desktop.
Step 1 in answering https://crypto.stackexchange.com/questions/72900/how-are-the-four-8-bit-permutation-tables-of-present-cipher-calculated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdio.h> | |
#define BIT64VERSION 0 | |
/* to play with the PRESENT light-weight cipher */ | |
/*nibble box */ | |
uint8_t sbox[16]={0xC, 5, 6, 0xB, 9,0,0xA, 0xD,3,0xE, 0xF, 8,4,7,1,2}; | |
uint8_t inv_sbox[16]; /* go back, computed below */ | |
uint8_t perm_index[64]; /* bit i goes to place perm_index[i], computed below */ | |
uint8_t invperm_index[64]; /*ditto for inverse */ | |
static void compute_inv_sbox(void){ | |
uint8_t i; | |
for (i=0; i< 16; i++){ | |
inv_sbox[sbox[i]]= i; | |
} | |
return; | |
} | |
static void compute_index_tables(void){ | |
uint8_t i,j; | |
for (i=0; i<64; i++){ | |
j= perm_index[i]= 16 * (i % 4) + (i/4); | |
invperm_index[j]=i; | |
} | |
return; | |
} | |
#if BIT64VERSION | |
static uint64_t S(uint64_t in){ | |
uint64_t out=0; | |
uint8_t i; | |
for (i=0; i<16; i++){ /* 16 nobbles substitued */ | |
out |= (sbox[(in >> (4*i)) & 0xF]) << (4*i); | |
} | |
return out; | |
} | |
static uint64_t invS(uint64_t in){ | |
uint64_t out=0; | |
uint8_t i; | |
for (i=0; i<16; i++){ /* 16 nobbles substitued */ | |
out |= (inv_sbox[(in >> (4*i)) & 0xF]) << (4*i); | |
} | |
return out; | |
} | |
static uint64_t P(uint64_t in){ | |
uint64_t out=0; | |
uint8_t i; | |
for (i=0; i < 64; i++){ | |
out |= ((in >> i) & 1) << (perm_index[i]); | |
} | |
return out; | |
} | |
static uint64_t invP(uint64_t in){ | |
uint64_t out=0; | |
uint8_t i; | |
for (i=0; i < 64; i++){ | |
out |= ((in >> i) & 1) << (invperm_index[i]); | |
} | |
return out; | |
} | |
#endif | |
static void whence_bit(uint8_t i){ | |
uint8_t ii=invperm_index[i]; | |
fprintf(stdout, "bit %d = byte %d, subbit %d from byte %d, subbit %d\n", i, i/8, i%8, ii/8, ii%8); | |
return; | |
} | |
int main(void){ | |
/* init */ | |
compute_inv_sbox(); | |
compute_index_tables(); | |
for (int i=0; i<64; i++){ | |
whence_bit(i); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment