-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from geonnave/add-python-wrapper
Add `lakers-python`
- Loading branch information
Showing
24 changed files
with
757 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
# build | ||
target/* | ||
bin | ||
.pytest_cache | ||
__pycache__ | ||
.venv* | ||
|
||
# hax | ||
*.fst | ||
|
||
# in Rust, libraries should avoid checking the lock file in version control | ||
# read more: https://doc.rust-lang.org/cargo/faq.html#why-do-binaries-have-cargolock-in-version-control-but-not-libraries | ||
Cargo.lock | ||
|
||
cscope* | ||
|
||
# editors | ||
*.swp | ||
*.swo | ||
cscope* | ||
.DS_Store | ||
.vscode | ||
*.fst | ||
|
||
bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "lakers-python" # this will be the name of the package on pypi | ||
edition = "2021" | ||
version ="0.1.2" | ||
repository.workspace = true | ||
license.workspace = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
pyo3 = { version = "0.20.2", features = ["extension-module"] } | ||
lakers = { package = "lakers", path = "../lib", default-features = false, features = ["ead-authz"] } | ||
lakers-shared = { path = "../shared", features = ["python-bindings"] } | ||
lakers-crypto = { path = "../crypto", default-features = false, features = ["rustcrypto"] } | ||
|
||
[lib] | ||
name = "lakers" # this will be the name of the python package (as in `import lakers`), and it must match the module name in lib.rs | ||
crate-type = ["cdylib"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Lakers Python | ||
Python wrapper for the [`lakers` crate](https://github.com/openwsn-berkeley/lakers). | ||
|
||
# Installation and usage | ||
|
||
```console | ||
pip install lakers-python | ||
``` | ||
|
||
```python | ||
import lakers | ||
|
||
# generate a keypair | ||
lakers.p256_generate_key_pair() | ||
|
||
# instantiate a initiator and prepare EDHOC's message 1 | ||
initiator = lakers.EdhocInitiator() | ||
message_1 = initiator.prepare_message_1(c_i=None, ead_1=None) | ||
|
||
# for more examples, see the tests in the repository | ||
``` | ||
|
||
# Development | ||
|
||
To build and test: | ||
```bash | ||
maturin develop | ||
pytest | ||
``` | ||
|
||
## Requirements | ||
|
||
The maturin executable must be available. The recommended way is to install and use it in a virtual environment: | ||
|
||
``` | ||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
pip install -U pip maturin pytest | ||
pip freeze | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[build-system] | ||
requires = ["maturin>=1.0,<2.0"] | ||
build-backend = "maturin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use lakers::*; | ||
use pyo3::prelude::*; | ||
|
||
#[pyclass(name = "AuthzAutenticator")] | ||
pub struct PyAuthzAutenticator { | ||
authenticator: ZeroTouchAuthenticator, | ||
authenticator_wait: ZeroTouchAuthenticatorWaitVoucherResp, | ||
} | ||
|
||
#[pymethods] | ||
impl PyAuthzAutenticator { | ||
#[new] | ||
fn new() -> Self { | ||
Self { | ||
authenticator: ZeroTouchAuthenticator::default(), | ||
authenticator_wait: ZeroTouchAuthenticatorWaitVoucherResp::default(), | ||
} | ||
} | ||
|
||
pub fn process_ead_1( | ||
&mut self, | ||
ead_1: EADItem, | ||
message_1: Vec<u8>, | ||
) -> PyResult<(Vec<u8>, Vec<u8>)> { | ||
let message_1 = EdhocMessageBuffer::new_from_slice(message_1.as_slice()).unwrap(); // FIXME: avoid unwrap | ||
let (state, loc_w, voucher_request) = | ||
self.authenticator.process_ead_1(&ead_1, &message_1)?; | ||
self.authenticator_wait = state; | ||
Ok(( | ||
Vec::from(loc_w.as_slice()), | ||
Vec::from(voucher_request.as_slice()), | ||
)) | ||
} | ||
|
||
pub fn prepare_ead_2(&self, voucher_response: Vec<u8>) -> PyResult<EADItem> { | ||
let voucher_response = | ||
EdhocMessageBuffer::new_from_slice(voucher_response.as_slice()).unwrap(); // FIXME: avoid unwrap | ||
Ok(self.authenticator_wait.prepare_ead_2(&voucher_response)?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod authenticator; | ||
pub use authenticator::*; | ||
mod server; | ||
pub use server::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use lakers::*; | ||
use lakers_crypto::default_crypto; | ||
use pyo3::prelude::*; | ||
|
||
#[pyclass(name = "AuthzEnrollmentServer")] | ||
pub struct PyAuthzEnrollmentServer { | ||
server: ZeroTouchServer, | ||
} | ||
|
||
#[pymethods] | ||
impl PyAuthzEnrollmentServer { | ||
#[new] | ||
pub fn new(w: Vec<u8>, cred_v: Vec<u8>, acl: Option<Vec<u8>>) -> Self { | ||
let mut w_arr = BytesP256ElemLen::default(); | ||
w_arr.copy_from_slice(&w.as_slice()); | ||
let acl = if let Some(acl) = acl { | ||
Some(EdhocMessageBuffer::new_from_slice(acl.as_slice()).unwrap()) | ||
} else { | ||
None | ||
}; | ||
|
||
Self { | ||
server: ZeroTouchServer::new(w_arr, cred_v.as_slice(), acl), | ||
} | ||
} | ||
|
||
fn handle_voucher_request(&self, vreq: Vec<u8>) -> PyResult<Vec<u8>> { | ||
let vreq = EdhocMessageBuffer::new_from_slice(vreq.as_slice()).unwrap(); | ||
match self | ||
.server | ||
.handle_voucher_request(&mut default_crypto(), &vreq) | ||
{ | ||
Ok(voucher_response) => Ok(Vec::from(voucher_response.as_slice())), | ||
Err(error) => Err(error.into()), | ||
} | ||
} | ||
} |
Oops, something went wrong.