/*
 encrypt.c - PSEC-2 encryption function

 Copyright NTT MCL, 2000.

 Duncan S Wong
 Security Group, NTT MCL
 July 2000
*/

#include <stdio.h>
#include <gmp.h>
#include "ec_arith.h"
#include "psec2.h"
#include "hash.h"
#include "utils.h"
#include "random.h"

void BulkEncrypt(BYTE *message, CIPHER_INFO *cipherInfo, BYTE *K, WORD KLen,
	              PSEC2_CIPHERTEXT *ciphertext);

/*
 PSEC-2 encryption

 Return: TRUE if succeed; otherwise FALSE
*/
BYTE PSEC2_Encryption (
   BYTE             *message,       /* plaintext */
   CIPHER_INFO      *cipherInfo,
   PSEC2_PUB_KEY    *publicKey,     /* PSEC2 public key */
   PSEC2_CIPHERTEXT *ciphertext,    /* returned ciphertext */
   EC_PARAM         *E
)
{
mpz_t m, t, r, m_n_r;
EC_POINT Q;
BYTE *m_raw, *t_raw, *r_raw, *K_raw, *tmp_ptr, *m_n_r_raw;
RANDOM_STRUCT randomStruct;
unsigned int bytesNeeded;
static unsigned char seedByte = 0;
long i;

	mpz_init(m);
	mpz_init(r);
	mpz_init(m_n_r);
	mpz_init(t);
	m_raw = (BYTE *) malloc (publicKey->mLen/8);
	r_raw = (BYTE *) malloc (publicKey->rLen/8);
	K_raw = (BYTE *) malloc (publicKey->gLen/8);
	m_n_r_raw = (BYTE *) malloc ((publicKey->mLen + publicKey->rLen)/8);
	t_raw = (BYTE *) malloc (publicKey->hLen/8);

	memset(m_raw, 0, publicKey->mLen/8);

	/* convert (BYTE *) message to raw format */
	mpz_set_str(m, message, 0);
	WORD2BYTE(m_raw+(publicKey->mLen/8-ABS(m->_mp_size)*4), m->_mp_d,
	          ABS(m->_mp_size));

	/*
	printf("PSEC2_encryption: m_raw =\n");
	for(i=0; i< publicKey->mLen/8; i++)
		printf("%02x ", m_raw[i]);
	printf("\n\n");
	*/

	/* initialize random number generator */
	RandomInit(&randomStruct);

	/* initialize with all zero seed bytes for testing purpose only */
	while (1) {
		GetRandomBytesNeeded (&bytesNeeded, &randomStruct);
		if (bytesNeeded == 0) break;
		RandomUpdate (&randomStruct, &seedByte, 1);
	}

	/* generate r */
	GenerateBytes(r_raw, publicKey->rLen/8, &randomStruct);

	/*
	printf("PSEC2_Encryption: r_raw =\n");
	for(i=0; i< publicKey->rLen/8; i++)
		printf("%02x ", r_raw[i]);
	printf("\n\n");
	*/

	BYTE2WORD(r, r_raw, publicKey->rLen/8);
	printf("r = 0x%s\n", mpz_get_str(NULL, 16, r));

	/* compute K_raw := G(r_raw) */
	indexedSHA( K_raw, publicKey->gLen/8, r_raw, publicKey->rLen/8);

	/*
	printf("PSEC2_Encryption: K_raw =\n");
	for(i=0; i< publicKey->gLen/8; i++)
		printf("%02x ", K_raw[i]);
	printf("\n\n");
	*/

	/* construct m_n_r_raw := m_raw || r_raw */
	assignBYTE(m_n_r_raw, m_raw, publicKey->mLen/8);
	tmp_ptr = m_n_r_raw + publicKey->mLen/8;
	assignBYTE(tmp_ptr, r_raw, publicKey->rLen/8);

	/*
	printf("PSEC2_Encryption: m_n_r_raw =\n");
	for(i=0; i<(publicKey->mLen + publicKey->rLen)/8; i++)
		printf("%02x ", m_n_r_raw[i]);
	printf("\n\n");
	*/

	/* compute t_raw := H(m_n_r_raw) */
	indexedSHA(t_raw, publicKey->hLen/8, m_n_r_raw,
	           (publicKey->mLen + publicKey->rLen)/8);

	/*
	printf("PSEC2_Encryption: t_raw =\n");
	for(i=0; i<publicKey->hLen/8; i++)
		printf("%02x ", t_raw[i]);
	printf("\n\n");
	*/

	BYTE2WORD(t, t_raw, publicKey->hLen/8);

	/* compute Q = t * publicKey->pk */
	EC_initPoint (&Q);
	EC_Mult(&Q, t, &(publicKey->pk), E);

	/*
	printf("PSEC2_Encryption: Q =\n");
	printf("%s\n%s\n%d\n\n", mpz_get_str(NULL, 16, Q.x),
	       mpz_get_str(NULL, 16, Q.y), Q.inf_id);
	*/

	/* compute C1 = t * E->P */
	EC_initPoint (&(ciphertext->C1));
	EC_Mult(&(ciphertext->C1), t, &(E->P), E);

	/*
	printf("PSEC2_Encryption: ciphertext->C1 =\n");
	printf("%s\n%s\n%d\n\n", mpz_get_str(NULL, 16, (ciphertext->C1).x),
	       mpz_get_str(NULL, 16, (ciphertext->C1).y), (ciphertext->C1).inf_id);
	*/
	
	/* compute c2 = r \xor x_Q */
	mpz_init(ciphertext->c2);
	mpz_xor(ciphertext->c2, r, Q.x);

	/*
	printf("PSEC2_Encryption: ciphertext->c2 =\n");
	printf("%s\n\n", mpz_get_str(NULL, 16, ciphertext->c2));
	*/

	/* compute c3 */
	BulkEncrypt(m_raw, cipherInfo, K_raw, publicKey->gLen, ciphertext);

	/*
	printf("PSEC2_Encryption: ciphertext->c3 =\n");
	for(i=0; i< ciphertext->cipherLen; i++)
		printf("%02x ", ciphertext->c3[i]);
	printf("\n\n");
	*/


	/* clean up */
	mpz_clear(m);
	mpz_clear(r);
	mpz_clear(m_n_r);
	mpz_clear(t);

	memset(m_raw, 0, publicKey->mLen/8);
	memset(r_raw, 0, publicKey->rLen/8);
	memset(K_raw, 0, publicKey->gLen/8);
	memset(m_n_r_raw, 0, (publicKey->mLen + publicKey->rLen)/8);
	memset(t_raw, 0, publicKey->hLen/8);
	free(m_raw);
	free(r_raw);
	free(K_raw);
	free(m_n_r_raw);
	free(t_raw);

	return TRUE;
}


void BulkEncrypt(BYTE *message, CIPHER_INFO *cipherInfo, BYTE *K, WORD KLen,
	              PSEC2_CIPHERTEXT *ciphertext)
{
   if(cipherInfo->algorithmID == ENCRYPT_ALG_XOR) {
      ciphertext->cipherLen = Vernam(K, KLen, message,
		                               ciphertext->c3);
   }
}
