3DES Encryption

I am looking for sample code on how to encrypt a string with 3DES. I am using the following code that I got from the NEX_GenCRYPT_programming_guide.pdf:

void encryptdata(char *key1, char *key2, char *key3,
char *in, char *out, int len)
{
NGCkey64 convkey;
NGCdesctx ks1, ks2, ks3;
int i;

/* initialise context /
ngcDES_ConvKey((NGCkey56
)key1, &convkey);
ngcDES_Init(&ks1, &convkey,0);
ngcDES_ConvKey((NGCkey56*)key2, &convkey);
ngcDES_Init(&ks2, &convkey,0);
ngcDES_ConvKey((NGCkey56*)key3, &convkey);
ngcDES_Init(&ks3, &convkey,0);

/* encrypt data (assuming len is multiple of 8) */
for (i=0; i<len; i+=8)
{
ngc3DES_ECB_Crypt(&ks1, &ks2, &ks3, in+i, out+i, NGC_DES_ENCRYPT);

}
}
void decryptdata(char *key1, char *key2, char *key3,
char *in, char *out, int len)
{
NGCkey64 convkey;
NGCdesctx ks1, ks2, ks3;
int i;

/* initialise context /
ngcDES_ConvKey((NGCkey56
)key1, &convkey);
ngcDES_Init(&ks1, &convkey,0);
ngcDES_ConvKey((NGCkey56*)key2, &convkey);
ngcDES_Init(&ks2, &convkey,0);
ngcDES_ConvKey((NGCkey56*)key3, &convkey);
ngcDES_Init(&ks3, &convkey,0);

/* decrypt data (assuming len is multiple of 8) */
for (i=0; i<len; i+=8)
ngc3DES_ECB_Crypt(&ks1, &ks2, &ks3, in+i, out+i, NGC_DES_DECRYPT);
}

void adl_main ( adl_InitType_e InitType )
{
TRACE (( 1, “Embedded Application : Main” ));

char *out_1;
char *in_1;  

in_1 = "hello123";
 encryptdata("getBla92","geteee92","getrrr92",in_1,out_1,8);
adl_atSendResponse ( ADL_AT_UNS, out_1);

}

When I run the program on the Q2686 and start the application it runs up to this line:
/* encrypt data (assuming len is multiple of 8) */
for (i=0; i<len; i+=8)
{
ngc3DES_ECB_Crypt(&ks1, &ks2, &ks3, in+i, out+i, NGC_DES_ENCRYPT);

}
It then restarts, this then continues.
Can anyone help my on how to use 3DES, by telling me what I am doing wrong or giving me another example. The security plug in is enabled on the hardware.

Riaan Pieters
mail@cellremote.co.za

Encryption functions can take a long time - it the watchdog biting…?

Hi, thanks for the reply, would really like to sort this one out!
I inserted the following line to eliminate the watchdog factor:

adl_wdPut2Sleep (ADL_TMR_S_TO_TICK(20));

But still the same problem occurs, any other advice I can try?

Hiya,

In your adl_main(), call

adl_InitGetType()

to find out why the CPU restarted.

You may find that your watchdog delay is not long enough. As awneil notes, encryption functions can take a long time…

Another option is to try and factor your code and run it a bit at a time (using a timer and state machine). This will let the main Radio firmware have a chance to run and keep the watchdog happy.

ciao, Dave