Conversation
xorptr
commented
Sep 3, 2026
xorptr
force-pushed
the
optional_manuf_certgen_fields
branch
2 times, most recently
from
September 14, 2026 02:50
b0bfbe4 to
f099aad
Compare
xorptr
marked this pull request as ready for review
September 14, 2026 02:55
sasdf
approved these changes
Sep 14, 2026
sasdf
left a comment
Contributor
There was a problem hiding this comment.
LGTM. I feel we have some code duplication between normal and optional fields in ujson_derive.h, but we can refactor them in other PRs later.
Perso changes to support ML-DSA certificates during provisioning recently added new fields in [`ManufCertgenInputs`](https://github.com/lowRISC/opentitan/blob/2a28b8761d771a889056b4b02d991acd6a6a4082/sw/device/lib/testing/json/provisioning_data.h#L140-L142). This breaks the compatibility between the new perso tool running on the host and older perso firmware binaries. The new tool sends these additional fields in JSON data to the older binaries, and older firmware binaries [fail to deserialize](https://github.com/lowRISC/opentitan/blob/2a28b8761d771a889056b4b02d991acd6a6a4082/sw/device/lib/ujson/ujson_derive.h#L235) the JSON data for `ManufCertgenInputs`. This caused CI tests to fail in downstream SKU specific repositories. This commit declares new ujson macros to support declaring optional fields (but not strings). - When expanding to declarations in C, the macro will create 2 struct members for each optional field: one denoting whether the optional field is present or not, and the other denoting data in the optional field which is valid only if it is present. For Rust, the macro, expansion creates a single member with type `Option<_>` and marks it with `serde` attribute to skip serializing that field if it is `None` - When serializing an optional field, C code will skip putting out JSON string for the field if it is marked as not present using respective struct member. Rust code uses the `serde` attribute mentioned above to skip putting out JSON for that field if it is set to `None` - The code computes the number of fields that will be put out during serialization so that it can decide whether or not to put `,` at end of the field data. This logic used to store number of fields declared in the struct (determined at compile time). With optional fields, this is changed to determine the number of such fields based on struct members that indicate whether the optional field is present or not - When deserializing an optional field, C code will initialize the struct members to assume that the field is not present. If the field is found to be present while parsing JSON string, the code will mark the struct member to indicate so and copy the field data in respective struct member. For Rust code, I am relying on `serde` to take the appropriate action when deserializing JSON data - The expansion for declarations, serialization, and deserialization of non-optional fields is not changed I also removed the field `generate_mldsa_uds_cert` from `ManufCertgenInputs` since the struct member that indicates whether `dice_mldsa_auth_key_key_id` is present or not now does the job for that field, i.e. ML-DSA certificate support is enabled depending on whether the optional field `dice_mldsa_auth_key_key_id` is present or not. With the changes mentioned above, the new host tool does not send newly added fields in JSON data to older perso firmware binaries and this avoids failures in tests that do this I also added various tests to check various cases with optional fields in general, and also with `ManufCertgenInputs` in particular. Tested by running the following targets locally: - `//sw/device/lib/testing/json:provisioning_data_test` - `//sw/host/provisioning/ujson_lib:ujson_lib_test` - `//sw/device/lib/ujson:example_test` - `//sw/device/lib/ujson/rust:roundtrip_test` - `//sw/host/provisioning/orchestrator/tests:e2e_emulation_dice_mldsa_cw340_test` with bazel arg `--//sw/host/provisioning/cert_lib:enable_mldsa_signing_for_test` - `//sw/host/provisioning/orchestrator/tests:e2e_multistage_emulation_dice_mldsa_cw340_test` with bazel arg `--//sw/host/provisioning/cert_lib:enable_mldsa_signing_for_test` Signed-off-by: Lovepreet Singh <lpsingh@google.com>
xorptr
force-pushed
the
optional_manuf_certgen_fields
branch
from
September 14, 2026 18:11
f099aad to
bec667f
Compare
Author
|
Rebased on top of latest commit in |
Author
Yes. There are too many places in code where I needed to make change for adding 3rd argument to macros defining structs with |
cfrantz
approved these changes
Sep 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR makes changes to address the issue pointed out by @sasdf in #31051 (comment)
This PR addresses that issue by adding support to declare optional fields in structs that are serialized and deserialized by ujson. With this new support the new perso tool will not send newly added fields (to support ML-DSA provisioning) to older perso firmware binaries, thereby addressing the issue mentioned above
The changes add new macros where few of the fields can be optional.The macros are expanded to declare structs, and declare functions for serialization and deserialization during compilation. I have added few unit tests to check that structs with optional fields produce expected JSON data.