Go Down

Topic: new AES library (Read 65337 times) previous topic - next topic

paul393

The code is written for 8-bit microcontrollers as the comments mention.

You'll have to look for 8-bit and 16-bit assumptions if you want to port it
to a 32 bit machine.
Thanks for the reply!

I've found some pseudo code for AES ( http://www.cs.utsa.edu/~wagner/laws/AESintro.html ) that has helped me understand this some more -- but I'm still struggling to find where the '8 bit assumptions' are precisely.

It seems from what I can find one difference between the 8 and 32 bit micros with Arduino is the size of int variables (16 and 32 bit, respectively). Are variables such as the two below therefore the ones causing me problems in this library?

int n_block
int keylen

For instance the line:
byte next = keylen ;

byte is 8 bits regardless, but the size of keylen is different between the 8 and 32 bit systems. Is this my problem?

Any additional pointers would be greatly appreciated as I try to work through this.










d3str0y3rport

Hi could you help me making this library able for the Arduino Zero

Thank you!!!

pYro_65

This library is intended for 8 bit CPU's. You'll probably have better performance finding a 32-bit version.

However you should be able to go through and change the data types (int) to their AVR sized fixed width equivalents (int becomes int16_t and such).
Forum Mod anyone?
https://arduino.land/Moduino/

gpra

Hi MarkT,

sorry but your lib don't work correct.
I've do decode AES128 CBC, encoded from another device. Only the first block is correct, all others wrong.
Please check against
http://aes.online-domain-tools.com/

Here you find usefull info and test-vectors:
http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf

Greetings,
gprand

zasf

Hello gpra,

it would be nice from you if you could provide the proof of what you're saying, for example some code that shows incorrect blocks compared to test vectors :)

MarkT

#35
Oct 19, 2015, 05:01 am Last Edit: Oct 19, 2015, 05:05 am by MarkT
Was this the exact same CBC mode and with the precise IV in the right endianess to match
my code?  All these details matter, you have to check.  Perhaps you could provide a specific
example case referenced to the documentation?

Have you run the example that runs the test vectors?  Does it work for you?  Seen my #2 post?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

gpra

Hi,

yes, I'll evaluate an example in the next days. Test program "aes.pde" did run correct. But it does encrypt and then decrypt, this seems to compensate the difference.

Greetings,

gpra
 

zasf

In the "aes" example, line:

Code: [Select]
succ = aes.decrypt (cipher, plain) ;

should read:

Code: [Select]
succ = aes.decrypt (cipher, check) ;

With original aes example code, when running:
Code: [Select]
void prekey_test ()
{
  prekey (128, 4) ;
  prekey (192, 3) ;
  prekey (256, 2) ;
  prekey (128, 1) ;
  prekey (192, 1) ;
  prekey (256, 1) ;
}


check variable is untouched after prekey(256,2), that is why if you comment statements before prekey(128, 1), right decryption won't be shown. In order to see it, you also should change plain to something like:

Code: [Select]
byte plain[] =
{
  0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
} ;


Is it possible that variable names are inverted in decrypt function in AES.cpp? Line:

Code: [Select]
byte AES::decrypt (byte plain [N_BLOCK], byte cipher [N_BLOCK])
{
...
}


should read:

Code: [Select]
byte AES::decrypt (byte cipher [N_BLOCK], byte plain [N_BLOCK])
{
...
}

relic1974

I too am very new to AES. I've learned enough to know basically how it works, but do not quite understand the methods in the arduino library. I'd like to ask for a simple example of encrypting/decrypting a 16 character string.

Could someone respond with some syntax using this AES library to do just that? Sorry but even looking at the library files themselves I am still somewhat lost.

I'm unsure how to set the 'password' and for my purposes I would ALWAYS be encrpyting 16 characters @ 128 bit. If I had an example using the libraries keywords/methods it would really help me to understand the rest of it.

A good example would be 128 bit encrypt/decryption of the string "abcdefghijklmnop" with a password of "panthers".

Any example or help would be greatly appreciated and would help me to understand the library better.

bigpjo

Excellent, library very quick and efficient however, there is an issue with the CBC decrypt, below is my code running on arduino

Code: [Select]
#include <Base64.h>
#include <AES.h>

AES aes ;

byte key[] =
{
  12,13,14,15,15,16,17,18,19,10,11,12,13,14,15,16
 ,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
} ;

byte my_iv[] =
{
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
} ;

//byte cipher [4*N_BLOCK] ;
byte check [4*N_BLOCK] ;

void loop ()
{}


void setup ()
{
  Serial.begin (57600) ;

  delay(30000);
 
  Serial.println("testing mode") ;

  byte succ = aes.set_key (key, 256) ;

  byte iv [N_BLOCK] ;
  for (byte i = 0 ; i < 16 ; i++)
      iv[i] = my_iv[i] ;

  int decodedLen = 32;
  byte decoded[] = {70,33,158,65,106,36,73,238,61,225,69,115,121,47,148,114,238,92,183,97,37,152,168,11,248,112,41,187,179,2,119,250};

  succ = aes.cbc_decrypt (decoded, check, (decodedLen/N_BLOCK), iv) ;

  aes.clean();

  for(int i=0; i<decodedLen;i++)
  {
    Serial.print((char)check[i]);
  }
  Serial.println("");
}




This is decrypted but not correctly, the second block, first byte us incorrect, decrypted the string should read Decoded hello world encrypted!!! but this library decodes as Decoded hello wnrld encrypted!!!

The encrypted text is from a C# console app

Code: [Select]
using System;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sandbox
{
    class Program
    {
        static void Main(string[] args)
        {
            var isLE = BitConverter.IsLittleEndian;


            byte[] encrypData = System.Text.Encoding.UTF8.GetBytes("Decoded hello world encrypted!!!");

            using (AesManaged crypto = new AesManaged())
            {
                crypto.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                crypto.Key = new byte[] {12,13,14,15,15,16,17,18,19,10,11,12,13,14,15,16
                                        ,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
                crypto.Mode = CipherMode.CBC;

                using (var encryptor = crypto.CreateEncryptor())
                {
                    var outBuffer = new byte[32];

                    encryptor.TransformBlock(encrypData, 0, 32, outBuffer, 0);

                }
            }
        }
    }
}


Any ideas what could be causing the issue? I have checked from previous threads about endian and I can confirm both are little.

bigpjo

After my previous post I noticed an issue with my_iv, had a 01 as last byte and should have been 00, corrected and can confirm the library works 100%, nice work MARKT

alias33

Hi
I have quastion about library "AES.h " .

I am use arduino DUE
how I can do this with your library ?
enter voice by microphone in (A0)
the voice out encryption by " AES" from (DAC0)
the encryption voice enter to (A1)
then the voice out decryption from ( DAC1)



Alejandr010

Hi at all, I'm trying to use this aes lib on my arduino uno to encrypt and decrypt a string, how can I do? thank you

Altelectronics

Hi Mark,

I might came across a minor bug (with a workaround) in your library.
how to reproduce
if you run your example code with the following calls

void prekey_test ()
{
  //prekey (128, 2) ;
  //prekey (192, 3) ;
  //prekey (256, 2) ;
  //prekey (128, 1) ;
  //prekey (192, 1) ;
  //prekey (256, 1) ;
  prekey (256, 1) ;
}

you get

testng modeset_key 256 ->0 took 748us
encrypt 0 took 716us
decrypt 0 took 968us
F3 44 81 EC 3C C6 27 BA CD 5D C3 FB 08 F2 73 E6
63 60 DD 58 F0 F1 A4 93 8B 40 6A 8E AA 7E 0A 14
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


so the decrypt for block==1 fails
however if you run the following calls

void prekey_test ()
{
  //prekey (128, 2) ;
  //prekey (192, 3) ;
  prekey (256, 2) ;
  //prekey (128, 1) ;
  //prekey (192, 1) ;
  //prekey (256, 1) ;
  prekey (256, 1) ;
}


then the both work fine

testng modeset_key 256 ->0 took 748us
encrypt 0 took 1492us
decrypt 0 took 2044us
F3 44 81 EC 3C C6 27 BA CD 5D C3 FB 08 F2 73 E6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
6A BB C9 E8 A0 FD 6D E7 FB 8A A5 D8 A6 CF 4C 7D D3 2B 9D 08 7B AA 9D 43 09 56 B0 D9 21 0B 62 C8
F3 44 81 EC 3C C6 27 BA CD 5D C3 FB 08 F2 73 E6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
D3 2B 9D 08 7B AA 9D 43 09 56 B0 D9 21 0B 62 C8
set_key 256 ->0 took 752us
encrypt 0 took 708us
decrypt 0 took 972us
F3 44 81 EC 3C C6 27 BA CD 5D C3 FB 08 F2 73 E6
63 60 DD 58 F0 F1 A4 93 8B 40 6A 8E AA 7E 0A 14
F3 44 81 EC 3C C6 27 BA CD 5D C3 FB 08 F2 73 E6


not a big deal - but it might worth a look.
Thanks for the great work!

Best Regards,
Aurelian














Go Up