Skip to content

Commit

Permalink
Merge pull request #306 from geonnave/logs-rust-to-python
Browse files Browse the repository at this point in the history
python: emit logs up from rust
  • Loading branch information
geonnave authored Aug 30, 2024
2 parents 21e5e73 + e08c646 commit a09314c
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ bin
.pytest_cache
__pycache__
.venv*
wheelhouse

# hax
*.fst
Expand Down
3 changes: 3 additions & 0 deletions ead/lakers-ead-authz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ repository.workspace = true

[dependencies]
lakers-shared.workspace = true
defmt-or-log = { version = "0.2.1", default-features = false }
log = { version = "0.4", optional = true }
defmt = { version = "0.3", optional = true }

[dev-dependencies]
lakers-crypto.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions ead/lakers-ead-authz/src/authenticator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::consts::*;
use crate::shared::*;
use defmt_or_log::trace;
use lakers_shared::*;

#[derive(Debug, Default)]
Expand All @@ -20,6 +21,7 @@ impl ZeroTouchAuthenticator {
),
EDHOCError,
> {
trace!("Enter process_ead_1");
let opaque_state: Option<EdhocMessageBuffer> = None; // TODO: receive as parameter

if ead_1.label != EAD_AUTHZ_LABEL || ead_1.value.is_none() {
Expand All @@ -42,6 +44,7 @@ impl ZeroTouchAuthenticatorWaitVoucherResp {
&self,
voucher_response: &EdhocMessageBuffer,
) -> Result<EADItem, EDHOCError> {
trace!("Enter prepare_ead_2");
let (_message_1, voucher, _opaque_state) = parse_voucher_response(&voucher_response)?;

Ok(EADItem {
Expand Down
5 changes: 5 additions & 0 deletions ead/lakers-ead-authz/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::consts::*;
use crate::shared::*;
use crate::ZeroTouchError;
use defmt_or_log::trace;
use lakers_shared::{Crypto as CryptoTrait, *};

#[derive(Default, Debug)]
Expand All @@ -26,6 +27,7 @@ pub struct ZeroTouchDeviceDone {

impl ZeroTouchDevice {
pub fn new(id_u: EdhocMessageBuffer, g_w: BytesP256ElemLen, loc_w: EdhocMessageBuffer) -> Self {
trace!("Initializing ZeroTouchDevice");
ZeroTouchDevice { id_u, g_w, loc_w }
}

Expand All @@ -35,6 +37,7 @@ impl ZeroTouchDevice {
secret: BytesP256ElemLen,
ss: u8,
) -> (ZeroTouchDeviceWaitEAD2, EADItem) {
trace!("Enter prepare_ead_1");
// PRK = EDHOC-Extract(salt, IKM)
let prk = compute_prk_from_secret(crypto, &secret);

Expand All @@ -61,6 +64,7 @@ impl ZeroTouchDevice {

impl ZeroTouchDeviceWaitEAD2 {
pub fn set_h_message_1(&mut self, h_message_1: BytesHashLen) {
trace!("Enter set_h_message_1");
self.h_message_1 = h_message_1;
}

Expand All @@ -70,6 +74,7 @@ impl ZeroTouchDeviceWaitEAD2 {
ead_2: EADItem,
cred_v: &[u8],
) -> Result<ZeroTouchDeviceDone, ZeroTouchError> {
trace!("Enter process_ead_2");
if ead_2.label != EAD_AUTHZ_LABEL {
return Err(ZeroTouchError::InvalidEADLabel);
}
Expand Down
6 changes: 6 additions & 0 deletions ead/lakers-ead-authz/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::shared::*;
use defmt_or_log::trace;
use lakers_shared::{Crypto as CryptoTrait, *};

/// This server also stores an ACL
Expand All @@ -12,6 +13,7 @@ pub struct ZeroTouchServer {

impl ZeroTouchServer {
pub fn new(w: BytesP256ElemLen, cred_v: &[u8], acl: Option<EdhocMessageBuffer>) -> Self {
trace!("Initializing ZeroTouchServer");
let cred_v: EdhocMessageBuffer = cred_v.try_into().unwrap();
ZeroTouchServer { w, cred_v, acl }
}
Expand All @@ -30,6 +32,7 @@ impl ZeroTouchServer {
crypto: &mut Crypto,
vreq: &EdhocMessageBuffer,
) -> Result<EdhocMessageBuffer, EDHOCError> {
trace!("Enter handle_voucher_request");
let (message_1, opaque_state) = parse_voucher_request(vreq)?;
let (_method, _suites_i, g_x, _c_i, ead_1) = parse_message_1(&message_1)?;
let prk = compute_prk(crypto, &self.w, &g_x);
Expand Down Expand Up @@ -62,6 +65,7 @@ pub struct ZeroTouchServerUserAcl {

impl ZeroTouchServerUserAcl {
pub fn new(w: BytesP256ElemLen, cred_v: &[u8]) -> Self {
trace!("Initializing ZeroTouchServerUserAcl");
let cred_v: EdhocMessageBuffer = cred_v.try_into().unwrap();
Self { w, cred_v }
}
Expand All @@ -71,6 +75,7 @@ impl ZeroTouchServerUserAcl {
crypto: &mut Crypto,
vreq: &EdhocMessageBuffer,
) -> Result<EdhocMessageBuffer, EDHOCError> {
trace!("Enter decode_voucher_request");
let (message_1, _opaque_state) = parse_voucher_request(vreq)?;
let (_method, _suites_i, g_x, _c_i, ead_1) = parse_message_1(&message_1)?;
let prk = compute_prk(crypto, &self.w, &g_x);
Expand All @@ -86,6 +91,7 @@ impl ZeroTouchServerUserAcl {
crypto: &mut Crypto,
vreq: &EdhocMessageBuffer,
) -> Result<EdhocMessageBuffer, EDHOCError> {
trace!("Enter prepare_voucher");
let (message_1, opaque_state) = parse_voucher_request(vreq)?;
let (_method, _suites_i, g_x, _c_i, _ead_1) = parse_message_1(&message_1)?;
let prk = compute_prk(crypto, &self.w, &g_x);
Expand Down
2 changes: 1 addition & 1 deletion examples/coap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
lakers = { package = "lakers", path = "../../lib", features = [ "log" ] }
lakers-ead-authz = { path = "../../ead/lakers-ead-authz" }
lakers-ead-authz = { path = "../../ead/lakers-ead-authz", features = [ "log" ] }
lakers-crypto = { path = "../../crypto/", features = [ "rustcrypto" ] }
hexlit = "0.5.3"
coap = { version = "0.13" }
Expand Down
6 changes: 4 additions & 2 deletions lakers-python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ license.workspace = true

[dependencies]
pyo3 = { version = "0.22", features = ["extension-module"] }
lakers = { package = "lakers", path = "../lib", default-features = false }
lakers-ead-authz = { path = "../ead/lakers-ead-authz" }
lakers = { package = "lakers", path = "../lib", default-features = false, features = [ "log" ] }
lakers-ead-authz = { path = "../ead/lakers-ead-authz", features = [ "log" ] }
lakers-shared = { path = "../shared", features = ["python-bindings", "quadruple_sizes"] }
lakers-crypto = { path = "../crypto", default-features = false, features = ["rustcrypto"] }
log = "0.4"
env_logger = "0.9"

[dev-dependencies]
# We don't need it to build, but it is listed in the manifest Cargo.toml, and
Expand Down
7 changes: 7 additions & 0 deletions lakers-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ message_1 = initiator.prepare_message_1(c_i=None, ead_1=None)
# for more examples, see the tests in the repository
```

## Logs
To show logs emitted by the wrapped Rust implementation, set the `RUST_LOG` variable, e.g.:

```bash
RUST_LOG=trace python -c "import lakers"
```

# Development

To build and test:
Expand Down
2 changes: 2 additions & 0 deletions lakers-python/src/ead_authz/authenticator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use lakers::*;
use lakers_ead_authz::*;
use log::trace;
use pyo3::{
prelude::*,
types::{PyBytes, PyString},
Expand All @@ -15,6 +16,7 @@ pub struct PyAuthzAutenticator {
impl PyAuthzAutenticator {
#[new]
fn new() -> Self {
trace!("Initializing AuthzAutenticator");
Self {
authenticator: ZeroTouchAuthenticator::default(),
authenticator_wait: ZeroTouchAuthenticatorWaitVoucherResp::default(),
Expand Down
2 changes: 2 additions & 0 deletions lakers-python/src/ead_authz/device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use lakers::*;
use lakers_crypto::default_crypto;
use lakers_ead_authz::*;
use log::trace;
use pyo3::{exceptions::PyBaseException, prelude::*, types::PyBytes};

#[pyclass(name = "AuthzDevice")]
Expand All @@ -14,6 +15,7 @@ pub struct PyAuthzDevice {
impl PyAuthzDevice {
#[new]
fn new(id_u: Vec<u8>, g_w: Vec<u8>, loc_w: &str) -> Self {
trace!("Initializing AuthzDevice");
let id_u = EdhocMessageBuffer::new_from_slice(id_u.as_slice()).unwrap();
let loc_w = EdhocMessageBuffer::new_from_slice(loc_w.as_bytes()).unwrap();
let mut g_w_arr = BytesP256ElemLen::default();
Expand Down
3 changes: 3 additions & 0 deletions lakers-python/src/ead_authz/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use lakers::*;
use lakers_crypto::default_crypto;
use lakers_ead_authz::*;
use log::trace;
use pyo3::{prelude::*, types::PyBytes};

#[pyclass(name = "AuthzEnrollmentServer")]
Expand All @@ -13,6 +14,7 @@ impl PyAuthzEnrollmentServer {
#[new]
#[pyo3(signature = (w, cred_v, acl=None))]
pub fn new(w: Vec<u8>, cred_v: Vec<u8>, acl: Option<Vec<u8>>) -> Self {
trace!("Initializing AuthzEnrollmentServer");
let mut w_arr = BytesP256ElemLen::default();
w_arr.copy_from_slice(&w.as_slice());
let acl = if let Some(acl) = acl {
Expand Down Expand Up @@ -51,6 +53,7 @@ pub struct PyAuthzServerUserAcl {
impl PyAuthzServerUserAcl {
#[new]
pub fn new(w: Vec<u8>, cred_v: Vec<u8>) -> Self {
trace!("Initializing AuthzServerUserAcl");
let mut w_arr = BytesP256ElemLen::default();
w_arr.copy_from_slice(&w.as_slice());

Expand Down
2 changes: 2 additions & 0 deletions lakers-python/src/initiator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use lakers::*;
use lakers_crypto::{default_crypto, CryptoTrait};
use log::trace;
use pyo3::{prelude::*, types::PyBytes};

#[pyclass(name = "EdhocInitiator")]
Expand All @@ -16,6 +17,7 @@ pub struct PyEdhocInitiator {
impl PyEdhocInitiator {
#[new]
fn new() -> Self {
trace!("Initializing EdhocInitiator");
let mut crypto = default_crypto();
let suites_i =
prepare_suites_i(&crypto.supported_suites(), EDHOCSuite::CipherSuite2.into()).unwrap();
Expand Down
7 changes: 7 additions & 0 deletions lakers-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
/// Note that this module is not restricted by no_std.
use lakers::*;
// use lakers_ead_authz::consts::*;
use env_logger;
use lakers_crypto::{default_crypto, CryptoTrait};
use log::trace;
use pyo3::wrap_pyfunction;
use pyo3::{prelude::*, types::PyBytes};

Expand Down Expand Up @@ -69,6 +71,11 @@ impl AutoCredential {
#[pymodule]
#[pyo3(name = "lakers")]
fn lakers_python(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
// initialize the logger once when the module is imported
if env_logger::try_init().is_ok() {
trace!("lakers-python initialized from Rust side.");
}

m.add_function(wrap_pyfunction!(p256_generate_key_pair, m)?)?;
m.add_function(wrap_pyfunction!(py_credential_check_or_fetch, m)?)?;
// edhoc items
Expand Down
2 changes: 2 additions & 0 deletions lakers-python/src/responder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use lakers::*;
use lakers_crypto::{default_crypto, CryptoTrait};
use log::trace;
use pyo3::{prelude::*, types::PyBytes};

#[pyclass(name = "EdhocResponder")]
Expand All @@ -17,6 +18,7 @@ pub struct PyEdhocResponder {
impl PyEdhocResponder {
#[new]
fn new(r: Vec<u8>, cred_r: super::AutoCredential) -> PyResult<Self> {
trace!("Initializing EdhocResponder");
let (y, g_y) = default_crypto().p256_generate_key_pair();

let cred_r = cred_r.to_credential()?;
Expand Down

0 comments on commit a09314c

Please sign in to comment.