diff mbox series

[meta-swupdate,4/6] swupdate-common: swupdate-lib: Add SWUPDATE_USE_UNIQUE_IVS

Message ID 20261104170443.3265957-5-colinmca242@gmail.com
State New
Headers show
Series Improve signing/encryption and add asymmetric encryption support | expand

Commit Message

Colin Pinnell McAllister Nov. 4, 2026, 5:04 p.m. UTC
Adds a variable to make swupdate_get_IV return a unique random iv per
artifact instead of forcing downstream users to override swupdate_get_IV
themselves.

Also ensures that swupdate_get_IV will not return a random iv for the
sw-description file. Only the iv within the SWUPDATE_AES_FILE can be
used since there is no way to pass the random iv used for a
sw-description to swupdate during a software update.

Signed-off-by: Colin Pinnell McAllister <colinmca242@gmail.com>
---
 README                                 | 16 ++++++++++++++++
 classes-recipe/swupdate-common.bbclass |  7 +++++--
 classes-recipe/swupdate-lib.bbclass    |  4 +++-
 3 files changed, 24 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/README b/README
index e873baa..1f4a26a 100644
--- a/README
+++ b/README
@@ -96,6 +96,22 @@  which is included in the SWU file.
 Encrypted private keys are not currently supported since a secure
 mechanism must exist to provide the passphrase.
 
+SWU image encryption
+--------------------
+
+Artifacts are encrypted with AES-256-CBC when `SWUPDATE_IMAGES_ENCRYPTED[<file>] = "1"`
+is set for them. By default the key and IV are taken from `SWUPDATE_AES_FILE`.
+
+  * Set `SWUPDATE_USE_UNIQUE_IVS = "1"` to generate a random IV per artifact
+    instead of using the one from `SWUPDATE_AES_FILE`. Reference it in
+    sw-description with `ivt = "$swupdate_get_IV(<file>)";`.
+
+  * An IV can be pinned for a given file with `SWUPDATE_IV[<file>] = "<hex>"`.
+
+sw-description itself is encrypted when `SWUPDATE_ENCRYPT_SWDESC = "1"` is
+set. It always uses the key and IV from `SWUPDATE_AES_FILE`, since the device
+decrypts it with that key and IV before it can read any per-artifact IV.
+
 Maintainer
 ----------
 
diff --git a/classes-recipe/swupdate-common.bbclass b/classes-recipe/swupdate-common.bbclass
index 19ee40f..3107aaa 100644
--- a/classes-recipe/swupdate-common.bbclass
+++ b/classes-recipe/swupdate-common.bbclass
@@ -22,6 +22,8 @@  do_swuimage[sstate-inputdirs] = "${SWUDEPLOYDIR}"
 do_swuimage[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}"
 do_swuimage[stamp-extra-info] = "${IMAGE_MACHINE_SUFFIX}"
 
+SWUPDATE_USE_UNIQUE_IVS ?= "0"
+
 python () {
     deps = " " + swupdate_getdepends(d)
     d.appendVarFlag('do_swuimage', 'depends', deps)
@@ -181,8 +183,9 @@  def prepare_sw_description(d):
     if encrypt:
         bb.note("Encryption of sw-description")
         shutil.copyfile(os.path.join(workdir, 'sw-description'), os.path.join(workdir, 'sw-description.plain'))
+        # sw-description must use the static IV from SWUPDATE_AES_FILE: the
+        # device decrypts it with that key/IV, so a unique IV cannot be used here
         key,iv = swupdate_extract_keys(d.getVar('SWUPDATE_AES_FILE'))
-        iv = swupdate_get_IV(d, workdir, 'sw-description')
         swupdate_encrypt_file(os.path.join(workdir, 'sw-description.plain'), os.path.join(workdir, 'sw-description'), key, iv)
 
     signing = d.getVar('SWUPDATE_SIGNING')
@@ -269,9 +272,9 @@  def swupdate_add_src_uri(d, list_for_cpio):
             dst = os.path.join(workdir, "%s" % filename )
             if encrypted == '1':
                 bb.note("Encryption requested for %s" %(filename))
+                iv = swupdate_get_IV(d, workdir, filename)
                 if not key or not iv:
                     bb.fatal("Encryption required for %s, but no key or IV found" % filename)
-                iv = swupdate_get_IV(d, workdir, filename)
                 swupdate_encrypt_file(local, dst, key, iv)
             else:
                 shutil.copyfile(local, dst)
diff --git a/classes-recipe/swupdate-lib.bbclass b/classes-recipe/swupdate-lib.bbclass
index 37910ba..87a922a 100644
--- a/classes-recipe/swupdate-lib.bbclass
+++ b/classes-recipe/swupdate-lib.bbclass
@@ -41,10 +41,12 @@  def swupdate_get_sha256(d, s, filename):
     return m.hexdigest()
 
 def swupdate_get_IV(d, s, filename):
+    if d.getVar('SWUPDATE_USE_UNIQUE_IVS') == '1':
+        return swupdate_get_unique_IV(d, s, filename)
     # By default preserve original behavior: use IV from SWUPDATE_AES_FILE.
     aes_file = d.getVar('SWUPDATE_AES_FILE')
     if not aes_file:
-        bb.fatal("SWUPDATE_AES_FILE must be set to encrypt %s" % filename)
+        bb.fatal("SWUPDATE_AES_FILE must be set to encrypt %s unless SWUPDATE_USE_UNIQUE_IVS = \"1\"" % filename)
     key,iv = swupdate_extract_keys(aes_file)
     return iv