Pages: 1 [2]   Go Down
Author Topic: new AES library  (Read 12029 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great lib! Thanks MarkT!

Does it work for larger keys, like 1024 or 2048 ?

I've written an AES (Advanced Encryption Standard) library for Arduino.  It supports 128, 192 and 256 bit key sizes.  Code space overhead is about 4KB I think, each instance requires 240 bytes RAM for subkeys.  Fairly tightly coded and checked against official test vectors for ECB mode.  Also CBC mode supported.

Its derived from Brian Gladman's implementation for byte-oriented processors but signficantly modified.  See the README and examples.

Functions:
Code:
  byte set_key (byte key[], int keylen) ;
  void clean () ;  // delete key schedule after use
  void copy_n_bytes (byte * dest, byte * src, byte n) ;

  byte encrypt (byte plain [N_BLOCK], byte cipher [N_BLOCK]) ;
  byte cbc_encrypt (byte * plain, byte * cipher, int n_block, byte iv [N_BLOCK]) ;

  byte decrypt (byte cipher [N_BLOCK], byte plain [N_BLOCK]) ;
  byte cbc_decrypt (byte * cipher, byte * plain, int n_block, byte iv [N_BLOCK]) ;

Lives here: http://utter.chaos.org.uk:/~markt/AES-library.zip

Feedback welcome.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am working on simple encryption on my Arduino to be able to talk back and forth with web service, and stumbled upon this useful library. However, I have few questions regarding the CRC_decrypt and encrypt.

I got this example from somewhere in the thread and modify in a little bit.

Code:
#include <AES.h>

#define KEYLENGTH 32  // this means 32 bit encryption only following values are allowed 16, 128, 24, 192, 32, 256
AES aes;
char PassString[] = "This is hard to believe but true however";// this must be at least KEYLENGTH characters long
byte my_iv[N_BLOCK] =
{
  1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
} ;
byte key[KEYLENGTH];
void setup()
{
Serial.begin(115200);

Serial.println("Starting AES test");
for (int i = 0; i < KEYLENGTH; i++)
{
key[i] = PassString[i];
}

if (aes.set_key(key, KEYLENGTH) != 0)
{
Serial.println(F("Failed to set key"));
}

}

// The loop function is called in an endless loop
void loop()
{

char Message[] = "A top secret message. 123456789012345678901234";
byte plain[N_BLOCK];
byte cipher[N_BLOCK];
byte decrypted[N_BLOCK];

  byte iv [N_BLOCK] ;
  
Serial.print(F("message : '"));
Serial.print(Message);
Serial.println(F("'"));
Serial.print(F("plain binary: '"));
for (int i = 0; i < N_BLOCK; i++)
{
plain[i] = Message[i];
cipher[i] = 0;
decrypted[i] = 0;
Serial.print(plain[i]);
}
Serial.println(F("'"));

Serial.print(F("plain char: '"));
for (int i = 0; i < N_BLOCK; i++)
{
Serial.print(char(plain[i]));
}
Serial.println(F("'"));

for (byte i = 0 ; i < 16 ; i++)
      iv[i] = my_iv[i] ;
if (aes.cbc_encrypt(plain, cipher,4,iv) == 0)
{
Serial.print(F("encrypted : '"));
for (int i = 0; i < N_BLOCK; i++)
{
Serial.print(cipher[i]);
}
Serial.println(F("'"));
} else
{
Serial.println(F("Failed to encrypt"));
}

if (aes.cbc_decrypt(cipher, decrypted,4,iv) == 0)
{
Serial.print(F("decrypted binary : '"));
for (int i = 0; i < N_BLOCK; i++)
{
Serial.print(decrypted[i]);
}
Serial.println(F("'"));

Serial.print(F("decrypted char : '"));
for (int i = 0; i < N_BLOCK; i++)
{
Serial.print(char(decrypted[i]));
}
Serial.println(F("'"));
} else
{
Serial.println(F("Failed to decrypt"));
}

delay(5000);
}

The result seemed to be stable, as in I get it decrypt and encrypt. How do I print those bytes in readable character? encrypted and decrypted plain text. if I cannot have this printed in ascii format, I cannot prove that I can decrypt the message in the web server.

Thanks.
« Last Edit: July 03, 2014, 09:57:29 am by dkosasih » Logged

0
Offline Offline
Shannon Member
****
Karma: 198
Posts: 11620
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As hexadecimal is conventional.
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mark, 
Sorry,  I'm afraid I don't understand what you mean.  : smiley-red
Logged

0
Offline Offline
Shannon Member
****
Karma: 198
Posts: 11620
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you have binary data to display, it is conventional to display it in
hexadecimal.

What exactly are you wanting to achieve and do you understand about
cryptography?
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is what I try to achieve.
I tried to encrypt "dkosasih" in node.js I got 1200d4366608c5e16de242ea5aae753e.
I would've thought that it will work the same way in arduino.
Instead, when I try to encrypt the same word in arduino, I get numbers.

How do I send those number, in the form of '1200d4366608c5e16de242ea5aae753e' so that I can send them through the network and be able to be decrypted by the node.js and vice versa.
I hope I am making sense here.
Logged

Pages: 1 [2]   Go Up
Jump to: