Function returning wrong value in lpc824

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Function returning wrong value in lpc824

502 次查看
johnygeorgemala
Contributor I

I am working on porting an application running on `Arduino Mega` to `LPC824`.

The following piece of code is working differently for both the platforms.

/** * Calculation of CMAC */ void cmac(const uint8_t* data, uint8_t dataLength) { uint8_t trailer[1] = {0x80}; uint8_t bytes[_lenRnd]; uint8_t temp[_lenRnd]; memcpy(temp, data, dataLength); concatArray(temp, dataLength, trailer, 1); dataLength ++; addPadding(temp, dataLength); memcpy(bytes, _sk2, _lenRnd); xorBytes(bytes,temp,_lenRnd); aes128_ctx_t ctx; aes128_init(_sessionkey, &ctx); uint8_t* chain = aes128_enc_sendMode(bytes, _lenRnd, &ctx, _ivect); Board_UARTPutSTR("chain\n\r"); printBytes(chain, 16, true); memcpy(_ivect, chain, _lenRnd); //memcpy(_ivect, aes128_enc_sendMode(bytes,_lenRnd,&ctx,_ivect), _lenRnd); memcpy(_cmac,_ivect, _lenRnd); Board_UARTPutSTR("Initialization vector\n\r"); printBytes(_ivect, 16, true); }

I am expecting a value like `{0x5d, 0xa8, 0x0f, 0x1f, 0x1c, 0x03, 0x7f, 0x16, 0x7e, 0xe5, 0xfd,

0xf3, 0x45, 0xb7, 0x73, 0xa2}` for the `chain` variable.

But the follow function is working differently. The print inside the function has

the correct value which I want `({5d, 0xa8, 0x0f, 0x1f, 0x1c, 0x03, 0x7f, 0x16,

0x7e, 0xe5, 0xfd, 0xf3, 0x45, 0xb7, 0x73, 0xa2})`.

But when the function returns `chain` is having a different value, compared to what I

am expecting, I get the following value for `chain`

`{0x00, 0x20, 0x00, 0x10, 0x03, 0x01, 0x00, 0x00, 0xd5, 0x00, 0x00, 0x00, 0xd7, 0x00, 0x00, 0x00}`

Inside the function, the result is correct. But it returns a wrong value to the

function which called it. Why is it happening so ?

uint8_t* aes128_enc_sendMode(unsigned char* data, unsigned short len, aes128_ctx_t* key, const unsigned char* iv) { unsigned char tmp[16]; uint8_t chain[16]; unsigned char c; unsigned char i; memcpy(chain, iv, 16); while (len >= 16) { memcpy(tmp, data, 16); //xorBytes(tmp,chain,16); for (i = 0; i < 16; i++) { tmp[i] = tmp[i] ^ chain[i]; } aes128_enc(tmp, key); for (i = 0; i < 16; i++) { //c = data[i]; data[i] = tmp[i]; chain[i] = tmp[i]; } len -= 16; data += 16; } Board_UARTPutSTR("Chain!!!:"); printBytes(chain, 16, true); return chain; }

标签 (1)
标记 (2)
0 项奖励
1 回复

367 次查看
lpcxpresso_supp
NXP Employee
NXP Employee

So you appear to be trying to return a pointer to a local array (so on the stack). Thus once you have returned from this function,  those values could very easily be overwritten by anything else using the stack (for instance an interrupt being triggered).

Regards,

LPCXpresso Support

0 项奖励