<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal>I have a program that for some time now under SUSE Linux Enterprise Server has worked fine.  Recently, it was moved over to an OpenSUSE 13.2 system and a problem was encountered.  The program interfaces to a 3<sup>rd</sup> party  and data is received into our program where the data block consists of some header information and AES encrypted data.  Using the OpenSSL libcrypto library we successfully interfaced to this system under SLES.  However, under OpenSUSE we consistently see the an error where the end of the decrypted data contains garbage.  I have identified where the problem happens and have a work around but in looking at the code, I don’t see why there would be a problem.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I have created a test program that simulates the problem.  The test program works fine under SUSE Linux Enterprise Server 11 and on a Red Hat 7.2 Enterprise Linux but fails on OpenSUSE 13.2 using different release levels of the OpenSSL libraries.  Under SLES and Red Hat, the decrypted data is cleanly returned.  Under OpenSUSE, most of the data is decrypted cleanly except for some garbage that appears toward the end of the data block.  The returned block of data is correct, then contains some garbage, and then ends correct.  The code for my sample program is below but the line causing the problem is a memcpy() where I shift the encrypted data to the front of the data block for processing.  The line in my sample program is below:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>   // Generates Garbage<o:p></o:p></p><p class=MsoNormal>   memcpy(encbuf, encbuf+100, enclen);                 <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>If I move the encrypted data to temporary buffer before moving it to the start of the encbuf, the garbage is not generated.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>   // This does not generate garbage<o:p></o:p></p><p class=MsoNormal>   memcpy(tmpbuf, encbuf+100, enclen);                 <o:p></o:p></p><p class=MsoNormal>   memcpy(encbuf, tmpbuf, enclen);                 <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>My sample program takes a defined buffer of clear text, encrypts it using a key and IV and then decrypts it back and displays the result.  Condensed code is as follows:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#include <stdio.h>      <o:p></o:p></p><p class=MsoNormal>#include <stdlib.h>     <o:p></o:p></p><p class=MsoNormal>#include <string.h>     <o:p></o:p></p><p class=MsoNormal>#include <unistd.h>     <o:p></o:p></p><p class=MsoNormal>#include <time.h>       <o:p></o:p></p><p class=MsoNormal>#include <fcntl.h>      <o:p></o:p></p><p class=MsoNormal>#include <sys/types.h>  <o:p></o:p></p><p class=MsoNormal>                        <o:p></o:p></p><p class=MsoNormal>#include <openssl/evp.h><o:p></o:p></p><p class=MsoNormal>                        <o:p></o:p></p><p class=MsoNormal>#define EVP_DECRYPT   0 <o:p></o:p></p><p class=MsoNormal>#define EVP_ENCRYPT   1<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>char clrbuf[100000];<o:p></o:p></p><p class=MsoNormal>char encbuf[100000];<o:p></o:p></p><p class=MsoNormal>char tmpbuf[100000];<o:p></o:p></p><p class=MsoNormal>int clrlen;         <o:p></o:p></p><p class=MsoNormal>int enclen;         <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>char enckey[1024];      <o:p></o:p></p><p class=MsoNormal>unsigned char enciv[16];<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>main()<o:p></o:p></p><p class=MsoNormal>{<o:p></o:p></p><p class=MsoNormal>   int rc;<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>   // Set clear text to 50 lines of text<o:p></o:p></p><p class=MsoNormal>   sprintf(clrbuf,                                                   <o:p></o:p></p><p class=MsoNormal>         "0001this is a test this is a test this is a test this is a test\n" \<o:p></o:p></p><p class=MsoNormal>         "0002this is a test this is a test this is a test this is a test\n" \<o:p></o:p></p><p class=MsoNormal>         "0003this is a test this is a test this is a test this is a test\n" \<o:p></o:p></p><p class=MsoNormal>         // etc etc etc……………….<o:p></o:p></p><p class=MsoNormal>         "0048this is a test this is a test this is a test this is a test\n" \<o:p></o:p></p><p class=MsoNormal>         "0049this is a test this is a test this is a test this is a test\n" \<o:p></o:p></p><p class=MsoNormal>         "0050this is a test this is a test this is a test this is a test\n"  <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>   sprintf(enckey, "this is the key this is the key ");<o:p></o:p></p><p class=MsoNormal>   sprintf(enciv, "1234567890123456");<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>   // Encrypt the data and simulate a 100 byte header by returning encrypted data 100 bytes into the data block<o:p></o:p></p><p class=MsoNormal>   //<o:p></o:p></p><p class=MsoNormal>   memcpy(encbuf, "Some header stuff that will need to be removed", 46);<o:p></o:p></p><p class=MsoNormal>   rc = evp_aes256_cbc(clrbuf, strlen(clrbuf), encbuf+100, &enclen, enckey, enciv, EVP_ENCRYPT);                  <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>   // Now remove the header by shifting the encrypted data to the start of the data block and decrypt<o:p></o:p></p><p class=MsoNormal>   // This is where doing the memcpy() as coded in OpenSUSE results in garbage at the end of clrbuf<o:p></o:p></p><p class=MsoNormal>   // but everything is returned correctly in SLES and Red Hat<o:p></o:p></p><p class=MsoNormal>   //<o:p></o:p></p><p class=MsoNormal>   // This work fines on all OSes:<o:p></o:p></p><p class=MsoNormal>   //         memcpy(tmpbuf, encbuf+100, enclen);                 <o:p></o:p></p><p class=MsoNormal>   //         memcpy(encbuf, tmpbuf, enclen);                 <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>   memcpy(encbuf, encbuf+100, enclen);                 <o:p></o:p></p><p class=MsoNormal>   rc = evp_aes256_cbc(encbuf, enclen, clrbuf, &clrlen, enckey, enciv, EVP_DECRYPT);<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>   printf("Decrypt: rc=%d  EncLen=%d  ClrLen=%d\n", rc, enclen, clrlen);<o:p></o:p></p><p class=MsoNormal>   printf("Data:\n\n<\n%s\n>\n\n", clrbuf);                             <br>}<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/****************************************************************************/  <o:p></o:p></p><p class=MsoNormal>                                                                                <o:p></o:p></p><p class=MsoNormal>evp_aes256_cbc(char *InBuf, int InLen, char *OutBuf, int OutLen, char *Key, char *IV, int EncryptFlag)              <o:p></o:p></p><p class=MsoNormal>{                                                                               <o:p></o:p></p><p class=MsoNormal>   EVP_CIPHER_CTX ctx;                                                          <o:p></o:p></p><p class=MsoNormal>   int padlen;                                                                  <o:p></o:p></p><p class=MsoNormal>                                                                                <o:p></o:p></p><p class=MsoNormal>   EVP_CIPHER_CTX_init(&ctx);                                                   <o:p></o:p></p><p class=MsoNormal>                                                                                <o:p></o:p></p><p class=MsoNormal>   if (! EVP_CipherInit_ex(&ctx, EVP_aes_256_cbc(), NULL, Key, IV, EncryptFlag))<o:p></o:p></p><p class=MsoNormal>      return(0);                                                                <o:p></o:p></p><p class=MsoNormal>                                                              <o:p></o:p></p><p class=MsoNormal>   if (! EVP_CipherUpdate(&ctx, OutBuf, OutLen, InBuf, InLen))<o:p></o:p></p><p class=MsoNormal>      return(0);                                              <o:p></o:p></p><p class=MsoNormal>                                                              <o:p></o:p></p><p class=MsoNormal>   if (! EVP_CipherFinal_ex(&ctx, OutBuf+(*OutLen), &padlen)) <o:p></o:p></p><p class=MsoNormal>      return(0);                                              <o:p></o:p></p><p class=MsoNormal>                                                              <o:p></o:p></p><p class=MsoNormal>   *OutLen = *OutLen + padlen;                                <o:p></o:p></p><p class=MsoNormal>                                                              <o:p></o:p></p><p class=MsoNormal>   EVP_CIPHER_CTX_cleanup(&ctx);                              <o:p></o:p></p><p class=MsoNormal>                                                              <o:p></o:p></p><p class=MsoNormal>   return(1);                                                 <o:p></o:p></p><p class=MsoNormal>}                                                             <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>On SLES and Red Hat, final output looks like:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>0046this is a test this is a test this is a test this is a test<o:p></o:p></p><p class=MsoNormal>0047this is a test this is a test this is a test this is a test<o:p></o:p></p><p class=MsoNormal>0048this is a test this is a test this is a test this is a test<o:p></o:p></p><p class=MsoNormal>0049this is a test this is a test this is a test this is a test<o:p></o:p></p><p class=MsoNormal>0050this is a test this is a test this is a test this is a test<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>On OpenSUSE, final output can look like:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>0046this is a test this is a test this is a test this is a test                 <o:p></o:p></p><p class=MsoNormal>0047this is a test this is a test this is a test this is a test                 <o:p></o:p></p><p class=MsoNormal>0048this is a test this is a tes<span style='font-family:"Arial",sans-serif'>╧┬</span>S_úReÅ<span style='font-family:"Arial",sans-serif'>▌</span>       |τZk<span style='font-family:"Arial",sans-serif'>╠</span>½çP≥ii≡w<span style='font-family:"Arial",sans-serif'>╙</span>p<span style='font-family:"Arial",sans-serif'>▓</span>8ª'MêÅt<span style='font-family:"Arial",sans-serif'>▒</span>g{Y¥ΩEô¬<o:p></o:p></p><p class=MsoNormal>⌡n}⌐*<span style='font-family:"Arial",sans-serif'>╘</span>¿µ2└<span style='font-family:"Arial",sans-serif'>╠</span>LS4=Qüü<span style='font-family:"Arial",sans-serif'>├</span>;~<span style='font-family:"Arial",sans-serif'>╕</span>Ç<<span style='font-family:"Arial",sans-serif'>╗</span>^¿ßD0<span style='font-family:"Arial",sans-serif'>┤</span>T.OQΣq#≈                                         <o:p></o:p></p><p class=MsoNormal>                                       xest                                     <o:p></o:p></p><p class=MsoNormal>0050this is a test this is a test this is a test this is a test                 <o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Any thoughts?<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Thanks<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>