コード例 #1
0
ファイル: fmd.c プロジェクト: zizilala/projects_wince_new
//-----------------------------------------------------------------------------
BOOL
NAND_Read(
    HANDLE hNand,
    BYTE *pData,
    int size,
    BYTE *pEcc
    )
{
    UINT32 fifoLevel;
    NandDevice_t *pDevice = (NandDevice_t*)hNand;

    // Start ECC if a valid ECC buffer is passed in
    if (pEcc != NULL)
        {
        ECC_Init(pDevice->pGpmcRegs, pDevice->ECCCfg, pDevice->ECCtype, NAND_ECC_READ);
        }

    // enable prefetch if it's been properly configured
    if (pDevice->prefetchMode == kPrefetchRead )
        {
        SETREG32(&pDevice->pGpmcRegs->GPMC_PREFETCH_CONTROL, 
            GPMC_PREFETCH_CONTROL_STARTENGINE
            );

        // start copying data into passed in buffer
        while (size > 0)
            {
            // wait for fifo threshold to be reached
            fifoLevel = 0;
            while (fifoLevel < FIFO_THRESHOLD)
                {
                fifoLevel = INREG32(&pDevice->pGpmcRegs->GPMC_PREFETCH_STATUS);
                fifoLevel &= GPMC_PREFETCH_STATUS_FIFOMASK;
                fifoLevel >>= GPMC_PREFETCH_STATUS_FIFOSHIFT;
                }

            // copy data to buffer
            memcpy(pData, (BYTE*)pDevice->pFifo, FIFO_THRESHOLD);
            pData += FIFO_THRESHOLD;
            size -= FIFO_THRESHOLD;        
            }

        // NOTE:
        //  Prefetch engine will automatically stop on the completion
        // of data transfer
        pDevice->prefetchMode = kPrefetchOff;
        }
コード例 #2
0
ファイル: ECCDemo.c プロジェクト: 296791897/keilprj
/** 
 * @brief 	ECC测试函数
 * @param 
 * @return 
 * @note
 */
void ECC_test(U32 bitLen)
{
	ECC_POINT x, y, z;
#ifdef ECC_FIX_INPUT_TEST
	ECC_POINT R1, R2, R3;
#endif
	U32 t0[17]={0}, t1[17]={0}, t2[17]={0}, t3[17]={0}, temp[18]={0};
	U32 a[17]={0}, b[17]={0};
	U32 ECC_WordLen;
	U32 *ECC_n1 = NULL;
	ECC_PARA para;

	switch(bitLen)
	{
		case 160 :
			{
				para.ECC_NUMBIT = 160;
				para.ECC_a = (U32 *)ECC_160_a;
				para.ECC_b = (U32 *)ECC_160_b;
				para.ECC_n = (U32 *)ECC_160_n;
				para.ECC_p = (U32 *)ECC_160_p;
				para.ECC_G0x = (U32 *)ECC_160_Gx;
				para.ECC_G0y = (U32 *)ECC_160_Gy;

				ECC_n1 = (U32 *)ECC_160_n1;

#ifdef ECC_FIX_INPUT_TEST
				R1.x = (U32 *)ECC_160_R1_x;
				R1.y = (U32 *)ECC_160_R1_y;
				R2.x = (U32 *)ECC_160_R2_x;
				R2.y = (U32 *)ECC_160_R2_y;
				R3.x = (U32 *)ECC_160_R3_x;
				R3.y = (U32 *)ECC_160_R3_y;
#endif
				break;
			}

		case 192 :
			{
				para.ECC_NUMBIT = 192;
				para.ECC_a = (U32 *)ECC_192_a;
				para.ECC_b = (U32 *)ECC_192_b;
				para.ECC_n = (U32 *)ECC_192_n;
				para.ECC_p = (U32 *)ECC_192_p;
				para.ECC_G0x = (U32 *)ECC_192_Gx;
				para.ECC_G0y = (U32 *)ECC_192_Gy;

				ECC_n1 = (U32 *)ECC_192_n1;

#ifdef ECC_FIX_INPUT_TEST
				R1.x = (U32 *)ECC_192_R1_x;
				R1.y = (U32 *)ECC_192_R1_y;
				R2.x = (U32 *)ECC_192_R2_x;
				R2.y = (U32 *)ECC_192_R2_y;
				R3.x = (U32 *)ECC_192_R3_x;
				R3.y = (U32 *)ECC_192_R3_y;
#endif
				break;
			}

		case 256 :
			{
				para.ECC_NUMBIT = 256;
				para.ECC_a = (U32 *)ECC_256_a;
				para.ECC_b = (U32 *)ECC_256_b;
				para.ECC_n = (U32 *)ECC_256_n;
				para.ECC_p = (U32 *)ECC_256_p;
				para.ECC_G0x = (U32 *)ECC_256_Gx;
				para.ECC_G0y = (U32 *)ECC_256_Gy;

				ECC_n1 = (U32 *)ECC_256_n1;

#ifdef ECC_FIX_INPUT_TEST
				R1.x = (U32 *)ECC_256_R1_x;
				R1.y = (U32 *)ECC_256_R1_y;
				R2.x = (U32 *)ECC_256_R2_x;
				R2.y = (U32 *)ECC_256_R2_y;
				R3.x = (U32 *)ECC_256_R3_x;
				R3.y = (U32 *)ECC_256_R3_y;
#endif
				break;
			}

		case 302 :
			{
				para.ECC_NUMBIT = 302;
				para.ECC_a = (U32 *)ECC_302_a;
				para.ECC_b = (U32 *)ECC_302_b;
				para.ECC_n = (U32 *)ECC_302_n;
				para.ECC_p = (U32 *)ECC_302_p;
				para.ECC_G0x = (U32 *)ECC_302_Gx;
				para.ECC_G0y = (U32 *)ECC_302_Gy;

				ECC_n1 = (U32 *)ECC_302_n1;

#ifdef ECC_FIX_INPUT_TEST
				R1.x = (U32 *)ECC_302_R1_x;
				R1.y = (U32 *)ECC_302_R1_y;
				R2.x = (U32 *)ECC_302_R2_x;
				R2.y = (U32 *)ECC_302_R2_y;
				R3.x = (U32 *)ECC_302_R3_x;
				R3.y = (U32 *)ECC_302_R3_y;
#endif
				break;
			}

		case 384 :
			{
				para.ECC_NUMBIT = 384;
				para.ECC_a = (U32 *)ECC_384_a;
				para.ECC_b = (U32 *)ECC_384_b;
				para.ECC_n = (U32 *)ECC_384_n;
				para.ECC_p = (U32 *)ECC_384_p;
				para.ECC_G0x = (U32 *)ECC_384_Gx;
				para.ECC_G0y = (U32 *)ECC_384_Gy;

				ECC_n1 = (U32 *)ECC_384_n1;

#ifdef ECC_FIX_INPUT_TEST
				R1.x = (U32 *)ECC_384_R1_x;
				R1.y = (U32 *)ECC_384_R1_y;
				R2.x = (U32 *)ECC_384_R2_x;
				R2.y = (U32 *)ECC_384_R2_y;
				R3.x = (U32 *)ECC_384_R3_x;
				R3.y = (U32 *)ECC_384_R3_y;
#endif
				break;
			}

		case 512 :
			{
				para.ECC_NUMBIT = 512;
				para.ECC_a = (U32 *)ECC_512_a;
				para.ECC_b = (U32 *)ECC_512_b;
				para.ECC_n = (U32 *)ECC_512_n;
				para.ECC_p = (U32 *)ECC_512_p;
				para.ECC_G0x = (U32 *)ECC_512_Gx;
				para.ECC_G0y = (U32 *)ECC_512_Gy;

				ECC_n1 = (U32 *)ECC_512_n1;

#ifdef ECC_FIX_INPUT_TEST
				R1.x = (U32 *)ECC_512_R1_x;
				R1.y = (U32 *)ECC_512_R1_y;
				R2.x = (U32 *)ECC_512_R2_x;
				R2.y = (U32 *)ECC_512_R2_y;
				R3.x = (U32 *)ECC_512_R3_x;
				R3.y = (U32 *)ECC_512_R3_y;
#endif
				break;
			}

		case 521 :
			{
				para.ECC_NUMBIT = 521;
				para.ECC_a = (U32 *)ECC_521_a;
				para.ECC_b = (U32 *)ECC_521_b;
				para.ECC_n = (U32 *)ECC_521_n;
				para.ECC_p = (U32 *)ECC_521_p;
				para.ECC_G0x = (U32 *)ECC_521_Gx;
				para.ECC_G0y = (U32 *)ECC_521_Gy;

				ECC_n1 = (U32 *)ECC_521_n1;

#ifdef ECC_FIX_INPUT_TEST
				R1.x = (U32 *)ECC_521_R1_x;
				R1.y = (U32 *)ECC_521_R1_y;
				R2.x = (U32 *)ECC_521_R2_x;
				R2.y = (U32 *)ECC_521_R2_y;
				R3.x = (U32 *)ECC_521_R3_x;
				R3.y = (U32 *)ECC_521_R3_y;
#endif
				break;
			}		
	}


	ECC_Init(&para);

	ECC_WordLen = (para.ECC_NUMBIT + 0x1F)>>5;

	x.x = para.ECC_G0x;
	x.y = para.ECC_G0y;
	y.x = (U32 *)t0;
	y.y = (U32 *)t1;
	z.x = (U32 *)t2;
	z.y = (U32 *)t3;


#ifdef ECC_FIX_INPUT_TEST
	Memcpy_U32(a, para.ECC_a, ECC_WordLen);
	Memcpy_U32(b, para.ECC_b, ECC_WordLen);
#else
	GetTrueRand32(a, ECC_WordLen, 0);
	GetTrueRand32(b, ECC_WordLen, 0);
#endif


	printf("\r\n\r\n --------ECC%d Test------",para.ECC_NUMBIT);

	printf("\r\n --------Test for a*p + b*p = (a+b)*p------");
	ECC_PointMul(a, ECC_WordLen, &x, &y);
	ECC_PointMul(b, ECC_WordLen, &x, &z);
	ECC_PointAdd(&y, &z, &z);

	ECC_ModAdd(a, b, para.ECC_n, ECC_WordLen, temp);
	ECC_PointMul(temp, ECC_WordLen, &x, &y);

#ifdef ECC_FIX_INPUT_TEST
	if(Point_Cmp(&y, &z, ECC_WordLen) || Point_Cmp(&y, &R1, ECC_WordLen))
#else
		if(Point_Cmp(&y, &z, ECC_WordLen))
#endif
		{
			printf("\r\n a*P + b*P = (a+b)*P test failed!");
			print_buf_32(a, ECC_WordLen, "a");
			print_buf_32(b, ECC_WordLen, "b");
			print_buf_32(y.x, ECC_WordLen, "y.x");
			print_buf_32(y.y, ECC_WordLen, "y.y");
			print_buf_32(z.x, ECC_WordLen, "z.x");
			print_buf_32(z.y, ECC_WordLen, "z.y");

			while(1);//出错则停止
		}


	printf("\r\n --------Test for a*p + (-b*p) = (a-b)*p------");
	ECC_PointMul(a, ECC_WordLen, &x, &y);
	ECC_PointMul(b, ECC_WordLen, &x, &z);

	ECC_ModSub(para.ECC_p, z.y, para.ECC_p, ECC_WordLen, z.y);
	ECC_PointAdd(&y, &z, &z);

	ECC_ModSub(a, b, para.ECC_n, ECC_WordLen, temp);
	ECC_PointMul(temp, ECC_WordLen, &x, &y);

#ifdef ECC_FIX_INPUT_TEST
	if(Point_Cmp(&y, &z, ECC_WordLen) || Point_Cmp(&y, &R2, ECC_WordLen))
#else
		if(Point_Cmp(&y, &z, ECC_WordLen))
#endif
		{
			printf("\r\n a*p + (-b*p) = (a-b)*p test failed!");
			print_buf_32(a, ECC_WordLen, "a");
			print_buf_32(b, ECC_WordLen, "b");
			print_buf_32(y.x, ECC_WordLen, "y.x");
			print_buf_32(y.y, ECC_WordLen, "y.y");
			print_buf_32(z.x, ECC_WordLen, "z.x");
			print_buf_32(z.y, ECC_WordLen, "z.y");

			while(1);//出错则停止
		}


	printf("\r\n --------Test for a*(b*P) = b*(a*P) = (a*b)*P------");
	ECC_PointMul(a, ECC_WordLen, &x, &y);
	ECC_PointMul(b, ECC_WordLen, &y, &y);

	ECC_ModMul(a, b, para.ECC_n, ECC_WordLen, temp);
	ECC_PointMul(temp, ECC_WordLen, &x, &z);

#ifdef ECC_FIX_INPUT_TEST
	if(Point_Cmp(&y, &z, ECC_WordLen) || Point_Cmp(&y, &R3, ECC_WordLen))
#else
		if(Point_Cmp(&y, &z, ECC_WordLen))
#endif
		{
			printf("\r\n b*(a*P) = (a*b)*P test failed!");
			print_buf_32(a, ECC_WordLen, "a");
			print_buf_32(b, ECC_WordLen, "b");
			print_buf_32(y.x, ECC_WordLen, "y.x");
			print_buf_32(y.y, ECC_WordLen, "y.y");
			print_buf_32(z.x, ECC_WordLen, "z.x");
			print_buf_32(z.y, ECC_WordLen, "z.y");

			while(1);//出错则停止
		}

	ECC_PointMul(a, ECC_WordLen, &x, &y);
	ECC_PointMul(b, ECC_WordLen, &y, &y);

#ifdef ECC_FIX_INPUT_TEST
	if(Point_Cmp(&y, &z, ECC_WordLen) || Point_Cmp(&y, &R3, ECC_WordLen))
#else
		if(Point_Cmp(&y, &z, ECC_WordLen))
#endif
		{
			printf("\r\n a*(b*P) = (a*b)*P test failed!");
			print_buf_32(a, ECC_WordLen, "a");
			print_buf_32(b, ECC_WordLen, "b");
			print_buf_32(y.x, ECC_WordLen, "y.x");
			print_buf_32(y.y, ECC_WordLen, "y.y");
			print_buf_32(z.x, ECC_WordLen, "z.x");
			print_buf_32(z.y, ECC_WordLen, "z.y");

			while(1);//出错则停止
		}


	printf("\r\n --------Test for PointMul: (k)*G = (k mod n)G ------");
#ifdef ECC_FIX_INPUT_TEST
	memset(temp, 0x3A, (ECC_WordLen+1)*4);	
#else
	GetTrueRand32(temp, ECC_WordLen+1, 0);
#endif

	ECC_PointMul(temp, ECC_WordLen+1, &x, &y);			//注意:点乘kP中的大数k不能超过576比特,即18个字

	ECC_Mod(temp, ECC_WordLen+1, para.ECC_n, ECC_WordLen, temp);
	ECC_PointMul(temp, ECC_WordLen, &x, &z);

	if(Point_Cmp(&y, &z, ECC_WordLen))
	{
		printf("\r\n (k)*G = (k mod n)G test failed!");
		print_buf_32(temp, ECC_WordLen+1, "temp");
		print_buf_32(y.x, ECC_WordLen, "y.x");
		print_buf_32(y.y, ECC_WordLen, "y.y");
		print_buf_32(z.x, ECC_WordLen, "z.x");
		print_buf_32(z.y, ECC_WordLen, "z.y");

		while(1);//出错则停止
	}


	printf("\r\n --------Test for PointMul: (n+1)*G = G ------");
	ECC_PointMul(ECC_n1, ECC_WordLen, &x, &z);
	if(Point_Cmp(&x, &z, ECC_WordLen))
	{
		printf("\r\n --------Test for PointMul: (n+1)*G = G Fail------");
		print_buf_32(z.x, ECC_WordLen, "z.x");
		print_buf_32(z.y, ECC_WordLen, "z.y");

		while(1);//出错则停止
	}						  


	printf("\r\n --------ECC_TestPoint----------------");
	ECC_PointMul(temp, ECC_WordLen, &x, &z);
	if(ECC_TestPoint(&z)==0)
	{
		printf("\r\n --------Point is not on the curve! Fail!-------");
		print_buf_32(z.x, ECC_WordLen, "z.x");
		print_buf_32(z.y, ECC_WordLen, "z.y");

		while(1);//出错则停止         
	}


	printf("\r\n --------ECC_ModMul Test----------------");
	Memcpy_U32(t0, para.ECC_p, ECC_WordLen);
	Memcpy_U32(t1, para.ECC_p, ECC_WordLen);
	t0[0]-=1;
	t1[0]-=1;
	ECC_ModMul(t0, t1, para.ECC_p, ECC_WordLen, t2);   //t2 should be 1; 
	SetZero_U32(t1, ECC_WordLen);
	t1[0]=1;
	if(memcmp(t1, t2, ECC_WordLen<<2))
	{
		printf("\r\n --------ECC_ModMul Test Fail-----------");

		while(1);//出错则停止
	}


	printf("\r\n --------ECC_ModInv Test -----------");
	Memcpy_U32(t1, para.ECC_n, ECC_WordLen);
	t1[0]-=1;
	SetZero_U32(t2, ECC_WordLen);
	ECC_ModInv(t1, para.ECC_n, GetBitLen_U32(para.ECC_n, ECC_WordLen), t2);  //t2 should be t1, namely -1 ;
	if(memcmp(t1, t2, ECC_WordLen<<2))
	{
		printf("\r\n --------ECC_ModInv Test Fail-----------");

		while(1);//出错则停止
	}

	ECC_Close();
}
コード例 #3
0
ファイル: ECCtest.c プロジェクト: SilentCircle/silent-text
static SCLError sTestECC(int keySize)
{
#define PTsize 32

    SCLError     err = kSCLError_NoErr;
    int     i;
    
    uint8_t        PT[PTsize];
    
    uint8_t        CT[256];
    size_t         CTlen = 0;
    
    uint8_t        DT[PTsize];
    size_t         DTlen = 0;
    
    uint8_t         pubKey[256];
    size_t          pubKeyLen = 0;
    
    uint8_t         privKey[256];
    size_t          privKeyLen = 0;
 
    bool isPrivate = false;
    size_t  importKeySize = 0;
    bool    isANSIx963 = false;

    //   uint8_t             tempBuf[256];
    //   unsigned long       tempLen;
    
    
    OPTESTLogInfo("\tECC-%d \n",  keySize);
    
    ECC_ContextRef ecc = kInvalidECC_ContextRef;
    ECC_ContextRef eccPub = kInvalidECC_ContextRef;
    
    // fill PT
    for(i = 0; i< PTsize; i++) PT[i]= i;
    
    err = ECC_Init(&ecc);

    OPTESTLogVerbose("\t\tGenerate Pub Key (%ld bytes)\n", pubKeyLen);
    err = ECC_Generate(ecc, keySize); CKERR;
    
    err =  ECC_Export_ANSI_X963( ecc, pubKey, sizeof(pubKey), &pubKeyLen);CKERR;
    OPTESTLogVerbose("\t\tExport Public Key (%ld bytes)\n", pubKeyLen);
    dumpHex(IF_LOG_DEBUG, pubKey,  (int)pubKeyLen, 0);

    err = ECC_Import_Info( pubKey, pubKeyLen, &isPrivate, &isANSIx963, &importKeySize );CKERR;
    OPTESTLogVerbose("\t\t\t%d bit %s%s key\n", (int)importKeySize , isANSIx963 ?"ANSI x9.63 ":"", isPrivate ?"private":"public");
    
    err =  ECC_Export(ecc, true, privKey, sizeof(privKey), &privKeyLen);CKERR;
    OPTESTLogVerbose("\t\tExport Private Key (%ld bytes)\n", privKeyLen);
    dumpHex(IF_LOG_DEBUG, privKey,  (int)privKeyLen, 0);
    
    err = ECC_Import_Info( privKey, privKeyLen, &isPrivate, &isANSIx963, &importKeySize );CKERR;
    OPTESTLogVerbose("\t\t\t%d bit %s%s key\n", (int)importKeySize , isANSIx963 ?"ANSI x9.63 ":"", isPrivate ?"private":"public");

    // delete keys
    if(ECC_ContextRefIsValid(ecc) ) ECC_Free(ecc );
    ecc = kInvalidECC_ContextRef;
    
    err = ECC_Init(&eccPub);
    err = ECC_Import_ANSI_X963( eccPub, pubKey, pubKeyLen);CKERR;
    
    importKeySize = 0;
    err =  ECC_KeySize(eccPub, &importKeySize);
    OPTESTLogVerbose("\t\tImported %d bit public key\n", (int)importKeySize  );
    
    err = ECC_Encrypt(eccPub, PT, sizeof(PT),  CT, sizeof(CT), &CTlen);CKERR;
    OPTESTLogVerbose("\t\tEncrypt message: (%ld bytes)\n", CTlen);
    dumpHex(IF_LOG_DEBUG, CT,  (int)CTlen, 0);
    
     err = ECC_Init(&ecc);
    err = ECC_Import(ecc, privKey, privKeyLen);CKERR;

    err =  ECC_KeySize(ecc, &importKeySize);
    OPTESTLogVerbose("\t\tImported %d bit private key\n", (int)importKeySize  );
    
    err = ECC_Decrypt(ecc, CT, CTlen,  DT, sizeof(DT), &DTlen); CKERR;
    
    /* check against know-answer */
    err= compareResults( DT, PT, PTsize , kResultFormat_Byte, "ECC Decrypt"); CKERR;
   OPTESTLogVerbose("\t\tDecrypted OK\n");
   dumpHex(IF_LOG_DEBUG, DT,  (int)DTlen, 0);
    
    err = ECC_Sign(ecc, PT, sizeof(PT),  CT, sizeof(CT), &CTlen);CKERR;
    OPTESTLogVerbose("\t\tSigned message (%ld bytes)\n", CTlen);
    dumpHex(IF_LOG_DEBUG, CT,  (int)CTlen, 0);
    
    err = ECC_Verify(ecc, CT, CTlen, PT, sizeof(PT));
    OPTESTLogVerbose("\t\tVerify = %s\n",  IsSCLError(err)?"fail":"pass");
    
    PT[3]= 9;
    err = ECC_Verify(ecc, CT, CTlen, PT, sizeof(PT));
    OPTESTLogVerbose("\t\tVerify bad packet = %s\n",  IsSCLError(err)?"fail":"pass");
    if(err == kSCLError_BadIntegrity) err = kSCLError_NoErr;
    
    OPTESTLogVerbose("\n");
done:
    
    if(ECC_ContextRefIsValid(ecc) ) ECC_Free(ecc );
    if(ECC_ContextRefIsValid(eccPub)) ECC_Free(eccPub);
    
    return err;
    
}
コード例 #4
0
ファイル: ECCtest.c プロジェクト: SilentCircle/silent-text
SCLError sTestECC_DH(int keySize)
{
    
    SCLError     err = kSCLError_NoErr;
    
    uint8_t         pubKey1[256];
    size_t          pubKeyLen1 = 0;
    uint8_t         privKey1[256];
    size_t          privKeyLen1 = 0;

    uint8_t         pubKey2[256];
    size_t          pubKeyLen2 = 0;
    uint8_t         privKey2[256];
    size_t          privKeyLen2 = 0;
  
    
    ECC_ContextRef eccPriv = kInvalidECC_ContextRef;
    ECC_ContextRef eccPub  = kInvalidECC_ContextRef;
    
    uint8_t         Z1       [256];
    size_t          Zlen1;
    uint8_t         Z2       [256];
    size_t          Zlen2;
    
    
    OPTESTLogInfo("\tTesting ECC-DH (%d)\n",  keySize);
    
    /* create keys   */
    OPTESTLogDebug("\t\tGenerate Key 1\n");
    err = ECC_Init(&eccPriv); CKERR;
    err = ECC_Generate(eccPriv, keySize ); CKERR;
  
    err =  ECC_Export_ANSI_X963( eccPriv, pubKey1, sizeof(pubKey1), &pubKeyLen1);CKERR;
    err =  ECC_Export(eccPriv, true, privKey1, sizeof(privKey1), &privKeyLen1);CKERR;
    
    OPTESTLogDebug("\t\tKey 1 Pub/Priv  (%ld,%ld) bytes\n", pubKeyLen1, privKeyLen1);
    OPTESTLogDebug("\t\tPublic\n");
    dumpHex(IF_LOG_DEBUG, pubKey1,  (int)pubKeyLen1, 0);
    OPTESTLogDebug("\t\tPrivate\n");
    dumpHex(IF_LOG_DEBUG, privKey1,  (int)privKeyLen1, 0);
    OPTESTLogDebug("\n");
    
    if(ECC_ContextRefIsValid(eccPriv) ) ECC_Free(eccPriv );
    eccPriv = kInvalidECC_ContextRef;

    
    OPTESTLogDebug("\t\tGenerate Key 2\n");
    err = ECC_Init(&eccPriv); CKERR;
    err = ECC_Generate(eccPriv, keySize ); CKERR;
    
    err =  ECC_Export_ANSI_X963( eccPriv, pubKey2, sizeof(pubKey2), &pubKeyLen2);CKERR;
    err =  ECC_Export(eccPriv, true, privKey2, sizeof(privKey2), &privKeyLen2);CKERR;
    
    OPTESTLogDebug("\t\tKey 2 Pub/Priv  (%ld,%ld) bytes\n", pubKeyLen2, privKeyLen2);
    OPTESTLogDebug("\t\tPublic\n");
    dumpHex(IF_LOG_DEBUG, pubKey2,  (int)pubKeyLen2, 0);
    OPTESTLogDebug("\t\tPrivate\n");
    dumpHex(IF_LOG_DEBUG, privKey2,  (int)privKeyLen2, 0);
    OPTESTLogDebug("\n");
    
    // delete keys
   if(ECC_ContextRefIsValid(eccPriv) ) ECC_Free(eccPriv );
    eccPriv = kInvalidECC_ContextRef;
    
    OPTESTLogDebug("\t\tCalculate Secret for Key1 -> Key2\n");
    err = ECC_Init(&eccPriv);
    err = ECC_Import(eccPriv, privKey1, privKeyLen1);CKERR;
 
    err = ECC_Init(&eccPub);
    err = ECC_Import_ANSI_X963( eccPub, pubKey2, pubKeyLen2);CKERR;
    
    /* Kdk = MAC(Htotal,Z)    where Z is the DH of Pki and PKr */
    Zlen1 = sizeof(Z1);
     err = ECC_SharedSecret(eccPriv, eccPub, Z1, sizeof(Z1), &Zlen1);CKERR;

    OPTESTLogVerbose("\t\tECC Shared Secret (Z1):  (%ld bytes)\n",Zlen1);
    dumpHex(IF_LOG_DEBUG, Z1,  (int)Zlen1 , 0);
    OPTESTLogDebug("\n");

    
    // delete keys
    if(ECC_ContextRefIsValid(eccPriv) ) ECC_Free(eccPriv );
    eccPriv = kInvalidECC_ContextRef;
    // delete keys
    if(ECC_ContextRefIsValid(eccPub) ) ECC_Free(eccPub );
    eccPub = kInvalidECC_ContextRef;
    
    OPTESTLogDebug("\t\tCalculate Secret for Key2 -> Key1\n");
    err = ECC_Init(&eccPriv);
    err = ECC_Import(eccPriv, privKey2, privKeyLen2);CKERR;
    
    err = ECC_Init(&eccPub);
    err = ECC_Import_ANSI_X963( eccPub, pubKey1, pubKeyLen1);CKERR;
    
    /* Kdk = MAC(Htotal,Z)    where Z is the DH of Pki and PKr */
    Zlen2 = sizeof(Z2);
    err = ECC_SharedSecret(eccPriv, eccPub, Z2, sizeof(Z2), &Zlen2);CKERR;
    
    OPTESTLogVerbose("\t\tECC Shared Secret (Z2):  (%ld bytes)\n",Zlen2);
    dumpHex(IF_LOG_DEBUG, Z2,  (int)Zlen2 , 0);
    OPTESTLogDebug("\n");
    
    OPTESTLogVerbose("\t\tCompare Secrets\n");
   err = compare2Results(Z1, Zlen1, Z2, Zlen2, kResultFormat_Byte, "ECC Shared Secret");CKERR;
    
    
done:
    if(eccPriv)
    {
        ECC_Free(eccPriv);
        eccPriv = kInvalidECC_ContextRef;
    }
    
    if(eccPub)
    {
        ECC_Free(eccPub);
        eccPub = kInvalidECC_ContextRef;
    }
    
    
    return err;
    
}
コード例 #5
0
ファイル: SCKeysTest.c プロジェクト: SilentCircle/silent-text
static SCLError sSCKeyTest2(SCKeySuite keySuite, SCKeySuite lockingKeySuite)
{
    SCLError        err = kSCLError_NoErr;
    SCKeyContextRef sKey = kInvalidSCKeyContextRef;
    SCKeyContextRef key1 = kInvalidSCKeyContextRef;
    SCKeyContextRef key2 = kInvalidSCKeyContextRef;
    SCKeyContextRef signKey = kInvalidSCKeyContextRef;
    ECC_ContextRef  ecc = kInvalidECC_ContextRef;
    SCKeySuite      keyType;
    SCKeySuite      keyType1;
    SCKeyPropertyType propType;
    
    Cipher_Algorithm    algorithm = kCipher_Algorithm_Invalid;
    size_t              symKeyLen = 0;
    
    uint8_t*        keyData = NULL;
    size_t          keyDataLen = 0;
    uint8_t*        keyData1 = NULL;
    size_t          keyDataLen1 = 0;
    size_t          ecckeysize = 0;
    uint8_t         symKey[32];
    uint8_t         IV[32];
    
    char*           user1 = "*****@*****.**";
    char*           user2 = "*****@*****.**";
    
    char            comment[256] = {0};
    char*           nonce = "some stupid nonce data";
    time_t          startDate  = time(NULL) ;
    time_t          expireDate  = startDate + (3600 * 24);
    time_t          date1;
    char*           commentProperty = "comment";
    bool            isLocked = true;
   
    sprintf(comment,"Optest %s ECC Key", sckey_suite_table(keySuite));
    OPTESTLogInfo("\tTesting %s Key Save/Restore using %s\n",
                  sckey_suite_table(keySuite),sckey_suite_table(lockingKeySuite));
   
    switch (keySuite) {
        case kSCKeySuite_ECC384:
            ecckeysize = 384;
            break;
            
        case kSCKeySuite_ECC414:
            ecckeysize = 414;
            break;
            
        default:
            RETERR(kSCLError_BadParams);
            break;
    }
    
    // fixed SYM key test
    for(int i = 0; i< sizeof(symKey); i++) symKey[i] = i;
    for(int i = 0; i< sizeof(IV); i++) IV[i] = i;

    // create a SCKey with sym data
    err = SCKeyImport_Symmetric(lockingKeySuite, symKey, (uint8_t*)nonce, strlen(nonce), &sKey); CKERR;
    err = SCKeyCipherForKeySuite(lockingKeySuite, &algorithm, &symKeyLen);  CKERR;
    err = SCKeySetProperty (sKey, kSCKeyProp_IV, SCKeyPropertyType_Binary,  IV, symKeyLen ); CKERR;
  
    OPTESTLogVerbose("\t\tGenerate %d bit ECC key \n", ecckeysize);
    err = ECC_Init(&ecc);
    err = ECC_Generate(ecc, ecckeysize); CKERR;
    
    OPTESTLogVerbose("\t\tImport %d bit ECC key to SCKEy \n", ecckeysize);
    err = SCKeyImport_ECC( ecc, (uint8_t*)nonce, strlen(nonce),  &key1); CKERR;
    ECC_Free(ecc );
    ecc = kInvalidECC_ContextRef;
 
    err = SCKeySetProperty (key1, commentProperty, SCKeyPropertyType_UTF8String, comment, strlen(comment) ); CKERR;
    err = SCKeySetProperty(key1, kSCKeyProp_StartDate,  SCKeyPropertyType_Time ,  &startDate, sizeof(time_t)); CKERR;
    err = SCKeySetProperty(key1, kSCKeyProp_ExpireDate,  SCKeyPropertyType_Time ,  &expireDate, sizeof(time_t)); CKERR;
    err = SCKeySetProperty (key1, kSCKeyProp_Owner, SCKeyPropertyType_UTF8String, user1, strlen(user1) ); CKERR;
  
    // create a signing key to sign test wkey with
    err = SCKeyNew(keySuite, (uint8_t*)nonce, strlen(nonce),  &signKey); CKERR;
    err = SCKeySetProperty (signKey, kSCKeyProp_Owner, SCKeyPropertyType_UTF8String, user2 , strlen(user2) ); CKERR;
    err = SCKeySignKey( signKey, key1,NULL); CKERR;
    
    // serialize it
    err = SCKeySerialize(key1, &keyData, &keyDataLen); CKERR;
    OPTESTLogDebug("\t\tPublic Key Packet: (%ld bytes)\n%s\n",keyDataLen, (char*)keyData);
    XFREE(keyData); keyData = NULL;

    err = SCKeySerializePrivateWithSCKey(key1, sKey, &keyData, &keyDataLen); CKERR;
    OPTESTLogDebug("\t\tPrivate Key Packet: (%ld bytes)\n%s\n",keyDataLen, (char*)keyData);
    
    OPTESTLogVerbose("\t\tImport Private Key Packet \n" );
    err = SCKeyDeserialize(keyData,  keyDataLen, &key2);CKERR;
    XFREE(keyData); keyData = NULL;
  
    OPTESTLogVerbose("\t\tUnlock Private Key Packet \n" );
    err = SCKeyIsLocked(key2,&isLocked); CKERR;
     // the freshly iported key should be locked
    ASSERTERR(!isLocked, kSCLError_SelfTestFailed);
    err = SCKeyUnlockWithSCKey(key2, sKey); CKERR;
  
    // check reimport
    OPTESTLogVerbose("\t\tCompare Deserialized Key \n" );
    err = SCKeyGetProperty(key2, kSCKeyProp_SCKeySuite,   NULL,  &keyType1, sizeof(SCKeySuite),  NULL); CKERR;
    err = compareResults(&keyType, &keyType, sizeof(SCKeySuite), kResultFormat_Byte, "keySuite");CKERR;
    
    err = SCKeyGetAllocatedProperty(key1, kSCKeyProp_Locator,NULL,  (void*)&keyData ,  &keyDataLen); CKERR;
    err = SCKeyGetAllocatedProperty(key2, kSCKeyProp_Locator,&propType,  (void*)&keyData1 ,  &keyDataLen1); CKERR;
    ASSERTERR(propType != SCKeyPropertyType_Binary,  kSCLError_SelfTestFailed);
    err = compareResults(keyData, keyData1, keyDataLen, kResultFormat_Byte, "locator");CKERR;
    XFREE(keyData); keyData = NULL;
    XFREE(keyData1); keyData1 = NULL;
    
    err = SCKeyGetProperty(key2, kSCKeyProp_StartDate,   &propType,  &date1, sizeof(time_t),  &keyDataLen); CKERR;
    ASSERTERR(propType != SCKeyPropertyType_Time,  kSCLError_SelfTestFailed);
    err = compareResults(&startDate, &date1, sizeof(time_t), kResultFormat_Byte, "startDate");CKERR;
    
    err = SCKeyGetProperty(key2, kSCKeyProp_ExpireDate,   &propType,  &date1, sizeof(time_t),  &keyDataLen); CKERR;
    ASSERTERR(propType != SCKeyPropertyType_Time,  kSCLError_SelfTestFailed);
    err = compareResults(&expireDate, &date1, sizeof(time_t), kResultFormat_Byte, "expireDate");CKERR;
 
// TODO: we need to add code in SCKeys to xport the key signatures and verify them
    
    OPTESTLogVerbose(" \n" );
    
done:
    if(ECC_ContextRefIsValid(ecc) )
        ECC_Free(ecc );
    
    if(SCKeyContextRefIsValid(key1))
        SCKeyFree(key1);
    
    if(SCKeyContextRefIsValid(key2))
        SCKeyFree(key2);

    if(keyData)
        XFREE(keyData);

    if(keyData1)
        XFREE(keyData1);

    return err;
};
コード例 #6
0
ファイル: PKtest.c プロジェクト: ioerror/silent-text
int TestECC_DH()
{
    int     err = CRYPT_OK;
    
    ECC_ContextRef  key1 = NULL;
    ECC_ContextRef  key2 = NULL;
  
    uint8_t        keyBuf[512];
    unsigned long  keyLen = 0;

    uint8_t         Z       [256];
    unsigned long   x;
    int i;

#define ECC_KEY_SIZE 384
    
    printf("\nTesting ECC-DH (%d)\n\n", ECC_KEY_SIZE);

    for(i = 0; i <1; i++)
    {
        printf("----------Key Set %d ----------\n\n", i);
        /* create keys   */
        err = ECC_Init(&key1); CKERR;
        err = ECC_Generate(key1, ECC_KEY_SIZE ); CKERR;
        
        err = ECC_Init(&key2); CKERR;
        err = ECC_Generate(key2, ECC_KEY_SIZE ); CKERR;
        
        
        printf("Key1  \n");
        err = ECC_Export(key1, false, keyBuf, sizeof(keyBuf), &keyLen); CKERR;
        printf("\tPub Key (%ld bytes)\n", keyLen);
        dumpHex(keyBuf,  keyLen,0);
        
        err = ECC_Export(key1, true, keyBuf, sizeof(keyBuf), &keyLen); CKERR;
        printf("\n\tPriv Key (%ld bytes)\n", keyLen);
        dumpHex(keyBuf,  keyLen,0);
        
        printf("\nKey2  \n");
        err = ECC_Export(key2, false, keyBuf, sizeof(keyBuf), &keyLen); CKERR;
        printf("\tPub Key (%ld bytes)\n", keyLen);
        dumpHex(keyBuf,  keyLen,0);
        
        /* Kdk = MAC(Htotal,Z)    where Z is the DH of Pki and PKr */
        x = sizeof(Z); 
        
        err = ECC_SharedSecret(key1, key2, Z, sizeof(Z), &x);
        /* at this point we dont need the ECC keys anymore. Clear them */
        
        printf("\nECC Shared Secret (Z):  (%ld bytes)\n",x);
        dumpHex(Z,  x , 0);
        printf("\n");
        
        if(key1)
        {
            ECC_Free(key1);
            key1 = kInvalidECC_ContextRef;
        }
        
        if(key2)
        {
            ECC_Free(key2);
            key2 = kInvalidECC_ContextRef;
        }
        
    }
done:
    
    if(key1)
    {
        ECC_Free(key1);
        key1 = kInvalidECC_ContextRef;
    }
    
    if(key2)
    {
        ECC_Free(key2);
        key2 = kInvalidECC_ContextRef;
    }
    
    return err;
    
}
コード例 #7
0
ファイル: fmd.c プロジェクト: zizilala/projects_wince_new
//-----------------------------------------------------------------------------
HANDLE 
NAND_Initialize(
    LPCTSTR szContext,
    PCI_REG_INFO *pRegIn,
    PCI_REG_INFO *pRegOut
    )
{
    DWORD chipSelect = BSPGetNandCS();
    const NAND_INFO *  pBSPNandInfo;
    HANDLE hDevice = NULL;
    UINT ffPrefetchMode = 0;
    UINT8 manufacturer, device;
    NandDevice_t *pDevice = &s_Device;
#ifndef BOOT_MODE    
    DWORD dwKernelRet;
#endif

    UNREFERENCED_PARAMETER(pRegOut);
    UNREFERENCED_PARAMETER(szContext);
    // initialize structure
    memset(pDevice, 0, sizeof(NandDevice_t));

#ifdef BOOT_MODE    
    pDevice->pGpmcRegs = (OMAP_GPMC_REGS*)OALPAtoUA(SOCGetGPMCAddress(0));
    pDevice->pFifo = (NANDREG*)OALPAtoUA(pRegIn->MemBase.Reg[0]);

    /* Get ECC mode from BootCfg */
    pDevice->ECCtype = g_ecctype;
    if((pDevice->ECCtype > BCH8bit) || (pDevice->ECCtype < Hamming1bit))
    	{
            pDevice->ECCtype = Hamming1bit;
            RETAILMSG(TRUE, (L"Incorrect ECC type setting\r\n"));			
    	}
#else    
    if (szContext != NULL)
	{
        if (InitializePointers(szContext, pDevice) == FALSE) goto cleanUp;
	}
    else
	{
        PHYSICAL_ADDRESS pa;
        
        // if there's not context string then use global macros

        pa.QuadPart = pRegIn->MemBase.Reg[0];
        pDevice->memLen[0] = pRegIn->MemLen.Reg[0];
        pDevice->pGpmcRegs = MmMapIoSpace(pa, pDevice->memLen[0], FALSE);
        if (pDevice->pGpmcRegs == NULL) goto cleanUp;

        pa.QuadPart = pRegIn->MemBase.Reg[1];
        pDevice->memLen[1] = pRegIn->MemLen.Reg[1];
        pDevice->pFifo = MmMapIoSpace(pa, pDevice->memLen[1], FALSE);
        if (pDevice->pGpmcRegs == NULL) goto cleanUp;

	}

	if (!KernelIoControl(IOCTL_HAL_GET_ECC_TYPE, NULL, 0, &pDevice->ECCtype, sizeof(DWORD), &dwKernelRet))
	{
		RETAILMSG( TRUE,(TEXT("Failed to read Ecc type\r\n")));
		pDevice->ECCtype = Hamming1bit;
	}   
	
	RETAILMSG(TRUE, (L"ECC TYPE is %s\r\n", (pDevice->ECCtype==Hamming1bit)? L"Hamming 1 bit" :
									(pDevice->ECCtype==BCH4bit)? L"BCH 4 bit" : L"BCH 8 bit"));
	
#endif

    pDevice->pNandCmd = (NANDREG*)((UINT32)pDevice->pGpmcRegs + offset(OMAP_GPMC_REGS, GPMC_NAND_COMMAND_0) + (0x30 * chipSelect));
    pDevice->pNandAddress= (NANDREG*)((UINT32)pDevice->pGpmcRegs + offset(OMAP_GPMC_REGS, GPMC_NAND_ADDRESS_0) + (0x30 * chipSelect));
    pDevice->pNandData= (NANDREG*)((UINT32)pDevice->pGpmcRegs + offset(OMAP_GPMC_REGS, GPMC_NAND_DATA_0) + (0x30 * chipSelect));
    // Enable GPMC wait-to-nowait edge detection mechanism on NAND R/B pin
    NAND_Enable(pDevice, TRUE);

    // Write RESET command
    // (a reset aborts any current READ, WRITE (PROGRAM) or ERASE operation)
    NAND_SendCommand(pDevice, NAND_CMD_RESET);

    // Wait for NAND
    while ((NAND_GetStatus(pDevice) & NAND_STATUS_READY) == 0);

    // Send Read ID Command
    NAND_SendCommand(pDevice, NAND_CMD_READID);

    // Send Address 00h
    WRITE_NAND(pDevice->pNandAddress, 0);

    // Read the manufacturer ID & device code
    manufacturer = (UINT8)READ_NAND(pDevice->pNandData);
    device = (UINT8)READ_NAND(pDevice->pNandData);

 
    if ((pBSPNandInfo = BSPGetNandInfo(manufacturer,device))==NULL)
    {                
        goto cleanUp;
    }
    if ((pBSPNandInfo->sectorSize != 2048) && (pBSPNandInfo->wordData != 2))
    {
        ERRORMSG(1,(TEXT("FMD driver supports only 16bits large page (2KB) devices\r\n")));
        goto cleanUp;
    }
    pDevice->nandInfo = *pBSPNandInfo;

    pDevice->IrqWait = BSPGetNandIrqWait();

    /* ECCCfg: 16bit bus width, cs0, 4 - 512 bytes blocks per page */
    pDevice->ECCCfg = (GPMC_ECC_CONFIG_16BIT | (chipSelect << 1) | (0x3<<4)); 
    pDevice->ECCsize = (pDevice->ECCtype == Hamming1bit ) ? ECC_BYTES_HAMMING : 
		                      (pDevice->ECCtype == BCH4bit ) ? ECC_BYTES_BCH4 : ECC_BYTES_BCH8; 
							  
    //  Enable and reset ECC engine (workaround for engine giving 0s first time)
    ECC_Init(pDevice->pGpmcRegs, pDevice->ECCCfg, pDevice->ECCtype, NAND_ECC_READ);
    ECC_Reset(pDevice->pGpmcRegs);

    //  Only enable during NAND read/write/erase operations
    NAND_Enable(pDevice, FALSE);

    // configure the prefetch engine
    pDevice->prefetchMode = kPrefetchOff;
    OUTREG32(&pDevice->pGpmcRegs->GPMC_PREFETCH_CONTROL, 0);

    // set prefetch mask
    ffPrefetchMode = GPMC_PREFETCH_CONFIG_SYNCHROMODE |
                     GPMC_PREFETCH_CONFIG_PFPWENROUNDROBIN |
                     GPMC_PREFETCH_CONFIG_ENABLEOPTIMIZEDACCESS |
                     GPMC_PREFETCH_CONFIG_WAITPINSELECTOR(chipSelect) |
                     GPMC_PREFETCH_CONFIG_FIFOTHRESHOLD(FIFO_THRESHOLD) |
                     GPMC_PREFETCH_CONFIG_ENGINECSSELECTOR(chipSelect);

    OUTREG32(&pDevice->pGpmcRegs->GPMC_PREFETCH_CONFIG1, ffPrefetchMode);

    // configure prefetch engine
    OUTREG32(&pDevice->pGpmcRegs->GPMC_PREFETCH_CONFIG2, 
        pBSPNandInfo->sectorSize
        );
        
    SETREG32(&pDevice->pGpmcRegs->GPMC_PREFETCH_CONFIG1, 
        GPMC_PREFETCH_CONFIG_ENABLEENGINE
        );

    // We are done
    hDevice = pDevice;

cleanUp:
    return hDevice;
}