/* -----------------------------------------------------------------------
 *
 * 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 verify_stream()
{
	s4byte	ret		= -1;

	/* set algo
	 */
	s2byte	algo	= ECCTK_ECC_ALGO_ECNR;

	/* set input/output file name
	 */
	s1byte	kpubfile[]	= "public_key_file",
			signfile[]	= "signature_file";

	/* set memory buffer to verify
	 */
	u1byte	in[] = "Elliptic Curve Cryptographic ToolKit";

	/* set size of memory buffer
	 */
	s4byte	in_size = sizeof(in);

	/* public key context
	 */
	KPUB_CTX *kpub;

	/* signature context
	 */
	SIGN_CTX *sign;

	/* read public key on file
	 */
	kpub = ReadFileKpubCtx(kpubfile);
	if (kpub == NULL)
	{
		fprintf(stderr, "Read public key on %s failed\n", kpubfile);
		return EXIT_FAILURE;
	}

	/* read signature on file
	 */
	sign = ReadFileSignCtx(signfile);
	if (sign == NULL)
	{
		fprintf(stderr, "Read signature on %s failed\n", signfile);

		KpubCtxFree(kpub);
		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_ECDSA:
		ret = VerifyStreamEcdsa(in, in_size, sign, kpub);
		break;
	case ECCTK_ECC_ALGO_ECNR:
		ret = VerifyStreamEcnr(in, in_size, sign, kpub);
		break;
	default:
		ret = -1;
		break;
	}

	if (ret != ECCTK_ECC_NO_ERROR)
		fprintf(stderr, "Verify stream failed (Error : %ld)\n", ret);
	else
		fprintf(stdout, "Verify stream success\n");

	/* free context
	 */
	KpubCtxFree(kpub);
	SignCtxFree(sign);

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

	return EXIT_SUCCESS;
}
