Compare commits

..

10 Commits

3 changed files with 69 additions and 15 deletions

10
Cargo.lock generated
View File

@@ -13,20 +13,20 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.82"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519"
checksum = "25bdb32cbbdce2b519a9cd7df3a678443100e265d5e25ca763b7572a5104f5f3"
[[package]]
name = "libc"
version = "0.2.153"
version = "0.2.154"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
[[package]]
name = "shared_memory_heap"
version = "0.1.0"
source = "git+https://gitea.rixxc.de/rixxc/shared_memory_heap.git#5cd37dfc93aa9bc44df47571396dff70d773fcb2"
source = "git+https://gitea.rixxc.de/rixxc/shared_memory_heap.git#18f1a1b9a6f2d29f215cb238e470ca91f9e03bc3"
dependencies = [
"libc",
]

View File

@@ -46,12 +46,15 @@ impl Agent {
if child == 0 {
// child
let path = CString::new(path.as_os_str().as_encoded_bytes()).unwrap();
let data_fd = CString::new(data_fd.to_string()).unwrap();
let sync_fd = CString::new(sync_fd.to_string()).unwrap();
let args = [data_fd.as_ptr(), sync_fd.as_ptr(), ptr::null()];
let keyfile = CString::new(std::env::var("KEY_FILE").expect("KEY_FILE environment variable missing")).unwrap();
let args = [data_fd.as_ptr(), sync_fd.as_ptr(), keyfile.as_ptr(), ptr::null()];
execve(
path.as_os_str().as_encoded_bytes().as_ptr() as *const c_char,
path.as_ptr() as *const c_char,
args.as_ptr(),
ptr::null(),
);

View File

@@ -6,8 +6,7 @@ pub use shared_memory_heap::sharedptr::SharedPtr;
use agent::Agent;
use std::{
path::Path,
sync::{LazyLock, Mutex},
ops::Deref, path::Path, sync::{LazyLock, Mutex}
};
static AGENT: LazyLock<Mutex<Agent>> = LazyLock::new(|| {
@@ -17,13 +16,53 @@ static AGENT: LazyLock<Mutex<Agent>> = LazyLock::new(|| {
});
#[derive(Debug)]
pub struct X25519PrivKey<'a>(SharedPtr<'a, 8>);
pub struct X25519PrivKey(SharedPtr<8>);
#[derive(Debug)]
pub struct X25519PubKey<'a>(SharedPtr<'a, 32>);
pub struct X25519PubKey(SharedPtr<32>);
#[derive(Debug)]
pub struct X25519SharedKey<'a>(SharedPtr<'a, 32>);
pub struct X25519SharedKey(SharedPtr<32>);
pub fn x25519_keygen<'a>() -> (X25519PrivKey<'a>, X25519PubKey<'a>) {
impl From<&[u8; 32]> for X25519PubKey {
fn from(value: &[u8; 32]) -> Self {
let mut pk = SharedPtr::<32>::new().unwrap();
pk.copy_from_slice(value);
X25519PubKey(pk)
}
}
impl From<&[u8; 8]> for X25519PrivKey {
fn from(value: &[u8; 8]) -> Self {
let mut sk = SharedPtr::<8>::new().unwrap();
sk.copy_from_slice(value);
X25519PrivKey(sk)
}
}
impl Deref for X25519PrivKey {
type Target = [u8; 8];
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl Deref for X25519PubKey {
type Target = [u8; 32];
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl Deref for X25519SharedKey {
type Target = [u8; 32];
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
pub fn x25519_keygen() -> (X25519PrivKey, X25519PubKey) {
let sk = X25519PrivKey(SharedPtr::<8>::new().unwrap());
let pk = X25519PubKey(SharedPtr::<32>::new().unwrap());
@@ -36,13 +75,25 @@ pub fn x25519_keygen<'a>() -> (X25519PrivKey<'a>, X25519PubKey<'a>) {
(sk, pk)
}
pub fn x25519<'a>(sk: &X25519PrivKey, pk: &X25519PubKey) -> X25519SharedKey<'a> {
pub fn x22519_pubkey(sk: &X25519PrivKey) -> X25519PubKey {
let pk = X25519PubKey(SharedPtr::<32>::new().unwrap());
let mut agent = AGENT.lock().unwrap();
unsafe {
agent.perform_ipc_call(1, &[sk.0.get_offset(), pk.0.get_offset()]);
}
pk
}
pub fn x25519(sk: &X25519PrivKey, pk: &X25519PubKey) -> X25519SharedKey {
let out = X25519SharedKey(SharedPtr::<32>::new().unwrap());
let mut agent = AGENT.lock().unwrap();
unsafe {
agent.perform_ipc_call(1, &[out.0.get_offset(), sk.0.get_offset(), pk.0.get_offset()]);
agent.perform_ipc_call(2, &[out.0.get_offset(), sk.0.get_offset(), pk.0.get_offset()]);
}
out