diff mbox series

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

Message ID 20261104170443.3265957-7-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 encrypt each artifact with its own random AES key,
referenced from sw-description with $swupdate_get_unique_key(<file>),
instead of a single key shared from SWUPDATE_AES_FILE. A key can be
pinned for a given file with SWUPDATE_KEY[<file>].

Because the keys are embedded in sw-description, this is only safe when
sw-description is itself encrypted, so parse-time validation rejects
SWUPDATE_USE_UNIQUE_KEYS without SWUPDATE_ENCRYPT_SWDESC.

Signed-off-by: Colin Pinnell McAllister <colinmca242@gmail.com>
---
 README                                 | 12 +++++++++++-
 classes-recipe/swupdate-common.bbclass |  9 +++++++++
 classes-recipe/swupdate-lib.bbclass    |  9 +++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/README b/README
index ddcadcb..8e5bb74 100644
--- a/README
+++ b/README
@@ -106,7 +106,14 @@  is set for them. By default the key and IV are taken from `SWUPDATE_AES_FILE`.
     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>"`.
+  * Set `SWUPDATE_USE_UNIQUE_KEYS = "1"` to generate a random key per artifact
+    instead of using the one from `SWUPDATE_AES_FILE`. Reference it in
+    sw-description with `aes-key = "$swupdate_get_unique_key(<file>)";`.
+    Since the keys are embedded in sw-description, this requires sw-description
+    to be encrypted (see below).
+
+  * A key or IV can be pinned for a given file with
+    `SWUPDATE_KEY[<file>] = "<hex>"` / `SWUPDATE_IV[<file>] = "<hex>"`.
 
 There are 2 mechanisms to encrypt sw-description itself:
 
@@ -129,6 +136,9 @@  There are 2 mechanisms to encrypt sw-description itself:
     so that any listed recipient can decrypt it. See SWUpdate's
     "Asymmetrically Encrypted Update Images" documentation.
 
+  * This is the mechanism to use with `SWUPDATE_USE_UNIQUE_KEYS`, as described
+    in that documentation.
+
 When sw-description is encrypted, signing is performed on the plaintext
 sw-description.
 
diff --git a/classes-recipe/swupdate-common.bbclass b/classes-recipe/swupdate-common.bbclass
index 5f0d934..64ea73d 100644
--- a/classes-recipe/swupdate-common.bbclass
+++ b/classes-recipe/swupdate-common.bbclass
@@ -23,6 +23,7 @@  do_swuimage[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}"
 do_swuimage[stamp-extra-info] = "${IMAGE_MACHINE_SUFFIX}"
 
 SWUPDATE_ENCRYPT_SWDESC ?= ""
+SWUPDATE_USE_UNIQUE_KEYS ?= "0"
 SWUPDATE_USE_UNIQUE_IVS ?= "0"
 SWUPDATE_ENCRYPT_CMS_CERTS ?= ""
 
@@ -40,6 +41,10 @@  def swupdate_check_encryption_config(d):
     if encrypt == "CMS" and not d.getVar('SWUPDATE_ENCRYPT_CMS_CERTS'):
         bb.fatal("SWUPDATE_ENCRYPT_SWDESC = \"CMS\" requires SWUPDATE_ENCRYPT_CMS_CERTS")
 
+    if d.getVar('SWUPDATE_USE_UNIQUE_KEYS') == "1" and not d.getVar('SWUPDATE_ENCRYPT_SWDESC'):
+        bb.fatal("SWUPDATE_USE_UNIQUE_KEYS requires SWUPDATE_ENCRYPT_SWDESC (AES or CMS), "
+                 "otherwise per-image keys would be shipped in a plaintext sw-description")
+
 python () {
     deps = " " + swupdate_getdepends(d)
     d.appendVarFlag('do_swuimage', 'depends', deps)
@@ -299,6 +304,8 @@  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))
+                if d.getVar("SWUPDATE_USE_UNIQUE_KEYS") == "1":
+                    key = swupdate_get_unique_key(d, workdir, 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)
@@ -322,6 +329,8 @@  def add_image_to_swu(d, deploydir, imagename, s, encrypt, list_for_cpio):
             key,iv = swupdate_extract_keys(aes_file)
         bb.note("Encryption requested for %s" %(imagename))
         # Key by the name used inside the SWU so sw-description lookups match
+        if d.getVar("SWUPDATE_USE_UNIQUE_KEYS") == "1":
+            key = swupdate_get_unique_key(d, s, target_imagename)
         iv = swupdate_get_IV(d, s, target_imagename)
         if not key or not iv:
             bb.fatal("Encryption required for %s, but no key or IV found" % target_imagename)
diff --git a/classes-recipe/swupdate-lib.bbclass b/classes-recipe/swupdate-lib.bbclass
index a3dce8d..15109fb 100644
--- a/classes-recipe/swupdate-lib.bbclass
+++ b/classes-recipe/swupdate-lib.bbclass
@@ -79,6 +79,15 @@  def swupdate_get_unique_IV(d, s, filename):
         d.setVarFlag("SWUPDATE_IV", filename, iv)
     return iv
 
+def swupdate_get_unique_key(d, s, filename):
+    # Only safe when sw-description is encrypted, since the key is embedded in it
+    from secrets import token_hex
+    key = d.getVarFlag("SWUPDATE_KEY", filename, True)
+    if not key:
+        key = token_hex(32)
+        d.setVarFlag("SWUPDATE_KEY", filename, key)
+    return key
+
 def swupdate_get_size(d, s, filename):
     import os