NRF_CRYPTO AESCCM and python AES CCM encryption not gives the same output

from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
from Crypto.Util.strxor import strxor

# Key derivation parameters
# password = b"your_secret_password"
# salt = get_random_bytes(16)
# key = PBKDF2(password, salt, dkLen=32)
key=b"kwb3IfPnbMx8RvGYm3yl4O6E2ZNZ4Gwq"

# AES-CCM parameters
nonce = bytes.fromhex("00000000000000000000000000")
associated_data = bytes.fromhex("AABBCCDD")
plaintext = b"ABC"
print(plaintext.hex())
# Initialize AES-CCM cipher
cipher = AES.new(key, AES.MODE_CCM, nonce=nonce,mac_len=4,assoc_len=4,use_aesni=0)

# Encrypt the data
cipher.update(associated_data)
ciphertext = cipher.encrypt(plaintext)
mac=cipher.digest()
# Print the encrypted data and MAC
print("Ciphertext:", ciphertext.hex())
print("nonce",nonce.hex(),nonce.__len__())
print("key",key.__str__(),key.__len__())
print("MAC:", mac.hex())
cipher = AES.new(key, AES.MODE_CCM, nonce=nonce,mac_len=4,assoc_len=4,use_aesni=0)
cipher.update(associated_data)
ciphertext=cipher.decrypt(ciphertext)
print("Ciphertext:", ciphertext.hex())

Hi Nordic Team,

NRF crypto example  AES CCM main.c code follows NIST standards and  Pycryptodome library follows the same. I tried encryption on both platform but the results will be different, So How can I match the outputs. Suggest any methods to sync the same.  

The Outputs are given below

C Code Output

Plain text (hex) (len: 3) ----41 42 43

Encrypted text (hex) (len: 3) ----86 9E 06

MAC (hex) (len: 4) ---- 06 C6 68 5D

Python Code Output

Plain text -- 414243

Ciphertext: --e1 29 d1

MAC:-- bc 3a b0 c8

 I hereby attached the python code. The parameters of AES CCM are same for both python and C code 

Related