Problem with RSA encryption


#1

Greetings.

I´m trying to calculate the RSA signature with the following expression:

s = m^d mod n

So, d and n are 1024 bits numbers, they are bignums.

I´m using two aproaches for doing this calculations:

  1. Function ngcBNModExp.
  2. Function ngcRSA_mod_exp.

But for both functions i´m having an ARM Data Access exception, and a WatchDog reset. I´m using Q2687G module with Security Lib version 1.5.

The following is the data and the code I´m using for the first function. For the second function the code is similar but look at this first:

unsigned char n[] = { 0xE7, 0x00, 0x49, 0x42, 0xC2, 0x5E,
		0xBB, 0x8B, 0x93, 0x0D, 0x23, 0xE8, 0x7A, 0xBD, 0xEF, 0x10, 0xD6, 0xBA,
		0xCF, 0x93, 0x2D, 0x46, 0xB1, 0x36, 0xF3, 0x51, 0x32, 0xBF, 0x87, 0xED,
		0x5B, 0x7A, 0x0A, 0x63, 0x81, 0xA8, 0xA8, 0x46, 0x1F, 0xA7, 0x68, 0xBE,
		0x25, 0x16, 0x49, 0xD9, 0x99, 0x91, 0xEA, 0x28, 0xFC, 0xCD, 0x5F, 0x62,
		0x79, 0x5D, 0x26, 0x86, 0x34, 0xE3, 0x30, 0x96, 0x8D, 0x1C, 0x52, 0xA8,
		0xC1, 0x4B, 0x14, 0xDD, 0xFD, 0x34, 0x1D, 0x0B, 0x92, 0xC5, 0xD6, 0x3C,
		0xBF, 0xE8, 0xCE, 0x30, 0xBE, 0x0F, 0xD0, 0xB0, 0xB4, 0x06, 0xAD, 0x38,
		0xD3, 0x4B, 0x7E, 0x2C, 0x6D, 0x65, 0xB1, 0x39, 0x51, 0xF9, 0x93, 0x47,
		0x21, 0xEE, 0x9C, 0xDA, 0xBB, 0xD8, 0x3D, 0x4D, 0x0B, 0x3B, 0x38, 0x0F,
		0x39, 0x17, 0x2A, 0x63, 0x41, 0x58, 0x9D, 0x9B, 0xA3, 0x62, 0x3F, 0x04,
		0x6D, 0x0B, 0x1B, 0xD1, 0x6A, 0x3B, 0xF6, 0x83, 0xA3, 0xC6, 0x80, 0x7D,
		0xF0, 0xF1, 0xDF, 0x1F, 0x2F, 0x62, 0x06, 0xA2, 0xFE, 0xA4, 0x9C, 0xD8,
		0xDB, 0x45, 0x0E, 0x88, 0x75, 0x13, 0xDA, 0x04, 0x7A, 0x66, 0x84, 0xEB,
		0x31, 0x52, 0x46, 0x75, 0xE8, 0xE1, 0xDB, 0xF1, 0x08, 0x3C, 0xF6, 0x72,
		0xA6, 0x38, 0x21, 0x5D, 0x2A, 0x0C, 0x33, 0x08, 0x44, 0x54, 0x24, 0xC7,
		0x4F, 0xC2, 0xE5, 0x50, 0xA3, 0x53, 0x96, 0x87, 0x51, 0x7C, 0x18, 0xFA,
		0xF7, 0xD5, 0x8A, 0xB7, 0xFA, 0x74, 0x4A, 0x00, 0xE8, 0xB0, 0x73, 0x17,
		0x67, 0xC5, 0x2A, 0xC8, 0xFE, 0x7B, 0x5D, 0x7E, 0xAD, 0x63, 0x29, 0x21,
		0x60, 0x7D, 0xA0, 0x9D, 0xE9, 0x36, 0x63, 0xE5, 0xA9, 0x92, 0x36, 0x9F,
		0x87, 0xF3, 0xFB, 0xEF, 0xA0, 0x77, 0x57, 0x89, 0x71, 0xC0, 0x9F, 0xBC,
		0x6E, 0x55, 0x09, 0x9E, 0x8D, 0xC0, 0x8C, 0x5B, 0xFD, 0x2D };

unsigned char d[] = { 0xDC, 0x08, 0x38, 0xC0, 0x3F, 0x56,
		0x6E, 0xFC, 0x06, 0xE9, 0x18, 0x50, 0x04, 0x42, 0x50, 0xAA, 0x50, 0x6F,
		0x06, 0xF3, 0xA0, 0xC7, 0xDA, 0x5B, 0x86, 0x2E, 0xCC, 0xD1, 0x05, 0x8F,
		0x80, 0x77, 0xE1, 0xD7, 0x60, 0x94, 0x9F, 0x1C, 0x94, 0x60, 0x28, 0xF4,
		0x1D, 0x6D, 0x7A, 0x90, 0xAB, 0xA8, 0x02, 0x4C, 0x62, 0x68, 0xD9, 0x37,
		0x7A, 0x74, 0xD1, 0x12, 0x3A, 0xE4, 0x40, 0xCE, 0xD6, 0xC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0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x30, 0x21,
		0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04,
		0x14, 0x2E, 0x98, 0x6D, 0x96, 0xCF, 0x38, 0x82, 0x6F, 0x8B, 0x0E, 0x8C,
		0xC0, 0x41, 0x29, 0x21, 0x19, 0x2A, 0x8E, 0xC4, 0x50 };

NGCbnctx *ctx;
	ctx = ngcBNCtxNew((void *) NULL);

	if (ctx == NULL)
		TRACE((DS_ERROR_TRACE_LEVEL, "Context is null!" ));

    TRACE((DS_SUCCESS_TRACE_LEVEL, "RSA test" ));

    NGCbignum * pre_encry = ngcBNNew ( ctx->heap );
    NGCbignum * _n = ngcBNNew ( ctx->heap );
    NGCbignum * _d = ngcBNNew ( ctx->heap );

    NGCbignum * _s = ngcBNNew ( ctx->heap );

    if (pre_encry == NULL ||
    		_n == NULL ||
    		_d == NULL ||
    		_s == NULL )
    		TRACE((DS_ERROR_TRACE_LEVEL, "All or more than one bignum variable is null!" ));

    pre_encry = ngcBNbin2bn ( block,  sizeof(block),  pre_encry );
	_n = ngcBNbin2bn ( n,  sizeof ( n ),  _n );
	_d = ngcBNbin2bn ( d,  sizeof ( d ),  _d );

	TRACE((DS_SUCCESS_TRACE_LEVEL, "Loaded RSA variables!" ));

	TRACE((DS_SUCCESS_TRACE_LEVEL, "ngcBNModExp ..."));
	ngcBNModExp(_s, pre_encry, _d, _n, ctx);
	TRACE((DS_SUCCESS_TRACE_LEVEL, "Done!"));

Any help would be really appreciated.

Thanks in advance,

Sebastián Villa
SYTECSA


#2

Hi,

Some “security” functions need a lot of CPU resources.

This is the case with ngcBNModExp function: in your case it takes more than 40s to compute the result (on Q2687 running at 104MHz).

But if an Open AT task is using the CPU more than 8 seconds then a watchdog reset occurs.

So, for such functions you need to do 2 things:

  • switch the CPU speed from NORMAL (26Mhz) to BOOST (104MHz) --> this will increase the computation speed.
  • delay the watchdog reset to 60 seconds (or more) --> this will forbid the watchdog reset.

Nota: if you are doing your heavy computations in the main Open AT task then AT commands will be not available during the computation.

s32 VariSpeedHandle, TreatmentDuration;

VariSpeedHandle = adl_vsSubscribe();

...

adl_wdPut2Sleep(ADL_TMR_S_TO_TICK(60));
adl_vsSetClockMode ( VariSpeedHandle, ADL_VS_MODE_BOOST );
ngcBNModExp(_s, pre_encry, _d, _n, ctx);
adl_vsSetClockMode ( VariSpeedHandle, ADL_VS_MODE_STANDARD );
TreatmentDuration = adl_wdAwake();
TRACE((1, "Done in %d ticks!", ADL_TMR_S_TO_TICK(60) - TreatmentDuration));

Regards,

JayM2M