You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ag...@apache.org on 2021/03/16 20:36:00 UTC

[mynewt-artifact] branch master updated: Fix encrypted image support

This is an automated email from the ASF dual-hosted git repository.

agross pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-artifact.git


The following commit(s) were added to refs/heads/master by this push:
     new 64169ed  Fix encrypted image support
     new 21a0320  Merge pull request #31 from utzig/fix-encrypted-images
64169ed is described below

commit 64169ed99ee78ce7b12445fd389907199470d808
Author: Fabio Utzig <ut...@apache.org>
AuthorDate: Mon Mar 15 20:13:49 2021 -0300

    Fix encrypted image support
    
    When encrypting images, the hash was being calculated using the
    encrypted image body. This is invalid for MCUboot, the hash must be
    calculated first from the plain body and afterwards this body must be
    encrypted. This commit fixes the behavior.
    
    Signed-off-by: Fabio Utzig <ut...@apache.org>
---
 image/create.go | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/image/create.go b/image/create.go
index 129baaa..0217f2c 100644
--- a/image/create.go
+++ b/image/create.go
@@ -553,19 +553,28 @@ func (ic *ImageCreator) Create() (Image, error) {
 	img.Header.ProtSz = calcProtSize(img.ProtTlvs)
 
 	// Followed by data.
+	var hashBytes []byte
+	var err error
 	if ic.PlainSecret != nil {
+		// For encrypted images, must calculate the hash with the plain
+		// body and encrypt the payload afterwards
+		img.Body = append(img.Body, ic.Body...)
+		hashBytes, err = img.CalcHash(ic.InitialHash)
+		if err != nil {
+			return img, err
+		}
 		encBody, err := sec.EncryptAES(ic.Body, ic.PlainSecret, ic.Nonce)
 		if err != nil {
 			return img, err
 		}
+		img.Body = nil
 		img.Body = append(img.Body, encBody...)
 	} else {
 		img.Body = append(img.Body, ic.Body...)
-	}
-
-	hashBytes, err := img.CalcHash(ic.InitialHash)
-	if err != nil {
-		return img, err
+		hashBytes, err = img.CalcHash(ic.InitialHash)
+		if err != nil {
+			return img, err
+		}
 	}
 
 	// Hash TLV.