I am trying to use this library to encrypt some data I'm sending to a Raspberry Pi using a transceiver module. In order to make a small test prototype I've modified the example code provided with this library so that it performs 256 2 block CBC encryption and then prints the cipher to the Serial monitor.
#include <AES.h>
AES aes ;
byte key[] =
{
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
} ;
byte plain[] =
{
// 0xf3, 0x44, 0x81, 0xec, 0x3c, 0xc6, 0x27, 0xba, 0xcd, 0x5d, 0xc3, 0xfb, 0x08, 0xf2, 0x73, 0xe6
0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
} ;
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) ;
Serial.print ("testng mode") ;
prekey_test () ;
// otfly_test () ;
// otfly_test256 () ;
}
void prekey (int bits, int blocks)
{
byte iv [N_BLOCK] ;
long t0 = micros () ;
byte succ = aes.set_key (key, bits) ;
long t1 = micros()-t0 ;
Serial.print ("set_key ") ; Serial.print (bits) ; Serial.print (" ->") ; Serial.print ((int) succ) ;
Serial.print (" took ") ; Serial.print (t1) ; Serial.println ("us") ;
t0 = micros () ;
if (blocks == 1)
succ = aes.encrypt (plain, cipher) ;
else
{
for (byte i = 0 ; i < 16 ; i++)
iv[i] = my_iv[i] ;
succ = aes.cbc_encrypt (plain, cipher, blocks, iv) ;
}
t1 = micros () - t0 ;
Serial.print ("encrypt ") ; Serial.print ((int) succ) ;
Serial.print (" took ") ; Serial.print (t1) ; Serial.println ("us") ;
Serial.println();
Serial.println("-------------------");
for(byte i = 0; i < sizeof(cipher); i++) {
Serial.print(cipher[i], HEX);
}
Serial.println("");
Serial.println("------------------------------");
}
void prekey_test ()
{
prekey (256,2) ;
}
I am then taking the cipher text and copying it to Python on the Raspberry Pi to the decryption code which uses the pycryptodome library, which is where I don't understand what to do. I am unsure of the block length of this library (I've serially printed N_BLOCK and found it to be 16, but the cipher array is 64 bytes long...) and I continually get a padding error in Python. I realise this is more of a Python problem than anything else but I have tried everything I can think of.
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
from Crypto.Util.Padding import pad, unpad
BLOCK_SIZE = 64 # is that right?
def encrypt(plaintext, key, IV):
cipher = AES.new(key, AES.MODE_CBC, IV)
cipher_bytes = cipher.encrypt(pad(plaintext, BLOCK_SIZE))
return(cipher_bytes)
def decrypt(ct, passkey, IV):
cipher = AES.new(passkey, AES.MODE_CBC, IV)
decrypted = cipher.decrypt(ct)
return((unpad(decrypted, BLOCK_SIZE)))
passkey = b'80000000000000000000000000000000' #have I converted the passkey from the example code correctly?
IV = b'0000000000000001' # have I converted the IV from the example code correctly?
ct = b"F7B337A3C81C50169E4851F823E8C8B78EB31B013171CA23641F0D385DB38B00000000000000000000000000000000"
print(decrypt(ct, passkey, IV))
"""
>> ValueError: Data must be padded to 16 byte boundary in CBC mode
"""