Skip to main content

wasi_crypto_preview/
lib.rs

1#![allow(
2    clippy::unit_arg,
3    clippy::useless_conversion,
4    clippy::new_without_default,
5    clippy::new_ret_no_self,
6    clippy::too_many_arguments
7)]
8#![allow(unused_imports, dead_code)]
9#[macro_use]
10extern crate derivative;
11
12mod array_output;
13mod asymmetric_common;
14mod error;
15mod handles;
16mod key_exchange;
17mod key_manager;
18mod options;
19mod signatures;
20mod symmetric;
21mod version;
22mod wasi_glue;
23
24use std::collections::HashMap;
25use std::rc::Rc;
26
27use crate::types as guest_types;
28use array_output::*;
29use asymmetric_common::*;
30use handles::*;
31use key_exchange::*;
32use key_manager::*;
33use options::*;
34use signatures::*;
35use symmetric::*;
36
37pub use asymmetric_common::{KeyPairEncoding, PublicKeyEncoding};
38pub use error::CryptoError;
39pub use handles::Handle;
40pub use signatures::SignatureEncoding;
41pub use version::Version;
42
43pub fn witx_interfaces() -> HashMap<&'static str, &'static str> {
44    let mut map = HashMap::new();
45    map.insert(
46        "proposal_common.witx",
47        include_str!(concat!(
48            env!("CARGO_MANIFEST_DIR"),
49            "/witx/proposal_common.witx"
50        )),
51    );
52    map.insert(
53        "proposal_asymmetric_common.witx",
54        include_str!(concat!(
55            env!("CARGO_MANIFEST_DIR"),
56            "/witx/proposal_asymmetric_common.witx"
57        )),
58    );
59    map.insert(
60        "proposal_signatures.witx",
61        include_str!(concat!(
62            env!("CARGO_MANIFEST_DIR"),
63            "/witx/proposal_signatures.witx"
64        )),
65    );
66    map.insert(
67        "proposal_symmetric.witx",
68        include_str!(concat!(
69            env!("CARGO_MANIFEST_DIR"),
70            "/witx/proposal_symmetric.witx"
71        )),
72    );
73    map.insert(
74        "proposal_kx.witx",
75        include_str!(concat!(
76            env!("CARGO_MANIFEST_DIR"),
77            "/witx/proposal_kx.witx"
78        )),
79    );
80    map.insert(
81        "wasi_ephemeral_crypto.witx",
82        include_str!(concat!(
83            env!("CARGO_MANIFEST_DIR"),
84            "/witx/wasi_ephemeral_crypto.witx"
85        )),
86    );
87    map
88}
89
90wiggle::from_witx!({
91    witx: ["$CARGO_MANIFEST_DIR/witx/wasi_ephemeral_crypto.witx"],
92    ctx: WasiCryptoCtx
93});
94
95pub mod wasi_modules {
96    pub use crate::{
97        wasi_ephemeral_crypto_asymmetric_common, wasi_ephemeral_crypto_common,
98        wasi_ephemeral_crypto_signatures, wasi_ephemeral_crypto_symmetric,
99    };
100}
101
102pub struct HandleManagers {
103    pub array_output: HandlesManager<ArrayOutput>,
104    pub options: HandlesManager<Options>,
105    pub keypair: HandlesManager<KeyPair>,
106    pub publickey: HandlesManager<PublicKey>,
107    pub secretkey: HandlesManager<SecretKey>,
108    pub signature_state: HandlesManager<SignatureState>,
109    pub signature: HandlesManager<Signature>,
110    pub signature_verification_state: HandlesManager<SignatureVerificationState>,
111    pub symmetric_state: HandlesManager<SymmetricState>,
112    pub symmetric_key: HandlesManager<SymmetricKey>,
113    pub symmetric_tag: HandlesManager<SymmetricTag>,
114    pub key_manager: HandlesManager<KeyManager>,
115}
116
117#[derive(Copy, Clone, Debug, PartialEq, Eq)]
118pub enum AlgorithmType {
119    Signatures,
120    Symmetric,
121    KeyExchange,
122}
123
124impl From<guest_types::AlgorithmType> for AlgorithmType {
125    fn from(options_type: guest_types::AlgorithmType) -> Self {
126        match options_type {
127            guest_types::AlgorithmType::Signatures => AlgorithmType::Signatures,
128            guest_types::AlgorithmType::Symmetric => AlgorithmType::Symmetric,
129            guest_types::AlgorithmType::KeyExchange => AlgorithmType::KeyExchange,
130        }
131    }
132}
133
134pub struct CryptoCtx {
135    pub(crate) handles: HandleManagers,
136}
137
138#[derive(Clone)]
139pub struct WasiCryptoCtx {
140    ctx: Rc<CryptoCtx>,
141}
142
143impl CryptoCtx {
144    pub fn new() -> Self {
145        CryptoCtx {
146            handles: HandleManagers {
147                array_output: HandlesManager::new(0x00),
148                options: HandlesManager::new(0x01),
149                keypair: HandlesManager::new(0x02),
150                publickey: HandlesManager::new(0x03),
151                secretkey: HandlesManager::new(0x04),
152                signature_state: HandlesManager::new(0x05),
153                signature: HandlesManager::new(0x06),
154                signature_verification_state: HandlesManager::new(0x07),
155                symmetric_state: HandlesManager::new(0x08),
156                symmetric_key: HandlesManager::new(0x09),
157                symmetric_tag: HandlesManager::new(0x0a),
158                key_manager: HandlesManager::new(0x0b),
159            },
160        }
161    }
162}
163
164impl WasiCryptoCtx {
165    pub fn new() -> Self {
166        WasiCryptoCtx {
167            ctx: Rc::new(CryptoCtx::new()),
168        }
169    }
170}