mpyaes - utility library around ucryptolib.aes

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Iyassou
Posts: 40
Joined: Sun Jun 26, 2016 9:15 am

mpyaes - utility library around ucryptolib.aes

Post by Iyassou » Sat Sep 12, 2020 2:50 pm

Hello all.

mpyaes (GitHub) is a utility library around ucryptolib's aes class. It handles AES encryption and decryption of bytes-like objects and files, and implements PKCS7 padding, which is handled in the background.

Encrypting and decrypting files makes use of a block-sized buffer and memoryview to not consume too much memory. Decryption is always zero-copy, made possible by decrypting in place and returning uctypes.bytearray_at up to where the padding begins.

Tested and working are:
  • 128-bit ECB mode
  • 128-bit CBC mode
  • 256-bit ECB mode
  • 256-bit CBC mode
Support for CTR mode is there, but I couldn't test it on my ESP32. Thoughts appreciated.

User avatar
rcolistete
Posts: 338
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: mpyaes - utility library around ucryptolib.aes

Post by rcolistete » Sun Sep 13, 2020 6:35 pm

Thanks, with your module it is a lot easier to encrypt/decrypt text and files.
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

vodkawasserfall
Posts: 3
Joined: Tue Feb 02, 2021 3:10 am

Re: mpyaes - utility library around ucryptolib.aes

Post by vodkawasserfall » Tue Feb 02, 2021 4:15 am

hi there!

is it possible to encode the bytearray to something like hex to be compatible with a php approach ?

my goal is to exchange encrypted strings/json between a php/webserver and an ESP32

Code: Select all

bytearray(b'\xe4\xb3\x90\xc3\x0b\x80%\xb3\xc2\n\xc3nY\xdfv\xc9\xd3X8\x82Y\xd8\xd7\xbc\xd0\xafP\xbdJ~\xe5\xdf\x8a\xbc\x9cU\xfd\xa3\x9a\x8d\x1a\xed\xdd\x99\x9a\xa5Ll\xff\xaa\xef\xf0\xfbU)o\xb11\xacC\x981\x0b\xdf')

Code: Select all

1ac012ea060810c35baa48df24d9cc6227ac583ddcdfd4cff796b94819b2c3a276dc9e64fe30d96eff3eca8358b4ebe8cd4be6816489072e3641c6cea8d4202b

i don't really comprehend the encoding of binary bytearrays and strings.. still reading into it

what i've tried so far:

https://github.com/iyassou/mpyaes

Code: Select all

# https://github.com/iyassou/mpyaes

import mpyaes
import binascii

binary_string = binascii.unhexlify("1ac012ea060810c35baa48df24d9cc6227ac583ddcdfd4cff796b94819b2c3a276dc9e64fe30d96eff3eca8358b4ebe8cd4be6816489072e3641c6cea8d4202b")
key ='123456789123456'

aes = mpyaes.new(key, mpyaes.MODE_CBC, binary_string[0:16])
txt = aes.decrypt(binary_string[48:])
txt
https://stackoverflow.com/a/46872528

Code: Select all

<?p]hp

# https://stackoverflow.com/a/46872528

function encrypt($plaintext, $password) {
    $method = "AES-256-CBC";
    $key = hash('sha256', $password, true);
    $iv = openssl_random_pseudo_bytes(16);

    $ciphertext = openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv);
    $hash = hash_hmac('sha256', $ciphertext . $iv, $key, true);

    return $iv . $hash . $ciphertext;
}

function decrypt($ivHashCiphertext, $password) {
    $method = "AES-256-CBC";
    $iv = substr($ivHashCiphertext, 0, 16);
    $hash = substr($ivHashCiphertext, 16, 32);
    $ciphertext = substr($ivHashCiphertext, 48);
    $key = hash('sha256', $password, true);

    if (!hash_equals(hash_hmac('sha256', $ciphertext . $iv, $key, true), $hash)) return null;

    return openssl_decrypt($ciphertext, $method, $key, OPENSSL_RAW_DATA, $iv);
}

$key = '123456789123456';
$txt = 'plain text';

echo $txt.PHP_EOL;

$enc= encrypt($txt,$key);

echo bin2hex($enc).PHP_EOL;

echo decrypt($enc,$passwd).PHP_EOL;



User avatar
jimmo
Posts: 2244
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: mpyaes - utility library around ucryptolib.aes

Post by jimmo » Thu Feb 04, 2021 5:41 am

See reply on other thread. viewtopic.php?f=2&t=9724&p=54394#p54394

Post Reply