/* -----------------------------------------------------------------------
 *
 * Copyright (c) 2002-2005 Mr Ludovic FLAMENT <ludovic.flament@free.fr>, LIEVIN, FRANCE
 *
 * Termes :
 *
 * L'utilisation et/ou la redistribution de ce code (avec ou sans modification),
 * ainsi que de tous les composants du produit (librairies, documentation, exemples, ...)
 * est soumise aux termes de la licence qui vous est attribuée par son auteur.
 *
 * -----------------------------------------------------------------------
 */

#include <stdlib.h>
#include <stdio.h>

#include <include/ecctk-prototype.h>
#include <include/ecctk-cipher.h>

void decrypt_file()
{
	s4byte	ret		= -1;

	/* set algo and decryption mode
	 */
#ifndef ECCTK_EVALUATION
	u2byte	algo	= ECCTK_CIPHER_ALGO_RIJNDAEL,
#else
	u2byte	algo	= ECCTK_CIPHER_ALGO_BLOWFISH,
#endif
			mode	= ECCTK_CIPHER_MODE_CBC;
	
	/* set input/output file name
	 */
	s1byte	infile[]	= "protected_file",
			outfile[]	= "unprotected_file";

	/* set a static 256 bits key and 128 bits iv
	 * in case of ECCTK_CIPHER_MODE_ECB mode, iv is ignore but can be pass at all function
	 */
	u1byte	key[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
					 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
					 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
					 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
			iv[]  = {0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
					 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00};

	switch (algo)
	{
	case ECCTK_CIPHER_ALGO_DES:
		ret = DES_FileDecrypt(infile, outfile, key, sizeof(key)/4, iv, sizeof(iv)/2, 1, mode);
		break;
	case ECCTK_CIPHER_ALGO_3DES_EDE_2:
		ret = DES_FileDecrypt(infile, outfile, key, sizeof(key)/2, iv, sizeof(iv)/2, 2, mode);
		break;
	case ECCTK_CIPHER_ALGO_3DES_EDE_3:
		ret = DES_FileDecrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv)/2, 3, mode);
		break;
	case ECCTK_CIPHER_ALGO_RC2:
		ret = RC2_FileDecrypt(infile, outfile, key, sizeof(key)/2, iv, sizeof(iv)/2, mode);
		break;
	case ECCTK_CIPHER_ALGO_RC4:
		ret = RC4_FileDecrypt(infile, outfile, key, sizeof(key));
		break;
	case ECCTK_CIPHER_ALGO_CAST128:
		ret = CAST128_FileDecrypt(infile, outfile, key, sizeof(key)/2, iv, sizeof(iv)/2, mode);
		break;
	case ECCTK_CIPHER_ALGO_BLOWFISH:
		ret = BLOWFISH_FileDecrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv)/2, mode);
		break;
#ifndef ECCTK_EVALUATION
	case ECCTK_CIPHER_ALGO_MARS:
		ret = MARS_FileDecrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv), mode);
		break;
	case ECCTK_CIPHER_ALGO_SERPENT:
		ret = SERPENT_FileDecrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv), mode);
		break;
	case ECCTK_CIPHER_ALGO_TWOFISH:
		ret = TWOFISH_FileDecrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv), mode);
		break;
	case ECCTK_CIPHER_ALGO_RIJNDAEL:
		ret = RIJNDAEL_FileDecrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv), mode);
		break;
#endif
	default:
		break;
	}

	if (ret != ECCTK_CIPHER_NO_ERROR)
		printf("Decrypt %s failed (Error : %ld)\n", infile, ret);
	else
		printf("Decrypt %s success, %s generated\n", infile, outfile);
}
