<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[wolfSSL - Embedded SSL Library — [SOLVED] AES encrypt and decrypt error]]></title>
		<link>https://www.wolfssl.com/forums/topic872-solved-aes-encrypt-and-decrypt-error.html</link>
		<atom:link href="https://www.wolfssl.com/forums/feed-rss-topic872.xml" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in [SOLVED] AES encrypt and decrypt error.]]></description>
		<lastBuildDate>Fri, 28 Oct 2016 16:08:40 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: [SOLVED] AES encrypt and decrypt error]]></title>
			<link>https://www.wolfssl.com/forums/post2797.html#p2797</link>
			<description><![CDATA[<p>Hi Jesussotofan,</p><p>One final comment on this.</p><p>If you were to change the way you are getting the length of the key you could use the original key of &quot;1234567890abcdef&quot;.</p><p>You currently are using &quot;sizeof&quot; which is why it&#039;s counting the null terminator or &#039;\0&#039; character in the length. You have two options when passing into our library:</p><p>Option 1:<br /></p><div class="codebox"><pre><code>ret = wc_AesSetKey(&amp;enc, key, strlen(key), iv, AES_ENCRYPTION);

NOTE: strlen will count the length of the string MINUS the null terminator giving you a length of 16 instead of 17</code></pre></div><p>Option 2:<br /></p><div class="codebox"><pre><code>ret = wc_AesSetKey(&amp;enc, key, sizeof(key) - 1, iv, AES_ENCRYPTION);

NOTE: Do the same as you did originally and pass in sizeof(key) - 1. This way you don&#039;t have a hex value of 00 or &#039;\0&#039; included in the key.</code></pre></div><p>Regards,</p><p>Kaleb</p>]]></description>
			<author><![CDATA[null@example.com (Kaleb J. Himes)]]></author>
			<pubDate>Fri, 28 Oct 2016 16:08:40 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post2797.html#p2797</guid>
		</item>
		<item>
			<title><![CDATA[Re: [SOLVED] AES encrypt and decrypt error]]></title>
			<link>https://www.wolfssl.com/forums/post2796.html#p2796</link>
			<description><![CDATA[<p>Hi jesussotofan,</p><p>I have taken your code, re-compiled and run the test on Linux, I&#039;m giving you my source code, just update the print statements and you should observe intended test case.</p><br /><div class="codebox"><pre><code>#include &lt;stdio.h&gt;
#include &lt;wolfssl/options.h&gt;
#include &lt;wolfssl/wolfcrypt/settings.h&gt;
#include &lt;wolfssl/wolfcrypt/aes.h&gt;

int main(void) {
Aes enc;
Aes dec;
int ret;

int x;

byte key[] = {&quot;234567890abcdef&quot;};

byte iv[]  = {&quot;1234567890abcdef&quot;};

byte plain[32]  = {&quot;Hola mundoHola mundoHola mundo&quot;};   // an increment of 16, fill with data
byte plain2[32];
byte cipher[32];

printf(&quot;sizeof key = %d\n&quot;, (int) sizeof(key));
printf(&quot;sizeof cipher = %d\n&quot;, (int) sizeof(cipher));
printf(&quot;sizeof plain = %d\n&quot;, (int) sizeof(plain));

printf(&quot;plain value                      &quot;);

for(x=0; x&lt;16; x++){
  printf(&quot;%02x&quot;, plain[x]);
  printf(&quot; &quot;);
  }
printf(&quot; \n&quot;);



printf(&quot;printing key value               &quot;);

for(x=0; x&lt;16; x++){
  printf(&quot;%02x&quot;, key[x]);
  printf(&quot; &quot;);
  }
printf(&quot; \n&quot;);


printf(&quot;printing iv value                &quot;);
for(x=0; x&lt;16; x++){
  printf(&quot;%02x&quot;, iv[x]);
  printf(&quot; &quot;);
  }

printf(&quot; \n&quot;);

//encrypt
ret = wc_AesSetKey(&amp;enc, key, sizeof(key), iv, AES_ENCRYPTION);
if (ret != 0) {
    printf(&quot;Encrypt set key failed with error: %d\n&quot;, ret);
    return ret;
}
ret = wc_AesCbcEncrypt(&amp;enc, cipher, plain, sizeof(plain));
if (ret != 0) {
    printf(&quot;AesCbcEncrypt failed with error: %d\n&quot;, ret);
    return ret;
}



printf(&quot;cipher value                     &quot;);

for(x=0; x&lt;16; x++){
  printf(&quot;%02x&quot;, cipher[x]);
  printf(&quot; &quot;);
  }
printf(&quot; \n&quot;);



printf(&quot;printing key value before cipher &quot;);

for(x=0; x&lt;16; x++){
  printf(&quot;%02x&quot;, key[x]);
  printf(&quot; &quot;);
  }
printf(&quot; \n&quot;);


printf(&quot;printing iv value before cipher  &quot;);
for(x=0; x&lt;16; x++){
  printf(&quot;%02x&quot;, iv[x]);
  printf(&quot; &quot;);
  }
printf(&quot; \n&quot;);

//decrypt
ret = wc_AesSetKey(&amp;dec, key, sizeof(key), iv, AES_DECRYPTION);
if (ret != 0) {
    printf(&quot;Decrypt set key failed with error: %d\n&quot;, ret);
    return ret;
}
ret = wc_AesCbcDecrypt(&amp;dec, plain2, cipher, sizeof(cipher));
if (ret != 0) {
    printf(&quot;AesCbcDecrypt failed with error: %d\n&quot;, ret);
    return ret;
}


printf(&quot;plain2 value                     &quot;);

for(x=0; x&lt;16; x++){
  printf(&quot;%02x&quot;, plain2[x]);
  printf(&quot; &quot;);
  }

printf(&quot; \n&quot;);
printf(&quot; \n&quot;);
printf(&quot; \n&quot;);

}</code></pre></div><br /><p>Regards,</p><p>Kaleb</p>]]></description>
			<author><![CDATA[null@example.com (Kaleb J. Himes)]]></author>
			<pubDate>Fri, 28 Oct 2016 15:58:04 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post2796.html#p2796</guid>
		</item>
		<item>
			<title><![CDATA[Re: [SOLVED] AES encrypt and decrypt error]]></title>
			<link>https://www.wolfssl.com/forums/post2794.html#p2794</link>
			<description><![CDATA[<p>Hi jesussotofan,</p><p>the key must be modulo AES_SIZE (16). A byte array containing 1234567890abcdef is size 17 (array indexing if it contains 16 elements it&#039;s one larger for the &#039;\0&#039; or NULL character on the end of the string in c. Change your key to &quot;234567890abcdef&quot; and you test should work.</p><br /><p>One other note you are printing out HEX values and looping 32 times. Each hex byte is 8 bytes long. You&#039;re working with an array of 32 8-byte elements and the print in the for loop prints out 2 8-bytes elements each loop. You should only loop 16 times, not 32. You&#039;re printing memory off the end of your buffers.</p><div class="codebox"><pre><code>for(x=0; x&lt;16; x++){
  Serial.print(plain[x], HEX);
  Serial.print(&quot; &quot;);
  }
Serial.println(&quot; &quot;);</code></pre></div><br /><p>ALSO it is highly recommended you check your return codes and you would have seen the error happening IE:</p><div class="codebox"><pre><code>wc_AesSetKey(&amp;enc, key, sizeof(key), iv, AES_ENCRYPTION);

SHOULD BE:

/* make an integer &quot;ret&quot; */
ret = wc_AesSetKey(&amp;enc, key, sizeof(key), iv, AES_ENCRYPTION);
if (ret != 0) {
    Serial.println(&quot; Aes Set Key Failed with error code: &quot;);
    Serial.print(ret, INTEGER); /* or however you output an int I&#039;m not familiar with this print functionality */
}</code></pre></div><p>And do the same for other wolfSSL API&#039;s.</p><br /><p>Regards,</p><p>Kaleb</p>]]></description>
			<author><![CDATA[null@example.com (Kaleb J. Himes)]]></author>
			<pubDate>Thu, 27 Oct 2016 23:13:22 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post2794.html#p2794</guid>
		</item>
		<item>
			<title><![CDATA[[SOLVED] AES encrypt and decrypt error]]></title>
			<link>https://www.wolfssl.com/forums/post2788.html#p2788</link>
			<description><![CDATA[<p>Hi, I followed the examples from wolfssl documentation to use AES. -&gt; <a href="https://www.wolfssl.com/wolfSSL/Docs-wolfssl-manual-10-wolfcrypt-usage-reference.html.">https://www.wolfssl.com/wolfSSL/Docs-wo … ence.html.</a></p><p>After executing this code:</p><div class="codebox"><pre><code>#include &lt;wolfssl.h&gt;
#include &lt;wolfssl/wolfcrypt/aes.h&gt;

int x = 0;
 
void setup(void){

  while(!Serial);
  Serial.begin(9600);
}
 
void loop(void){

//AES block
Aes enc;
Aes dec;

byte key[] = {&quot;1234567890abcdef&quot;};

byte iv[]  = {&quot;1234567890abcdef&quot;};

byte plain[32]  = {&quot;Hola mundoHola mundoHola mundo&quot;};   // an increment of 16, fill with data
byte plain2[32];
byte cipher[32];

Serial.print(&quot;plain value  &quot;);

for(x=0; x&lt;32; x++){
  Serial.print(plain[x], HEX);
  Serial.print(&quot; &quot;);
  }
Serial.println(&quot; &quot;);


Serial.print(&quot;printing key value &quot;);

for(x=0; x&lt;32; x++){
  Serial.print(key[x], HEX);
  Serial.print(&quot; &quot;);
  }
Serial.println(&quot; &quot;);


Serial.print(&quot;printing iv value &quot;);
for(x=0; x&lt;32; x++){
  Serial.print(iv[x], HEX);
  Serial.print(&quot; &quot;);
  }

Serial.println(&quot; &quot;);

//encrypt
wc_AesSetKey(&amp;enc, key, sizeof(key), iv, AES_ENCRYPTION);
wc_AesCbcEncrypt(&amp;enc, cipher, plain, sizeof(plain));



Serial.print(&quot;cipher value &quot;);

for(x=0; x&lt;32; x++){
  Serial.print(cipher[x], HEX);
  Serial.print(&quot; &quot;);
  }
Serial.println(&quot; &quot;);



Serial.print(&quot;printing key value before cipher&quot;);

for(x=0; x&lt;32; x++){
  Serial.print(key[x], HEX);
  Serial.print(&quot; &quot;);
  }
Serial.println(&quot; &quot;);


Serial.print(&quot;printing iv value before cipher&quot;);
for(x=0; x&lt;32; x++){
  Serial.print(iv[x], HEX);
  Serial.print(&quot; &quot;);
  }
Serial.println(&quot; &quot;);

//decrypt
wc_AesSetKey(&amp;dec, key, sizeof(key), iv, AES_DECRYPTION);
wc_AesCbcDecrypt(&amp;dec, plain2, cipher, sizeof(cipher));


Serial.print(&quot;plain2 value  &quot;);

for(x=0; x&lt;32; x++){
  Serial.print(plain2[x], HEX);
  Serial.print(&quot; &quot;);
  }

Serial.println(&quot; &quot;);
Serial.println(&quot; &quot;);
Serial.println(&quot; &quot;);

 delay(10000);
}</code></pre></div><p>We get this:</p><p><span class="postimg"><img src="https://fotos.subefotos.com/aecc999c283568d93e3b9318406c7a28o.jpg" alt="https://fotos.subefotos.com/aecc999c283568d93e3b9318406c7a28o.jpg" /></span></p><p>We can see that is not changing any input parameter at the function, but in each Arduino iteration we see differents values from encryption and decryption.<br />Besides decryption value never matches with value that we had before encrypting.<br />I&#039;ve even used two variables &quot;plain&quot; and &quot;plain2&quot; to avoid any possible conflict.</p><p>NOTE: &quot;for&quot; loops just serve to print on screen, do not affect functionality, so you can ignore it.</p><br /><p>Any idea why it happens?</p>]]></description>
			<author><![CDATA[null@example.com (jesussotofan)]]></author>
			<pubDate>Wed, 26 Oct 2016 17:51:11 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post2788.html#p2788</guid>
		</item>
	</channel>
</rss>
