<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>LPC MicrocontrollersのトピックRe: Function returning wrong value in lpc824</title>
    <link>https://community.nxp.com/t5/LPC-Microcontrollers/Function-returning-wrong-value-in-lpc824/m-p/641633#M25375</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So you appear to be trying to&amp;nbsp;return a pointer to a local array (so on the stack). Thus once you have returned from&amp;nbsp;this function, &amp;nbsp;those values could very easily be overwritten by anything else using the stack (for instance an interrupt being triggered).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;LPCXpresso Support&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 04 Apr 2017 07:26:28 GMT</pubDate>
    <dc:creator>lpcxpresso_supp</dc:creator>
    <dc:date>2017-04-04T07:26:28Z</dc:date>
    <item>
      <title>Function returning wrong value in lpc824</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Function-returning-wrong-value-in-lpc824/m-p/641632#M25374</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="" style="color: #393318; border: 0px; font-size: 13px; margin: 0px 0px 1em; padding: 5px;"&gt;&lt;P&gt;I am working on porting an application running on `Arduino Mega` to `LPC824`. &lt;/P&gt;&lt;P&gt;The following piece of code is working differently for both the platforms.&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;/**
 * 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, &amp;amp;ctx);

 uint8_t* chain = aes128_enc_sendMode(bytes, _lenRnd, &amp;amp;ctx, _ivect);
 Board_UARTPutSTR("chain\n\r");
 printBytes(chain, 16, true);

 memcpy(_ivect, chain, _lenRnd);

 //memcpy(_ivect, aes128_enc_sendMode(bytes,_lenRnd,&amp;amp;ctx,_ivect), _lenRnd);

 memcpy(_cmac,_ivect, _lenRnd);

 Board_UARTPutSTR("Initialization vector\n\r");
 printBytes(_ivect, 16, true);
 }&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I am expecting a value like `{0x5d, 0xa8, 0x0f, 0x1f, 0x1c, 0x03, 0x7f, 0x16, 0x7e, 0xe5, 0xfd,&lt;/P&gt;&lt;P&gt; 0xf3, 0x45, 0xb7, 0x73, 0xa2}` for the `chain` variable. &lt;/P&gt;&lt;P&gt;But the follow function is working differently. The print inside the function has &lt;/P&gt;&lt;P&gt;the correct value which I want `({5d, 0xa8, 0x0f, 0x1f, 0x1c, 0x03, 0x7f, 0x16, &lt;/P&gt;&lt;P&gt;0x7e, 0xe5, 0xfd, 0xf3, 0x45, 0xb7, 0x73, 0xa2})`.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But when the function returns `chain` is having a different value, compared to what I &lt;/P&gt;&lt;P&gt;am expecting, I get the following value for `chain` &lt;/P&gt;&lt;P&gt;`{0x00, 0x20, 0x00, 0x10, 0x03, 0x01, 0x00, 0x00, 0xd5, 0x00, 0x00, 0x00, 0xd7, 0x00, 0x00, 0x00}`&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Inside the function, the result is correct. But it returns a wrong value to the &lt;/P&gt;&lt;P&gt;function which called it. Why is it happening so ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;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 &amp;gt;= 16) {
 memcpy(tmp, data, 16);

 //xorBytes(tmp,chain,16);
 for (i = 0; i &amp;lt; 16; i++) {
 tmp[i] = tmp[i] ^ chain[i];
 }

 aes128_enc(tmp, key);

 for (i = 0; i &amp;lt; 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;
 }&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;CODE style="border: 0px; font-size: 13px;"&gt;&lt;SPAN class="" style="color: #303336; border: 0px; font-size: 13px;"&gt;
&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 Mar 2017 23:17:32 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Function-returning-wrong-value-in-lpc824/m-p/641632#M25374</guid>
      <dc:creator>johnygeorgemala</dc:creator>
      <dc:date>2017-03-31T23:17:32Z</dc:date>
    </item>
    <item>
      <title>Re: Function returning wrong value in lpc824</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/Function-returning-wrong-value-in-lpc824/m-p/641633#M25375</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So you appear to be trying to&amp;nbsp;return a pointer to a local array (so on the stack). Thus once you have returned from&amp;nbsp;this function, &amp;nbsp;those values could very easily be overwritten by anything else using the stack (for instance an interrupt being triggered).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;LPCXpresso Support&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Apr 2017 07:26:28 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/Function-returning-wrong-value-in-lpc824/m-p/641633#M25375</guid>
      <dc:creator>lpcxpresso_supp</dc:creator>
      <dc:date>2017-04-04T07:26:28Z</dc:date>
    </item>
  </channel>
</rss>

