Code:
/* encrypt V1.1 Fri Oct 18 04:28:03 NZDT 2002 */
/* File de/encryption, using libtomcrypt */
/* Written by Daniel Richards <[email protected]> */
/* Help from Tom St Denis with various bits */
/* This code is public domain, no rights reserved. */
/* Encrypts by default, -d flag enables decryption */
/* ie: ./encrypt blowfish story.txt story.ct */
/* ./encrypt -d blowfish story.ct story.pt */
#include "tomcrypt.h"
#include "f2c/fglExt.h"
#include <ctype.h>
#include <string.h>
int errno;
unsigned char *toHex(unsigned char *dst, const unsigned char *src)
{
static const unsigned char nibble[] =
{
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
unsigned char *start = dst;
while ( *src )
{
*dst++ = nibble [ *src >> 4 ];
*dst++ = nibble [ *src++ & 0xF ];
}
*dst = '\0';
return start;
}
unsigned char *fromHex(unsigned char *dst, const unsigned char *src)
{
unsigned char *start = dst;
while ( *src )
{
if ( isdigit(*src) )
{
*dst = *src++ - '0';
}
else
{
*dst = *src++ - 'A' + 10;
}
*dst <<= 4;
if ( isdigit(*src) )
{
*dst++ += *src++ - '0';
}
else
{
*dst++ += *src++ - 'A' + 10;
}
}
*dst = '\0';
return start;
}
int aescrypt(int n)
{
unsigned char plaintext[512],ciphertext[512], vsData[512];
unsigned char tmpkey[512], key[MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
unsigned long outlen, y, ivsize, x;
unsigned char over [ sizeof vsData * 2 - 1 ], back [ sizeof vsData ];
int decrypt;
symmetric_CTR ctr;
int cipher_idx, hash_idx, ks;
char *infile, *outfile, *cipher;
prng_state prng;
FILE *fdkey;
unsigned char hexval[512], retHex[512];
/* register algs, so they can be printed */
register_algs();
popvchar((char*)vsData, sizeof(vsData));
/* popquote(vsData, sizeof(vsData)); */
popint(&decrypt);
cipher = "aes";
if (decrypt) {
printf("Decrypt Data Length=%d\n", strlen(vsData));
fromHex(retHex, vsData);
}
/*
if (!strcmp(argv[1], "-d")) {
decrypt = 1;
cipher = argv[2];
infile = argv[3];
outfile = argv[4];
} else {
decrypt = 0;
cipher = argv[1];
infile = argv[2];
outfile = argv[3];
}
*/
cipher_idx = find_cipher(cipher);
if (cipher_idx == -1) {
printf("Invalid cipher specified.\n");
exit(-1);
}
hash_idx = find_hash("sha256");
if (hash_idx == -1) {
printf("Invalid cipher specified.\n");
exit(-1);
}
hash_idx = find_hash("sha256");
if (hash_idx == -1) {
printf("LTC_SHA256 not found...?\n");
exit(-1);
}
ivsize = cipher_descriptor[cipher_idx].block_length;
ks = hash_descriptor[hash_idx].hashsize;
if (cipher_descriptor[cipher_idx].keysize(&ks) != CRYPT_OK) {
printf("Invalid keysize???\n");
exit(-1);
}
/*
printf("\nEnter key: ");
fgets((char *)tmpkey,sizeof(tmpkey), stdin);
*/
fdkey = fopen("/data3/cc.key","rb");
if (fdkey == NULL) {
perror("Can't open key file");
exit(-1);
}
if (fread(tmpkey,1,sizeof(tmpkey),fdkey) != sizeof(tmpkey)) {
}
fclose(fdkey);
outlen = sizeof(key);
if ((errno = hash_memory(hash_idx,tmpkey,strlen((char *)tmpkey),key,&outlen)) != CRYPT_OK) {
printf("Error hashing key: %s\n", error_to_string(errno));
exit(-1);
}
if (decrypt) {
/* Need to read in IV from the inputted string*/
strncpy(IV, retHex, ivsize);
IV[ivsize] = '\0';
if (strlen(IV) != ivsize) {
printf("Error reading IV from input.\n");
exit(-1);
}
if ((errno = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) {
printf("ctr_start error: %s\n",error_to_string(errno));
exit(-1);
}
/* get everything after the IV from the encrypted data */
strncpy(ciphertext, &retHex[ivsize], strlen(retHex)-ivsize);
*(ciphertext + strlen(retHex)-ivsize) = '\0';
/* IV done -do decryption */
if ((errno = ctr_decrypt(ciphertext,plaintext,sizeof(plaintext),&ctr)) != CRYPT_OK) {
printf("ctr_decrypt error: %s\n", error_to_string(errno));
exit(-1);
}
printf("Decrypt: %s\n", plaintext);
pushquote(plaintext, strlen(plaintext));
} else { /* encrypt */
/* Setup yarrow for random bytes for IV */
if ((errno = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) {
printf("Error setting up PRNG, %s\n", error_to_string(errno));
}
/* You can use rng_get_bytes on platforms that support it */
/* x = rng_get_bytes(IV,ivsize,NULL);*/
x = yarrow_read(IV,ivsize,&prng);
if (x != ivsize) {
printf("Error reading PRNG for IV required.\n");
exit(-1);
}
/* Save the IV */
strcpy(retHex, IV);
/* do the encryption */
if ((errno = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) {
printf("ctr_start error: %s\n",error_to_string(errno));
exit(-1);
}
if ((errno = ctr_encrypt(vsData, ciphertext,strlen(vsData),&ctr)) != CRYPT_OK) {
printf("ctr_encrypt error: %s\n", error_to_string(errno));
exit(-1);
}
/* append the ciphertext to the IV */
strcat(retHex, ciphertext);
/* convert the encrypted string to hex */
toHex(over, retHex);
pushvchar(over, strlen(over));
}
return 1;
}