Skip to content

Rust wrapper: add copy() to SHA256 and SHA384 via wc_Sha256Copy/wc_Sha384Copy#10425

Open
MarkAtwood wants to merge 1 commit into
wolfSSL:masterfrom
MarkAtwood:feat/rust-sha-copy
Open

Rust wrapper: add copy() to SHA256 and SHA384 via wc_Sha256Copy/wc_Sha384Copy#10425
MarkAtwood wants to merge 1 commit into
wolfSSL:masterfrom
MarkAtwood:feat/rust-sha-copy

Conversation

@MarkAtwood

@MarkAtwood MarkAtwood commented May 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements the Clone trait for SHA256 and SHA384, backed by wc_Sha256Copy and wc_Sha384Copy respectively.

This allows mid-computation hash state to be cloned into an independent context in O(1), without re-hashing from scratch. The motivating use case is the rustls TLS provider, which needs to snapshot transcript hash state at multiple points during the handshake.

Changes

  • wrapper/rust/wolfssl-wolfcrypt/src/sha.rs: implement Clone for SHA256 and SHA384 using MaybeUninit and the corresponding C copy functions via FFI. Panics on copy failure (should not occur under normal conditions).

Notes

  • No public API breaking changes.
  • No new dependencies.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the Rust wolfssl-wolfcrypt SHA wrappers to support O(1) snapshotting of in-progress transcript hashes by adding copy() methods backed by wolfCrypt’s wc_Sha256Copy and wc_Sha384Copy.

Changes:

  • Add SHA256::copy() implemented via wc_Sha256Copy.
  • Add SHA384::copy() implemented via wc_Sha384Copy.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread wrapper/rust/wolfssl-wolfcrypt/src/sha.rs Outdated
Comment thread wrapper/rust/wolfssl-wolfcrypt/src/sha.rs Outdated
Comment thread wrapper/rust/wolfssl-wolfcrypt/src/sha.rs Outdated
@github-actions

github-actions Bot commented May 7, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

gcc-arm-cortex-m0plus

  • FLASH: .text +680 B (+1.1%, 64,155 B / 262,144 B, total: 24% used)

gcc-arm-cortex-m3

  • FLASH: .text +1,224 B (+1.0%, 122,481 B / 262,144 B, total: 47% used)

gcc-arm-cortex-m4

  • FLASH: .rodata.CSWTCH.1 +4 B, .rodata.str1.1 +54 B, .rodata.wolfSSL_ERR_reason_error_string.str1.1 +88 B, .text +1,472 B (+0.8%, 200,320 B / 262,144 B, total: 76% used)

gcc-arm-cortex-m4-baremetal

  • FLASH: .text +768 B (+1.2%, 66,827 B / 262,144 B, total: 25% used)

gcc-arm-cortex-m4-crypto-only

  • FLASH: .rodata.CSWTCH.1 +4 B, .rodata.str1.1 +54 B, .text +960 B (+0.6%, 174,568 B / 262,144 B, total: 67% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +1,344 B (+0.7%, 180,888 B / 1,048,576 B, total: 17% used)

gcc-arm-cortex-m4-min-ecc

  • FLASH: .text +704 B (+1.2%, 61,741 B / 262,144 B, total: 24% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .rodata +480 B, .text +4,416 B (+0.6%, 771,148 B / 1,048,576 B, total: 74% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: .rodata.CSWTCH.1 +4 B, .rodata.str1.1 +54 B, .text +1,536 B (+0.8%, 212,588 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m4-pq

  • FLASH: .rodata +140 B, .text +2,112 B (+0.8%, 279,396 B / 1,048,576 B, total: 27% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .rodata +136 B, .text +2,240 B (+0.7%, 324,992 B / 1,048,576 B, total: 31% used)

gcc-arm-cortex-m4-sp-math

  • FLASH: .text +704 B (+1.2%, 61,741 B / 262,144 B, total: 24% used)

gcc-arm-cortex-m4-tls12

  • FLASH: .text +1,216 B (+1.0%, 123,213 B / 262,144 B, total: 47% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .rodata.CSWTCH.1 +4 B, .rodata.str1.1 +54 B, .rodata.wolfSSL_ERR_reason_error_string.str1.1 +88 B, .text +1,472 B (+0.7%, 236,082 B / 262,144 B, total: 90% used)

gcc-arm-cortex-m7

  • FLASH: .rodata.CSWTCH.1 +4 B, .rodata.str1.1 +54 B, .rodata.wolfSSL_ERR_reason_error_string.str1.1 +88 B, .text +1,472 B (+0.8%, 200,320 B / 262,144 B, total: 76% used)

gcc-arm-cortex-m7-pq

  • FLASH: .rodata +140 B, .text +2,048 B (+0.8%, 279,972 B / 1,048,576 B, total: 27% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .rodata.CSWTCH.1 +4 B, .rodata.str1.1 +54 B, .rodata.wolfSSL_ERR_reason_error_string.str1.1 +88 B, .text +1,536 B (+0.7%, 236,146 B / 262,144 B, total: 90% used)

linuxkm-pie

  • Data: __patchable_function_entries +1,344 B (+5.6%, 25,552 B)

linuxkm-standard

  • Data: __patchable_function_entries +2,296 B (+5.0%, 48,216 B)

stm32-sim-stm32h753

  • FLASH: .text +1,968 B (+1.1%, 183,460 B / 2,097,152 B, total: 9% used)

@MarkAtwood

Copy link
Copy Markdown
Contributor Author

All review feedback addressed in 71ce78c:

  1. Uninitialized dst — now uses Self::new() to properly initialize before wc_Sha*Copy
  2. &self unsoundness — changed to &mut self to model potential src mutation on MAXQ10XX
  3. Tests — added test_sha256_copy and test_sha384_copy with independent expected digests

Ready for re-review.

Add copy(&mut self) methods backed by wc_Sha256Copy/wc_Sha384Copy
for transcript snapshotting (e.g. TLS handshake hashing).

Uses &mut self (not Clone trait) because wc_ShaCopy may mutate src
on some platforms. Initializes dst before calling Copy to avoid
passing uninitialized memory to wc_ShaFree inside the C function.

Includes unit tests verifying independent copy semantics with
externally computed SHA-256/SHA-384 test vectors.
@MarkAtwood MarkAtwood force-pushed the feat/rust-sha-copy branch from 71ce78c to af04efe Compare June 9, 2026 15:26
@MarkAtwood MarkAtwood closed this Jul 9, 2026
@MarkAtwood MarkAtwood reopened this Jul 9, 2026
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.

4 participants