RE: Problems with AES-CFB1

2011-11-02 Thread Dave Thompson
> From: owner-openssl-us...@openssl.org On Behalf Of Michael S. Zick
> Sent: Tuesday, 01 November, 2011 09:15

> On Mon October 31 2011, Dave Thompson wrote:

> > compiled without error, and gave the symptom reported -- 
> > because CRYPTO_cfb128_1_encrypt treats the length as bits 
> >
> 
> My copy from the mailing list used: AES_cfb1_encrypt(...)
> (Like the title of the post.)
> 
> Can't see how yours got like the above, unless you retyped
> the post and your fingers just automatically corrected the 
> function call being used.
> 
AES_cfb1_encrypt calls CRYPTO_cfb128_1_encrypt, and it is 
the latter which actually interprets the length argument.
I guess I could have detailed that, but I assume(d) anyone 
who cares about implementation looks at the code.
AES_cfb1_ does repeat the comment from CRYPT_cfb128_1_.

> > not bytes (apparently only used for AES and Camellia).
> > 97 bits is 12 bytes plus 1 bit, and that 'extra' bit 
> > is 0 and thus 'disappears'.
> >
> 
> Yeah, I was thinking of suggesting "man ..." also.
> Maybe I should have to have been complete.
> 
There isn't a man page for aes.3, nor camellia.3 
which seem to be the only two that work this way.
If fact not most of the symmetric algorithms.

DES_cfb_encrypt(,,1) puts each bit in a separate byte, 
so in that case the distinction doesn't occur.


__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   majord...@openssl.org


Re: Problems with AES-CFB1

2011-11-02 Thread Ananthasayanan Kandiah
Yep, that solved it! That makes sense.

Thankyou so much!


On Wed, Nov 2, 2011 at 12:08 AM, re.est  wrote:

>  Hello,
>
> I added *8 in length for both encrypt/decrypt call to make it bit length.
> AES_cfb1_encrypt(data, ciphertext, length*8, &key, iv, &num,
> AES_ENCRYPT);
>
> As you can see, cfb128_1 has uses bit as length in API
> void CRYPTO_cfb128_1_encrypt(const unsigned char *in, unsigned char *out,
> size_t bits, const void *key, ...
>
> unlike other cfb APIs.
>
>
>
>
>
> On 11/01/2011 09:48 PM, Ananthasayanan Kandiah wrote:
>
> Hi,
>
> I would be grateful if you could expand on this. I've tried simply placing
> the bit length for the AES_set_encrypt_key call and it still produces the
> same result.
>
>
> Thanks,
> Anantha
>
> On Tue, Nov 1, 2011 at 8:10 PM, re est  wrote:
>
>> Hi,
>>
>> I have tried your code and replaced the length param with bit length
>> (*8) instead.
>> It worked. It  seems that there are inconsistent with the usage of API.
>>
>> - re.est
>>
>> On Sun, Oct 30, 2011 at 4:55 PM, Ananthasayanan Kandiah
>>  wrote:
>> > Hi,
>> >
>> > I'm trying to use AES-CFB1 through the "low-level" calls. Here's the
>> example
>> > program I have come up with:
>> >
>> > #include 
>> > #include 
>> > #include 
>> > #include 
>> >
>> > #define  KEY_SIZE 16
>> >
>> > int main(void)
>> > {
>> > inti;
>> > AES_KEYkey;
>> > BIO*bio_out;
>> >
>> > unsigned char key_data[KEY_SIZE] = {
>> > 0xfe, 0xec, 0x82, 0x17, 0xb5, 0x1, 0x98, 0x6b,
>> > 0x5e, 0xf1, 0xb8, 0x6, 0x52, 0x74, 0x2e, 0x52
>> > };
>> >
>> > unsigned char iv[AES_BLOCK_SIZE];
>> >
>> > unsigned char const iv_data[AES_BLOCK_SIZE] = {
>> > 0x10, 0x8a, 0xc9, 0x30, 0xb7, 0xf2, 0x35, 0x21,
>> > 0xfb, 0xac, 0x6b, 0xdf, 0x80, 0x95, 0xeb, 0x1e
>> > };
>> >
>> > char*   data= "Internet is a wonderful mechanism for making a
>> fool "
>> >   "of yourself in front of a very large audience";
>> >
>> >
>> > int length  = (int) strlen(data);
>> >
>> > int num = 0;
>> >
>> > /* Allocate some space for the ciphertext and plaintext */
>> > char*ciphertext = (char*) malloc(sizeof(char) * length);
>> > char*plaintext  = (char*) malloc(sizeof(char) * length);
>> >
>> > /* Copy the IV data to the IV array */
>> > memcpy(iv, iv_data, AES_BLOCK_SIZE);
>> >
>> > /* Set the encrypt key structure using the predefined key */
>> > AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);
>> >
>> > /* Carry out the encryption */
>> > AES_cfb1_encrypt(data, ciphertext, length, &key, iv, &num,
>> AES_ENCRYPT);
>> >
>> > /* Setup output */
>> > bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
>> >
>> > BIO_printf(bio_out, "Original plaintext: %s\n\n", data);
>> >
>> > BIO_printf(bio_out, "Ciphertext: ");
>> >
>> > /* Print out the ciphertext */
>> > for (i = 0; i < length; i++)
>> > BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]);
>> >
>> > BIO_printf(bio_out, "\n\n");
>> >
>> > /* Start the decryption process */
>> >
>> > /* First, copy the original IV data back to the IV array - as it was
>> > overwritten
>> >  * during the encryption process
>> >  */
>> > memcpy(iv, iv_data, AES_BLOCK_SIZE);
>> >
>> > /* Reset how far we've gone through the IV */
>> > num = 0;
>> >
>> > /* Carry out the decryption */
>> > AES_cfb1_encrypt(ciphertext, plaintext, length, &key, iv, &num,
>> > AES_DECRYPT);
>> >
>> > BIO_printf(bio_out, "Recovered plaintext: ");
>> >
>> > /* print out the plaintext */
>> > for (i = 0; i < length; i++)
>> > BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);
>> >
>> > BIO_printf(bio_out, "\n\n");
>> >
>> > BIO_free(bio_out);
>> >
>> > free(ciphertext);
>> > free(plaintext);
>> >
>> >
>> > return 0;
>> > }
>> >
>> > When I run it, the output which I receive is:
>> >
>> > Original plaintext: Internet is a wonderful mechanism for making a
>> foolof
>> > yourself in front of a very large audience
>> >
>> > Ciphertext:
>> >
>> 92c0883c54eb8df072b43278
>> >
>> 
>> >
>> > Recovered plaintext: Internet is
>> >
>> >
>> >
>> d:\ananthasayanan\stuff\projects\programming\openssl\openssl-1.0.0d\demos\crypto\low_level\ciphers\symmetric\aes\aes_cfb
>> > 1_128\x64\Release>aes_cfb1_128.exe
>> > Original plaintext: Internet is a wonderful mechanism for making a fool
>> of
>> > yourself in front of a very large audience
>> >
>> > Ciphertext:
>> >
>> 92c0883c54eb8df072b43278
>> >
>> 00
>> >
>> > Recovered plaintext: Internet 

Re: Problems with AES-CFB1

2011-11-01 Thread re.est

Hello,

I added *8 in length for both encrypt/decrypt call to make it bit length.
AES_cfb1_encrypt(data, ciphertext, length*8, &key, iv, &num, 
AES_ENCRYPT);


As you can see, cfb128_1 has uses bit as length in API
void CRYPTO_cfb128_1_encrypt(const unsigned char *in, unsigned char *out,
size_t bits, const void *key, ...

unlike other cfb APIs.




On 11/01/2011 09:48 PM, Ananthasayanan Kandiah wrote:

Hi,

I would be grateful if you could expand on this. I've tried simply 
placing the bit length for the AES_set_encrypt_key call and it still 
produces the same result.



Thanks,
Anantha

On Tue, Nov 1, 2011 at 8:10 PM, re est > wrote:


Hi,

I have tried your code and replaced the length param with bit length
(*8) instead.
It worked. It  seems that there are inconsistent with the usage of
API.

- re.est

On Sun, Oct 30, 2011 at 4:55 PM, Ananthasayanan Kandiah
mailto:ananthasaya...@obtino.com>> wrote:
> Hi,
>
> I'm trying to use AES-CFB1 through the "low-level" calls. Here's
the example
> program I have come up with:
>
> #include 
> #include 
> #include 
> #include 
>
> #define  KEY_SIZE 16
>
> int main(void)
> {
> inti;
> AES_KEYkey;
> BIO*bio_out;
>
> unsigned char key_data[KEY_SIZE] = {
> 0xfe, 0xec, 0x82, 0x17, 0xb5, 0x1, 0x98, 0x6b,
> 0x5e, 0xf1, 0xb8, 0x6, 0x52, 0x74, 0x2e, 0x52
> };
>
> unsigned char iv[AES_BLOCK_SIZE];
>
> unsigned char const iv_data[AES_BLOCK_SIZE] = {
> 0x10, 0x8a, 0xc9, 0x30, 0xb7, 0xf2, 0x35, 0x21,
> 0xfb, 0xac, 0x6b, 0xdf, 0x80, 0x95, 0xeb, 0x1e
> };
>
> char*   data= "Internet is a wonderful mechanism for
making a fool "
>   "of yourself in front of a very large
audience";
>
>
> int length  = (int) strlen(data);
>
> int num = 0;
>
> /* Allocate some space for the ciphertext and plaintext */
> char*ciphertext = (char*) malloc(sizeof(char) * length);
> char*plaintext  = (char*) malloc(sizeof(char) * length);
>
> /* Copy the IV data to the IV array */
> memcpy(iv, iv_data, AES_BLOCK_SIZE);
>
> /* Set the encrypt key structure using the predefined key */
> AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);
>
> /* Carry out the encryption */
> AES_cfb1_encrypt(data, ciphertext, length, &key, iv, &num,
AES_ENCRYPT);
>
> /* Setup output */
> bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
>
> BIO_printf(bio_out, "Original plaintext: %s\n\n", data);
>
> BIO_printf(bio_out, "Ciphertext: ");
>
> /* Print out the ciphertext */
> for (i = 0; i < length; i++)
> BIO_printf(bio_out, "%02x", ((unsigned
char*)ciphertext)[i]);
>
> BIO_printf(bio_out, "\n\n");
>
> /* Start the decryption process */
>
> /* First, copy the original IV data back to the IV array -
as it was
> overwritten
>  * during the encryption process
>  */
> memcpy(iv, iv_data, AES_BLOCK_SIZE);
>
> /* Reset how far we've gone through the IV */
> num = 0;
>
> /* Carry out the decryption */
> AES_cfb1_encrypt(ciphertext, plaintext, length, &key, iv, &num,
> AES_DECRYPT);
>
> BIO_printf(bio_out, "Recovered plaintext: ");
>
> /* print out the plaintext */
> for (i = 0; i < length; i++)
> BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);
>
> BIO_printf(bio_out, "\n\n");
>
> BIO_free(bio_out);
>
> free(ciphertext);
> free(plaintext);
>
>
> return 0;
> }
>
> When I run it, the output which I receive is:
>
> Original plaintext: Internet is a wonderful mechanism for making
a foolof
> yourself in front of a very large audience
>
> Ciphertext:
>

92c0883c54eb8df072b43278
>


>
> Recovered plaintext: Internet is
>
>
>

d:\ananthasayanan\stuff\projects\programming\openssl\openssl-1.0.0d\demos\crypto\low_level\ciphers\symmetric\aes\aes_cfb
> 1_128\x64\Release>aes_cfb1_128.exe
> Original plaintext: Internet is a wonderful mechanism for making
a fool of
> yourself in front of a very large audience
>
> Ciphertext:
>

92c0883c54eb8df072b43278
>

0

Re: Problems with AES-CFB1

2011-11-01 Thread re.est

Hello,

I added *8 in length for both encrypt/decrypt call to make it bit length.
AES_cfb1_encrypt(data, ciphertext, length*8, &key, iv, &num, 
AES_ENCRYPT);


CRYPTO_cfb128_1_encrypt accepts bit length unlike other CRYPTO_cfb128XX 
apis.



On 11/01/2011 09:48 PM, Ananthasayanan Kandiah wrote:

Hi,

I would be grateful if you could expand on this. I've tried simply 
placing the bit length for the AES_set_encrypt_key call and it still 
produces the same result.



Thanks,
Anantha

On Tue, Nov 1, 2011 at 8:10 PM, re est > wrote:


Hi,

I have tried your code and replaced the length param with bit length
(*8) instead.
It worked. It  seems that there are inconsistent with the usage of
API.

- re.est

On Sun, Oct 30, 2011 at 4:55 PM, Ananthasayanan Kandiah
mailto:ananthasaya...@obtino.com>> wrote:
> Hi,
>
> I'm trying to use AES-CFB1 through the "low-level" calls. Here's
the example
> program I have come up with:
>
> #include 
> #include 
> #include 
> #include 
>
> #define  KEY_SIZE 16
>
> int main(void)
> {
> inti;
> AES_KEYkey;
> BIO*bio_out;
>
> unsigned char key_data[KEY_SIZE] = {
> 0xfe, 0xec, 0x82, 0x17, 0xb5, 0x1, 0x98, 0x6b,
> 0x5e, 0xf1, 0xb8, 0x6, 0x52, 0x74, 0x2e, 0x52
> };
>
> unsigned char iv[AES_BLOCK_SIZE];
>
> unsigned char const iv_data[AES_BLOCK_SIZE] = {
> 0x10, 0x8a, 0xc9, 0x30, 0xb7, 0xf2, 0x35, 0x21,
> 0xfb, 0xac, 0x6b, 0xdf, 0x80, 0x95, 0xeb, 0x1e
> };
>
> char*   data= "Internet is a wonderful mechanism for
making a fool "
>   "of yourself in front of a very large
audience";
>
>
> int length  = (int) strlen(data);
>
> int num = 0;
>
> /* Allocate some space for the ciphertext and plaintext */
> char*ciphertext = (char*) malloc(sizeof(char) * length);
> char*plaintext  = (char*) malloc(sizeof(char) * length);
>
> /* Copy the IV data to the IV array */
> memcpy(iv, iv_data, AES_BLOCK_SIZE);
>
> /* Set the encrypt key structure using the predefined key */
> AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);
>
> /* Carry out the encryption */
> AES_cfb1_encrypt(data, ciphertext, length, &key, iv, &num,
AES_ENCRYPT);
>
> /* Setup output */
> bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
>
> BIO_printf(bio_out, "Original plaintext: %s\n\n", data);
>
> BIO_printf(bio_out, "Ciphertext: ");
>
> /* Print out the ciphertext */
> for (i = 0; i < length; i++)
> BIO_printf(bio_out, "%02x", ((unsigned
char*)ciphertext)[i]);
>
> BIO_printf(bio_out, "\n\n");
>
> /* Start the decryption process */
>
> /* First, copy the original IV data back to the IV array -
as it was
> overwritten
>  * during the encryption process
>  */
> memcpy(iv, iv_data, AES_BLOCK_SIZE);
>
> /* Reset how far we've gone through the IV */
> num = 0;
>
> /* Carry out the decryption */
> AES_cfb1_encrypt(ciphertext, plaintext, length, &key, iv, &num,
> AES_DECRYPT);
>
> BIO_printf(bio_out, "Recovered plaintext: ");
>
> /* print out the plaintext */
> for (i = 0; i < length; i++)
> BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);
>
> BIO_printf(bio_out, "\n\n");
>
> BIO_free(bio_out);
>
> free(ciphertext);
> free(plaintext);
>
>
> return 0;
> }
>
> When I run it, the output which I receive is:
>
> Original plaintext: Internet is a wonderful mechanism for making
a foolof
> yourself in front of a very large audience
>
> Ciphertext:
>

92c0883c54eb8df072b43278
>


>
> Recovered plaintext: Internet is
>
>
>

d:\ananthasayanan\stuff\projects\programming\openssl\openssl-1.0.0d\demos\crypto\low_level\ciphers\symmetric\aes\aes_cfb
> 1_128\x64\Release>aes_cfb1_128.exe
> Original plaintext: Internet is a wonderful mechanism for making
a fool of
> yourself in front of a very large audience
>
> Ciphertext:
>

92c0883c54eb8df072b43278
>

00
>
> Recovered plaintext: Internet is
>
> As you can see, the ciphertext that is produced is only 12

Re: Problems with AES-CFB1

2011-11-01 Thread Ananthasayanan Kandiah
Hi,

I would be grateful if you could expand on this. I've tried simply placing
the bit length for the AES_set_encrypt_key call and it still produces the
same result.


Thanks,
Anantha

On Tue, Nov 1, 2011 at 8:10 PM, re est  wrote:

> Hi,
>
> I have tried your code and replaced the length param with bit length
> (*8) instead.
> It worked. It  seems that there are inconsistent with the usage of API.
>
> - re.est
>
> On Sun, Oct 30, 2011 at 4:55 PM, Ananthasayanan Kandiah
>  wrote:
> > Hi,
> >
> > I'm trying to use AES-CFB1 through the "low-level" calls. Here's the
> example
> > program I have come up with:
> >
> > #include 
> > #include 
> > #include 
> > #include 
> >
> > #define  KEY_SIZE 16
> >
> > int main(void)
> > {
> > inti;
> > AES_KEYkey;
> > BIO*bio_out;
> >
> > unsigned char key_data[KEY_SIZE] = {
> > 0xfe, 0xec, 0x82, 0x17, 0xb5, 0x1, 0x98, 0x6b,
> > 0x5e, 0xf1, 0xb8, 0x6, 0x52, 0x74, 0x2e, 0x52
> > };
> >
> > unsigned char iv[AES_BLOCK_SIZE];
> >
> > unsigned char const iv_data[AES_BLOCK_SIZE] = {
> > 0x10, 0x8a, 0xc9, 0x30, 0xb7, 0xf2, 0x35, 0x21,
> > 0xfb, 0xac, 0x6b, 0xdf, 0x80, 0x95, 0xeb, 0x1e
> > };
> >
> > char*   data= "Internet is a wonderful mechanism for making a
> fool "
> >   "of yourself in front of a very large audience";
> >
> >
> > int length  = (int) strlen(data);
> >
> > int num = 0;
> >
> > /* Allocate some space for the ciphertext and plaintext */
> > char*ciphertext = (char*) malloc(sizeof(char) * length);
> > char*plaintext  = (char*) malloc(sizeof(char) * length);
> >
> > /* Copy the IV data to the IV array */
> > memcpy(iv, iv_data, AES_BLOCK_SIZE);
> >
> > /* Set the encrypt key structure using the predefined key */
> > AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);
> >
> > /* Carry out the encryption */
> > AES_cfb1_encrypt(data, ciphertext, length, &key, iv, &num,
> AES_ENCRYPT);
> >
> > /* Setup output */
> > bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
> >
> > BIO_printf(bio_out, "Original plaintext: %s\n\n", data);
> >
> > BIO_printf(bio_out, "Ciphertext: ");
> >
> > /* Print out the ciphertext */
> > for (i = 0; i < length; i++)
> > BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]);
> >
> > BIO_printf(bio_out, "\n\n");
> >
> > /* Start the decryption process */
> >
> > /* First, copy the original IV data back to the IV array - as it was
> > overwritten
> >  * during the encryption process
> >  */
> > memcpy(iv, iv_data, AES_BLOCK_SIZE);
> >
> > /* Reset how far we've gone through the IV */
> > num = 0;
> >
> > /* Carry out the decryption */
> > AES_cfb1_encrypt(ciphertext, plaintext, length, &key, iv, &num,
> > AES_DECRYPT);
> >
> > BIO_printf(bio_out, "Recovered plaintext: ");
> >
> > /* print out the plaintext */
> > for (i = 0; i < length; i++)
> > BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);
> >
> > BIO_printf(bio_out, "\n\n");
> >
> > BIO_free(bio_out);
> >
> > free(ciphertext);
> > free(plaintext);
> >
> >
> > return 0;
> > }
> >
> > When I run it, the output which I receive is:
> >
> > Original plaintext: Internet is a wonderful mechanism for making a foolof
> > yourself in front of a very large audience
> >
> > Ciphertext:
> >
> 92c0883c54eb8df072b43278
> >
> 
> >
> > Recovered plaintext: Internet is
> >
> >
> >
> d:\ananthasayanan\stuff\projects\programming\openssl\openssl-1.0.0d\demos\crypto\low_level\ciphers\symmetric\aes\aes_cfb
> > 1_128\x64\Release>aes_cfb1_128.exe
> > Original plaintext: Internet is a wonderful mechanism for making a fool
> of
> > yourself in front of a very large audience
> >
> > Ciphertext:
> >
> 92c0883c54eb8df072b43278
> >
> 00
> >
> > Recovered plaintext: Internet is
> >
> > As you can see, the ciphertext that is produced is only 12 bytes in
> length.
> > Hence, the recovered plaintext has only 12 characters. I have had a look
> at
> > the source-code for AES_cfb1_encrypt and the comment says that the input
> > should be "packed". What does this mean? Am I doing something wrong or is
> > there a bug with AES-CFB1?
> >
> >
> > Thanks,
> > Anantha
> >
> >
> __
> OpenSSL Project http://www.openssl.org
> User Support Mailing Listopenssl-users@openssl.org
> Automated List Manager   majord...@openssl.org
>


Re: Problems with AES-CFB1

2011-11-01 Thread Michael S. Zick
On Mon October 31 2011, Dave Thompson wrote:
> > From: owner-openssl-us...@openssl.org On Behalf Of Michael S. Zick
> > Sent: Sunday, 30 October, 2011 06:36
> 
> > On Sun October 30 2011, Ananthasayanan Kandiah wrote:
> > > #include 
> > > #include 
> > > #include 
> > > #include 
> > > 
> > > #define  KEY_SIZE 16
> 
> > Ask the compiler to help you:
> > 
> > mszick@wolf466:~/crypto$ gcc -Wall -E aes_test.c | grep "_SIZE"
> > 
> > aes_test.c:6:9: error: macro names must be identifiers
> >     unsigned char key_data[KEY_SIZE] = {
> >     AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);
> > 
> I don't know what happened to the message you got,
> 

My copy sent by the mailing list and processed by who-knows-what
before I got it had unicode buried in the whitespaces.

> but I got "#define  KEY_SIZE 16" which is a valid identifier,
>

That is the way my eyes read it also, but gcc -E wasn't showing
it as being expanded. ()

Since I couldn't tell if what I got was what the OP was trying
to compile, I suggested the OP check its expansion themself.

> 
> compiled without error, and gave the symptom reported -- 
> because CRYPTO_cfb128_1_encrypt treats the length as bits 
>

My copy from the mailing list used: AES_cfb1_encrypt(...)
(Like the title of the post.)

Can't see how yours got like the above, unless you retyped
the post and your fingers just automatically corrected the 
function call being used.

> not bytes (apparently only used for AES and Camellia).
> 97 bits is 12 bytes plus 1 bit, and that 'extra' bit 
> is 0 and thus 'disappears'.
>

Yeah, I was thinking of suggesting "man ..." also.
Maybe I should have to have been complete.

Mike 
> Note these CFB-1 and CFB-8 modes don't update 'num', 
> so OP's line 68 is unnecessary (but harmless).
> CFB-block (16bytes=128bits) does.
> 
> 
> __
> OpenSSL Project http://www.openssl.org
> User Support Mailing Listopenssl-users@openssl.org
> Automated List Manager   majord...@openssl.org
> 
> 


__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   majord...@openssl.org


Re: Problems with AES-CFB1

2011-11-01 Thread re est
Hi,

I have tried your code and replaced the length param with bit length
(*8) instead.
It worked. It  seems that there are inconsistent with the usage of API.

- re.est

On Sun, Oct 30, 2011 at 4:55 PM, Ananthasayanan Kandiah
 wrote:
> Hi,
>
> I'm trying to use AES-CFB1 through the "low-level" calls. Here's the example
> program I have come up with:
>
> #include 
> #include 
> #include 
> #include 
>
> #define  KEY_SIZE 16
>
> int main(void)
> {
>     int            i;
>     AES_KEY        key;
>     BIO*        bio_out;
>
>     unsigned char key_data[KEY_SIZE] = {
>     0xfe, 0xec, 0x82, 0x17, 0xb5, 0x1, 0x98, 0x6b,
>     0x5e, 0xf1, 0xb8, 0x6, 0x52, 0x74, 0x2e, 0x52
>     };
>
>     unsigned char iv[AES_BLOCK_SIZE];
>
>     unsigned char const iv_data[AES_BLOCK_SIZE] = {
>     0x10, 0x8a, 0xc9, 0x30, 0xb7, 0xf2, 0x35, 0x21,
>     0xfb, 0xac, 0x6b, 0xdf, 0x80, 0x95, 0xeb, 0x1e
>     };
>
>     char*   data    = "Internet is a wonderful mechanism for making a fool "
>   "of yourself in front of a very large audience";
>
>
>     int length  = (int) strlen(data);
>
>     int num = 0;
>
>     /* Allocate some space for the ciphertext and plaintext */
>     char*    ciphertext = (char*) malloc(sizeof(char) * length);
>     char*    plaintext  = (char*) malloc(sizeof(char) * length);
>
>     /* Copy the IV data to the IV array */
>     memcpy(iv, iv_data, AES_BLOCK_SIZE);
>
>     /* Set the encrypt key structure using the predefined key */
>     AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);
>
>     /* Carry out the encryption */
>     AES_cfb1_encrypt(data, ciphertext, length, &key, iv, &num, AES_ENCRYPT);
>
>     /* Setup output */
>     bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
>
>     BIO_printf(bio_out, "Original plaintext: %s\n\n", data);
>
>     BIO_printf(bio_out, "Ciphertext: ");
>
>     /* Print out the ciphertext */
>     for (i = 0; i < length; i++)
>     BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]);
>
>     BIO_printf(bio_out, "\n\n");
>
>     /* Start the decryption process */
>
>     /* First, copy the original IV data back to the IV array - as it was
> overwritten
>  * during the encryption process
>  */
>     memcpy(iv, iv_data, AES_BLOCK_SIZE);
>
>     /* Reset how far we've gone through the IV */
>     num = 0;
>
>     /* Carry out the decryption */
>     AES_cfb1_encrypt(ciphertext, plaintext, length, &key, iv, &num,
> AES_DECRYPT);
>
>     BIO_printf(bio_out, "Recovered plaintext: ");
>
>     /* print out the plaintext */
>     for (i = 0; i < length; i++)
>     BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);
>
>     BIO_printf(bio_out, "\n\n");
>
>     BIO_free(bio_out);
>
>     free(ciphertext);
>     free(plaintext);
>
>
>     return 0;
> }
>
> When I run it, the output which I receive is:
>
> Original plaintext: Internet is a wonderful mechanism for making a foolof
> yourself in front of a very large audience
>
> Ciphertext:
> 92c0883c54eb8df072b43278
> 
>
> Recovered plaintext: Internet is
>
>
> d:\ananthasayanan\stuff\projects\programming\openssl\openssl-1.0.0d\demos\crypto\low_level\ciphers\symmetric\aes\aes_cfb
> 1_128\x64\Release>aes_cfb1_128.exe
> Original plaintext: Internet is a wonderful mechanism for making a fool of
> yourself in front of a very large audience
>
> Ciphertext:
> 92c0883c54eb8df072b43278
> 00
>
> Recovered plaintext: Internet is
>
> As you can see, the ciphertext that is produced is only 12 bytes in length.
> Hence, the recovered plaintext has only 12 characters. I have had a look at
> the source-code for AES_cfb1_encrypt and the comment says that the input
> should be "packed". What does this mean? Am I doing something wrong or is
> there a bug with AES-CFB1?
>
>
> Thanks,
> Anantha
>
>
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   majord...@openssl.org


RE: Problems with AES-CFB1

2011-10-31 Thread Dave Thompson
> From: owner-openssl-us...@openssl.org On Behalf Of Michael S. Zick
> Sent: Sunday, 30 October, 2011 06:36

> On Sun October 30 2011, Ananthasayanan Kandiah wrote:
> > #include 
> > #include 
> > #include 
> > #include 
> > 
> > #define  KEY_SIZE 16

> Ask the compiler to help you:
> 
> mszick@wolf466:~/crypto$ gcc -Wall -E aes_test.c | grep "_SIZE"
> 
> aes_test.c:6:9: error: macro names must be identifiers
>     unsigned char key_data[KEY_SIZE] = {
>     AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);
> 
I don't know what happened to the message you got, 
but I got "#define  KEY_SIZE 16" which is a valid identifier, 
compiled without error, and gave the symptom reported -- 
because CRYPTO_cfb128_1_encrypt treats the length as bits 
not bytes (apparently only used for AES and Camellia).
97 bits is 12 bytes plus 1 bit, and that 'extra' bit 
is 0 and thus 'disappears'.

Note these CFB-1 and CFB-8 modes don't update 'num', 
so OP's line 68 is unnecessary (but harmless).
CFB-block (16bytes=128bits) does.


__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   majord...@openssl.org


Re: Problems with AES-CFB1

2011-10-30 Thread Michael S. Zick
On Sun October 30 2011, Ananthasayanan Kandiah wrote:
> #include 
> #include 
> #include 
> #include 
> 
> #define  KEY_SIZE 16
> 
> int main(void)
> {
>     int            i;
>     AES_KEY        key;
>     BIO*        bio_out;
> 
>     unsigned char key_data[KEY_SIZE] = {
>         0xfe, 0xec, 0x82, 0x17, 0xb5, 0x1, 0x98, 0x6b,
>         0x5e, 0xf1, 0xb8, 0x6, 0x52, 0x74, 0x2e, 0x52
>     };
>

Ask the compiler to help you:

mszick@wolf466:~/crypto$ gcc -Wall -E aes_test.c | grep "_SIZE"

aes_test.c:6:9: error: macro names must be identifiers
    unsigned char key_data[KEY_SIZE] = {
    AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);

Mike 
>     unsigned char iv[AES_BLOCK_SIZE];
> 
>     unsigned char const iv_data[AES_BLOCK_SIZE] = {
>         0x10, 0x8a, 0xc9, 0x30, 0xb7, 0xf2, 0x35, 0x21,
>         0xfb, 0xac, 0x6b, 0xdf, 0x80, 0x95, 0xeb, 0x1e
>     };
> 
>     char*   data    = "Internet is a wonderful mechanism for making a fool
> "
>                       "of yourself in front of a very large audience";
> 
> 
>     int     length  = (int) strlen(data);
> 
>     int     num = 0;
> 
>     /* Allocate some space for the ciphertext and plaintext */
>     char*    ciphertext = (char*) malloc(sizeof(char) * length);
>     char*    plaintext  = (char*) malloc(sizeof(char) * length);
> 
>     /* Copy the IV data to the IV array */
>     memcpy(iv, iv_data, AES_BLOCK_SIZE);
> 
>     /* Set the encrypt key structure using the predefined key */
>     AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);
> 
>     /* Carry out the encryption */
>     AES_cfb1_encrypt(data, ciphertext, length, &key, iv, &num, AES_ENCRYPT);
> 
>     /* Setup output */
>     bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
> 
>     BIO_printf(bio_out, "Original plaintext: %s\n\n", data);
> 
>     BIO_printf(bio_out, "Ciphertext: ");
> 
>     /* Print out the ciphertext */
>     for (i = 0; i < length; i++)
>         BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]);
> 
>     BIO_printf(bio_out, "\n\n");
> 
>     /* Start the decryption process */
> 
>     /* First, copy the original IV data back to the IV array - as it was
> overwritten
>      * during the encryption process
>      */
>     memcpy(iv, iv_data, AES_BLOCK_SIZE);
> 
>     /* Reset how far we've gone through the IV */
>     num = 0;
> 
>     /* Carry out the decryption */
>     AES_cfb1_encrypt(ciphertext, plaintext, length, &key, iv, &num,
> AES_DECRYPT);
> 
>     BIO_printf(bio_out, "Recovered plaintext: ");
> 
>     /* print out the plaintext */
>     for (i = 0; i < length; i++)
>         BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);
> 
>     BIO_printf(bio_out, "\n\n");
> 
>     BIO_free(bio_out);
> 
>     free(ciphertext);
>     free(plaintext);
> 
> 
>     return 0;
> }
> 


__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   majord...@openssl.org


Problems with AES-CFB1

2011-10-30 Thread Ananthasayanan Kandiah
Hi,

I'm trying to use AES-CFB1 through the "low-level" calls. Here's the
example program I have come up with:

#include 
#include 
#include 
#include 

#define  KEY_SIZE 16

int main(void)
{
inti;
AES_KEYkey;
BIO*bio_out;

unsigned char key_data[KEY_SIZE] = {
0xfe, 0xec, 0x82, 0x17, 0xb5, 0x1, 0x98, 0x6b,
0x5e, 0xf1, 0xb8, 0x6, 0x52, 0x74, 0x2e, 0x52
};

unsigned char iv[AES_BLOCK_SIZE];

unsigned char const iv_data[AES_BLOCK_SIZE] = {
0x10, 0x8a, 0xc9, 0x30, 0xb7, 0xf2, 0x35, 0x21,
0xfb, 0xac, 0x6b, 0xdf, 0x80, 0x95, 0xeb, 0x1e
};

char*   data= "Internet is a wonderful mechanism for making a fool
"
  "of yourself in front of a very large audience";


int length  = (int) strlen(data);

int num = 0;

/* Allocate some space for the ciphertext and plaintext */
char*ciphertext = (char*) malloc(sizeof(char) * length);
char*plaintext  = (char*) malloc(sizeof(char) * length);

/* Copy the IV data to the IV array */
memcpy(iv, iv_data, AES_BLOCK_SIZE);

/* Set the encrypt key structure using the predefined key */
AES_set_encrypt_key(key_data, KEY_SIZE * 8, &key);

/* Carry out the encryption */
AES_cfb1_encrypt(data, ciphertext, length, &key, iv, &num, AES_ENCRYPT);

/* Setup output */
bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);

BIO_printf(bio_out, "Original plaintext: %s\n\n", data);

BIO_printf(bio_out, "Ciphertext: ");

/* Print out the ciphertext */
for (i = 0; i < length; i++)
BIO_printf(bio_out, "%02x", ((unsigned char*)ciphertext)[i]);

BIO_printf(bio_out, "\n\n");

/* Start the decryption process */

/* First, copy the original IV data back to the IV array - as it was
overwritten
 * during the encryption process
 */
memcpy(iv, iv_data, AES_BLOCK_SIZE);

/* Reset how far we've gone through the IV */
num = 0;

/* Carry out the decryption */
AES_cfb1_encrypt(ciphertext, plaintext, length, &key, iv, &num,
AES_DECRYPT);

BIO_printf(bio_out, "Recovered plaintext: ");

/* print out the plaintext */
for (i = 0; i < length; i++)
BIO_printf(bio_out, "%c", ((unsigned char*)plaintext)[i]);

BIO_printf(bio_out, "\n\n");

BIO_free(bio_out);

free(ciphertext);
free(plaintext);


return 0;
}

When I run it, the output which I receive is:

Original plaintext: Internet is a wonderful mechanism for making a foolof
yourself in front of a very large audience

Ciphertext:
92c0883c54eb8df072b43278


Recovered plaintext: Internet is


d:\ananthasayanan\stuff\projects\programming\openssl\openssl-1.0.0d\demos\crypto\low_level\ciphers\symmetric\aes\aes_cfb
1_128\x64\Release>aes_cfb1_128.exe
Original plaintext: Internet is a wonderful mechanism for making a fool of
yourself in front of a very large audience

Ciphertext:
92c0883c54eb8df072b43278
00

Recovered plaintext: Internet is

As you can see, the ciphertext that is produced is only 12 bytes in length.
Hence, the recovered plaintext has only 12 characters. I have had a
look atthe source-code for AES_cfb1_encrypt
and the comment says that the input should be "packed". What does this
mean? Am I doing something wrong or is there a bug with AES-CFB1?


Thanks,
Anantha