|
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <comutil.h>
#include <time.h>
#pragma once
#ifndef _SHA2_H
#define _SHA2_H
#include <limits.h>
#define sfx_lo(x,y) x##y
#define sfx_hi(x,y) sfx_lo(x,y)
#define n_u32(p) sfx_hi(0x##p,s_u32)
#define n_u64(p) sfx_hi(0x##p,s_u64)
#if UINT_MAX == 0xffffffff
typedef unsigned int sha2_32t;
#define s_u32 u
#elif ULONG_MAX == 0xffffffff
typedef unsigned long sha2_32t;
#define s_u32 ul
#else
#error Please define sha2_32t as an unsigned 32 bit type in sha2.h
#endif
#if defined( _MSC_VER )
typedef unsigned __int64 sha2_64t;
#define s_u64 ui64
#elif ULONG_MAX == 0xffffffffffffffff
typedef unsigned long sha2_64t;
#define s_u64 ul
#elif ULONG_MAX == 0xffffffff
typedef unsigned long long sha2_64t;
#define s_u64 ull
#else
#error Please define sha2_64t as an unsigned 64 bit type in sha2.h
#endif
#if defined(__cplusplus)
extern "C"
{
#endif
#define BYTES_PER_BLOCK 32
#define SHA256_DIGEST_SIZE 32
#define SHA384_DIGEST_SIZE 48
#define SHA512_DIGEST_SIZE 64
#define SHA256_BLOCK_SIZE 64
#define SHA384_BLOCK_SIZE 128
#define SHA512_BLOCK_SIZE 128
#define SHA2_DIGEST_SIZE SHA256_DIGEST_SIZE
#define SHA2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
#define SHA2_GOOD 0
#define SHA2_BAD 1
typedef struct
{ sha2_32t count[2];
sha2_32t hash[8];
sha2_32t wbuf[16];
} sha256_ctx;
typedef struct
{ sha2_64t count[2];
sha2_64t hash[8];
sha2_64t wbuf[16];
} sha512_ctx;
typedef sha512_ctx sha384_ctx;
typedef struct
{ union
{ sha256_ctx ctx256[1];
sha512_ctx ctx512[1];
} uu[1];
sha2_32t sha2_len;
} sha2_ctx;
void sha256_compile(sha256_ctx ctx[1]);
void sha512_compile(sha512_ctx ctx[1]);
void sha256_begin(sha256_ctx ctx[1]);
void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);
void sha256_end(unsigned char hval[], sha256_ctx ctx[1]);
void sha256(unsigned char hval[], const unsigned char data[], unsigned long len);
void sha384_begin(sha384_ctx ctx[1]);
#define sha384_hash sha512_hash
void sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
void sha384(unsigned char hval[], const unsigned char data[], unsigned long len);
void sha512_begin(sha512_ctx ctx[1]);
void sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);
void sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
void sha512(unsigned char hval[], const unsigned char data[], unsigned long len);
int sha2_begin(unsigned long size, sha2_ctx ctx[1]);
void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);
void sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
int sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);
#if defined(__cplusplus)
}
#endif
#endif
#define MAX_WORDS 524288
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
unsigned long genrand_int32(void);
static unsigned long mt[N];
static int mti=N+1;
static unsigned long accum;
#define SHA_2 /* for dynamic hash length */
#define SHA_256
#include <string.h>
#include <stdlib.h>
#if defined(__GNU_LIBRARY__)
# include <byteswap.h>
# include <endian.h>
#elif defined(__CRYPTLIB__)
# if defined( INC_ALL )
# include "crypt.h"
# elif defined( INC_CHILD )
# include "../crypt.h"
# else
# include "crypt.h"
# endif
# if defined(DATA_LITTLEENDIAN)
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
# else
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
# endif
#elif defined(_MSC_VER)
# include <stdlib.h>
#elif !defined(WIN32)
# include <stdlib.h>
# if !defined (_ENDIAN_H)
# include
# else
# include _ENDIAN_H
# endif
#endif
#define SHA_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
#define SHA_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
#if !defined(PLATFORM_BYTE_ORDER)
#if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)
# if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
# if defined(BYTE_ORDER)
# if (BYTE_ORDER == LITTLE_ENDIAN)
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
# elif (BYTE_ORDER == BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
# endif
# endif
# elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
# elif !defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
# endif
#elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN)
# if defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
# if defined(_BYTE_ORDER)
# if (_BYTE_ORDER == _LITTLE_ENDIAN)
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
# elif (_BYTE_ORDER == _BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
# endif
# endif
# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
# elif !defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
# endif
#elif 0 /* **** EDIT HERE IF NECESSARY **** */
#define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
#elif 0 /* **** EDIT HERE IF NECESSARY **** */
#define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
#elif (('1234' >> 24) == '1')
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
#elif (('4321' >> 24) == '1')
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
#endif
#endif
#if !defined(PLATFORM_BYTE_ORDER)
# error Please set undetermined byte order (lines 159 or 161 of sha2.c).
#endif
#ifdef _MSC_VER
#pragma intrinsic(memcpy)
#endif
#define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
#if !defined(bswap_32)
#define bswap_32(x) (rotr32((x), 24) & 0x00ff00ff | rotr32((x), 8) & 0xff00ff00)
#endif
#if (PLATFORM_BYTE_ORDER == SHA_LITTLE_ENDIAN)
#define SWAP_BYTES
#else
#undef SWAP_BYTES
#endif
#if defined(SHA_2) || defined(SHA_256)
#define SHA256_MASK (SHA256_BLOCK_SIZE - 1)
#if defined(SWAP_BYTES)
#define bsw_32(p,n) { int _i = (n); while(_i--) p[_i] = bswap_32(p[_i]); }
#else
#define bsw_32(p,n)
#endif
#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#define s256_0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))
#define s256_1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))
#define g256_0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))
#define g256_1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))
#define h2(i) ctx->wbuf[i & 15] += \
g256_1(ctx->wbuf[(i + 14) & 15]) + ctx->wbuf[(i + 9) & 15] + g256_0(ctx->wbuf[(i + 1) & 15])
#define h2_cycle(i,j) \
v[(7 - i) & 7] += (j ? h2(i) : ctx->wbuf[i & 15]) + k256[i + j] \
+ s256_1(v[(4 - i) & 7]) + ch(v[(4 - i) & 7], v[(5 - i) & 7], v[(6 - i) & 7]); \
v[(3 - i) & 7] += v[(7 - i) & 7]; \
v[(7 - i) & 7] += s256_0(v[(0 - i) & 7]) + maj(v[(0 - i) & 7], v[(1 - i) & 7], v[(2 - i) & 7])
const sha2_32t k256[64] =
{ n_u32(428a2f98), n_u32(71374491), n_u32(b5c0fbcf), n_u32(e9b5dba5),
n_u32(3956c25b), n_u32(59f111f1), n_u32(923f82a4), n_u32(ab1c5ed5),
n_u32(d807aa98), n_u32(12835b01), n_u32(243185be), n_u32(550c7dc3),
n_u32(72be5d74), n_u32(80deb1fe), n_u32(9bdc06a7), n_u32(c19bf174),
n_u32(e49b69c1), n_u32(efbe4786), n_u32(0fc19dc6), n_u32(240ca1cc),
n_u32(2de92c6f), n_u32(4a7484aa), n_u32(5cb0a9dc), n_u32(76f988da),
n_u32(983e5152), n_u32(a831c66d), n_u32(b00327c8), n_u32(bf597fc7),
n_u32(c6e00bf3), n_u32(d5a79147), n_u32(06ca6351), n_u32(14292967),
n_u32(27b70a85), n_u32(2e1b2138), n_u32(4d2c6dfc), n_u32(53380d13),
n_u32(650a7354), n_u32(766a0abb), n_u32(81c2c92e), n_u32(92722c85),
n_u32(a2bfe8a1), n_u32(a81a664b), n_u32(c24b8b70), n_u32(c76c51a3),
n_u32(d192e819), n_u32(d6990624), n_u32(f40e3585), n_u32(106aa070),
n_u32(19a4c116), n_u32(1e376c08), n_u32(2748774c), n_u32(34b0bcb5),
n_u32(391c0cb3), n_u32(4ed8aa4a), n_u32(5b9cca4f), n_u32(682e6ff3),
n_u32(748f82ee), n_u32(78a5636f), n_u32(84c87814), n_u32(8cc70208),
n_u32(90befffa), n_u32(a4506ceb), n_u32(bef9a3f7), n_u32(c67178f2),
};
const sha2_32t i256[8] =
{
n_u32(6a09e667), n_u32(bb67ae85), n_u32(3c6ef372), n_u32(a54ff53a),
n_u32(510e527f), n_u32(9b05688c), n_u32(1f83d9ab), n_u32(5be0cd19)
};
void sha256_begin(sha256_ctx ctx[1])
{
ctx->count[0] = ctx->count[1] = 0;
memcpy(ctx->hash, i256, 8 * sizeof(sha2_32t));
}
void sha256_compile(sha256_ctx ctx[1])
{ sha2_32t v[8], j;
memcpy(v, ctx->hash, 8 * sizeof(sha2_32t));
for(j = 0; j < 64; j += 16)
{
h2_cycle( 0, j); h2_cycle( 1, j); h2_cycle( 2, j); h2_cycle( 3, j);
h2_cycle( 4, j); h2_cycle( 5, j); h2_cycle( 6, j); h2_cycle( 7, j);
h2_cycle( 8, j); h2_cycle( 9, j); h2_cycle(10, j); h2_cycle(11, j);
h2_cycle(12, j); h2_cycle(13, j); h2_cycle(14, j); h2_cycle(15, j);
}
ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
ctx->hash[4] += v[4]; ctx->hash[5] += v[5]; ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
}
void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1])
{ sha2_32t pos = (sha2_32t)(ctx->count[0] & SHA256_MASK),
space = SHA256_BLOCK_SIZE - pos;
const unsigned char *sp = data;
if((ctx->count[0] += len) < len)
++(ctx->count[1]);
while(len >= space)
{
memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
sp += space; len -= space; space = SHA256_BLOCK_SIZE; pos = 0;
bsw_32(ctx->wbuf, SHA256_BLOCK_SIZE >> 2)
sha256_compile(ctx);
}
memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
}
static sha2_32t m1[4] =
{
n_u32(00000000), n_u32(ff000000), n_u32(ffff0000), n_u32(ffffff00)
};
static sha2_32t b1[4] =
{
n_u32(80000000), n_u32(00800000), n_u32(00008000), n_u32(00000080)
};
void sha256_end(unsigned char hval[], sha256_ctx ctx[1])
{ sha2_32t i = (sha2_32t)(ctx->count[0] & SHA256_MASK);
bsw_32(ctx->wbuf, (i + 3) >> 2)
ctx->wbuf[i >> 2] = (ctx->wbuf[i >> 2] & m1[i & 3]) | b1[i & 3];
if(i > SHA256_BLOCK_SIZE - 9)
{
if(i < 60) ctx->wbuf[15] = 0;
sha256_compile(ctx);
i = 0;
}
else
i = (i >> 2) + 1;
while(i < 14)
ctx->wbuf[i++] = 0;
ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
ctx->wbuf[15] = ctx->count[0] << 3;
sha256_compile(ctx);
for(i = 0; i < SHA256_DIGEST_SIZE; ++i)
hval[i] = (unsigned char)(ctx->hash[i >> 2] >> 8 * (~i & 3));
}
void sha256(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha256_ctx cx[1];
sha256_begin(cx); sha256_hash(data, len, cx); sha256_end(hval, cx);
}
#endif
#if defined(SHA_2)
#define CTX_256(x) ((x)->uu->ctx256)
int sha2_begin(unsigned long len, sha2_ctx ctx[1])
{ unsigned long l = len;
switch(len)
{
case 256: l = len >> 3;
case 32: CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;
memcpy(CTX_256(ctx)->hash, i256, 32); break;
default: return SHA2_BAD;
}
ctx->sha2_len = l; return SHA2_GOOD;
}
void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1])
{
switch(ctx->sha2_len)
{
case 32: sha256_hash(data, len, CTX_256(ctx)); return;
}
}
void sha2_end(unsigned char hval[], sha2_ctx ctx[1])
{
switch(ctx->sha2_len)
{
case 32: sha256_end(hval, CTX_256(ctx)); return;
}
}
int sha2(unsigned char hval[], unsigned long size,
const unsigned char data[], unsigned long len)
{ sha2_ctx cx[1];
if(sha2_begin(256, cx) == SHA2_GOOD)
{
sha2_hash(data, len, cx); sha2_end(hval, cx); return SHA2_GOOD;
}
else
return SHA2_BAD;
}
#endif
void init_genrand(unsigned long s)
{
mt[0]= s & 0xffffffffUL;
accum = 0;
for (mti=1; mti<n;> mt[mti] =
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
mt[mti] &= 0xffffffffUL;
}
}
void init_by_array(unsigned long init_key[], int key_length)
{
int i, j, k, dummy;
memset(mt, 0, 4*N);
init_genrand(19650218UL);
i=1; j=0;
k = (N>key_length ? N : key_length);
for (; k; k--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
+ init_key[j] + j;
mt[i] &= 0xffffffffUL;
i++; j++;
if (i>=N) { mt[0] = mt[N-1]; i=1; }
if (j>=key_length) j=0;
}
for (k=N-1; k; k--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
- i;
mt[i] &= 0xffffffffUL;
i++;
if (i>=N) { mt[0] = mt[N-1]; i=1; }
}
mt[0] = 0x80000000UL;
for (i=0; i<64; i++) dummy = genrand_int32();
}
unsigned long genrand_int32(void)
{
int repetition = 0;
unsigned long y;
static unsigned long mag01[2]={0x0UL, MATRIX_A};
for (; repetition<2; repetition++) {
if (mti >= N) {
int kk;
if (mti == N+1)
init_genrand(5489UL);
for (kk=0;kk<n-m;kk++)> y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
for (;kk<n-1;kk++)> y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
mti = 0;
}
y = mt[mti++];
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
accum += y;
}
return accum;
}
#define ENCRYPT 0
#define DECRYPT 1
int main(int argc, char * argv[])
{
int IV_processed = 0;
FILE * fd_in, * fd_out;
unsigned char buffer_area[MAX_WORDS], keystream;
size_t bytes_read = 0;
unsigned int i, j, k, keysize = 0;
int eof, filename_size, direction, muddle = 0;
int start_fill, blocks_processed = 0;
char output_filename[1000];
const char * my_out = output_filename;
unsigned long multipart_key[N];
sha256_ctx m_sha256, m2_sha256, file_sha256, comp_sha256;
unsigned char pBuf[4096], pTemp[256];
unsigned long uRead = 0;
FILETIME idleTimePtr, kernelTimePtr, userTimePtr;
if (argc == 1) {
printf("Invoke encryption using\n\n");
printf(" \"%s file passphrase\"\n\n", argv[0]);
printf("Encrypted/decrypted output will be the filename with an\n");
printf("underscore appended/removed. The encrypted file will be\n");
printf("32 bytes larger than the plain text file\n");
exit(0);
} else {
if (argc < 3) {
printf("too few arguments\n");
exit(0);
}
}
memset(buffer_area, 0, sizeof(buffer_area));
memset(output_filename, 0, sizeof(output_filename));
memset(multipart_key, 0, 4*N);
GetSystemTimes(&idleTimePtr, &kernelTimePtr, &userTimePtr);
multipart_key[0] = idleTimePtr.dwLowDateTime;
multipart_key[1] = idleTimePtr.dwHighDateTime;
multipart_key[2] = kernelTimePtr.dwLowDateTime;
multipart_key[3] = kernelTimePtr.dwHighDateTime;
multipart_key[4] = userTimePtr.dwLowDateTime;
multipart_key[5] = userTimePtr.dwHighDateTime;
multipart_key[6] = time(NULL);
multipart_key[7] = clock();
char * string = argv[2];
for (i = 0; i < strlen(argv[2]); i++) {
multipart_key[8+(i>>2)] |= string[i] << (8*(i&3));
}
keysize = (i >> 2) + 1;
if (i&3) keysize++;
keysize += 8;
init_by_array(&multipart_key[0], keysize);
memset(&multipart_key[0], 0, 32);
for (i = 0; i < BYTES_PER_BLOCK; i++) {
buffer_area[i] = genrand_int32() >> 24;
}
for (k = 0; k < 32; k++) {
multipart_key[(k>>2)] |= buffer_area[k] << (8*(k&3));
}
if (argc >= 3) {
strcpy((char *)&pBuf[0], argv[2]);
sha256_begin(&m_sha256);
sha256_hash(pBuf, (unsigned long)strlen(argv[2]), &m_sha256);
sha256_end(pTemp, &m_sha256);
}
if (_access((const char *)argv[1], 0) != 0) {
printf("file does not exist\n");
exit(0);
}
filename_size = strlen(argv[1]);
strcpy(output_filename, argv[1]);
if (output_filename[filename_size-1] == 0x5f) {
direction = DECRYPT;
output_filename[filename_size-1] = 0;
start_fill = 0;
} else {
direction = ENCRYPT;
output_filename[filename_size] = 0x5f;
start_fill = BYTES_PER_BLOCK;
init_by_array(&multipart_key[0], keysize);
}
fd_in = fopen((const char *)argv[1], (const char *)("rb"));
fd_out = fopen(my_out, (const char *)("wb"));
sha256_begin(&file_sha256);
do {
bytes_read = fread( &buffer_area[start_fill], 1, (MAX_WORDS-start_fill), fd_in);
eof = feof(fd_in);
bytes_read += start_fill;
blocks_processed++;
i = 0;
while (i < bytes_read) {
if (ENCRYPT == direction) {
if ((i % 32) == 0) {
for (j = i; j <= i+31; j++) {
pBuf[j%32] = buffer_area[i+(j%32)];
}
if ((i+31) > bytes_read) {
sha256_hash(pBuf, bytes_read-i, &file_sha256);
} else {
sha256_hash(pBuf, 32, &file_sha256);
}
}
if (((i+1) % 32) == 0) {
for (j=0; j<8; j++) {
pBuf[32+4*j] = (m_sha256.hash[j] >> 24) & 0xff;
pBuf[33+4*j] = (m_sha256.hash[j] >> 16) & 0xff;
pBuf[34+4*j] = (m_sha256.hash[j] >> 8) & 0xff;
pBuf[35+4*j] = m_sha256.hash[j] & 0xff;
}
sha256_begin(&m2_sha256);
sha256_hash(pBuf, 64, &m2_sha256);
sha256_end(pTemp, &m2_sha256);
}
buffer_area[i] ^= m_sha256.hash[((i%32)/4)] >> ((i%4)*8);
if ((blocks_processed > 1) || (i>31)) {
keystream = genrand_int32() >> 24;
buffer_area[i] ^= keystream;
}
if ((++i % 32) == 0) {
memcpy(&m_sha256, &m2_sha256, sizeof(m_sha256));
}
} else {
if ((eof == 0) || (eof && (i < (bytes_read - 32)))) {
buffer_area[i] ^= m_sha256.hash[((i%32)/4)] >> ((i%4)*8);
if (IV_processed) {
keystream = genrand_int32() >> 24;
buffer_area[i] ^= keystream;
}
}
if (blocks_processed == 1) {
if (i == 31) {
memset(&multipart_key[0], 0, 32);
IV_processed = 1;
for (k = 0; k < 32; k++) {
multipart_key[(k>>2)] |= buffer_area[k] << (8*(k&3));
}
init_by_array(&multipart_key[0], keysize);
}
}
if (((i+1) % 32) == 0) {
for (j = i-31; j <= i; j++) {
pBuf[0 + (j-i+31)] = buffer_area[j];
}
for (j=0; j<8; j++) {
pBuf[32+4*j] = (m_sha256.hash[j] >> 24) & 0xff;
pBuf[33+4*j] = (m_sha256.hash[j] >> 16) & 0xff;
pBuf[34+4*j] = (m_sha256.hash[j] >> 8) & 0xff;
pBuf[35+4*j] = m_sha256.hash[j] & 0xff;
}
sha256_begin(&m_sha256);
sha256_hash(pBuf, 64, &m_sha256);
sha256_end(pTemp, &m_sha256);
}
i++;
}
}
if (DECRYPT == direction) {
if (blocks_processed == 1) {
if (eof == 0) {
fwrite(&buffer_area[32],1,bytes_read-32,fd_out);
sha256_hash(&buffer_area[0],bytes_read, &file_sha256);
} else {
fwrite(&buffer_area[32],1,bytes_read-64,fd_out);
sha256_hash(&buffer_area[0],bytes_read-32, &file_sha256);
memset(comp_sha256.hash, 0, 32);
for (j=0; j<8; j++) {
comp_sha256.hash[j] |= buffer_area[bytes_read-32+4*j] << 24;
comp_sha256.hash[j] |= buffer_area[bytes_read-31+4*j] << 16;
comp_sha256.hash[j] |= buffer_area[bytes_read-30+4*j] << 8;
comp_sha256.hash[j] |= buffer_area[bytes_read-29+4*j];
}
}
} else {
if (eof == 0) {
fwrite(&buffer_area[0],1,bytes_read,fd_out);
sha256_hash(&buffer_area[0],bytes_read, &file_sha256);
} else {
fwrite(&buffer_area[0],1,bytes_read-32,fd_out);
sha256_hash(&buffer_area[0],bytes_read-32, &file_sha256);
memset(comp_sha256.hash, 0, 32);
for (j=0; j<8; j++) {
comp_sha256.hash[j] |= buffer_area[bytes_read-32+4*j] << 24;
comp_sha256.hash[j] |= buffer_area[bytes_read-31+4*j] << 16;
comp_sha256.hash[j] |= buffer_area[bytes_read-30+4*j] << 8;
comp_sha256.hash[j] |= buffer_area[bytes_read-29+4*j];
}
}
}
} else {
fwrite(&buffer_area[0],1,bytes_read,fd_out);
}
start_fill = 0;
} while (eof == 0);
fclose(fd_in);
sha256_end(pTemp, &file_sha256);
for (j=0; j<8; j++) {
pBuf[0+4*j] = (file_sha256.hash[j] >> 24) & 0xff;
pBuf[1+4*j] = (file_sha256.hash[j] >> 16) & 0xff;
pBuf[2+4*j] = (file_sha256.hash[j] >> 8) & 0xff;
pBuf[3+4*j] = file_sha256.hash[j] & 0xff;
}
#if 0
printf("\nHash = ");
for (j = 0; j<8; j++) {
printf("0x%8x ", file_sha256.hash[j]);
}
printf("\n");
#endif
if (ENCRYPT == direction) {
fwrite(&pBuf[0],1,32,fd_out);
} else {
if (memcmp(&comp_sha256.hash, &file_sha256.hash, 32) != 0) {
#if 0
printf("\nstored hash = ");
for (j = 0; j<8; j++) {
printf("0x%8x ", comp_sha256.hash[j]);
}
printf("\n");
printf("calculated hash = ");
for (j = 0; j<8; j++) {
printf("0x%8x ", file_sha256.hash[j]);
}
printf("\n");
#endif
fclose(fd_out);
_unlink(my_out);
printf("\nIncorrect passphrase, try again\n");
exit(0);
}
}
fclose(fd_out);
exit(0);
}
</stdlib.h></stdlib.h></endian.h></byteswap.h></stdlib.h></string.h></limits.h></time.h></comutil.h></string.h></memory.h></stdlib.h></stdio.h></io.h>
|
|
|
|
|
<br />
#include <io.h><br />
#include <stdio.h><br />
#include <stdlib.h><br />
#include <memory.h><br />
#include <string.h><br />
#include <comutil.h><br />
#include <time.h><br />
<br />
#pragma once<br />
<br />
<br />
#ifndef _SHA2_H<br />
#define _SHA2_H<br />
<br />
#include <limits.h><br />
<br />
<br />
<br />
#define sfx_lo(x,y) x##y<br />
#define sfx_hi(x,y) sfx_lo(x,y)<br />
#define n_u32(p) sfx_hi(0x##p,s_u32)<br />
#define n_u64(p) sfx_hi(0x##p,s_u64)<br />
<br />
<br />
<br />
#if UINT_MAX == 0xffffffff<br />
typedef unsigned int sha2_32t;<br />
#define s_u32 u<br />
#elif ULONG_MAX == 0xffffffff<br />
typedef unsigned long sha2_32t;<br />
#define s_u32 ul<br />
#else<br />
#error Please define sha2_32t as an unsigned 32 bit type in sha2.h<br />
#endif<br />
<br />
<br />
<br />
#if defined( _MSC_VER )<br />
typedef unsigned __int64 sha2_64t;<br />
#define s_u64 ui64<br />
#elif ULONG_MAX == 0xffffffffffffffff<br />
typedef unsigned long sha2_64t;<br />
#define s_u64 ul<br />
#elif ULONG_MAX == 0xffffffff<br />
typedef unsigned long long sha2_64t; <br />
#define s_u64 ull<br />
#else<br />
#error Please define sha2_64t as an unsigned 64 bit type in sha2.h<br />
#endif<br />
<br />
#if defined(__cplusplus)<br />
extern "C"<br />
{<br />
#endif<br />
<br />
#define BYTES_PER_BLOCK 32<br />
#define BYTES_PER_WORD 4<br />
#define WORDS_PER_BLOCK (BYTES_PER_BLOCK/BYTES_PER_WORD)<br />
#define REHASH_SIZE (BYTES_PER_BLOCK*2)<br />
<br />
#define SHA256_DIGEST_SIZE 32<br />
#define SHA384_DIGEST_SIZE 48<br />
#define SHA512_DIGEST_SIZE 64<br />
<br />
#define SHA256_BLOCK_SIZE 64<br />
#define SHA384_BLOCK_SIZE 128<br />
#define SHA512_BLOCK_SIZE 128<br />
<br />
#define SHA2_DIGEST_SIZE SHA256_DIGEST_SIZE<br />
#define SHA2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE<br />
<br />
#define SHA2_GOOD 0<br />
#define SHA2_BAD 1<br />
<br />
<br />
<br />
typedef struct<br />
{ sha2_32t count[2];<br />
sha2_32t hash[8];<br />
sha2_32t wbuf[16];<br />
} sha256_ctx;<br />
<br />
<br />
<br />
typedef struct<br />
{ sha2_64t count[2];<br />
sha2_64t hash[8];<br />
sha2_64t wbuf[16];<br />
} sha512_ctx;<br />
<br />
typedef sha512_ctx sha384_ctx;<br />
<br />
<br />
<br />
typedef struct<br />
{ union<br />
{ sha256_ctx ctx256[1];<br />
sha512_ctx ctx512[1];<br />
} uu[1];<br />
sha2_32t sha2_len;<br />
} sha2_ctx;<br />
<br />
void sha256_compile(sha256_ctx ctx[1]);<br />
void sha512_compile(sha512_ctx ctx[1]);<br />
<br />
void sha256_begin(sha256_ctx ctx[1]);<br />
void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);<br />
void sha256_end(unsigned char hval[], sha256_ctx ctx[1]);<br />
void sha256(unsigned char hval[], const unsigned char data[], unsigned long len); <br />
<br />
void sha384_begin(sha384_ctx ctx[1]);<br />
#define sha384_hash sha512_hash<br />
void sha384_end(unsigned char hval[], sha384_ctx ctx[1]);<br />
void sha384(unsigned char hval[], const unsigned char data[], unsigned long len); <br />
<br />
void sha512_begin(sha512_ctx ctx[1]);<br />
void sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);<br />
void sha512_end(unsigned char hval[], sha512_ctx ctx[1]);<br />
void sha512(unsigned char hval[], const unsigned char data[], unsigned long len); <br />
<br />
int sha2_begin(unsigned long size, sha2_ctx ctx[1]);<br />
void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);<br />
void sha2_end(unsigned char hval[], sha2_ctx ctx[1]);<br />
int sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len); <br />
<br />
#if defined(__cplusplus)<br />
}<br />
#endif<br />
<br />
#endif<br />
<br />
#define MAX_WORDS 524288<br />
<br />
<br />
#define N 624<br />
#define M 397<br />
#define MATRIX_A 0x9908b0dfUL /* constant vector a */<br />
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */<br />
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */<br />
<br />
unsigned long genrand_int32(void);<br />
<br />
static unsigned long mt[N]; <br />
static int mti=N+1; <br />
static unsigned long accum; <br />
<br />
<br />
<br />
#define SHA_2 /* for dynamic hash length */<br />
#define SHA_256<br />
<br />
#include <string.h> <br />
#include <stdlib.h> <br />
<br />
<br />
<br />
<br />
#if defined(__GNU_LIBRARY__)<br />
# include <byteswap.h><br />
# include <endian.h><br />
#elif defined(__CRYPTLIB__)<br />
# if defined( INC_ALL )<br />
# include "crypt.h"<br />
# elif defined( INC_CHILD )<br />
# include "../crypt.h"<br />
# else<br />
# include "crypt.h"<br />
# endif<br />
# if defined(DATA_LITTLEENDIAN)<br />
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN<br />
# else<br />
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN<br />
# endif<br />
#elif defined(_MSC_VER)<br />
# include <stdlib.h><br />
#elif !defined(WIN32)<br />
# include <stdlib.h><br />
# if !defined (_ENDIAN_H)<br />
# include <br />
# else<br />
# include _ENDIAN_H<br />
# endif<br />
#endif<br />
<br />
<br />
#define SHA_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */<br />
#define SHA_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */<br />
<br />
#if !defined(PLATFORM_BYTE_ORDER)<br />
#if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)<br />
# if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)<br />
# if defined(BYTE_ORDER)<br />
# if (BYTE_ORDER == LITTLE_ENDIAN)<br />
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN<br />
# elif (BYTE_ORDER == BIG_ENDIAN)<br />
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN<br />
# endif<br />
# endif<br />
# elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) <br />
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN<br />
# elif !defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)<br />
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN<br />
# endif<br />
#elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN)<br />
# if defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)<br />
# if defined(_BYTE_ORDER)<br />
# if (_BYTE_ORDER == _LITTLE_ENDIAN)<br />
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN<br />
# elif (_BYTE_ORDER == _BIG_ENDIAN)<br />
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN<br />
# endif<br />
# endif<br />
# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) <br />
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN<br />
# elif !defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)<br />
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN<br />
# endif<br />
#elif 0 /* **** EDIT HERE IF NECESSARY **** */<br />
#define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN<br />
#elif 0 /* **** EDIT HERE IF NECESSARY **** */<br />
#define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN<br />
#elif (('1234' >> 24) == '1')<br />
# define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN<br />
#elif (('4321' >> 24) == '1')<br />
# define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN<br />
#endif<br />
#endif<br />
<br />
#if !defined(PLATFORM_BYTE_ORDER)<br />
# error Please set undetermined byte order (lines 159 or 161 of sha2.c).<br />
#endif<br />
<br />
#ifdef _MSC_VER<br />
#pragma intrinsic(memcpy)<br />
#endif<br />
<br />
#define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))<br />
<br />
#if !defined(bswap_32)<br />
#define bswap_32(x) (rotr32((x), 24) & 0x00ff00ff | rotr32((x), 8) & 0xff00ff00)<br />
#endif<br />
<br />
#if (PLATFORM_BYTE_ORDER == SHA_LITTLE_ENDIAN)<br />
#define SWAP_BYTES<br />
#else<br />
#undef SWAP_BYTES<br />
#endif<br />
<br />
#if defined(SHA_2) || defined(SHA_256)<br />
<br />
#define SHA256_MASK (SHA256_BLOCK_SIZE - 1)<br />
<br />
#if defined(SWAP_BYTES)<br />
#define bsw_32(p,n) { int _i = (n); while(_i--) p[_i] = bswap_32(p[_i]); }<br />
#else<br />
#define bsw_32(p,n) <br />
#endif<br />
<br />
<br />
<br />
#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))<br />
#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))<br />
<br />
#define s256_0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22)) <br />
#define s256_1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25)) <br />
#define g256_0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3)) <br />
#define g256_1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10)) <br />
<br />
<br />
<br />
<br />
<br />
#define h2(i) ctx->wbuf[i & 15] += \<br />
g256_1(ctx->wbuf[(i + 14) & 15]) + ctx->wbuf[(i + 9) & 15] + g256_0(ctx->wbuf[(i + 1) & 15])<br />
<br />
#define h2_cycle(i,j) \<br />
v[(7 - i) & 7] += (j ? h2(i) : ctx->wbuf[i & 15]) + k256[i + j] \<br />
+ s256_1(v[(4 - i) & 7]) + ch(v[(4 - i) & 7], v[(5 - i) & 7], v[(6 - i) & 7]); \<br />
v[(3 - i) & 7] += v[(7 - i) & 7]; \<br />
v[(7 - i) & 7] += s256_0(v[(0 - i) & 7]) + maj(v[(0 - i) & 7], v[(1 - i) & 7], v[(2 - i) & 7])<br />
<br />
<br />
<br />
const sha2_32t k256[64] =<br />
{ n_u32(428a2f98), n_u32(71374491), n_u32(b5c0fbcf), n_u32(e9b5dba5), <br />
n_u32(3956c25b), n_u32(59f111f1), n_u32(923f82a4), n_u32(ab1c5ed5), <br />
n_u32(d807aa98), n_u32(12835b01), n_u32(243185be), n_u32(550c7dc3), <br />
n_u32(72be5d74), n_u32(80deb1fe), n_u32(9bdc06a7), n_u32(c19bf174), <br />
n_u32(e49b69c1), n_u32(efbe4786), n_u32(0fc19dc6), n_u32(240ca1cc), <br />
n_u32(2de92c6f), n_u32(4a7484aa), n_u32(5cb0a9dc), n_u32(76f988da), <br />
n_u32(983e5152), n_u32(a831c66d), n_u32(b00327c8), n_u32(bf597fc7), <br />
n_u32(c6e00bf3), n_u32(d5a79147), n_u32(06ca6351), n_u32(14292967), <br />
n_u32(27b70a85), n_u32(2e1b2138), n_u32(4d2c6dfc), n_u32(53380d13), <br />
n_u32(650a7354), n_u32(766a0abb), n_u32(81c2c92e), n_u32(92722c85),<br />
n_u32(a2bfe8a1), n_u32(a81a664b), n_u32(c24b8b70), n_u32(c76c51a3), <br />
n_u32(d192e819), n_u32(d6990624), n_u32(f40e3585), n_u32(106aa070), <br />
n_u32(19a4c116), n_u32(1e376c08), n_u32(2748774c), n_u32(34b0bcb5), <br />
n_u32(391c0cb3), n_u32(4ed8aa4a), n_u32(5b9cca4f), n_u32(682e6ff3), <br />
n_u32(748f82ee), n_u32(78a5636f), n_u32(84c87814), n_u32(8cc70208), <br />
n_u32(90befffa), n_u32(a4506ceb), n_u32(bef9a3f7), n_u32(c67178f2),<br />
};<br />
<br />
<br />
<br />
const sha2_32t i256[8] =<br />
{<br />
n_u32(6a09e667), n_u32(bb67ae85), n_u32(3c6ef372), n_u32(a54ff53a),<br />
n_u32(510e527f), n_u32(9b05688c), n_u32(1f83d9ab), n_u32(5be0cd19)<br />
};<br />
<br />
<br />
void sha256_begin(sha256_ctx ctx[1])<br />
{<br />
ctx->count[0] = ctx->count[1] = 0;<br />
memcpy(ctx->hash, i256, 8 * sizeof(sha2_32t));<br />
}<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
void sha256_compile(sha256_ctx ctx[1])<br />
{ sha2_32t v[8], j;<br />
<br />
memcpy(v, ctx->hash, 8 * sizeof(sha2_32t));<br />
<br />
for(j = 0; j < 64; j += 16)<br />
{<br />
h2_cycle( 0, j); h2_cycle( 1, j); h2_cycle( 2, j); h2_cycle( 3, j);<br />
h2_cycle( 4, j); h2_cycle( 5, j); h2_cycle( 6, j); h2_cycle( 7, j);<br />
h2_cycle( 8, j); h2_cycle( 9, j); h2_cycle(10, j); h2_cycle(11, j);<br />
h2_cycle(12, j); h2_cycle(13, j); h2_cycle(14, j); h2_cycle(15, j);<br />
}<br />
<br />
ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ctx->hash[2] += v[2]; ctx->hash[3] += v[3];<br />
ctx->hash[4] += v[4]; ctx->hash[5] += v[5]; ctx->hash[6] += v[6]; ctx->hash[7] += v[7];<br />
}<br />
<br />
<br />
<br />
<br />
void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1])<br />
{ sha2_32t pos = (sha2_32t)(ctx->count[0] & SHA256_MASK), <br />
space = SHA256_BLOCK_SIZE - pos;<br />
const unsigned char *sp = data;<br />
<br />
if((ctx->count[0] += len) < len)<br />
++(ctx->count[1]);<br />
<br />
while(len >= space) <br />
{<br />
memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);<br />
sp += space; len -= space; space = SHA256_BLOCK_SIZE; pos = 0; <br />
bsw_32(ctx->wbuf, SHA256_BLOCK_SIZE >> 2)<br />
sha256_compile(ctx);<br />
}<br />
<br />
memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);<br />
}<br />
<br />
<br />
<br />
static sha2_32t m1[4] =<br />
{<br />
n_u32(00000000), n_u32(ff000000), n_u32(ffff0000), n_u32(ffffff00)<br />
};<br />
<br />
static sha2_32t b1[4] =<br />
{<br />
n_u32(80000000), n_u32(00800000), n_u32(00008000), n_u32(00000080)<br />
};<br />
<br />
void sha256_end(unsigned char hval[], sha256_ctx ctx[1])<br />
{ sha2_32t i = (sha2_32t)(ctx->count[0] & SHA256_MASK);<br />
<br />
bsw_32(ctx->wbuf, (i + 3) >> 2)<br />
<br />
<br />
<br />
<br />
<br />
<br />
ctx->wbuf[i >> 2] = (ctx->wbuf[i >> 2] & m1[i & 3]) | b1[i & 3];<br />
<br />
<br />
<br />
<br />
if(i > SHA256_BLOCK_SIZE - 9)<br />
{<br />
if(i < 60) ctx->wbuf[15] = 0;<br />
sha256_compile(ctx);<br />
i = 0;<br />
}<br />
else <br />
i = (i >> 2) + 1;<br />
<br />
while(i < 14) <br />
ctx->wbuf[i++] = 0;<br />
<br />
<br />
<br />
<br />
<br />
<br />
ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);<br />
ctx->wbuf[15] = ctx->count[0] << 3;<br />
<br />
sha256_compile(ctx);<br />
<br />
<br />
<br />
for(i = 0; i < SHA256_DIGEST_SIZE; ++i)<br />
hval[i] = (unsigned char)(ctx->hash[i >> 2] >> 8 * (~i & 3));<br />
}<br />
<br />
void sha256(unsigned char hval[], const unsigned char data[], unsigned long len) <br />
{ sha256_ctx cx[1];<br />
<br />
sha256_begin(cx); sha256_hash(data, len, cx); sha256_end(hval, cx);<br />
}<br />
<br />
#endif<br />
<br />
#if defined(SHA_2)<br />
<br />
#define CTX_256(x) ((x)->uu->ctx256)<br />
<br />
<br />
<br />
int sha2_begin(unsigned long len, sha2_ctx ctx[1])<br />
{ unsigned long l = len;<br />
switch(len)<br />
{<br />
case 256: l = len >> 3;<br />
case 32: CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;<br />
memcpy(CTX_256(ctx)->hash, i256, 32); break;<br />
<br />
default: return SHA2_BAD;<br />
}<br />
<br />
ctx->sha2_len = l; return SHA2_GOOD;<br />
}<br />
<br />
void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1])<br />
{<br />
switch(ctx->sha2_len)<br />
{<br />
case 32: sha256_hash(data, len, CTX_256(ctx)); return;<br />
<br />
}<br />
}<br />
<br />
void sha2_end(unsigned char hval[], sha2_ctx ctx[1])<br />
{<br />
switch(ctx->sha2_len)<br />
{<br />
case 32: sha256_end(hval, CTX_256(ctx)); return;<br />
<br />
}<br />
}<br />
<br />
int sha2(unsigned char hval[], unsigned long size,<br />
const unsigned char data[], unsigned long len)<br />
{ sha2_ctx cx[1];<br />
<br />
if(sha2_begin(256, cx) == SHA2_GOOD)<br />
{<br />
sha2_hash(data, len, cx); sha2_end(hval, cx); return SHA2_GOOD;<br />
}<br />
else<br />
return SHA2_BAD;<br />
}<br />
<br />
#endif<br />
<br />
<br />
<br />
<br />
<br />
<br />
void init_genrand(unsigned long s)<br />
{<br />
mt[0]= s & 0xffffffffUL;<br />
accum = 0;<br />
for (mti=1; mti<n;> mt[mti] = <br />
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); <br />
<br />
<br />
<br />
<br />
mt[mti] &= 0xffffffffUL;<br />
<br />
}<br />
}<br />
<br />
<br />
<br />
<br />
<br />
void init_by_array(unsigned long init_key[], int key_length)<br />
{<br />
int i, j, k, dummy;<br />
<br />
memset(mt, 0, 4*N);<br />
init_genrand(19650218UL);<br />
i=1; j=0;<br />
<br />
<br />
<br />
k = (N>key_length ? N : key_length);<br />
for (; k; k--) {<br />
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))<br />
+ init_key[j] + j; <br />
mt[i] &= 0xffffffffUL; <br />
i++; j++;<br />
if (i>=N) { mt[0] = mt[N-1]; i=1; }<br />
if (j>=key_length) j=0;<br />
}<br />
for (k=N-1; k; k--) {<br />
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))<br />
- i; <br />
mt[i] &= 0xffffffffUL; <br />
i++;<br />
if (i>=N) { mt[0] = mt[N-1]; i=1; }<br />
}<br />
<br />
mt[0] = 0x80000000UL; <br />
<br />
for (i=0; i<64; i++) dummy = genrand_int32();<br />
<br />
}<br />
<br />
<br />
unsigned long genrand_int32(void)<br />
{<br />
unsigned long y;<br />
static unsigned long mag01[2]={0x0UL, MATRIX_A};<br />
<br />
<br />
if (mti >= N) { <br />
int kk;<br />
<br />
if (mti == N+1) <br />
init_genrand(5489UL); <br />
<br />
for (kk=0;kk<n-m;kk++)> y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);<br />
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];<br />
}<br />
for (;kk<n-1;kk++)> y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);<br />
mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];<br />
}<br />
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);<br />
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];<br />
<br />
mti = 0;<br />
}<br />
<br />
y = mt[mti++];<br />
<br />
<br />
y ^= (y >> 11);<br />
y ^= (y << 7) & 0x9d2c5680UL;<br />
y ^= (y << 15) & 0xefc60000UL;<br />
y ^= (y >> 18);<br />
<br />
accum += y;<br />
<br />
return accum;<br />
}<br />
<br />
#define ENCRYPT 0<br />
#define DECRYPT 1<br />
<br />
<br />
<br />
int main(int argc, char * argv[])<br />
{<br />
int IV_processed = 0;<br />
FILE * fd_in, * fd_out;<br />
unsigned char buffer_area[MAX_WORDS], keystream;<br />
size_t bytes_read = 0;<br />
unsigned int i, j, k, keysize = 0;<br />
int eof, filename_size, direction, muddle = 0;<br />
int start_fill, blocks_processed = 0;<br />
char output_filename[1000];<br />
const char * my_out = output_filename;<br />
unsigned long multipart_key[N];<br />
sha256_ctx m_sha256, m2_sha256;<br />
unsigned char pBuf[4096], pTemp[256]; <br />
unsigned long uRead = 0;<br />
<br />
FILETIME idleTimePtr, kernelTimePtr, userTimePtr;<br />
<br />
memset(buffer_area, 0, sizeof(buffer_area));<br />
memset(output_filename, 0, sizeof(output_filename));<br />
<br />
memset(multipart_key, 0, 4*N);<br />
<br />
GetSystemTimes(&idleTimePtr, &kernelTimePtr, &userTimePtr);<br />
multipart_key[0] = idleTimePtr.dwLowDateTime;<br />
multipart_key[1] = idleTimePtr.dwHighDateTime;<br />
multipart_key[2] = kernelTimePtr.dwLowDateTime;<br />
multipart_key[3] = kernelTimePtr.dwHighDateTime;<br />
multipart_key[4] = userTimePtr.dwLowDateTime;<br />
multipart_key[5] = userTimePtr.dwHighDateTime;<br />
multipart_key[6] = time(NULL);<br />
multipart_key[7] = clock();<br />
<br />
char * string = argv[2];<br />
for (i = 0; i < strlen(argv[2]); i++) {<br />
multipart_key[WORDS_PER_BLOCK+(i>>2)] |= string[i] << (WORDS_PER_BLOCK*(i&3));<br />
}<br />
keysize = (i >> 2) + 1;<br />
if (i&3) keysize++;<br />
keysize += WORDS_PER_BLOCK;<br />
init_by_array(&multipart_key[0], keysize);<br />
<br />
memset(&multipart_key[0], 0, BYTES_PER_BLOCK);<br />
<br />
for (i = 0; i < BYTES_PER_BLOCK; i++) {<br />
buffer_area[i] = genrand_int32() >> 24;<br />
}<br />
<br />
for (k = 0; k < BYTES_PER_BLOCK; k++) {<br />
multipart_key[(k>>2)] |= buffer_area[k] << (WORDS_PER_BLOCK*(k&3));<br />
}<br />
<br />
if (argc == 1) {<br />
printf("Invoke encryption using\n\n");<br />
printf(" \"%s file passphrase\"\n", argv[0]);<br />
printf("Encrypted/decrypted output will be the filename with an\nunderscore appended/removed\n");<br />
printf("The encrypted file will be 32 bytes larger than the plain text file\n");<br />
exit(0);<br />
} else {<br />
if (argc < 3) {<br />
printf("too few arguments\n");<br />
exit(0);<br />
}<br />
}<br />
<br />
<br />
if (argc >= 3) {<br />
strcpy((char *)&pBuf[0], argv[2]);<br />
sha256_begin(&m_sha256);<br />
sha256_hash(pBuf, (unsigned long)strlen(argv[2]), &m_sha256);<br />
sha256_end(pTemp, &m_sha256);<br />
} else {<br />
printf("insufficient arguments\n");<br />
exit(0);<br />
}<br />
<br />
if (_access((const char *)argv[1], 0) != 0) {<br />
printf("file does not exist\n");<br />
exit(0);<br />
} <br />
<br />
filename_size = strlen(argv[1]);<br />
strcpy(output_filename, argv[1]);<br />
if (output_filename[filename_size-1] == 0x5f) {<br />
direction = DECRYPT;<br />
output_filename[filename_size-1] = 0;<br />
start_fill = 0;<br />
} else {<br />
direction = ENCRYPT;<br />
output_filename[filename_size] = 0x5f;<br />
start_fill = BYTES_PER_BLOCK;<br />
init_by_array(&multipart_key[0], keysize);<br />
}<br />
<br />
fd_in = fopen((const char *)argv[1], (const char *)("rb"));<br />
fd_out = fopen(my_out, (const char *)("wb"));<br />
<br />
<br />
<br />
do {<br />
bytes_read = fread( &buffer_area[start_fill], 1, (MAX_WORDS-start_fill), fd_in);<br />
bytes_read += start_fill;<br />
blocks_processed++;<br />
i = 0;<br />
while (i < bytes_read) {<br />
if (ENCRYPT == direction) {<br />
if ((i % BYTES_PER_BLOCK) == 0) {<br />
for (j = i; j <= i+BYTES_PER_BLOCK-1; j++) {<br />
pBuf[j%BYTES_PER_BLOCK] = buffer_area[i+(j%BYTES_PER_BLOCK)];<br />
}<br />
}<br />
if (((i+1) % BYTES_PER_BLOCK) == 0) {
for (j=0; j<8; j++) {<br />
pBuf[BYTES_PER_BLOCK+4*j] = (m_sha256.hash[j] >> 24) & 0xff;<br />
pBuf[BYTES_PER_BLOCK+1+4*j] = (m_sha256.hash[j] >> 16) & 0xff;<br />
pBuf[BYTES_PER_BLOCK+2+4*j] = (m_sha256.hash[j] >> 8) & 0xff;<br />
pBuf[BYTES_PER_BLOCK+3+4*j] = m_sha256.hash[j] & 0xff;<br />
} <br />
sha256_begin(&m2_sha256);<br />
sha256_hash(pBuf, REHASH_SIZE, &m2_sha256);<br />
sha256_end(pTemp, &m2_sha256);<br />
}<br />
buffer_area[i] ^= m_sha256.hash[((i%BYTES_PER_BLOCK)/BYTES_PER_WORD)] >> ((i%4)*8);<br />
<br />
if ((blocks_processed > 1) || (i>(BYTES_PER_BLOCK-1))) {<br />
keystream = genrand_int32() >> 24;<br />
buffer_area[i] ^= keystream;<br />
}<br />
if ((++i % BYTES_PER_BLOCK) == 0) {
<br />
memcpy(&m_sha256, &m2_sha256, sizeof(m_sha256));<br />
}<br />
} else {
<br />
<br />
buffer_area[i] ^= m_sha256.hash[((i%BYTES_PER_BLOCK)/BYTES_PER_WORD)] >> ((i%4)*8);<br />
if (IV_processed) {<br />
keystream = genrand_int32() >> 24;<br />
buffer_area[i] ^= keystream;<br />
}<br />
<br />
if (blocks_processed == 1) {<br />
if (i == (BYTES_PER_BLOCK-1)) { <br />
memset(&multipart_key[0], 0, BYTES_PER_BLOCK);<br />
IV_processed = 1; <br />
for (k = 0; k < BYTES_PER_BLOCK; k++) {<br />
multipart_key[(k>>2)] |= buffer_area[k] << (8*(k&3));<br />
}<br />
<br />
init_by_array(&multipart_key[0], keysize);<br />
}<br />
}<br />
if (((i+1) % BYTES_PER_BLOCK) == 0) {<br />
for (j = i-(BYTES_PER_BLOCK-1); j <= i; j++) {<br />
pBuf[0 + (j-i+BYTES_PER_BLOCK-1)] = buffer_area[j];<br />
}<br />
for (j=0; j<8; j++) {<br />
pBuf[BYTES_PER_BLOCK+4*j] = (m_sha256.hash[j] >> 24) & 0xff;<br />
pBuf[BYTES_PER_BLOCK+1+4*j] = (m_sha256.hash[j] >> 16) & 0xff;<br />
pBuf[BYTES_PER_BLOCK+2+4*j] = (m_sha256.hash[j] >> 8) & 0xff;<br />
pBuf[BYTES_PER_BLOCK+3+4*j] = m_sha256.hash[j] & 0xff;<br />
}<br />
sha256_begin(&m_sha256);<br />
sha256_hash(pBuf, REHASH_SIZE, &m_sha256);<br />
sha256_end(pTemp, &m_sha256);<br />
}<br />
i++;<br />
}<br />
}<br />
if ((DECRYPT == direction) && (blocks_processed == 1)) {<br />
fwrite(&buffer_area[BYTES_PER_BLOCK],1,bytes_read-BYTES_PER_BLOCK,fd_out);<br />
} else {<br />
fwrite(&buffer_area[0],1,bytes_read,fd_out);<br />
}<br />
start_fill = 0;<br />
eof = feof(fd_in);<br />
} while (eof == 0);<br />
<br />
fclose(fd_in);<br />
fclose(fd_out);<br />
exit(0);<br />
}<br />
<br />
</stdlib.h></stdlib.h></endian.h></byteswap.h></stdlib.h></string.h></limits.h></time.h></comutil.h></string.h></memory.h></stdlib.h></stdio.h></io.h>
|
|
|
|
|