Skip to contentSkip to conversation

sd: Small improvements around RPMB emulation

PATCH In review Jan Kiszka 4 participants 12 messages Apr 15 – Apr 17 Kbuild

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

cover sd: Small improvements around RPMB emulation

Besides a cosmetic fix for the emmc disk image generation script, this adds a property to the emmc model that allows to preset the authentication key. And that enables to build OPTEE without key writing support, bringing the related firmware much closer to real setups.

An alternative to this option would have been persisting additional state such as the key or the write counter on the disk image as well. This is still an option for the future, e.g. to model also write protection groups or other persistent states. As this requires another format extension of the eMMC disk image, I want for this much simpler and less invasive approach.

Jan

Jan Kiszka (2):
scripts: mkemmc.sh: Fix output pollution on missing images hw/sd/sdcard: Add support for pre-setting the authentication key

 hw/sd/sd.c        | 23 +++++++++++++++++++++++
 scripts/mkemmc.sh |  4 ++--
 2 files changed, 25 insertions(+), 2 deletions(-)
Signature · 1 line
2.47.3
author
#1 lore raw

PATCH 1/2 scripts: mkemmc.sh: Fix output pollution on missing images

From: Jan Kiszka <[email protected]>

Reorder the redirections so that the 2>/dev/null can actually take effect.

---
 scripts/mkemmc.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/mkemmc.sh +2 -2
index 429388213c..3d8546cdaa 100755
@@ -40,7 +40,7 @@ process_size() {
     alignment=$3
     image_arg=$4
     if [ "${image_arg#*:}" = "$image_arg"  ]; then
-        if ! size=$(wc -c < "$image_file" 2>/dev/null); then
+        if ! size=$(wc -c 2>/dev/null < "$image_file"); then
             echo "Missing $name image '$image_file'." >&2
             exit 1
         fi
@@ -102,7 +102,7 @@ check_truncation() {
     if [ "$image_file" = "/dev/zero" ]; then
         return
     fi
-    if ! actual_size=$(wc -c < "$image_file" 2>/dev/null); then
+    if ! actual_size=$(wc -c 2>/dev/null < "$image_file"); then
         echo "Missing image '$image_file'." >&2
         exit 1
     fi
Signature · 1 line
2.47.3
author
#3 lore raw

PATCH 2/2 hw/sd/sdcard: Add support for pre-setting the authentication key

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

In contrast to real eMMCs, we are not (yet) persisting the full state of the device. This particularly includes the authentication key used for RPMB access, complicating testing of firmware images which do not support initial provisioning of the key.

One way to address this would be, e.g., extending the eMMC disk image with a special sector to store further state. A simpler approach is used
here: Add another device property that allows to specify the authentication key, bringing up the eMMC as if the key has already been provisioned before.

This is how to tell qemu to use the OP-TEE test key:

-device emmc,[...],auth-key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

Or use this for machine-configured eMMCs:

-global emmc.auth-key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

---
 hw/sd/sd.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
hw/sd/sd.c +23 -0
index 37f6e0702b..13ea54df38 100644
@@ -205,6 +205,7 @@ struct SDState {
     QEMUTimer *ocr_power_timer;
     uint8_t dat_lines;
     bool cmd_line;
+    char *preset_auth_key;
 };
 
 static void sd_realize(DeviceState *dev, Error **errp);
@@ -3132,6 +3133,27 @@ static void sd_realize(DeviceState *dev, Error **errp)
                           "The RPMB partition size must be multiples of 128K"
                           "and not larger than 16384K.\n");
     }
+    if (sd_is_emmc(sd) && sd->preset_auth_key) {
+        if (strlen(sd->preset_auth_key) != 64) {
+            error_setg(errp,
+                       "Authentication key must be 32 bytes long, "
+                       "encoded hexadecimally");
+            return;
+        }
+
+        char *pos = sd->preset_auth_key;
+        unsigned int n;
+        for (n = 0; n < RPMB_KEY_MAC_LEN; n++, pos += 2) {
+            int chrs;
+            if (sscanf(pos, "%02hhx%n", &sd->rpmb.key[n], &chrs) != 1 ||
+                chrs != 2) {
+                error_setg(errp,
+                           "Authentication key contains invalid characters");
+                return;
+            }
+        }
+        sd->rpmb.key_set = 1;
+    }
 }
 
 static void emmc_realize(DeviceState *dev, Error **errp)
@@ -3156,6 +3178,7 @@ static const Property emmc_properties[] = {
     DEFINE_PROP_UINT64("boot-partition-size", SDState, boot_part_size, 0),
     DEFINE_PROP_UINT8("boot-config", SDState, boot_config, 0x0),
     DEFINE_PROP_UINT64("rpmb-partition-size", SDState, rpmb_part_size, 0),
+    DEFINE_PROP_STRING("auth-key", SDState, preset_auth_key),
 };
 
 static void sdmmc_common_class_init(ObjectClass *klass, const void *data)
Signature · 1 line
2.47.3
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] hw/sd/sdcard: Add support for pre-setting the authentication key

In reply to Jan Kiszka

Hi Jan,

Quoted 17 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

In contrast to real eMMCs, we are not (yet) persisting the full state of the device. This particularly includes the authentication key used for RPMB access, complicating testing of firmware images which do not support initial provisioning of the key.

One way to address this would be, e.g., extending the eMMC disk image with a special sector to store further state. A simpler approach is used
here: Add another device property that allows to specify the authentication key, bringing up the eMMC as if the key has already been provisioned before.

This is how to tell qemu to use the OP-TEE test key:

-device emmc,[...],auth-key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

Quoted 8 lines

Or use this for machine-configured eMMCs:

-global emmc.auth-key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

---
  hw/sd/sd.c | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)

Do you mind mentioning it in docs/system/devices/emmc.rst?

Otherwise,

#10 lore raw
In reply to Jan Kiszka

(Cc'ing Jerome to his new email)

Quoted 22 lines · Jan Kiszka

Besides a cosmetic fix for the emmc disk image generation script, this adds a property to the emmc model that allows to preset the authentication key. And that enables to build OPTEE without key writing support, bringing the related firmware much closer to real setups.

An alternative to this option would have been persisting additional state such as the key or the write counter on the disk image as well. This is still an option for the future, e.g. to model also write protection groups or other persistent states. As this requires another format extension of the eMMC disk image, I want for this much simpler and less invasive approach.

Jan

Jan Kiszka (2):
scripts: mkemmc.sh: Fix output pollution on missing images hw/sd/sdcard: Add support for pre-setting the authentication key

  hw/sd/sd.c        | 23 +++++++++++++++++++++++
  scripts/mkemmc.sh |  4 ++--
  2 files changed, 25 insertions(+), 2 deletions(-)
Acked-by on v1 2/2
#9 lore raw

Re: [PATCH 2/2] hw/sd/sdcard: Add support for pre-setting the authentication key

In reply to Jan Kiszka
Quoted 16 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

In contrast to real eMMCs, we are not (yet) persisting the full state of the device. This particularly includes the authentication key used for RPMB access, complicating testing of firmware images which do not support initial provisioning of the key.

One way to address this would be, e.g., extending the eMMC disk image with a special sector to store further state. A simpler approach is used
here: Add another device property that allows to specify the authentication key, bringing up the eMMC as if the key has already been provisioned before.

This is how to tell qemu to use the OP-TEE test key:

Quoted 7 lines

-device emmc,[...],auth-key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

Or use this for machine-configured eMMCs:

-global emmc.auth-key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

Quoted 55 lines
---
 hw/sd/sd.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
hw/sd/sd.c +23 -1
index 37f6e0702b..13ea54df38 100644
@@ -205,6 +205,7 @@ struct SDState {
     QEMUTimer *ocr_power_timer;
     uint8_t dat_lines;
     bool cmd_line;
+    char *preset_auth_key;
 };
 
 static void sd_realize(DeviceState *dev, Error **errp);
@@ -3132,6 +3133,27 @@ static void sd_realize(DeviceState *dev, Error **errp)
                           "The RPMB partition size must be multiples of 128K"
                           "and not larger than 16384K.\n");
     }
+    if (sd_is_emmc(sd) && sd->preset_auth_key) {
+        if (strlen(sd->preset_auth_key) != 64) {
+            error_setg(errp,
+                       "Authentication key must be 32 bytes long, "
+                       "encoded hexadecimally");
+            return;
+        }
+
+        char *pos = sd->preset_auth_key;
+        unsigned int n;
+        for (n = 0; n < RPMB_KEY_MAC_LEN; n++, pos += 2) {
+            int chrs;
+            if (sscanf(pos, "%02hhx%n", &sd->rpmb.key[n], &chrs) != 1 ||
+                chrs != 2) {
+                error_setg(errp,
+                           "Authentication key contains invalid characters");
+                return;
+            }
+        }
+        sd->rpmb.key_set = 1;
+    }
 }
 
 static void emmc_realize(DeviceState *dev, Error **errp)
@@ -3156,6 +3178,7 @@ static const Property emmc_properties[] = {
     DEFINE_PROP_UINT64("boot-partition-size", SDState, boot_part_size, 0),
     DEFINE_PROP_UINT8("boot-config", SDState, boot_config, 0x0),
     DEFINE_PROP_UINT64("rpmb-partition-size", SDState, rpmb_part_size, 0),
+    DEFINE_PROP_STRING("auth-key", SDState, preset_auth_key),
 };
 
 static void sdmmc_common_class_init(ObjectClass *klass, const void *data)
--

2.47.3

author
#5 lore raw

Re: [PATCH 2/2] hw/sd/sdcard: Add support for pre-setting the authentication key

In reply to Philippe Mathieu-Daudé
Quoted 26 lines · Philippe Mathieu-Daudé

Hi Jan,

Quoted 23 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

In contrast to real eMMCs, we are not (yet) persisting the full state of the device. This particularly includes the authentication key used for RPMB access, complicating testing of firmware images which do not support initial provisioning of the key.

One way to address this would be, e.g., extending the eMMC disk image with a special sector to store further state. A simpler approach is used
here: Add another device property that allows to specify the authentication key, bringing up the eMMC as if the key has already been provisioned before.

This is how to tell qemu to use the OP-TEE test key:

-device emmc,[...],auth- key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

Or use this for machine-configured eMMCs:

-global emmc.auth- key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

Quoted 6 lines
Quoted 4 lines

---
  hw/sd/sd.c | 23 +++++++++++++++++++++++

  1 file changed, 23 insertions(+)

Do you mind mentioning it in docs/system/devices/emmc.rst?

Right, there was something... v2 our patch on top, what do you prefer?

Jan

Quoted 3 lines

Otherwise,

Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center
#6 lore raw

Re: [PATCH 2/2] hw/sd/sdcard: Add support for pre-setting the authentication key

Quoted 27 lines · Jan Kiszka
Quoted 26 lines · Philippe Mathieu-Daudé

Hi Jan,

Quoted 23 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

In contrast to real eMMCs, we are not (yet) persisting the full state of the device. This particularly includes the authentication key used for RPMB access, complicating testing of firmware images which do not support initial provisioning of the key.

One way to address this would be, e.g., extending the eMMC disk image with a special sector to store further state. A simpler approach is used
here: Add another device property that allows to specify the authentication key, bringing up the eMMC as if the key has already been provisioned before.

This is how to tell qemu to use the OP-TEE test key:

-device emmc,[...],auth- key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

Or use this for machine-configured eMMCs:

-global emmc.auth- key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
---
   hw/sd/sd.c | 23 +++++++++++++++++++++++
   1 file changed, 23 insertions(+)

Do you mind mentioning it in docs/system/devices/emmc.rst?

Right, there was something... v2 our patch on top, what do you prefer?

If there is no need for v2, my preference is an inlined diff that I can apply directly saving both of us time :)

Quoted 7 lines

Jan

Quoted 3 lines

Otherwise,

author
#7 lore raw

Re: [PATCH 2/2] hw/sd/sdcard: Add support for pre-setting the authentication key

Quoted 36 lines · Philippe Mathieu-Daudé
Quoted 35 lines · Jan Kiszka
Quoted 33 lines · Philippe Mathieu-Daudé

Hi Jan,

Quoted 21 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

In contrast to real eMMCs, we are not (yet) persisting the full state of the device. This particularly includes the authentication key used for RPMB access, complicating testing of firmware images which do not support initial provisioning of the key.

One way to address this would be, e.g., extending the eMMC disk image with a special sector to store further state. A simpler approach is used
here: Add another device property that allows to specify the authentication key, bringing up the eMMC as if the key has already been provisioned before.

This is how to tell qemu to use the OP-TEE test key:

-device emmc,[...],auth- key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

Or use this for machine-configured eMMCs:

Quoted 7 lines

-global emmc.auth- key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

---
   hw/sd/sd.c | 23 +++++++++++++++++++++++

   1 file changed, 23 insertions(+)

Do you mind mentioning it in docs/system/devices/emmc.rst?

Quoted 5 lines
Quoted 1 line

Right, there was something... v2 our patch on top, what do you prefer?

If there is no need for v2, my preference is an inlined diff that I can apply directly saving both of us time :)

Here we go:

docs/system/devices/emmc.rst +23 -0
index e62adfd68e..e7d67c809d 100644
@@ -53,3 +53,26 @@ SDHCI controllers while assuming that the firmware needs a boot partitions of
     -drive file=emmc.img,if=none,format=raw,id=emmc-img
     -device sdhci-pci
     -device emmc,drive=emmc-img,boot-partition-size=1048576,rpmb-partition-size=2097152
+
+RPMB Authentication Key
+=======================
+
+A private shared key is used for authenticating requests of the host to the
+RPMB. A real eMMC stores this persistently and permits no reprogramming once it
+is set. QEMU emulates key programming but does not persist the key state
+across restarts. To emulate the state "key is set", the eMMC can be created
+with a user-provided key via the ``auth-key`` property:
+
+.. code-block:: console
+
+    -device emmc,[...],auth-key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4
+
+This sets the well-known test key of OP-TEE on emmc device creation. In case an
+eMMC is instantiated by the machine model already:
+
+.. code-block:: console
+
+    -global emmc.auth-key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4
+
+A key always consists of 32 bytes that have to be encoded as hex numbers,
+left-padding with zeros as needed.

Jan

Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center
Reviewed-by
#11 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 1 line · Philippe Mathieu-Daudé

(Cc'ing Jerome to his new email)

Thanks :)

Quoted 6 lines · Jan Kiszka
Quoted 5 lines

Besides a cosmetic fix for the emmc disk image generation script, this adds a property to the emmc model that allows to preset the authentication key. And that enables to build OPTEE without key writing support, bringing the related firmware much closer to real setups.

Quoted 6 lines
Quoted 6 lines

An alternative to this option would have been persisting additional state such as the key or the write counter on the disk image as well. This is still an option for the future, e.g. to model also write protection groups or other persistent states. As this requires another format extension of the eMMC disk image, I want for this much simpler and less invasive approach.

That's coool! For the series:

Thanks,

Signature · 1 line
Jerome
Quoted 11 lines
Quoted 10 lines

Jan

Jan Kiszka (2):
scripts: mkemmc.sh: Fix output pollution on missing images hw/sd/sdcard: Add support for pre-setting the authentication key

  hw/sd/sd.c        | 23 +++++++++++++++++++++++
  scripts/mkemmc.sh |  4 ++--
  2 files changed, 25 insertions(+), 2 deletions(-)
#8 lore raw

Re: [PATCH 2/2] hw/sd/sdcard: Add support for pre-setting the authentication key

In reply to Jan Kiszka
Quoted 66 lines · Jan Kiszka
Quoted 36 lines · Philippe Mathieu-Daudé
Quoted 35 lines · Jan Kiszka
Quoted 33 lines · Philippe Mathieu-Daudé

Hi Jan,

Quoted 21 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

In contrast to real eMMCs, we are not (yet) persisting the full state of the device. This particularly includes the authentication key used for RPMB access, complicating testing of firmware images which do not support initial provisioning of the key.

One way to address this would be, e.g., extending the eMMC disk image with a special sector to store further state. A simpler approach is used
here: Add another device property that allows to specify the authentication key, bringing up the eMMC as if the key has already been provisioned before.

This is how to tell qemu to use the OP-TEE test key:

-device emmc,[...],auth- key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

Or use this for machine-configured eMMCs:

Quoted 7 lines

-global emmc.auth- key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

---
    hw/sd/sd.c | 23 +++++++++++++++++++++++
    1 file changed, 23 insertions(+)

Do you mind mentioning it in docs/system/devices/emmc.rst?

Quoted 5 lines
Quoted 1 line

Right, there was something... v2 our patch on top, what do you prefer?

If there is no need for v2, my preference is an inlined diff that I can apply directly saving both of us time :)

Here we go:

docs/system/devices/emmc.rst +13 -0
index e62adfd68e..e7d67c809d 100644
@@ -53,3 +53,26 @@ SDHCI controllers while assuming that the firmware needs a boot partitions of
      -drive file=emmc.img,if=none,format=raw,id=emmc-img
      -device sdhci-pci
      -device emmc,drive=emmc-img,boot-partition-size=1048576,rpmb-partition-size=2097152
+
+RPMB Authentication Key
+=======================
+
+A private shared key is used for authenticating requests of the host to the
+RPMB. A real eMMC stores this persistently and permits no reprogramming once it
+is set. QEMU emulates key programming but does not persist the key state
+across restarts. To emulate the state "key is set", the eMMC can be created
+with a user-provided key via the ``auth-key`` property:
+
+.. code-block:: console
+
+    -device emmc,[...],auth-key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4

docs/system/devices/emmc.rst SDHCI controllers while assuming that the firmware needs a boot partitions of · line 78

Quoted 10 lines
docs/system/devices/emmc.rst +10 -0
+
+This sets the well-known test key of OP-TEE on emmc device creation. In case an
+eMMC is instantiated by the machine model already:
+
+.. code-block:: console
+
+    -global emmc.auth-key=D3EB3EC36E334C9F988CE2C0B85954610D2BCF8664844DF2AB56E6C61BB701E4
+
+A key always consists of 32 bytes that have to be encoded as hex numbers,
+left-padding with zeros as needed.

Thanks, series queued!