davylandman / aeslib Goto Github PK
View Code? Open in Web Editor NEWArduino Library for AES Encryption (source based on avr-crypto-lib)
License: GNU General Public License v3.0
Arduino Library for AES Encryption (source based on avr-crypto-lib)
License: GNU General Public License v3.0
Good afternoon. Tell me if there is a similar library for esp32. Thank you.
Hey guys, I am really struggling and could use some help. I am trying to encrypt a message using javascript using crypto-js and then decrypt on my Arduino using the AESLib. I have figured out how to set the key and iv in javascript but need to be able to use the same key and iv. Here is a my sample encryption code in javascript:
<script src="../aes.js"></script>
<script>
function run(){
var keyInHex='61626364656667686962656762666973';
var key = CryptoJS.enc.Hex.parse(keyInHex);
var hex = CryptoJS.enc.Hex.stringify(key);
var iv = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');
var encrypted = CryptoJS.AES.encrypt("Some secret Mess", key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
document.getElementById("p1").innerHTML = "Encrypted ciphertext: " + encrypted.ciphertext;
</script>
}
I am not really sure how to implement the decryption using this library to decode this message. I have googled lots and am really struggling with decrypting this on the Arduino. I know CryptoJS is a little more flexible and I am able to match the encryption settings in Javascript to match a configuration which AESLib is able to handle. At the end of the day I need to encrypt a message using CryptoJS and decrypt it on the Arduino platform. Any help is greatly appreciated.
this code work on lora 32u4?
I am trying to use the AESLib with Arduino 1.0.1 IDE
and the Arduino UNO
but I ran in to some syntax errors.
AES_test.cpp: In function ‘void setup()’:
AES_test:30: error: expected unqualified-id before ‘[’ token
AES_test:32: error: expected unqualified-id before ‘[’ token
AES_test:33: error: ‘key’ was not declared in this scope
AES_test:33: error: ‘data’ was not declared in this scope
#include <AESLib.h>
#include <aes256_dec.h>
#include <aes.h>
#include <aes_dec.h>
#include <bcal_aes128.h>
#include <aes_keyschedule.h>
#include <bcal-ofb.h>
#include <aes192_enc.h>
#include <bcal-cmac.h>
#include <aes128_dec.h>
#include <aes_sbox.h>
#include <bcal_aes256.h>
#include <bcal-basic.h>
#include <gf256mul.h>
#include <blockcipher_descriptor.h>
#include <aes_enc.h>
#include <aes256_enc.h>
#include <memxor.h>
#include <bcal-cbc.h>
#include <aes_types.h>
#include <aes_invsbox.h>
#include <aes192_dec.h>
#include <keysize_descriptor.h>
#include <aes128_enc.h>
#include <bcal_aes192.h>
void setup()
{
uint8_t[] key = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
Serial.begin(57600);
char[] data = "0123456789012345"; //16 chars == 16 bytes
aes128_enc_single(key, data);
Serial.print("encrypted:");
Serial.println(data);
aes128_dec_single(key, data);
Serial.print("decrypted:");
Serial.println(data);
}
void loop() {}
(hope this is the right place for this?)
Davy,
I was hoping you might be able to provide a brief snippet of code that shows how to use the CBC mode? I'm trying to use 128-bit CBC to encrypt a file on my PC and then transfer the data to the arduino, where it will be decrypted. For reference, I'm using the following command on my laptop (Linux) to encrypt the file:
openssl aes-128-cbc -salt -a -e -in message.txt -out message.enc
Additionally, at the moment when I include the AESLib.h library, all the Arduino IDE does on a compile is spit out this error:
avr-gcc: error: unrecognized command line option ‘-assembler-with-cpp’
Any help greatly appreciated!
Since it is assumed that a byte is 8 bits and we iterate over the data byte-per-byte, is there a reason to use a pointer to void
instead of pointers to uint8_t
?
Doing so would avoid warnings such as warning: dereferencing void * pointer
(which happens here and here) when passing data around and makes all the types somewhat more standardized against the stdint.h
library.
hi ....
file aes128_dec_single not found~~~!!!
Thank you for this library, very useful and much more simple to use than others! However I have had one issue when compiling on Arduino IDE, I have used your example below;
Serial.begin(57600);
uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char data[] = "0123456789012345"; //16 chars == 16 bytes
aes128_enc_single(key, data);
Serial.print("encrypted:");
Serial.println(data);
aes128_dec_single(key, data);
Serial.print("decrypted:");
Serial.println(data);
And I am getting the error "missing 'recipe.S.o.pattern' configuration parameter" when trying to compile. Have you seen this issue before or know a fix for it?
Kind Regards!
Hi, will there be any support for 32bit SAM arduinos?
When I try to compile the example sketch, I get the following error:
avr-gcc: eror: unrecognized command line option '-assembler-with-cpp'
I do not get the same error when using other libraries.
Edit: i use arduino 1.0.5 on linux
I tried to include this library to my project but the IDE generates following error:
processing.app.debug.RunnerException
at processing.app.debug.Compiler.getCommandCompilerS(Compiler.java:498)
at processing.app.debug.Compiler.compileFiles(Compiler.java:201)
at processing.app.debug.Compiler.compileLibraries(Compiler.java:590)
at processing.app.debug.Compiler.compile(Compiler.java:97)
at processing.app.Sketch.build(Sketch.java:1572)
at processing.app.Sketch.build(Sketch.java:1551)
at processing.app.Editor$DefaultRunHandler.run(Editor.java:1925)
at java.lang.Thread.run(Thread.java:619)
I started a new thread in the Arduino forum (http://forum.arduino.cc/index.php?topic=166133.0) and wrote about the problem.
EDIT:
I downloaded the source code of Arduino 1.52 beta and noticed that RunnerException triggers when compilation error occurs.
I've tried adding the include of avr/io.h. But I get a compiler error with that too saying it cant find avr/io.h.
Compiler Output:
In file included from D:\Program Files (x86)\Arduino\libraries\AESLib\aes_dec-asm_faster.S:28:0:
D:\Program Files (x86)\Arduino\libraries\AESLib\avr-asm-macros.S:31:20: fatal error: avr/io.h: No such file or directory
#include <avr/io.h>
^
compilation terminated.
exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).
First of all, thanks for this great library and apologies if this bug is entirely my doing. I am getting some odd crashing in a sketch of mine, and after several days of searching I whittled it down to this. The normal ECB AES128 works fine all the time (as far as I can tell), but CBC sometimes crashes. The only cause I can find is instantiating the class EthernetClient (from UIPEthernet.h). It may be the case that it's the fault of that library, but it crashes when the aes128_cbc_enc (or aes192_cbc_enc) function is called.
Sketch I used:
#include <Arduino.h>
#include <AESLib.h>
#include <UIPEthernet.h>
const uint8_t key[16] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'};
//const uint8_t key[24] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'};
//const uint8_t key[32] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'};
// Comment this out and it works
EthernetClient client;
void setup() {
Serial.begin(9600);
Serial.println("Starting EBC test");
uint8_t key[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
char data[] = "0123456789012345"; //16 chars == 16 bytes
aes128_enc_single(key, data);
Serial.print("encrypted:");
Serial.println(data);
aes128_dec_single(key, data);
Serial.print("decrypted:");
Serial.println(data);
Serial.println("Finished EBC test without seg fault");
delay(100);
cbcTest();
}
void loop() {}
void cbcTest() {
Serial.println("Entered cbcTest()");
//char data[] = "0123456789012345"; //16 chars == 16 bytes
uint16_t dataLength = 96;
char data[] = "abcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnop";
uint16_t keyLength = 16;
uint8_t iv[16] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'};
//uint8_t iv[24] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P','Q','R','S','T','U','V','W','X'};
//uint8_t iv[32] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'};
Serial.print("Plaintext: "); Serial.write(data, dataLength); Serial.println("");
Serial.print("key: "); Serial.write(key, keyLength); Serial.println("");
Serial.print("IV: "); Serial.write(iv, keyLength); Serial.println("");
delay(1000);
aes128_cbc_enc(key, iv, data, dataLength);
Serial.print("Ciphertext: "); Serial.write(data, dataLength); Serial.println("");
delay(100);
Serial.println("Finished cbcTest() without seg fault");
delay(100);
}
Here's the output from my included code:
Starting EBC test
encrypted:�%ì›]˜?÷ßE¤¨�žiü
decrypted:0123456789012345
Finished EBC test without seg fault
Entered cbcTest()
Plaintext: abcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnop
key: aaaaaaaaaaaaaaaa
IV: ABCDEFGHIJKLMNOP
Ciphertext: <lots of non-ASCII friendly characters>
Finished cbcTest() without seg fault
If you think this is a stupid question, or that the error is most likely in the other library could you tell me how you debug libraries such as your own (or even UIPEthernet.h)? The usual trick of using Serial.print is unsuitable for c libraries (I think).
encrypted_length is being redefined causing compilation failrues:
.pio/libdeps/nodemcu-32s/AESLib/src/AESLib.cpp: In member function 'uint16_t AESLib::encrypt64(const byte*, uint16_t, char*, const byte*, int, byte*)':
.pio/libdeps/nodemcu-32s/AESLib/src/AESLib.cpp:107:12: error: redeclaration of 'uint16_t encrypted_length'
uint16_t encrypted_length = base64_encode(output, (char *)cipher, aes.get_size() );
^~~~~~~~~~~~~~~~
.pio/libdeps/nodemcu-32s/AESLib/src/AESLib.cpp:103:12: note: 'uint16_t encrypted_length' previously declared here
uint16_t encrypted_length = aes.get_size();
The same bytes encrypted using AESLib with the same key, initialization vector and encryption mode have a different output than Java or Nodejs. Follows the example code:
#include <AESLib.h>
const PROGMEM char HEX_VALUES[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
#define CBLOCK (1 * N_BLOCK) + 1
char serialBuffer[120];
void setup() {
Serial.begin(115200);
}
void loop() {
crypt();
delay(10000);
}
void ByteToHexString(char * hexStrParam, unsigned char * byteArrayParam, unsigned int byteArrayLength)
{
unsigned char num;
for (int i = 0, u = 0; i < byteArrayLength; i++, u++)
{
num = byteArrayParam[i] >> 4;
hexStrParam[u] = (char)pgm_read_byte(HEX_VALUES + num);
num = byteArrayParam[i] & 0xf;
hexStrParam[++u] = (char)pgm_read_byte(HEX_VALUES + num);
}
}
void crypt() {
uint8_t key[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
uint8_t iv[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
char data[16];
aes_context ctx;
char auxBuffer[129];
memset(data, 0x00, 16);
memcpy(data, "rinaldi", 7);
ctx = aes128_cbc_enc_start((const uint8_t*)key, iv);
aes128_cbc_enc_continue(ctx, data, 16);
aes128_cbc_enc_finish(ctx);
memset(auxBuffer, 0x00, 129);
ByteToHexString(auxBuffer, (unsigned char *)data, sizeof(data));
sprintf(serialBuffer, "encrypted-cbc: %s", auxBuffer);
Serial.println(serialBuffer);
ctx = aes128_cbc_dec_start((const uint8_t*)key, iv);
aes128_cbc_dec_continue(ctx, data, 16);
aes128_cbc_dec_finish(ctx);
sprintf(serialBuffer, "decrypted-cbc: %s", data);
Serial.println(serialBuffer);
memset(auxBuffer, 0x00, 129);
ByteToHexString(auxBuffer, (unsigned char *)iv, sizeof(iv));
sprintf(serialBuffer, "iv: %s", auxBuffer);
Serial.println(serialBuffer);
}
Output:
encrypted-cbc: 2FD27523B3E1B5A122F397A5F976A680
decrypted-cbc: rinaldi
iv: 000102030405060708090A0B0C0D0E0F
This lines can be run with Nodejs:
var crypto = require("crypto");
var key = new Buffer([ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 ]);
var iv = new Buffer([ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 ]);
var cipher = crypto.createCipheriv("aes-128-cbc", key, iv);
var crypted = Buffer.concat([cipher.update(Buffer.from("rinaldi")), cipher.final()]);
var encryptedHex = crypted.toString('hex')
encryptedHex
Or execute this method with Java:
public static String encrypt() {
try {
byte key[] = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
byte iv[] = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv));
byte[] encrypted = cipher.doFinal("rinaldi".getBytes());
return Utils.HexString(encrypted);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
The output from both Nodejs and Java give me: 58346027133deead261e621b676f1bf6
The example given was only using the single one. Your help is much appreciated!
Hello
I used to your project to encrypt messadge "EEEEEEEEEEEEEEEE" : result was "xi6v "
Shouldn't this algoright return 16 bytes of encrypted data?
Thanks
I tested your Lib with the ESP8266 but it dont complie.
Is the a way to implement it without ASM?
how can we use it on lora replay attack?
Hi Davy,
First, thanks for the fabulous AES library. It's very easy to use on the Arduino platform. Excellent work.
That said, I could use your help. I'm moving data encrypted by your library from the Arduino to a Windows 8.1 platform where I want to decrypt it using a c# application. As of this moment, I've been unsuccessful doing so.
The following snippets show the encryption and base64 encoding of the encrypted char array on the arduino, which is then transferred over serial to the application on the Windows machine, where the c# Decrypt function shown below is executed.
However, I get "The input data is not a complete block" errors in the c# application when trying to decrypt the data and after many hours of trying to work through a solution, I'm hoping you can point me in the right direction.
Can you help? Thanks so much.
Nate
// Arduino
#include <AESLib.h>
#include <Base64.h>
void setup()
{
Serial.begin(9600);
}
void loop()
{
char data[17] = "1234123412341234";
const uint8_t key[] PROGMEM = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7};
aes128_enc_single(key, data);
char b64Data[base64_enc_len(sizeof(data))];
base64_encode(b64Data, data, sizeof(data));
}
// C#
public static string Decrypt(string cipherText)
{
string EncryptionKey = "1234567891234567";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
encryptor.Mode = CipherMode.CBC;
encryptor.Padding = PaddingMode.None;
Rfc2898DeriveBytes pdb = new
Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
[END C# CODE SNIPPET]
Running the example will ALWAYS report success but it is a false success report. Even if you change the line decLen = decrypt(...) to decLen = 0, it will report a successful decrypt though it didn't even decrypt it. Actually, I don't think it could ever work. The library indicates that encrypt and decrypt DO NOT use Base64 encoding but the example "simple" does a base64 encode before calling decrypt.
hi
i want to know if there is a solutions for the data that the amount superior than 16 bits
thanks
I was trying to use the library on arduino MEGA 2560 and I found this error could you please help me out.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.