// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "crypt2.h"
#include "stdio.h"
/*------------------------------------------------
Functionality implemented£ºcovert the RSA key pair of PKCS format to XCOS format, and the reverse operation
--------------------------------------------------*/
int main(int argc, char* argv[])
{
	R_RSA_PUBLIC_KEY    RpubKey;
	R_RSA_PRIVATE_KEY   RpriKey;
	COS_RSA_PUBLIC_KEY  XpubKey;
	COS_RSA_PRIVATE_KEY XpriKey;
	int nRet = 0;
	int i = 0,j=0;

	//generate RSA key pair
	printf("Generate a pair of RSA key...please wait......\n");
	//NOTE:
	//Before writing RpubKey and RpriKey into device using S4WriteFile, you should
	//truncate the starting 4 bytes, that is "unsigned int bits" part.
	//After that, the format is the same to that format of the key pair produced by 
	//"Generate key"page of Developer Test Tool.
	nRet = R_GenerateRsaKeys(&RpubKey,&RpriKey);
	if(nRet != RE_SUCCESS)
		printf("error! return: 0x%04x",nRet);
	else
	{
		printf("RSA public key:\n");
		for(i = 0, j=0; i < sizeof(R_RSA_PUBLIC_KEY); i++,j++)
		{
			printf("%02x ", ((BYTE *)&RpubKey)[i]);
			if(j == 15)
			{
				printf("\n");
				j = -1;
			}
		}

		printf("\nRSA private key:\n");
		for(i = 0, j=0; i < sizeof(R_RSA_PRIVATE_KEY); i++,j++)
		{
			printf("%02x ", ((BYTE *)&RpriKey)[i]);
			if(j == 15)
			{
				printf("\n");
				j = -1;
			}
		}

	}

	//Convert PKCS key pair to XCOS key pair
	printf("\n\nConvert this RSA key to XCOS key...........\n");

	X_Pub2Cos(&XpubKey,&RpubKey);
	printf("\nXCOS public key after converted:\n");

	for(i = 0, j=0; i < sizeof(COS_RSA_PUBLIC_KEY); i++,j++)
	{
		printf("%02x ", ((BYTE *)&XpubKey)[i]);
		if(j == 15)
		{
			printf("\n");
			j = -1;
		}
	}

	X_Pri2Cos(&XpriKey,&RpriKey);
	printf("\nXCOS private key after converted:\n");

	for(i = 0, j=0; i < sizeof(COS_RSA_PRIVATE_KEY); i++,j++)
	{
		printf("%02x ", ((BYTE *)&XpriKey)[i]);
		if(j == 15)
		{
			printf("\n");
			j = -1;
		}
	}


	printf("\n\n");
	//generate XCOS key pair
	printf("Generate a pair of XCOS key...please wait......\n");
	nRet = X_GenerateRsaKeys(&XpubKey,&XpriKey);
	if(nRet != RE_SUCCESS)
		printf("error! return: 0x%04x",nRet);
	else
	{
		printf("XCOS public key:\n");
		for(i = 0, j=0; i < sizeof(COS_RSA_PUBLIC_KEY); i++,j++)
		{
			printf("%02x ", ((BYTE *)&XpubKey)[i]);
			if(j == 15)
			{
				printf("\n");
				j = -1;
			}
		}

		printf("\nXCOS private key:\n");
		for(i = 0, j=0; i < sizeof(COS_RSA_PRIVATE_KEY); i++,j++)
		{
			printf("%02x ", ((BYTE *)&XpriKey)[i]);
			if(j == 15)
			{
				printf("\n");
				j = -1;
			}
		}

	}

	//convert XCOS key pair to PKCS key pair
	printf("\n\nConvert this  XCOS key to RSA key ...........\n");

	X_Cos2Pub(&RpubKey,&XpubKey);
	printf("\nRSA public key after converted:\n");

	for(i = 0, j=0; i < sizeof(R_RSA_PUBLIC_KEY); i++,j++)
	{
		printf("%02x ", ((BYTE *)&RpubKey)[i]);
		if(j == 15)
		{
			printf("\n");
			j = -1;
		}
	}

	X_Cos2Pri(&RpriKey,&XpriKey);
	printf("\nRSA private key after converted:\n");

	for(i = 0, j=0; i < sizeof(R_RSA_PRIVATE_KEY); i++,j++)
	{
		printf("%02x ", ((BYTE *)&RpriKey)[i]);
		if(j == 15)
		{
			printf("\n");
			j = -1;
		}
	}

	return 0;
}
