Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document aeios.AES.rekey() and refactor arg validation #7684

Merged
merged 3 commits into from
Mar 8, 2023
Merged

Document aeios.AES.rekey() and refactor arg validation #7684

merged 3 commits into from
Mar 8, 2023

Conversation

isacben
Copy link

@isacben isacben commented Mar 5, 2023

Hi,

This PR solves: aeios.AES.rekey() is not documented #7435

a) Documented the aeios.AES.rekey() function
b) Refactored the arguments validation. Please let me know if that was the idea of "canonicalize arg checking".

Tested with adafruit_feather_rp2040 with the following code:

import aesio
import os
from binascii import hexlify, unhexlify

plaintext = unhexlify("6bc1bee22e409f96e93d7e117393172a")
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
iv = unhexlify("000102030405060708090a0b0c0d0e0f")

cyphertext = bytearray(len(plaintext))
output = memoryview(cyphertext)
cipher = aesio.AES(key=key, mode=aesio.MODE_CBC, IV=iv)

# Encrypt
cipher.encrypt_into(plaintext, output)

print(end='\n\n')
print('input:     ', str(hexlify(plaintext)), end='\n')
print('encrypted: ', str(hexlify(output)), end='\n')

# Decrypt
plaintext = bytearray(len(plaintext))
cipher = aesio.AES(key=key, mode=aesio.MODE_CBC, IV=iv)

output = memoryview(plaintext)
cipher.decrypt_into(cyphertext, output)
print('decrypted: ', str(hexlify(output)), end='\n')

# Rekey
plaintext = unhexlify("6bc1bee22e409f96e93d7e117393172a")
key2 = unhexlify("2b7e151628aed2a6abf7158809cAAAAA")
cyphertext = bytearray(len(plaintext))
output = memoryview(cyphertext)

cipher.rekey(key=key2, IV=iv) # update key of the existing cipher object

# Encrypt
cipher.encrypt_into(plaintext, output)

print(end='\n')
print('Rekey...', end='\n')
print('input:     ', str(hexlify(plaintext)), end='\n')
print('encrypted: ', str(hexlify(output)), end='\n')

# Decrypt
plaintext = bytearray(len(plaintext))
cipher = aesio.AES(key=key2, mode=aesio.MODE_CBC, IV=iv)

output = memoryview(plaintext)
cipher.decrypt_into(cyphertext, output)
print('decrypted: ', str(hexlify(output)), end='\n\n')

Output:

input:      b'6bc1bee22e409f96e93d7e117393172a'
encrypted:  b'7649abac8119b246cee98e9b12e9197d'
decrypted:  b'6bc1bee22e409f96e93d7e117393172a'

Rekey...
input:      b'6bc1bee22e409f96e93d7e117393172a'
encrypted:  b'ff7e2905e0af08d89121429ee15b311b'
decrypted:  b'6bc1bee22e409f96e93d7e117393172a'

Thank you!

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question. Looks good otherwise. Thank you!

shared-bindings/aesio/aes.c Outdated Show resolved Hide resolved
@isacben isacben requested a review from tannewt March 6, 2023 20:09
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more spot. Thanks!

shared-bindings/aesio/aes.c Outdated Show resolved Hide resolved
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tannewt tannewt merged commit 9251c6f into adafruit:main Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants