* \license GPLv3 or later
*
*/
-#include <stdint.h>
-#include <string.h>
+
+#include "des.h"
const uint8_t sbox[256] = {
/* S-box 1 */
for(byte=0; byte<ob; ++byte){
uint8_t x,t=0;
for(bit=0; bit<8; ++bit){
- x=*ptable++ -1 ;
+ x = *ptable++ - 1;
t<<=1;
if((in[x/8]) & (0x80>>(x%8)) ){
t|=0x01;
uint64_t data;
uint8_t *sbp; /* sboxpointer */
permute((uint8_t*)e_permtab, (uint8_t*)&r, (uint8_t*)&data);
- for(i=0; i<7; ++i)
+ for(i=0; i<6; ++i)
((uint8_t*)&data)[i] ^= kr[i];
/* Sbox substitution */
/******************************************************************************/
+typedef struct {
+ union {
+ uint8_t v8[8];
+ uint32_t v32[2];
+ } d;
+} data_t;
+#define R (data.d.v32[1])
+#define L (data.d.v32[0])
+
void des_enc(void* out, const void* in, const void* key){
-#define R *((uint32_t*)&(data[4]))
-#define L *((uint32_t*)&(data[0]))
- uint8_t data[8],kr[6],k[7];
+ uint8_t kr[6], k[7];
uint8_t i;
+ data_t data;
- permute((uint8_t*)ip_permtab, (uint8_t*)in, data);
+ permute((uint8_t*)ip_permtab, (uint8_t*)in, data.d.v8);
permute((uint8_t*)pc1_permtab, (const uint8_t*)key, k);
+
for(i=0; i<8; ++i){
shiftkey(k);
if(ROTTABLE&((1<<((i<<1)+0))) )
L ^= R;
R ^= L;
- permute((uint8_t*)inv_ip_permtab, data, (uint8_t*)out);
+ permute((uint8_t*)inv_ip_permtab, data.d.v8, (uint8_t*)out);
}
/******************************************************************************/
void des_dec(void* out, const void* in, const uint8_t* key){
-#define R *((uint32_t*)&(data[4]))
-#define L *((uint32_t*)&(data[0]))
- uint8_t data[8],kr[6],k[7];
+ uint8_t kr[6],k[7];
int8_t i;
- permute((uint8_t*)ip_permtab, (uint8_t*)in, data);
+ data_t data;
+
+ permute((uint8_t*)ip_permtab, (uint8_t*)in, data.d.v8);
permute((uint8_t*)pc1_permtab, (const uint8_t*)key, k);
for(i=7; i>=0; --i){
L ^= R;
R ^= L;
- permute((uint8_t*)inv_ip_permtab, data, (uint8_t*)out);
+ permute((uint8_t*)inv_ip_permtab, data.d.v8, (uint8_t*)out);
}
/******************************************************************************/
des_dec(out, out, (uint8_t*)key + 0);
}
+ void tdes_2key_enc(void* out, const void* in, size_t length, const void* key, unsigned char iv[8]){
+
+ if( length % 8 ) return;
+
+ uint8_t i;
+ uint8_t* tin = (uint8_t*) in;
+ uint8_t* tout = (uint8_t*) out;
+
+ while( length > 0 )
+ {
+ for( i = 0; i < 8; i++ )
+ tout[i] = (unsigned char)( tin[i] ^ iv[i] );
+
+ des_enc(tout, tin, (uint8_t*)key + 0);
+ des_dec(tout, tout, (uint8_t*)key + 8);
+ des_enc(tout, tout, (uint8_t*)key + 0);
+
+ memcpy( iv, tout, 8 );
+
+ tin += 8;
+ tout += 8;
+ length -= 8;
+ }
+ }
+
+ void tdes_2key_dec(void* out, const void* in, size_t length, const void* key, unsigned char iv[8]){
+
+ if( length % 8 ) return;
+
+ uint8_t i;
+ unsigned char temp[8];
+ uint8_t* tin = (uint8_t*) in;
+ uint8_t* tout = (uint8_t*) out;
+
+ while( length > 0 )
+ {
+ memcpy( temp, tin, 8 );
+
+ des_dec(tout, tin, (uint8_t*)key + 0);
+ des_enc(tout, tout, (uint8_t*)key + 8);
+ des_dec(tout, tout, (uint8_t*)key + 0);
+
+ for( i = 0; i < 8; i++ )
+ tout[i] = (unsigned char)( tout[i] ^ iv[i] );
+
+ memcpy( iv, temp, 8 );
+
+ tin += 8;
+ tout += 8;
+ length -= 8;
+ }
+ }
+
+
/******************************************************************************/