mirror of
https://codeberg.org/scip/twenty4.git
synced 2025-12-17 12:00:57 +01:00
309 lines
7.9 KiB
C
309 lines
7.9 KiB
C
/*
|
|
******* THIS IS JUST FOR LEARINING CRYPTO, DO NOT EVER USE THIS FOR ANYTHING *******
|
|
|
|
This is the implementation of the fun stream cipher TWENTY4 by Thomas von Dein, 09/2015.
|
|
Published under the public domain, Creative Commons Zero License.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <math.h>
|
|
|
|
typedef uint8_t byte;
|
|
typedef uint32_t word;
|
|
typedef uint16_t half;
|
|
|
|
const byte kbox[] = {
|
|
0x53, 0x61, 0x6c, 0x74, 0x65, 0x64, 0x5f, 0xdf, 0x40, 0xc1, 0x9d, 0x46, 0x33, 0x45, 0x92, 0x95,
|
|
0xd8, 0x24, 0xf5, 0x1c, 0xe0, 0x29, 0xff, 0xa3, 0x71, 0x6f, 0x35, 0x2e, 0x4b, 0x0d, 0xa7, 0x5d,
|
|
0x97, 0xe1, 0x98, 0x58, 0x2b, 0xc4, 0xae, 0xe3, 0xec, 0xb8, 0x38, 0xee, 0x91, 0x2c, 0xb4, 0xa0,
|
|
0xc6, 0x34, 0x1f, 0x57, 0x0e, 0xc3, 0x4f, 0xb9, 0x80, 0x21, 0x5b, 0x06, 0xf6, 0x87, 0xfa, 0x5e,
|
|
0xe7, 0xda, 0xce, 0xdd, 0x23, 0xe9, 0x03, 0x39, 0xa5, 0x8e, 0xb6, 0xca, 0x3c, 0x7a, 0x44, 0x2d,
|
|
0x07, 0xcf, 0x1b, 0xd0, 0x94, 0x85, 0xc5, 0x20, 0xaa, 0x81, 0xc9, 0xb7, 0x2f, 0xfb, 0xb2, 0x50,
|
|
0x54, 0xf0, 0x14, 0xd9, 0x00, 0x67, 0x15, 0x9f, 0xa2, 0x02, 0x93, 0xcc, 0xdb, 0x8d, 0x30,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0xd0, 0xe2, 0x0c, 0x66, 0x96, 0x8e,
|
|
0xab, 0xfc, 0xc4, 0x1d, 0x6a, 0x6c, 0x3f, 0x9b, 0x9a, 0x51, 0xa2, 0x86, 0x52, 0x4a, 0x43, 0x14,
|
|
0x75, 0xff, 0xf5, 0xcd, 0x1b, 0x0d, 0x35, 0x24, 0x9c, 0xe1, 0x60, 0x73, 0x3e, 0x39, 0x53, 0x16,
|
|
0x50, 0x6b, 0xc9, 0x46, 0x57, 0x5c, 0x69, 0x79, 0x82, 0xf1, 0x27, 0x38, 0x34, 0xf6, 0x00, 0xa9,
|
|
|
|
};
|
|
|
|
byte revsbox[256];
|
|
|
|
#define K_HASH_ROUNDS 32
|
|
#define S_BOX_ROUNDS 17
|
|
|
|
|
|
byte rot8left(byte in, int rot) {
|
|
return (in >> (8-rot)) | (in << rot);
|
|
}
|
|
|
|
byte rot8right(byte in, int rot) {
|
|
return (in << (8-rot)) | (in >> rot);
|
|
}
|
|
|
|
void printbits(byte v) {
|
|
int i;
|
|
for(i = 7; i >= 0; i--) fprintf(stderr, "%c", '0' + ((v >> i) & 1));
|
|
}
|
|
|
|
void dump8(char *n, byte d) {
|
|
fprintf(stderr, "%s: %02x ", n, d);
|
|
printbits(d);
|
|
fprintf(stderr, "\n");
|
|
}
|
|
|
|
void dumpN(char *n, byte *d, size_t s) {
|
|
int l = strlen(n) + 9;
|
|
fprintf(stderr, "%s (%04ld): ", n, s);
|
|
size_t i;
|
|
int c;
|
|
for (i=0; i<s; ++i) {
|
|
fprintf(stderr, "%02x ", d[i]);
|
|
if(i % 8 == 7 && i > 0) {
|
|
fprintf(stderr, "\n");
|
|
for(c=0; c<l; ++c)
|
|
fprintf(stderr, " ");
|
|
}
|
|
}
|
|
fprintf(stderr, "\n");
|
|
}
|
|
|
|
/* for decryption */
|
|
void reverse_sbox() {
|
|
int i;
|
|
for(i=0; i<256; i++)
|
|
revsbox[sbox[i]] = i;
|
|
}
|
|
|
|
byte getiv() {
|
|
FILE *RAND;
|
|
byte rand;
|
|
|
|
if((RAND = fopen("/dev/urandom", "rb")) == NULL) {
|
|
perror("Could not open /dev/urandom");
|
|
exit(1);
|
|
}
|
|
|
|
if(fread(&rand, 1, 1, RAND) != 1) {
|
|
perror("Could not read from /dev/urandom");
|
|
exit(1);
|
|
}
|
|
|
|
fclose(RAND);
|
|
|
|
return rand;
|
|
}
|
|
|
|
byte rcon(byte in) {
|
|
byte c=1;
|
|
if(in == 0)
|
|
return 0;
|
|
|
|
while(in != 1) {
|
|
byte b;
|
|
b = c & 0x80;
|
|
c <<= 1;
|
|
if(b == 0x80) {
|
|
c ^= 0x1b;
|
|
}
|
|
in--;
|
|
}
|
|
return c;
|
|
}
|
|
|
|
/* we use rounds * 8bit sub keys expanded from
|
|
given password */
|
|
void keyhash(char *pw, byte *hash) {
|
|
byte iv;
|
|
int i, round;
|
|
unsigned int HEX;
|
|
size_t pwlen;
|
|
|
|
if(strncmp(pw, "0x", 2) == 0) {
|
|
/* hex pw */
|
|
sscanf(pw, "0x%02x", &HEX);
|
|
pw[0] = (byte)HEX;
|
|
pwlen = 1;
|
|
}
|
|
else {
|
|
pwlen = strlen(pw);
|
|
}
|
|
|
|
iv = kbox[(byte)pw[0]];
|
|
|
|
/* stretch pw */
|
|
for(i=0; i<K_HASH_ROUNDS; i++) {
|
|
if((size_t)i < pwlen)
|
|
hash[i] = iv ^ pw[i];
|
|
else
|
|
hash[i] = iv ^ kbox[i*8];
|
|
|
|
hash[i] = kbox[hash[i]];
|
|
iv = hash[i];
|
|
}
|
|
|
|
/* diffuse and confuse hash */
|
|
for(round=0; round<K_HASH_ROUNDS; round++) {
|
|
for(i=0; i<K_HASH_ROUNDS; i++) {
|
|
hash[i] = iv ^ (rot8left(hash[i], 3) ^ kbox[rcon(iv)]);
|
|
iv = hash[i];
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void reverse(byte a[], int sz) {
|
|
int i, j;
|
|
for (i = 0, j = sz; i < j; i++, j--) {
|
|
byte tmp = a[i];
|
|
a[i] = a[j];
|
|
a[j] = tmp;
|
|
}
|
|
}
|
|
|
|
void rotate(byte array[], int size, int amt) {
|
|
if (amt < 0)
|
|
amt = size + amt;
|
|
reverse(array, size-amt-1);
|
|
reverse(array+size-amt, amt-1);
|
|
reverse(array, size-1);
|
|
}
|
|
|
|
void rotatekey(byte *key, byte feedback) {
|
|
rotate(key, S_BOX_ROUNDS, 1);
|
|
int i;
|
|
for(i=0; i<S_BOX_ROUNDS; i++) {
|
|
key[i] = kbox[key[i] ^ feedback];
|
|
}
|
|
}
|
|
|
|
/* actual stream cipher:
|
|
- xor with round key
|
|
- apply sbox
|
|
- rotate left by (round mod 8) bits
|
|
- xor with (round key rotated left by 4 bits [halfes reversed])
|
|
*/
|
|
byte bytebox(byte in, byte *key, int encrypt) {
|
|
int i;
|
|
byte out = in;
|
|
|
|
if(encrypt) {
|
|
for(i=0; i<S_BOX_ROUNDS; i++) {
|
|
out ^= key[i];
|
|
out = sbox[out];
|
|
out = rot8left(out, i%8);
|
|
out ^= rot8right(key[i], 4);
|
|
}
|
|
rotatekey(key, out);
|
|
}
|
|
else {
|
|
for(i=S_BOX_ROUNDS-1; i>= 0; i--) {
|
|
out ^= rot8left(key[i], 4);
|
|
out = rot8right(out, i%8);
|
|
out = revsbox[out];
|
|
out ^= key[i];
|
|
}
|
|
rotatekey(key, in);
|
|
}
|
|
|
|
|
|
return out;
|
|
}
|
|
|
|
/* work on stdin and stdout */
|
|
int handleio(byte *key, int encrypt) {
|
|
byte in, out;
|
|
|
|
while (fread(&in, 1, 1, stdin) == 1) {
|
|
out = bytebox(in, key, encrypt);
|
|
fwrite(&out, 1, 1, stdout);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* work on stdin and stdout, in CBC 8bit mode */
|
|
int cbc_handleio(byte *key, int encrypt) {
|
|
byte in, out, iv;
|
|
|
|
if(encrypt) {
|
|
iv = getiv();
|
|
fwrite(&iv, 1, 1, stdout);
|
|
}
|
|
else {
|
|
fread(&iv, 1, 1, stdin);
|
|
}
|
|
|
|
while (fread(&in, 1, 1, stdin) == 1) {
|
|
if(encrypt) {
|
|
out = bytebox(iv ^ in, key, encrypt);
|
|
iv = out;
|
|
}
|
|
else {
|
|
out = iv ^ bytebox(in, key, encrypt);
|
|
iv = in;
|
|
}
|
|
|
|
fwrite(&out, 1, 1, stdout);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
byte key[K_HASH_ROUNDS];
|
|
int encrypt;
|
|
|
|
if(argc != 3) {
|
|
fprintf(stderr, "Usage: stream <passwd> <e|n>\ne=encrypt, n=decrypt\n");
|
|
return 1;
|
|
}
|
|
else {
|
|
encrypt = 0;
|
|
|
|
if(strcmp(argv[2], "e") == 0)
|
|
encrypt = 1;
|
|
|
|
reverse_sbox();
|
|
|
|
keyhash(argv[1], key);
|
|
|
|
return handleio(key, encrypt);
|
|
}
|
|
}
|
|
|
|
|
|
|