2022-10-09 16:50:40 +02:00
|
|
|
//! Symmetric and asymmetric waku messages [decrypting](https://rfc.vac.dev/spec/36/#decrypting-messages) methods
|
|
|
|
|
|
|
|
|
|
// std
|
2023-02-14 18:30:08 +01:00
|
|
|
use std::ffi::CString;
|
2022-10-09 16:50:40 +02:00
|
|
|
// crates
|
|
|
|
|
use aes_gcm::{Aes256Gcm, Key};
|
2022-10-17 19:30:07 +02:00
|
|
|
use secp256k1::SecretKey;
|
2022-10-09 16:50:40 +02:00
|
|
|
// internal
|
2023-02-14 18:30:08 +01:00
|
|
|
use crate::general::{DecodedPayload, Result, WakuMessage};
|
|
|
|
|
use crate::utils::decode_and_free_response;
|
2022-10-09 16:50:40 +02:00
|
|
|
|
|
|
|
|
/// Decrypt a message using a symmetric key
|
|
|
|
|
///
|
|
|
|
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_decode_symmetricchar-messagejson-char-symmetrickey)
|
|
|
|
|
pub fn waku_decode_symmetric(
|
|
|
|
|
message: &WakuMessage,
|
|
|
|
|
symmetric_key: &Key<Aes256Gcm>,
|
|
|
|
|
) -> Result<DecodedPayload> {
|
|
|
|
|
let symk = hex::encode(symmetric_key.as_slice());
|
2023-02-14 18:30:08 +01:00
|
|
|
|
|
|
|
|
let message_ptr = CString::new(
|
|
|
|
|
serde_json::to_string(&message)
|
|
|
|
|
.expect("WakuMessages should always be able to success serializing"),
|
|
|
|
|
)
|
|
|
|
|
.expect("CString should build properly from the serialized waku message")
|
|
|
|
|
.into_raw();
|
|
|
|
|
let symk_ptr = CString::new(symk)
|
|
|
|
|
.expect("CString should build properly from hex encoded symmetric key")
|
|
|
|
|
.into_raw();
|
|
|
|
|
|
|
|
|
|
let result_ptr = unsafe {
|
|
|
|
|
let res = waku_sys::waku_decode_symmetric(message_ptr, symk_ptr);
|
|
|
|
|
drop(CString::from_raw(message_ptr));
|
|
|
|
|
drop(CString::from_raw(symk_ptr));
|
|
|
|
|
res
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
decode_and_free_response(result_ptr)
|
2022-10-09 16:50:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Decrypt a message using a symmetric key
|
|
|
|
|
///
|
2022-11-02 16:21:15 +01:00
|
|
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_decode_asymmetricchar-messagejson-char-privatekey)
|
2022-10-09 16:50:40 +02:00
|
|
|
pub fn waku_decode_asymmetric(
|
|
|
|
|
message: &WakuMessage,
|
|
|
|
|
asymmetric_key: &SecretKey,
|
|
|
|
|
) -> Result<DecodedPayload> {
|
2022-10-17 19:30:07 +02:00
|
|
|
let sk = hex::encode(asymmetric_key.secret_bytes());
|
2023-02-14 18:30:08 +01:00
|
|
|
|
|
|
|
|
let message_ptr = CString::new(
|
|
|
|
|
serde_json::to_string(&message)
|
|
|
|
|
.expect("WakuMessages should always be able to success serializing"),
|
|
|
|
|
)
|
|
|
|
|
.expect("CString should build properly from the serialized waku message")
|
|
|
|
|
.into_raw();
|
|
|
|
|
let sk_ptr = CString::new(sk)
|
|
|
|
|
.expect("CString should build properly from hex encoded symmetric key")
|
|
|
|
|
.into_raw();
|
|
|
|
|
|
|
|
|
|
let result_ptr = unsafe {
|
|
|
|
|
let res = waku_sys::waku_decode_asymmetric(message_ptr, sk_ptr);
|
|
|
|
|
drop(CString::from_raw(message_ptr));
|
|
|
|
|
drop(CString::from_raw(sk_ptr));
|
|
|
|
|
res
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
decode_and_free_response(result_ptr)
|
2022-10-09 16:50:40 +02:00
|
|
|
}
|