Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of pkcs11_store.c module #481

Merged
merged 4 commits into from
Sep 6, 2024

Conversation

danielinux
Copy link
Member

@danielinux danielinux commented Jul 31, 2024

  • add unit tests for pkcs11_store.c
  • defects detected by unit tests are fixed
  • refactoring with a new powerfail-safe design

This new design uses two sectors at the beginning of the keyvault partition.
The first sector contains the allocation tables for the nodes in flash, including
a bitmap to keep track of the free slots.
The second sector is a pre-commit 'backup' sector, used to update the content of the
vault in two steps. This guarantees powerfail protection.

All hal_flash_erase / hal_flash_write operations are now on entire sectors.
Sector content is cached to memory, then written to the backup sector, then
committed to the actual destination sector.

@danielinux danielinux self-assigned this Jul 31, 2024
@danielinux
Copy link
Member Author

Awaiting feedback (zd18366)

obj = vault_descriptors[found];
memcpy(&obj->hdr, vault_base + found * KEYVAULT_OBJ_SIZE, sizeof(struct obj_hdr));
}
obj = XMALLOC(sizeof(struct store_object), NULL, DYNAMIC_TYPE_TMP_BUFFER);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider cast (struct store_object*).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XMALLOC is gone now

memcpy(&obj->hdr, vault_base + found * KEYVAULT_OBJ_SIZE, sizeof(struct obj_hdr));
}
obj = XMALLOC(sizeof(struct store_object), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (!obj) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer obj == NULL... thus not assuming NULL == 0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

obj = vault_descriptors[found];
memcpy(&obj->hdr, vault_base + found * KEYVAULT_OBJ_SIZE, sizeof(struct obj_hdr));
}
obj = XMALLOC(sizeof(struct store_object), NULL, DYNAMIC_TYPE_TMP_BUFFER);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider version that doesn't require heap..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the dynamic allocation in the last refactoring, however it seems that wolfPKCS11 still requires a heap, so I re-introduced the _sbrk in the designated pool.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, since wolfPKCS11 uses its own area, this heap can be in a bank that is securely erased on powerdown

@danielinux danielinux changed the title Added unit tests for pkcs11_store.c module Refactoring of pkcs11_store.c module Aug 5, 2024
@danielinux danielinux force-pushed the pkcs11-store-fixes branch 4 times, most recently from 997847b to 5b9278d Compare August 12, 2024 09:03
@dgarske dgarske self-requested a review September 5, 2024 15:13
@dgarske dgarske self-assigned this Sep 5, 2024
dgarske
dgarske previously approved these changes Sep 5, 2024
@danielinux
Copy link
Member Author

Ready to review

@danielinux danielinux assigned dgarske and unassigned danielinux Sep 5, 2024
dgarske
dgarske previously approved these changes Sep 5, 2024
@danielinux danielinux assigned dgarske and unassigned danielinux Sep 6, 2024
@dgarske dgarske merged commit 21464f7 into wolfSSL:master Sep 6, 2024
95 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants