Skip to content

Instantly share code, notes, and snippets.

@st4rk
Last active Mar 2, 2017
Embed
What would you like to do?
WIP DES Algorithm implementation focused to run on ATmega328p
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "des.h"
#ifdef DES_DEBUG
void printbin(unsigned char data) {
printf("0b");
for (int n = 0x80; n != 0x0; n/=0x2) {
printf("%d", (n & data) == 0 ? 0 : 1);
}
printf("\n");
}
#endif
/**
* ===================================================
* = This funcion does a permutation using a table =
* = Input: n bits key =
* = Input2: n-size permutation table =
* = Input3: tbl size =
* = Output: n bits permuted key =
* ===================================================
*/
void permutation(unsigned char *keyInput, unsigned char *tbl, unsigned char size, unsigned char *keyOutput) {
/**
* TODO: Improve this function
*/
unsigned char auxPerm = 0x0;
unsigned char auxI = 0x0;
for (int i = 0; i < size; i++) {
unsigned char currByte = (0b1111000 & tbl[i]) >> 3;
unsigned char bitToGet = (tbl[i] - (8 * currByte) - 1);
/**
* TODO: Improve this checking, find a way to avoid it
*/
if (bitToGet & 0x80) {
currByte--;
bitToGet = (tbl[i] - (8 * currByte) - 1);
}
#ifdef DES_DEBUG
printf("Operation Num: %d\n", i);
printf("CurrByte: 0x%X\n", currByte);
printf("BitToGet: 0x%X\n", (1 << bitToGet));
printf("Table: %d\n", tbl[i]);
printf("des_key: "); printbin(keyInput[currByte]);
#endif
if (keyInput[currByte] & (1 << bitToGet)) {
#ifdef DES_DEBUG
printf("keyN before:\n");
printbin(keyOutput[auxI]);
#endif
keyOutput[auxI] = keyOutput[auxI] | (1 << auxPerm);
#ifdef DES_DEBUG
printf("keyN after:\n");
printbin(keyOutput[auxI]);
#endif
}
auxPerm++;
if (auxPerm > 7) { auxPerm = 0; auxI++; }
}
}
/**
* ==========================================================================
* = Final Key Generation: Apply a set of left shifts and final permutation =
* = using the table PC 2, the result is the final key =
* = Input: 56 bits key =
* = Output: 48 bits key =
* ==========================================================================
*/
void finalKey(unsigned char *inputKey, unsigned char *outputKey, unsigned char interNum) {
/**
* The key is splint in two parts (C and D)
* each part has 28 bits size
*/
for (int i = 0; i < left_shifts[interNum]; i++) {
/**
* First part is the key C
*/
unsigned char hsb = 0x0;
unsigned char hsb2 = 0x0;
hsb = inputKey[3] & 0x1;
inputKey[3] = (inputKey[3] & 0xF0) | ((inputKey[3] & 0x0F) >> 0x1) | ((inputKey[0] & 0x1) << 0x3);
hsb2 = inputKey[2] & 0x1;
inputKey[2] = (inputKey[2] >> 0x1) | (hsb << 0x7);
hsb = inputKey[1] & 0x1;
inputKey[1] = (inputKey[1] >> 0x1) | (hsb2 << 0x7);
inputKey[0] = (inputKey[0] >> 0x1) | (hsb << 0x7);
/**
* Second part is the key D
*/
hsb = inputKey[3] & 0x10;
hsb2 = inputKey[6] & 0x1;
inputKey[6] = (inputKey[6] >> 0x1) | (hsb << 0x3);
hsb = inputKey[5] & 0x1;
inputKey[5] = (inputKey[5] >> 0x1) | (hsb2 << 0x7);
hsb2 = inputKey[4] & 0x1;
inputKey[4] = (inputKey[4] >> 0x1) | (hsb << 0x7);
inputKey[3] = ((inputKey[3] >> 0x1) & 0xF0) | (inputKey[3] & 0x0F) | (hsb2 << 0x7);
}
permutation(inputKey, pc_2, 48, outputKey);
}
int main(int argc, char **argv) {
/**
* DES has a 64-bits(8 bytes) key-length
*/
unsigned char des_key[8] = {
0b11001000,
0b00101100,
0b11101010,
0b10011110,
0b11011001,
0b00111101,
0b11111011,
0b10001111
};
/**
* Print the initial des_key in binary to check if everything is
* alright
*/
printf("Initial Key (in binary): \n");
for (int i = 0; i < 0x8; i++)
printbin(des_key[i]);
unsigned char keyN[7];
unsigned char fKey[7];
printf("KeyN: 0x%X - DesKey: 0x%X\n", sizeof(keyN), sizeof(des_key));
memset(keyN, 0x0, 7);
printf("First permutation...\n");
permutation(des_key, pc_1, 56, keyN);
printf("Permuted Key:\n");
for (int i = 0; i < 0x7; i++)
printbin(keyN[i]);
printf("-----------\n");
for (int i = 0x0; i < 0x10; i++) {
memset(fKey, 0x0, 0x7);
finalKey(keyN, fKey, i);
printf("Key Num: 0x%X\n", i);
for (int n = 0; n < 0x7; n++)
printbin(keyN[n]);
printf("Final Key: 0x%X\n", i);
for (int n = 0; n < 0x7; n++)
printbin(fKey[n]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment