To verify my understanding on AES-GCM & AES-CTR modes, I'm creating a simple example using python Crpyto.Cipher library. I'm expecting the same ciphertext is generated from both modes where both are using CTR method.
Since my intention is just to compare the encrypted result from the encryption engines, hence, I'm setting the message to all 0 (Hex format) for both GCM & CTR. Anything XOR with 0 will remain as the original Ciphertext.
On AES-CTR side, I'm setting the nonce to "00". This mean no nonce will be used, and by default the counter number will start from value 0.
On AES-GCM side, I'm setting the nonce (IV) to 16 bytes "00". I'm assuming this is equivalent to 0 starting value for counter.
Looking at the AES-GCM block diagram below, the first ciphertext I should get from AES-GCM should be simply the encryption result from the counter value 1.
However, I couldn't to get the same encryption result from AES-CTR & AES-GCM. Please enlighten me which part I'm making mistake? Lastly, I'm using the same 256-AES-key for the both encryption modes.
Here is the code:
key = bytes.fromhex('0123456789ABCDEF11113333555577770123456789ABCDEF1111333355557777')
msg = bytes.fromhex('00000000000000000000000000000000')
msg1 = bytes.fromhex('00000000000000000000000000000001')
###### AES-256 ECB Mode ######
aes1 = AES.new(key,AES.MODE_ECB)
print("AES-ECB Result, Counter 1: "+str(binascii.hexlify(aes1.encrypt(msg1)))+"\n")
###### AES-256 CTR Mode ######
aes1 = AES.new(key,AES.MODE_CTR,nonce=bytes.fromhex('00'))
print("AES-CTR Result, Counter 0: "+str(binascii.hexlify(aes1.encrypt(msg))))
print("AES-CTR Result, Counter 1: "+str(binascii.hexlify(aes1.encrypt(msg)))+"\n")
###### AES-256 GCM Mode ######
aes1 = AES.new(key, AES.MODE_GCM, nonce=bytes.fromhex('00000000000000000000000000000000'))
ciphertext, authTag = aes1.encrypt_and_digest(msg)
print("AES-GCM Result, Counter 0: "+str(binascii.hexlify(ciphertext)))
print("AES-GCM Initialization Vector: "+str(binascii.hexlify(aes1.nonce)))
Python Result:
AES-ECB Result, Counter 1: b'24c82c75b5546a77d20c9868503767b4'
AES-CTR Result, Counter 0: b'4a85984511e5ca3f03297d84c69584c4'
AES-CTR Result, Counter 1: b'24c82c75b5546a77d20c9868503767b4'
AES-GCM Result: b'dfff0d463d8254d7eb23887729b22a85'
AES-GCM Initialization Vector: b'00000000000000000000000000000000'