Created
April 4, 2014 12:00
-
-
Save maxymania/9973112 to your computer and use it in GitHub Desktop.
Block Sequence Cipher 1
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
/* | |
* I dedicate any and all copyright interest in this software to the | |
* public domain. I make this dedication for the benefit of the public at | |
* large and to the detriment of my heirs and successors. I intend this | |
* dedication to be an overt act of relinquishment in perpetuity of all | |
* present and future rights to this software under copyright law. | |
* | |
* For more information, please refer to <http://unlicense.org/> | |
*/ | |
#include <stdint.h> | |
/* | |
* Block Sequence Cipher 1 | |
* BSC1 encrypts a sequence of 8 bit blocks one by one. | |
* Unlike so called stream ciphers, BSC1 requires the data stream to be | |
* 8-bit aligned. | |
*/ | |
typedef struct{ | |
uint8_t sv[63]; | |
uint8_t key[65]; | |
int sp,kp; | |
} BSC1; | |
/* inits using a key with 256 bit(32 bytes) */ | |
void BSC1_init (BSC1* c,const uint8_t* key); | |
void BSC1_encrypt(BSC1* c,uint8_t* dst,const uint8_t* src,int size); | |
void BSC1_decrypt(BSC1* c,uint8_t* dst,const uint8_t* src,int size); | |
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
/* | |
* I dedicate any and all copyright interest in this software to the | |
* public domain. I make this dedication for the benefit of the public at | |
* large and to the detriment of my heirs and successors. I intend this | |
* dedication to be an overt act of relinquishment in perpetuity of all | |
* present and future rights to this software under copyright law. | |
* | |
* For more information, please refer to <http://unlicense.org/> | |
*/ | |
#include <stdint.h> | |
#include "bsc1.h" | |
static uint8_t ebox[256]={ | |
0x0E, 0x76, 0xAD, 0x1F, 0xE4, 0xE0, 0x5A, 0x45, 0x95, 0xE1, 0xB4, 0xAF, 0x6B, 0xD2, 0xC8, 0xE5, | |
0x56, 0x29, 0x2E, 0xA2, 0x0A, 0x72, 0x50, 0x31, 0xDF, 0x71, 0xFE, 0x5F, 0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6, 0x5B, | |
0x80, 0x68, 0x22, 0x4F, 0x77, 0xEF, 0x23, 0xD5, 0x83, 0xD3, 0x9D, 0x18, 0x49, 0x9F, 0xB9, 0xBB, | |
0xFB, 0xB2, 0x05, 0x81, 0xDE, 0x53, 0x33, 0x51, 0x12, 0xFC, 0x66, 0x94, 0x0D, 0x36, 0xE8, 0x3F, | |
0xC4, 0x4D, 0x78, 0xF1, 0xCC, 0xC3, 0x14, 0x52, 0x8D, 0x43, 0xD9, 0x6F, 0x20, 0x60, 0x1B, 0x40, | |
}; | |
static uint8_t dbox[256]={ | |
0xB6, 0xCA, 0x45, 0x7F, 0x8E, 0xE2, 0x80, 0x67, 0x69, 0x82, 0x14, 0x24, 0x5D, 0xEC, 0x00, 0x22, | |
0x8C, 0x9F, 0xE8, 0x33, 0xF6, 0x64, 0xAA, 0xBA, 0xDB, 0xB4, 0x90, 0xFE, 0x97, 0xCB, 0x4D, 0x03, | |
0xFC, 0x59, 0xD2, 0xD6, 0x1E, 0x63, 0x2D, 0x43, 0x78, 0x11, 0x2F, 0x98, 0x57, 0x9E, 0x12, 0x88, | |
0xA0, 0x17, 0x5F, 0xE6, 0x40, 0xA3, 0xED, 0x56, 0xC4, 0x26, 0xA5, 0x4C, 0xB3, 0xC8, 0x6A, 0xEF, | |
0xFF, 0xB5, 0x6B, 0xF9, 0x3A, 0x07, 0xCC, 0x79, 0x3E, 0xDC, 0x70, 0x36, 0xB9, 0xF1, 0x3B, 0xD3, | |
0x16, 0xE7, 0xF7, 0xE5, 0x62, 0x77, 0x10, 0x8A, 0x25, 0xC3, 0x06, 0xCF, 0xA7, 0xC6, 0xAE, 0x1B, | |
0xFD, 0x92, 0xB8, 0x4B, 0x29, 0x71, 0xEA, 0xC7, 0xD1, 0x85, 0x47, 0x0C, 0x1F, 0x76, 0x4A, 0xFB, | |
0x91, 0x19, 0x15, 0x74, 0xA8, 0x41, 0x01, 0xD4, 0xF2, 0x30, 0x75, 0xB1, 0xBC, 0x68, 0x48, 0xA9, | |
0xD0, 0xE3, 0x81, 0xD8, 0x89, 0xC2, 0x99, 0x94, 0x50, 0xA4, 0x1D, 0x1C, 0xA1, 0xF8, 0x6E, 0xB7, | |
0x5A, 0x95, 0x73, 0x3C, 0xEB, 0x08, 0x51, 0x54, 0x9A, 0xAF, 0x20, 0x2A, 0x52, 0xDA, 0x5E, 0xDD, | |
0xB2, 0x7C, 0x13, 0x61, 0xBE, 0xA6, 0xBD, 0x44, 0x6C, 0xAD, 0x2E, 0x55, 0xBF, 0x02, 0x27, 0x0B, | |
0x2C, 0x35, 0xE1, 0x46, 0x0A, 0x3F, 0x5C, 0x6D, 0x96, 0xDE, 0x7E, 0xDF, 0x53, 0x60, 0x3D, 0xC0, | |
0x87, 0xA2, 0xAC, 0xF5, 0xF0, 0x2B, 0xAB, 0x84, 0x0E, 0x4F, 0x9C, 0xC1, 0xF4, 0xBB, 0x28, 0x8D, | |
0x6F, 0xC5, 0x0D, 0xD9, 0x31, 0xD7, 0xCE, 0x9D, 0x49, 0xFA, 0x93, 0x21, 0x58, 0x65, 0xE4, 0x18, | |
0x05, 0x09, 0x39, 0x7B, 0x04, 0x0F, 0xC9, 0x7A, 0xEE, 0x9B, 0x34, 0xB0, 0x7D, 0x8B, 0x8F, 0xD5, | |
0xCD, 0xF3, 0x42, 0x37, 0x38, 0x66, 0x86, 0x32, 0x72, 0x23, 0x5B, 0xE0, 0xE9, 0x83, 0x1A, 0x4E, | |
}; | |
static const int MOD_ADLER = 65521; | |
/* | |
* Modified Adler 32 Checksum as Mini Hash | |
*/ | |
static void hash32(const uint8_t *key, int len, uint8_t *out) | |
{ | |
int a,b; | |
int i; | |
a=32761; | |
b=32761; | |
for(i=0;i<len;++i) | |
{ | |
a=(a+key[i])%MOD_ADLER; | |
b=(a+b)%MOD_ADLER; | |
} | |
out[0] = (b>>8 )&0xff; | |
out[1] = (b>>0 )&0xff; | |
out[2] = (a>>8 )&0xff; | |
out[3] = (a>>0 )&0xff; | |
} | |
/* | |
* This Hash Function creates an | |
* Hash that is 4 times longer than the | |
* Key. | |
*/ | |
static void KeyDerivationFunction(const uint8_t *key, uint8_t *out, int len) | |
{ | |
int i; | |
uint8_t r1[9]; | |
hash32(key,len,r1); | |
for(i=0;i<len;i++) | |
{ | |
hash32(key+i,1,r1+4); | |
r1[8] = i; | |
hash32(r1,9,out+(4*i)); | |
} | |
} | |
void BSC1_init (BSC1* c,const uint8_t* key) | |
{ | |
int i; | |
uint8_t hash[128]; | |
KeyDerivationFunction(key,hash,32); | |
for(i=0;i<63;++i) | |
c->sv [i]=hash[i]; | |
for(i=0;i<65;++i) | |
c->key[i]=hash[i+63]; | |
c->sp=0; | |
c->kp=0; | |
} | |
void BSC1_encrypt(BSC1* c,uint8_t* dst,const uint8_t* src,int size) | |
{ | |
int i; | |
uint8_t t,n; | |
for(i=0;i<size;++i) | |
{ | |
t = ebox[src[i]]; | |
t = ebox[t ^ (c->sv [c->sp])]; | |
t = ebox[t ^ (c->key[c->kp])]; | |
t = ebox[t ^ (c->sv [c->sp])]; | |
n = t; | |
t = ebox[t ^ (c->key[c->kp])]; | |
t = ebox[t ^ (c->sv [c->sp])]; | |
dst[i] = t; | |
c->sv[c->sp] = n; | |
c->sp=((c->sp)+1)%63; | |
c->kp=((c->kp)+1)%65; | |
} | |
} | |
void BSC1_decrypt(BSC1* c,uint8_t* dst,const uint8_t* src,int size) | |
{ | |
int i; | |
uint8_t t,o; | |
for(i=0;i<size;++i) | |
{ | |
o = c->sv[c->sp]; | |
t = src[i]; | |
t = dbox[t] ^ o; | |
t = dbox[t] ^ (c->key[c->kp]); | |
c->sv[c->sp] = t; | |
t = dbox[t] ^ o; | |
t = dbox[t] ^ (c->key[c->kp]); | |
t = dbox[t] ^ o; | |
dst[i] = dbox[t]; | |
c->sp=((c->sp)+1)%63; | |
c->kp=((c->kp)+1)%65; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment