more modifications

This commit is contained in:
git@daemon.de
2015-10-02 17:52:29 +02:00
parent 761544a28d
commit 47f0ff9e9c
3 changed files with 132 additions and 62 deletions

View File

@@ -27,26 +27,27 @@ typedef struct _ctx ctx;
ctx *context;
/* sbox used for i/o stream diffusion */
const uint8_t sbox[] = {
0x61, 0x2d, 0x19, 0xf3, 0xe5, 0xd9, 0xde, 0x5f, 0x41, 0x31, 0xa7, 0xc2, 0x48, 0x02, 0xef, 0x98,
0x67, 0xcb, 0x6e, 0x4c, 0xf4, 0x11, 0xfa, 0x87, 0x0f, 0x6f, 0x0a, 0x3b, 0x71, 0x09, 0x1a, 0xb8,
0x3c, 0x44, 0xd8, 0xd4, 0xc8, 0x91, 0x6d, 0x8c, 0x2f, 0xce, 0x85, 0x22, 0xd5, 0x08, 0xa6, 0x97,
0x68, 0xbc, 0x3a, 0xa0, 0xbf, 0xa5, 0x47, 0x94, 0x83, 0xd1, 0x18, 0x29, 0x03, 0xb2, 0xa4, 0xfe,
0xe4, 0x4d, 0xdf, 0x21, 0xc0, 0x70, 0x4f, 0x90, 0x04, 0x40, 0x0b, 0x49, 0xe0, 0x25, 0xd7, 0xda,
0xf8, 0x1f, 0x9e, 0x76, 0xbb, 0xaa, 0xc5, 0x2e, 0x72, 0x64, 0xd6, 0x74, 0x10, 0x78, 0xfd, 0x45,
0x80, 0x4e, 0x7f, 0x12, 0xb7, 0xc6, 0xea, 0xb3, 0x37, 0x5a, 0xf2, 0xc3, 0xb6, 0x5b, 0x81, 0x95,
0xbd, 0xb0, 0xae, 0x8f, 0xd2, 0xcf, 0x1e, 0xc7, 0xee, 0xa1, 0x7a, 0xb9, 0x06, 0xa8, 0xb1, 0x93,
0x30, 0xad, 0x33, 0x77, 0x3d, 0x7c, 0xb4, 0x36, 0x92, 0x15, 0x89, 0x7e, 0xe9, 0x17, 0x07, 0x8a,
0x9f, 0x32, 0x2c, 0xf9, 0xb5, 0x7d, 0xeb, 0x23, 0xdc, 0x2b, 0x63, 0x88, 0x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/* convert a 64bit number into an 8 element byte array */
void w2a(u64 in, uint8_t *out) {
out[0] = (in >> 56) & 0xFF;
@@ -59,6 +60,22 @@ void w2a(u64 in, uint8_t *out) {
out[7] = in & 0xFF;
}
/* apply sbox.
X axis = middle 4 bits
Y axis = outer bits (2 left, 2 right) */
byte apply_sbox(byte in) {
byte x, y;
/* middle 4 */
x = (in & 60) >> 2;
/* left 2 added with right 2 */
y = ((in & 192) >> 4) + (in & 3);
/* apply */
return sbox[x][y];
}
/* rotate 64bit number by 'rot' left */
u64 rot64left(u64 in, int rot) {
if(rot == 0) rot = 1;
@@ -70,6 +87,11 @@ u32 rot32left(u32 in, int rot) {
return (in >> (32-rot)) | (in << rot);
}
/* rotate 8bit number by 'rot' left */
byte rot8left(byte in, int rot) {
return (in >> (8-rot)) | (in << rot);
}
/* park-miller 32bit prng */
u32 _32_lcg_pm(u32 seed) {
return ((u64)seed * 48271UL) % 2147483647UL;
@@ -203,16 +225,26 @@ void diffuse_context() {
}
/* actual stream (1byte) encrypt/decrypt */
void io_loop() {
void io_loop(byte nonce) {
byte out, K[8];
int i;
w2a(combined64a(), K);
int i=0;
while(fread(&out, 1, 1, stdin) == 1) {
for(i=0; i<8; i++) out ^= sbox[K[i]]; /* apply our sbox */
fwrite(&out, 1, 1, stdout);
/* new prng round */
w2a(combined64a(), K);
for(i=0; i<8; i++) {
/* round, apply nonce to Ki and xor current input byte with the sbox of the result, repeat */
nonce ^= K[i];
out ^= apply_sbox(nonce);
/* rotate nonce left by 7 bits sometimes */
if(K[i] && K[i] % 53 == 0) {
nonce = rot8left(nonce, 7);
}
}
fwrite(&out, 1, 1, stdout);
}
fflush(stdout);
@@ -220,19 +252,20 @@ void io_loop() {
int main(int argc, char **argv) {
if(argc == 2) {
if(argc == 3) {
context = parseargs(argv[1]);
if(context == NULL) {
return 1;
}
else {
byte nonce = argv[2][0];
diffuse_context();
io_loop();
io_loop(nonce);
return 0;
}
}
else {
fprintf(stderr, "usage: twenty4 <20 byte hex key>\n");
fprintf(stderr, "usage: twenty4 <20 byte key> <1 byte nonce>\n");
return 1;
}
}