Closed ZiGaMi closed 1 year ago
Python script used for AES-CTR cryption:
import os
import binascii
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util import Counter
def print_debug(plain, encode, decode):
print("---------------------------------------------------------------")
print(" Plain: %s" % plain)
print("Encoded: %s" % encode)
print(" %s" % "".join( "\\x%02X" % b for b in encode ))
print("Decoded: %s" % decode)
print(" %s" % "".join( "\\x%02X" % b for b in decode ))
print("---------------------------------------------------------------")
print("")
def int_of_string(s):
return int(binascii.hexlify(s), 16)
def aes_encode(plain_data):
# AES Key and IV
key = b"\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c"
iv = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
# Create cipher
ctr = Counter.new(128, initial_value=int_of_string(iv))
cipher = AES.new(key, AES.MODE_CTR, counter=ctr)
# Encode
return list(cipher.encrypt( bytearray( plain_data )))
def aes_decode(cipher_data):
# AES Key and IV
key = b"\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c"
iv = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
# Create cipher
ctr = Counter.new(128, initial_value=int_of_string(iv))
_cipher = AES.new(key, AES.MODE_CTR, counter=ctr)
# Decrypt
return list(_cipher.decrypt( bytearray( cipher_data )))
if __name__ == "__main__":
# Init AES engine
#cipher = AES.new(key, AES.MODE_CBC, iv)
# =================================================================
# Plain data
plain_data = [ 0x00 ]
# Encode
encode_data = aes_encode(plain_data)
# Decode data
decode_data = aes_decode(encode_data)
# Show results
print_debug( plain_data, encode_data, decode_data )
# =================================================================
# Plain data
plain_data = [ 0x00 ]
# Encode
encode_data = aes_encode(plain_data)
# Decode data
decode_data = aes_decode(encode_data)
# Show results
print_debug( plain_data, encode_data, decode_data )
# =================================================================
# Plain data
plain_data = [ 0x00, 0x01, 0x02, 0x03 ]
# Encode
encode_data = aes_encode(plain_data)
# Decode data
decode_data = aes_decode(encode_data)
# Show results
print_debug( plain_data, encode_data, decode_data )
# =================================================================
# Plain data
plain_data = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F ]
# Encode
encode_data = aes_encode(plain_data)
# Decode data
decode_data = aes_decode(encode_data)
# Show results
print_debug( plain_data, encode_data, decode_data )
# =================================================================
# Plain data
plain_data = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xAA, 0xBB ]
# Encode
encode_data = aes_encode(plain_data)
# Decode data
decode_data = aes_decode(encode_data)
# Show results
print_debug( plain_data, encode_data, decode_data )
# =================================================================
With a combination of C code, using ST CMOX lib:
/* Initialize cryptographic library */
if ( cmox_initialize(NULL) != CMOX_INIT_SUCCESS )
{
PROJ_CFG_ASSERT( 0 );
}
static bool encode(const uint8_t * const p_plain, const uint32_t plain_size, uint8_t * const p_crypt, uint32_t * const p_crypt_size)
{
uint32_t rtn_status = cmox_cipher_encrypt( CMOX_AES_CTR_ENC_ALGO, (uint8_t*) p_plain, plain_size, Key, sizeof(Key), IV, sizeof(IV), (uint8_t*) p_crypt, (size_t*) p_crypt_size );
if ( CMOX_CIPHER_SUCCESS == rtn_status )
{
return true;
}
else
{
return false;
}
}
static bool decode(const uint8_t * const p_crypt, const uint32_t crypt_size, uint8_t * const p_plain, uint32_t * const p_plain_size)
{
uint32_t rtn_status = cmox_cipher_decrypt( CMOX_AES_CTR_DEC_ALGO, (uint8_t*) p_crypt, crypt_size, Key, sizeof(Key), IV, sizeof(IV), (uint8_t*) p_plain, (size_t*) p_plain_size );
if ( CMOX_CIPHER_SUCCESS == rtn_status )
{
return true;
}
else
{
return false;
}
}
DONE
CMOX: https://www.st.com/en/embedded-software/x-cube-cryptolib.html
With python tool: https://pycryptodome.readthedocs.io/en/latest/src/cipher/aes.html
You can use AES 128bit key CBC mode