Skip to content

Commit 739da08

Browse files
committed
dji_imah_fwsig: Allow 3072-bit auth key
No such keys are currenlty published; but now they are supported.
1 parent a746857 commit 739da08

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

dji_imah_fwsig.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# You should have received a copy of the GNU General Public License
2525
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2626

27-
__version__ = "0.3.1"
27+
__version__ = "0.3.2"
2828
__author__ = "Freek van Tienen, Jan Dumon, Mefistotelis @ Original Gangsters"
2929
__license__ = "GPL"
3030

@@ -824,7 +824,7 @@ def imah_read_fwsig_head(po):
824824
minames = minames_s.split(' ')
825825
pkghead.chunk_num = len(minames)
826826
pkghead.header_size = sizeof(pkghead) + sizeof(ImgChunkHeader)*pkghead.chunk_num
827-
pkghead.signature_size = 256
827+
pkghead.signature_size = 384 # Depends on auth key length - just storing plausible value now
828828
pkghead.update_payload_size(0)
829829

830830
del parser
@@ -933,13 +933,19 @@ def imah_unsign(po, fwsigfile):
933933
print("Computed header checksum 0x{:08X} and digest:\n{:s}"
934934
.format(checksum_enc, ' '.join("{:02X}".format(x) for x in header_digest.digest())))
935935

936-
if pkghead.signature_size != 256: # 2048 bit key length
936+
# 2048 bit key (and therefore signature) length - used since introduction of IMaH until 2022 (header_ver=1,2)
937+
# 3072 bit key (and therefore signature) length - used since wm260,zv900 in 2022 (header_ver=2)
938+
if pkghead.signature_size != 256 and pkghead.signature_size != 384:
937939
raise_or_warn(po, ValueError("Signed image file head signature has unexpected size."))
938940
head_signature = fwsigfile.read(pkghead.signature_size)
939941
if len(head_signature) != pkghead.signature_size:
940942
raise EOFError("Could not read signature of signed image file head.")
941943

942944
auth_key = imah_get_auth_params(po, pkghead)
945+
946+
if (auth_key.size_in_bytes() != len(head_signature)):
947+
raise_or_warn(po, ValueError("Image file head signature does not match the length of auth key."))
948+
943949
try:
944950
if pkgformat >= 2018:
945951
mgf = lambda x, y: pss.MGF1(x, y, SHA256)
@@ -1117,6 +1123,9 @@ def imah_sign(po, fwsigfile):
11171123
else:
11181124
chunk = imah_read_fwentry_head(po, i, miname)
11191125
chunks.append(chunk)
1126+
# Figure out signature length
1127+
auth_key = imah_get_auth_params(po, pkghead)
1128+
pkghead.signature_size = auth_key.size_in_bytes()
11201129
# Write the unfinished headers
11211130
fwsigfile.write(bytes(pkghead))
11221131
for chunk in chunks:
@@ -1260,7 +1269,6 @@ def imah_sign(po, fwsigfile):
12601269
print("{}: Computed header digest:\n{:s}".format(fwsigfile.name,
12611270
' '.join("{:02X}".format(x) for x in header_digest.digest())))
12621271

1263-
auth_key = imah_get_auth_params(po, pkghead)
12641272
if not hasattr(auth_key, 'd'):
12651273
raise ValueError("Cannot compute image file head signature, auth key '{:s}' has no private part."
12661274
.format(pkghead.auth_key.decode("utf-8")))

0 commit comments

Comments
 (0)