# Copyright (c) 2001-2020, Arm Limited and Contributors. All rights reserved. # # SPDX-License-Identifier: BSD-3-Clause OR Arm’s non-OSI source license # import os import abc import re import struct from utils.common import global_defines from utils.common import flags_global_defines from utils.common import cryptolayer from utils.common.exceptions import CertCreationError # ******************************************************************* # Modification to handle local directories vs Azure Storage # when looking for Key1/key2 certs in gen-toc.py # ******************************************************************* def getKeyCertificatePath(): return "cert/" # local files # ******************************************************************* # Modification to handle DEV vs PROD (Azure) Environments # when looking for the alif package name (suffix) in gen-toc.py # (to differentiate the name of packages for PROD vs DEV in APP Tools) # ******************************************************************* def getEnvironmentSuffix(): return '-dev' # DEV environment suffix class ArmCertificateHeader(abc.ABC): @property @abc.abstractmethod def magic_number(self): pass @property @abc.abstractmethod def cert_version_number(self): pass @property @abc.abstractmethod def signed_content_size(self): pass @property @abc.abstractmethod def flags(self): pass @abc.abstractmethod def serialize_to_bytes(self): pass class KeyArmCertificateHeader(ArmCertificateHeader): CERT_TOKEN = 0x53426b63 def __init__(self, cert_version, hbk_id, logger=None): self._cert_version = (cert_version[0] << 16) + cert_version[1] self._hbk_id = hbk_id self.logger = logger self._signed_content_size = (global_defines.HEADER_SIZE_IN_WORDS + global_defines.SW_VERSION_OBJ_SIZE_IN_WORDS + global_defines.PUBKEY_SIZE_WORDS + global_defines.NP_SIZE_IN_WORDS + global_defines.HASH_ALGORITHM_SHA256_SIZE_IN_WORDS) self._flags = hbk_id def magic_number(self): return self.CERT_TOKEN def cert_version_number(self): return self._cert_version def signed_content_size(self): return self._signed_content_size def flags(self): return self._flags def serialize_to_bytes(self): data = (struct.pack(' global_defines.AES_DECRYPT_KEY_SIZE_IN_BYTES: self.logger.warning("key size in aes-enc-key file is too big - truncating parameter") key_data_list = [int(item, 16) for item in str_key_data_bytes] key_data_list = key_data_list[:global_defines.AES_DECRYPT_KEY_SIZE_IN_BYTES] # combine nonce and address into iv (8 bytes nonce + 4 bytes address + 4 bytes of zeros) aes_iv = self._key_nonce + bytes(item["mem_load_address_byte_list"]) + bytes(4) # encrypt image new_image_filename = item["image_filename"][:-4] + global_defines.SW_COMP_FILE_NAME_POSTFIX hash_of_image, actual_image_size = cryptolayer.Common.encrypt_file_with_aes_ctr(item["image_filename"], new_image_filename, key_data_list, aes_iv, self._crypto_type, self.logger) else: raise CertCreationError("invalid aes-ce-id for image encryption flag in image of images-table - " "must have aes-enc-key for images to be encrypted") else: # do only hash with open(item["image_filename"], "rb") as input_file: plaintext = input_file.read() actual_image_size = len(plaintext) hash_of_image = cryptolayer.HashCrypto.calculate_sha256_hash(plaintext) # image_hash_and_size.append([hash_of_image, actual_image_size]) self._sw_records.append([hash_of_image, item["mem_load_address"], item["image_max_size"], item["is_aes_code_enc_used"]]) self._none_signed_info.append([item["flash_store_address"], actual_image_size]) @property def num_images(self): return self._num_images @property def sw_version(self): return self._sw_version @property def signer_rsa_public_key(self): return self._signer_rsa_public_key @property def key_nonce(self): return self._key_nonce def serialize_to_bytes(self): data = (self._signer_rsa_public_key + struct.pack('