Skip to content

Total re-design of Factory? #189

Open

Description

@ounsworth

We have a number of tickets open about factories. I think we agree that the current design of the factory basically needs to be deleted and re-designed from scratch.

Design goals:

  1. Provide an abstraction layer that keeps algorithm-specific code out of calling applications. For example, if an application needs a signature algorithm, it should be possible to build it abstractly so that the developer doesn't know or care whether it's ML-DSA-44, ML-DSA-87, ECDSA-P256, or something else. That essentially means that the abstraction layer should operate at the level of traits so that you only need to know that you're holding a &dyn Signer, or similar.
  2. Provide a string-to-object mapping; ie something that behaves something like let signer = SignerFactory::new_from_name("ML-DSA-65");
  3. That scaffolding will make it easy to build protocols like X.509, CMS, JWT, CWT, TLS (if we go there) because they just need a mapping table from however that protocol names algorithms (OIDs, codepoints, etc) to our canonical internal name, and then they can use the factory. In particular, that does separation of layers by keeping algorithm-specific junk out of the protocol layer.

We get to decide at what level of std, no_std+alloc, no_std, no_alloc we want the factory to be functional at. The current factory is attempting to be operable without an allocator, and this is the root of all the complexity. Doing this at the no_std+alloc level would be much simpler because then you'd have access to Box, which would make the whole thing easier.


PS -- if we trash the current factory crate, then that might soften the requirement that our traits.rs all need to be dyn-compatible, which would allow us to roll-back some of the hideous designs, like the fact that Hash's hash() isn't a static, but instead requires SHA256::new().hash(&msg); when it really should be SHA256::hash(&msg); for no good reason except that the latter is not dyn-compatible.

Activity

  1. dghgit commented on Nov 7, 2026

    @dghgit
    Contributor

    I agree we definitely need some examples of 3 first - there seems to be far to many options for how to roll this kind of stuff in Rust, each with their own particular set of, erm..., "advantages and disadvantages". If we could just get enough ASN.1 parsing/encoding to be able to sign/verify a certificate we'd then know what things would look like for everything from CMS to PGP (cashing in on bc-java/bc-csharp experience here). Although I suspect 1 and 2 will fall out of that, so perhaps things are reversed. Use of dyn does seem to be a popular lever though, there's still a bit to be done to support it.

    There's one wish that's come true already:

    use bouncycastle_core::traits::HashOneShot;
    use bouncycastle_sha2::SHA256;
    
    let digest = SHA256::hash(&msg);
    

    actually works now.

  2. dghgit commented on Nov 10, 2026

    @dghgit
    Contributor

    From PR #180

    1. The factory enums' size (HashFactory and XOFFactory about 1888 bytes, MACFactoryWorker 1872, from BLAKE3's 1728-byte CV stack): the open question in the body. The reviewer leans towards a CV stack sized to a documented input cap (2^40 bytes gives 30 CVs, about 1 KB saved) over keeping the full depth; still to decide.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions