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

[nrf fromlist] scripts: imgtool: pretended encryption flag #398

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions scripts/imgtool/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
overwrite_only=False, endian="little", load_addr=0,
rom_fixed=None, erased_val=None, save_enctlv=False,
security_counter=None, max_align=None,
non_bootable=False):
non_bootable=False, skip_encryption=False):

if load_addr and rom_fixed:
raise click.UsageError("Can not set rom_fixed and load_addr at the same time")
Expand Down Expand Up @@ -288,6 +288,7 @@ def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
self.enctlv_len = 0
self.max_align = max(DEFAULT_MAX_ALIGN, align) if max_align is None else int(max_align)
self.non_bootable = non_bootable
self.skip_encryption = skip_encryption

if self.max_align == DEFAULT_MAX_ALIGN:
self.boot_magic = bytes([
Expand Down Expand Up @@ -685,10 +686,11 @@ def create(self, key, public_key_format, enckey, dependencies=None,
nonce = bytes([0] * 16)
cipher = Cipher(algorithms.AES(plainkey), modes.CTR(nonce),
backend=default_backend())
encryptor = cipher.encryptor()
img = bytes(self.payload[self.header_size:])
self.payload[self.header_size:] = \
encryptor.update(img) + encryptor.finalize()
if not self.skip_encryption:
encryptor = cipher.encryptor()
img = bytes(self.payload[self.header_size:])
self.payload[self.header_size:] = \
encryptor.update(img) + encryptor.finalize()

self.payload += prot_tlv.get()
self.payload += tlv.get()
Expand Down
6 changes: 4 additions & 2 deletions scripts/imgtool/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ def convert(self, value, param, ctx):
help='Enable image compression using specified type. '
'Will fall back without image compression automatically '
'if the compression increases the image size.')
@click.option('--skip-encryption', default=False, is_flag=True,
help='Set encryption flags and TLV\'s without applying encryption.')
@click.option('-c', '--clear', required=False, is_flag=True, default=False,
help='Output a non-encrypted image with encryption capabilities,'
'so it can be installed in the primary slot, and encrypted '
Expand Down Expand Up @@ -449,7 +451,7 @@ def sign(key, public_key_format, align, version, pad_sig, header_size,
dependencies, load_addr, hex_addr, erased_val, save_enctlv,
security_counter, boot_record, custom_tlv, rom_fixed, max_align,
clear, fix_sig, fix_sig_pubkey, sig_out, user_sha, is_pure,
vector_to_sign, non_bootable):
vector_to_sign, non_bootable, skip_encryption):

if confirm:
# Confirmed but non-padded images don't make much sense, because
Expand All @@ -462,7 +464,7 @@ def sign(key, public_key_format, align, version, pad_sig, header_size,
endian=endian, load_addr=load_addr, rom_fixed=rom_fixed,
erased_val=erased_val, save_enctlv=save_enctlv,
security_counter=security_counter, max_align=max_align,
non_bootable=non_bootable)
non_bootable=non_bootable, skip_encryption=skip_encryption)
compression_tlvs = {}
img.load(infile)
key = load_key(key) if key else None
Expand Down