How to use the RustCrypto "ansi-x963-kdf" crate?

⚓ Rust    📅 2025-09-02    👤 surdeus    👁️ 4      

surdeus

I need to compute ANSI X9.63 key derivation function. So I try to use ansi-x963-kdf crate.

My Cargo.toml looks like this:

[dependencies]
ansi-x963-kdf = "0.0.1"

First, I try to run the provided example code:

use sha2::Sha256;

let mut key = [0u8; 16];
ansi_x963_kdf::derive_key_into::<Sha256>(b"secret", b"shared-info", &mut key).unwrap();

...but this will give the following error:

error[E0412]: cannot find type `Sha256` in this scope

Attempt 1

Adding sha2 crate to the Cargo.toml seems like the obvious solution:

[dependencies]
ansi-x963-kdf = "0.0.1"
sha2 = "0.10.9"

...but this will give error:

error[E0277]: the trait bound `CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, UInt<UInt<..., ...>, ...>, ...>>: FixedOutputReset` is not satisfied
   --> src/lib.rs:5:38
    |
5   |     ansi_x963_kdf::derive_key_into::<Sha256>(b"secret", b"shared-info", &mut key).unwrap();
    |                                      ^^^^^^ the trait `digest::FixedOutputReset` is not implemented for `CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, OidSha256>>`
    |
note: there are multiple different versions of crate `digest` in the dependency graph
   --> /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/digest-0.11.0-pre.9/src/lib.rs:107:1
    |
107 | pub trait FixedOutputReset: FixedOutput + Reset {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is the required trait
    |
   ::: src/lib.rs:1:5
    |
1   | use sha2::Sha256;
    |     ---- one version of crate `digest` used here, as a dependency of crate `sha2`
...
5   |     ansi_x963_kdf::derive_key_into::<Sha256>(b"secret", b"shared-info", &mut key).unwrap();
    |     ------------- one version of crate `digest` used here, as a dependency of crate `ansi_x963_kdf`
    |
   ::: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/digest-0.10.7/src/core_api/wrapper.rs:24:1
    |
24  | pub struct CoreWrapper<T>
    | ------------------------- this type doesn't implement the required trait
...
266 | pub trait CoreProxy: sealed::Sealed {
    | ----------------------------------- this is the found trait
    |
   ::: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/digest-0.11.0-pre.9/src/core_api/wrapper.rs:31:1
    |
31  | pub struct CoreWrapper<T: BufferKindUser> {
    | ----------------------------------------- this type implements the required trait
    = note: two types coming from two different versions of the same crate are different types even if they look the same
    = help: you can use `cargo tree` to explore your dependency tree
note: required by a bound in `derive_key_into`
   --> /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ansi-x963-kdf-0.0.1/src/lib.rs:21:17
    |
19  | pub fn derive_key_into<D>(secret: &[u8], shared_info: &[u8], key: &mut [u8]) -> Result<(), Error>
    |        --------------- required by a bound in this function
20  | where
21  |     D: Digest + FixedOutputReset,
    |                 ^^^^^^^^^^^^^^^^ required by this bound in `derive_key_into`
    = note: the full name for the type has been written to '/home/user/src/digitalkey-rust/rust/libs/x963-kdf/target/debug/deps/x963_kdf-2de751d8f03710d3.long-type-5266545726922841315.txt'
    = note: consider using `--verbose` to print the full type name to the console

error[E0277]: the trait bound `CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, UInt<UInt<UInt<..., ...>, ...>, ...>, ...>>: Digest` is not satisfied
   --> src/lib.rs:5:38
    |
5   |     ansi_x963_kdf::derive_key_into::<Sha256>(b"secret", b"shared-info", &mut key).unwrap();
    |                                      ^^^^^^ the trait `digest::digest::HashMarker` is not implemented for `CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, OidSha256>>`
    |
note: there are multiple different versions of crate `digest` in the dependency graph
   --> /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/digest-0.11.0-pre.9/src/digest.rs:10:1
    |
10  | pub trait HashMarker {}
    | ^^^^^^^^^^^^^^^^^^^^ this is the required trait
    |
   ::: src/lib.rs:1:5
    |
1   | use sha2::Sha256;
    |     ---- one version of crate `digest` used here, as a dependency of crate `sha2`
...
5   |     ansi_x963_kdf::derive_key_into::<Sha256>(b"secret", b"shared-info", &mut key).unwrap();
    |     ------------- one version of crate `digest` used here, as a dependency of crate `ansi_x963_kdf`
    |
   ::: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/digest-0.10.7/src/core_api/wrapper.rs:24:1
    |
24  | pub struct CoreWrapper<T>
    | ------------------------- this type doesn't implement the required trait
...
266 | pub trait CoreProxy: sealed::Sealed {
    | ----------------------------------- this is the found trait
    |
   ::: /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/digest-0.11.0-pre.9/src/core_api/wrapper.rs:31:1
    |
31  | pub struct CoreWrapper<T: BufferKindUser> {
    | ----------------------------------------- this type implements the required trait
    = note: two types coming from two different versions of the same crate are different types even if they look the same
    = help: you can use `cargo tree` to explore your dependency tree
    = note: required for `CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, OidSha256>>` to implement `digest::digest::Digest`
note: required by a bound in `derive_key_into`
   --> /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ansi-x963-kdf-0.0.1/src/lib.rs:21:8
    |
19  | pub fn derive_key_into<D>(secret: &[u8], shared_info: &[u8], key: &mut [u8]) -> Result<(), Error>
    |        --------------- required by a bound in this function
20  | where
21  |     D: Digest + FixedOutputReset,
    |        ^^^^^^ required by this bound in `derive_key_into`
    = note: the full name for the type has been written to '/home/user/src/digitalkey-rust/rust/libs/x963-kdf/target/debug/deps/x963_kdf-2de751d8f03710d3.long-type-9290383935925356502.txt'
    = note: consider using `--verbose` to print the full type name to the console

For more information about this error, try `rustc --explain E0277`.
error: could not compile `x963-kdf` (lib) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...

Attempt 2

After looking at the ansi-x963-kdf sources:

...I have tried:

[dependencies]
ansi-x963-kdf = "0.0.1"
sha2 = { version = "0.11.0-rc.0", default-features = false }

...but this will give different error:

error: failed to select a version for `digest`.
    ... required by package `sha2 v0.11.0-rc.0`
    ... which satisfies dependency `sha2 = "^0.11.0-rc.0"` of package `x963-kdf v0.1.0 (/home/user/src/digitalkey-rust/rust/libs/x963-kdf)`
versions that meet the requirements `^0.11.0-rc.0` are: 0.11.0-rc.0

all possible versions conflict with previously selected packages.

  previously selected package `digest v0.11.0-pre.9`
    ... which satisfies dependency `digest = "=0.11.0-pre.9"` of package `ansi-x963-kdf v0.0.1`
    ... which satisfies dependency `ansi-x963-kdf = "^0.0.1"` of package `x963-kdf v0.1.0 (/home/user/src/digitalkey-rust/rust/libs/x963-kdf)`

failed to select a version for `digest` which could resolve this conflict

:sad_but_relieved_face:

How can I fix this?

Thank you!

3 posts - 2 participants

Read full topic

🏷️ Rust_feed