/* -----------------------------------------------------------------------
 *
 * Copyright (c) 2008 EURL Cryptographie Ingénierie et Conseil <ecctk@cryptograph-ic.com>, AIX-NOULETTE, 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-ecc.h>

s2byte decrypt_file_asym()
{
	s4byte	ret		= -1;

	/* set algo
	 */
	s2byte	algo	= ECCTK_ECC_ALGO_ECIES;

	/* set input/output file name
	 */
	s1byte	infile[]	= "protected_file",
			outfile[]	= "unprotected_file",
			kprvfile[]	= "private_key_file";

	/* private key context
	 */
	KPRV_CTX *kprv;
	
	/* read private key on file (no password protected)
	 */
	kprv = ReadFileKprvCtx(kprvfile);
	if (kprv == NULL)
	{
		fprintf(stderr, "Read private key on %s failed\n", kprvfile);
		return EXIT_FAILURE;
	}

	/* Initialize value for all curve in memory
	 */
	ret = InitializeEcctkAll();
	if (ret != ECCTK_ECC_NO_ERROR)
	{
		fprintf(stderr, "InitializeEcctkAll failed (%ld)\n", ret);
		return EXIT_FAILURE;
	}

	switch (algo)
	{
	case ECCTK_ECC_ALGO_ECIES:
		ret = DecryptFileEcies(infile, outfile, kprv, NULL);
		break;
	case ECCTK_ECC_ALGO_PSEC3:
		ret = DecryptFilePsec3(infile, outfile, kprv, NULL);
		break;
	default:
		ret = -1;
		break;
	}

	if (ret != ECCTK_ECC_NO_ERROR)
		fprintf(stderr, "Decryp %s failed (Error : %ld)\n", infile, ret);
	else
		fprintf(stdout, "Decrypt %s success, %s generated\n", infile, outfile);

	/* free context
	 */
	KprvCtxFree(kprv);

	/* remove value for all curve in memory
	 */
	FreeEcctkAll();

	return EXIT_SUCCESS;
}
