Skip to main content

polymorph_webcrypto_guest/
aes_gcm.rs

1//! `aes-gcm` key creation (caller-nonce — see [`Aead`]'s nonce warning).
2
3use crate::{bindings, Aead, AeadKeyOptions, Error};
4
5pub use crate::bindings::aes_gcm::AesVariant;
6
7/// Import raw key material as the declared AES variant.
8pub async fn import_key_raw(
9    variant: AesVariant,
10    raw: impl Into<Vec<u8>>,
11    options: AeadKeyOptions,
12) -> Result<Aead, Error> {
13    Ok(Aead::from_raw(
14        bindings::aes_gcm::import_key_raw(variant, raw.into(), options.lower()).await?,
15    ))
16}
17
18/// Import an RFC 7517 `oct` JSON Web Key (as JSON text) as an AES-GCM key
19/// of the declared variant. See the WIT `mac-key.export-key-jwk` doc for
20/// the package-wide JWK contract.
21pub async fn import_key_jwk(
22    variant: AesVariant,
23    jwk: impl Into<String>,
24    options: AeadKeyOptions,
25) -> Result<Aead, Error> {
26    Ok(Aead::from_raw(
27        bindings::aes_gcm::import_key_jwk(variant, jwk.into(), options.lower()).await?,
28    ))
29}
30
31/// Generate a fresh random key of the declared AES variant.
32pub async fn generate_key(variant: AesVariant, options: AeadKeyOptions) -> Result<Aead, Error> {
33    Ok(Aead::from_raw(
34        bindings::aes_gcm::generate_key(variant, options.lower()).await?,
35    ))
36}
37
38/// Mint an AES-GCM key of the declared variant from a parameterized
39/// derivation: the derivation runs at the variant's key length and the
40/// result is subject to [`import_key_raw`]'s contract.
41///
42/// Requires the input's [`derive_key`](crate::DeriveOptions::derive_key)
43/// grant — and, for an *extractable* key,
44/// [`derive_bits`](crate::DeriveOptions::derive_bits) too; refusals fail
45/// [`Error::NotPermitted`].
46pub async fn derive_key(
47    variant: AesVariant,
48    input: &crate::DeriveInput,
49    options: AeadKeyOptions,
50) -> Result<Aead, Error> {
51    Ok(Aead::from_raw(
52        bindings::aes_gcm::derive_key(variant, input.as_raw(), options.lower()).await?,
53    ))
54}
55
56/// Mint an AES-GCM key of the declared variant from unwrapped key
57/// material read as raw bytes, subject to [`import_key_raw`]'s contract.
58/// Consumes the [`UnwrapInput`](crate::UnwrapInput).
59pub async fn unwrap_key_raw(
60    variant: AesVariant,
61    input: crate::UnwrapInput,
62    options: AeadKeyOptions,
63) -> Result<Aead, Error> {
64    Ok(Aead::from_raw(
65        bindings::aes_gcm::unwrap_key_raw(variant, input.into_raw(), options.lower()).await?,
66    ))
67}
68
69/// Mint an AES-GCM key from unwrapped key material read as an `oct` JWK,
70/// subject to [`import_key_jwk`]'s contract plus the unwrap-path
71/// `use`/`key_ops` checks. Consumes the [`UnwrapInput`](crate::UnwrapInput).
72pub async fn unwrap_key_jwk(
73    variant: AesVariant,
74    input: crate::UnwrapInput,
75    options: AeadKeyOptions,
76) -> Result<Aead, Error> {
77    Ok(Aead::from_raw(
78        bindings::aes_gcm::unwrap_key_jwk(variant, input.into_raw(), options.lower()).await?,
79    ))
80}