/* -----------------------------------------------------------------------
 *
 * 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 encrypt_stream()
{
	/* set algo and encryption mode
	 */
#ifndef ECCTK_EVALUATION
	s2byte	algo	= ECCTK_CIPHER_ALGO_RIJNDAEL,
#else
	s2byte	algo	= ECCTK_CIPHER_ALGO_BLOWFISH,
#endif
			mode	= ECCTK_CIPHER_MODE_CBC;
	
	/* set input stream value, and create output stream with necessary size
	 */
	u1byte	in[]	= "Elliptic Curve Cryptographic ToolKit",
			out[48];

	s4byte	ret		= -1,
			outsize	= sizeof(out);

	/* 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_StreamEncrypt(in, sizeof(in), out, &outsize, key, sizeof(key)/4, iv, sizeof(iv)/2, 1, mode);
			break;
		case ECCTK_CIPHER_ALGO_3DES_EDE_2:
			ret = DES_StreamEncrypt(in, sizeof(in), out, &outsize, key, sizeof(key)/2, iv, sizeof(iv)/2, 2, mode);
			break;
		case ECCTK_CIPHER_ALGO_3DES_EDE_3:
			ret = DES_StreamEncrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv)/2, 3, mode);
			break;
		case ECCTK_CIPHER_ALGO_RC2:
			ret = RC2_StreamEncrypt(in, sizeof(in), out, &outsize, key, sizeof(key)/2, iv, sizeof(iv)/2, mode);
			break;
		case ECCTK_CIPHER_ALGO_RC4:
			ret = RC4_StreamEncrypt(in, sizeof(in), out, &outsize, key, sizeof(key));
			break;
		case ECCTK_CIPHER_ALGO_CAST128:
			ret = CAST128_StreamEncrypt(in, sizeof(in), out, &outsize, key, sizeof(key)/2, iv, sizeof(iv)/2, mode);
			break;
		case ECCTK_CIPHER_ALGO_BLOWFISH:
			ret = BLOWFISH_StreamEncrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv)/2, mode);
			break;
#ifndef ECCTK_EVALUATION
		case ECCTK_CIPHER_ALGO_MARS:
			ret = MARS_StreamEncrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv), mode);
			break;
		case ECCTK_CIPHER_ALGO_SERPENT:
			ret = SERPENT_StreamEncrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv), mode);
			break;
		case ECCTK_CIPHER_ALGO_TWOFISH:
			ret = TWOFISH_StreamEncrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv), mode);
			break;
		case ECCTK_CIPHER_ALGO_RIJNDAEL:
			ret = RIJNDAEL_StreamEncrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv), mode);
			break;
#endif
		default:
			ret = -1;
			break;
	}

	if (ret != ECCTK_CIPHER_NO_ERROR)
		printf("Encrypt stream failed (Error : %ld)\n", ret);
	else
		printf("Encrypt stream success, result stream size = %ld\n", outsize);
}
