1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#![allow(
    clippy::unit_arg,
    clippy::useless_conversion,
    clippy::new_without_default,
    clippy::new_ret_no_self,
    clippy::too_many_arguments
)]
#![allow(unused_imports, dead_code)]
#[macro_use]
extern crate derivative;

mod array_output;
mod asymmetric_common;
mod error;
mod handles;
mod key_exchange;
mod key_manager;
mod options;
mod signatures;
mod symmetric;
mod version;
mod wasi_glue;

use std::collections::HashMap;
use std::rc::Rc;

use crate::types as guest_types;
use array_output::*;
use asymmetric_common::*;
use handles::*;
use key_exchange::*;
use key_manager::*;
use options::*;
use signatures::*;
use symmetric::*;

pub use asymmetric_common::{KeyPairEncoding, PublicKeyEncoding};
pub use error::CryptoError;
pub use handles::Handle;
pub use signatures::SignatureEncoding;
pub use version::Version;

pub fn witx_interfaces() -> HashMap<&'static str, &'static str> {
    let mut map = HashMap::new();
    map.insert(
        "proposal_common.witx",
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/witx/proposal_common.witx"
        )),
    );
    map.insert(
        "proposal_asymmetric_common.witx",
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/witx/proposal_asymmetric_common.witx"
        )),
    );
    map.insert(
        "proposal_signatures.witx",
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/witx/proposal_signatures.witx"
        )),
    );
    map.insert(
        "proposal_symmetric.witx",
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/witx/proposal_symmetric.witx"
        )),
    );
    map.insert(
        "proposal_kx.witx",
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/witx/proposal_kx.witx"
        )),
    );
    map.insert(
        "wasi_ephemeral_crypto.witx",
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/witx/wasi_ephemeral_crypto.witx"
        )),
    );
    map
}

wiggle::from_witx!({
    witx: ["$CARGO_MANIFEST_DIR/witx/wasi_ephemeral_crypto.witx"],
    ctx: WasiCryptoCtx
});

pub mod wasi_modules {
    pub use crate::{
        wasi_ephemeral_crypto_asymmetric_common, wasi_ephemeral_crypto_common,
        wasi_ephemeral_crypto_signatures, wasi_ephemeral_crypto_symmetric,
    };
}

pub struct HandleManagers {
    pub array_output: HandlesManager<ArrayOutput>,
    pub options: HandlesManager<Options>,
    pub keypair: HandlesManager<KeyPair>,
    pub publickey: HandlesManager<PublicKey>,
    pub secretkey: HandlesManager<SecretKey>,
    pub signature_state: HandlesManager<SignatureState>,
    pub signature: HandlesManager<Signature>,
    pub signature_verification_state: HandlesManager<SignatureVerificationState>,
    pub symmetric_state: HandlesManager<SymmetricState>,
    pub symmetric_key: HandlesManager<SymmetricKey>,
    pub symmetric_tag: HandlesManager<SymmetricTag>,
    pub key_manager: HandlesManager<KeyManager>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum AlgorithmType {
    Signatures,
    Symmetric,
    KeyExchange,
}

impl From<guest_types::AlgorithmType> for AlgorithmType {
    fn from(options_type: guest_types::AlgorithmType) -> Self {
        match options_type {
            guest_types::AlgorithmType::Signatures => AlgorithmType::Signatures,
            guest_types::AlgorithmType::Symmetric => AlgorithmType::Symmetric,
            guest_types::AlgorithmType::KeyExchange => AlgorithmType::KeyExchange,
        }
    }
}

pub struct CryptoCtx {
    pub(crate) handles: HandleManagers,
}

#[derive(Clone)]
pub struct WasiCryptoCtx {
    ctx: Rc<CryptoCtx>,
}

impl CryptoCtx {
    pub fn new() -> Self {
        CryptoCtx {
            handles: HandleManagers {
                array_output: HandlesManager::new(0x00),
                options: HandlesManager::new(0x01),
                keypair: HandlesManager::new(0x02),
                publickey: HandlesManager::new(0x03),
                secretkey: HandlesManager::new(0x04),
                signature_state: HandlesManager::new(0x05),
                signature: HandlesManager::new(0x06),
                signature_verification_state: HandlesManager::new(0x07),
                symmetric_state: HandlesManager::new(0x08),
                symmetric_key: HandlesManager::new(0x09),
                symmetric_tag: HandlesManager::new(0x0a),
                key_manager: HandlesManager::new(0x0b),
            },
        }
    }
}

impl WasiCryptoCtx {
    pub fn new() -> Self {
        WasiCryptoCtx {
            ctx: Rc::new(CryptoCtx::new()),
        }
    }
}