Skip to main content

polymorph_webcrypto_guest/
aes_cbc.rs

1//! `aes-cbc` key creation (the unauthenticated AES-CBC mode; prefer
2//! [`aes_gcm`](crate::aes_gcm) — see [`CipherKey`]'s warning).
3
4use crate::{bindings, CipherKey, CipherKeyOptions, Error};
5
6pub use crate::bindings::aes_cbc::AesVariant;
7
8/// Import raw key material as the declared AES variant.
9pub async fn import_key_raw(
10    variant: AesVariant,
11    raw: impl Into<Vec<u8>>,
12    options: CipherKeyOptions,
13) -> Result<CipherKey, Error> {
14    Ok(CipherKey::from_raw(
15        bindings::aes_cbc::import_key_raw(variant, raw.into(), options.lower()).await?,
16    ))
17}
18
19/// Import an RFC 7517 `oct` JSON Web Key (as JSON text) as a AES-CBC key
20/// of the declared variant. See the WIT `mac-key.export-key-jwk` doc for
21/// the package-wide JWK contract.
22pub async fn import_key_jwk(
23    variant: AesVariant,
24    jwk: impl Into<String>,
25    options: CipherKeyOptions,
26) -> Result<CipherKey, Error> {
27    Ok(CipherKey::from_raw(
28        bindings::aes_cbc::import_key_jwk(variant, jwk.into(), options.lower()).await?,
29    ))
30}
31
32/// Generate a fresh random key of the declared AES variant.
33pub async fn generate_key(
34    variant: AesVariant,
35    options: CipherKeyOptions,
36) -> Result<CipherKey, Error> {
37    Ok(CipherKey::from_raw(
38        bindings::aes_cbc::generate_key(variant, options.lower()).await?,
39    ))
40}
41
42/// Mint an AES-CBC key of the declared variant from a parameterized
43/// derivation. See [`aes_gcm::derive_key`](crate::aes_gcm::derive_key)
44/// for the grant contracts.
45pub async fn derive_key(
46    variant: AesVariant,
47    input: &crate::DeriveInput,
48    options: CipherKeyOptions,
49) -> Result<CipherKey, Error> {
50    Ok(CipherKey::from_raw(
51        bindings::aes_cbc::derive_key(variant, input.as_raw(), options.lower()).await?,
52    ))
53}