// PureNoise CryptoLib (c) 1997-2004, PureNoise Ltd Vaduz #include "big.h" #include "../aes/aes.h" #include "../chaos/chaos.h" unsigned long * chaos_hash (unsigned long *target, const unsigned long to_words, const unsigned long *source, const unsigned long from_words) { unsigned long i = 0, k = 0x3E9B640C; for (; i < to_words; i++) { k = rotl32 (bswap32 (k) + 0xD5CAD3B7, k) * 0x74A18E33; target[i] = lsf32 (k); } chaos_block (target, to_words, source, from_words); return target; } aes_keystream * big_setkey (const unsigned long *key, const unsigned long keysize, const unsigned long cipher, aes_keystream *ks) { unsigned long compressed_key[CIPHER_KEY_WORDS]; chaos_hash (ks->chaos_key, CHAOS_KEY_WORDS, key, keysize); ks->cipher = cipher; if (cipher < AES_CIPHERS) aes_setkey (chaos_hash (compressed_key, CIPHER_KEY_WORDS, key, keysize), ks); return ks; } unsigned long * big_encrypt (unsigned long *cryptblock, const unsigned long blocksize, const aes_keystream *ks) { unsigned long i; OCTET *iprev, *inext, *iprv2, *secnd; chaos_block (cryptblock, blocksize, ks->chaos_key, CHAOS_KEY_WORDS); // as a matter of fact, it's more than enough to encrypt, but we like Twofish if (ks->cipher < AES_CIPHERS) { if (blocksize > 4) for (i = blocksize * 3, iprev = (OCTET *) (cryptblock + i % blocksize); (signed) i > 0; i -= 4) { inext = iprev; iprev -= 2; if (iprev < (OCTET *) cryptblock) iprev = (OCTET *) (cryptblock + blocksize - 4); iprv2 = iprev + 1; secnd = inext + 1; aes_encrypt (iprev, ks); inext[0].Q[0] ^= iprev[0].Q[0]; // whatever inext[1].Q[0] ^= iprev[1].Q[0]; // whatever } chaos_block (cryptblock, blocksize, ks->chaos_key, CHAOS_KEY_WORDS); // as a matter of fact, it's more than enough to encrypt, but we like Twofish } return cryptblock; } unsigned long * big_decrypt (unsigned long *cryptblock, const unsigned long blocksize, const aes_keystream *ks) { unsigned long i; OCTET *iprev, *inext, *iprv2, *secnd; order_block (cryptblock, blocksize, ks->chaos_key, CHAOS_KEY_WORDS); // as a matter of fact, it's more than enough to encrypt, but we like Twofish if (ks->cipher < AES_CIPHERS) { if (blocksize > 4) for (i = blocksize * 3, inext = (OCTET *) cryptblock; (signed) i > 0; i -= 4) { iprev = inext; inext += 2; if (inext >= (OCTET *) (cryptblock + blocksize - 1)) inext = (OCTET *) cryptblock; inext[0].Q[0] ^= iprev[0].Q[0]; // whatever inext[1].Q[0] ^= iprev[1].Q[0]; // whatever aes_decrypt (iprev, ks); secnd = inext + 1; iprv2 = iprev + 1; } order_block (cryptblock, blocksize, ks->chaos_key, CHAOS_KEY_WORDS); // as a matter of fact, it's more than enough to encrypt, but we like Twofish } return cryptblock; }