Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align rustfmt.toml with rust-lightning #29

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
hard_tabs = true # use tab characters for indentation, spaces for alignment
use_field_init_shorthand = true
max_width = 120
use_small_heuristics = "Max"
chain_width = 80
fn_params_layout = "Compressed"
hard_tabs = true
use_field_init_shorthand = true
max_width = 100
match_block_trailing_comma = true
# UNSTABLE: format_code_in_doc_comments = true
# UNSTABLE: overflow_delimited_expr = true
# UNSTABLE: comment_width = 100
# UNSTABLE: format_macro_matchers = true
# UNSTABLE: format_strings = true
# UNSTABLE: group_imports = "StdExternalCrate"
31 changes: 23 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::sync::Arc;
use crate::error::VssError;
use crate::headers::{get_headermap, FixedHeaders, VssHeaderProvider};
use crate::types::{
DeleteObjectRequest, DeleteObjectResponse, GetObjectRequest, GetObjectResponse, ListKeyVersionsRequest,
ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse,
DeleteObjectRequest, DeleteObjectResponse, GetObjectRequest, GetObjectResponse,
ListKeyVersionsRequest, ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse,
};
use crate::util::retry::{retry, RetryPolicy};

Expand Down Expand Up @@ -37,13 +37,20 @@ impl<R: RetryPolicy<E = VssError>> VssClient<R> {

/// Constructs a [`VssClient`] from a given [`reqwest::Client`], using `base_url` as the VSS server endpoint.
pub fn from_client(base_url: String, client: Client, retry_policy: R) -> Self {
Self { base_url, client, retry_policy, header_provider: Arc::new(FixedHeaders::new(HashMap::new())) }
Self {
base_url,
client,
retry_policy,
header_provider: Arc::new(FixedHeaders::new(HashMap::new())),
}
}

/// Constructs a [`VssClient`] using `base_url` as the VSS server endpoint.
///
/// HTTP headers will be provided by the given `header_provider`.
pub fn new_with_headers(base_url: String, retry_policy: R, header_provider: Arc<dyn VssHeaderProvider>) -> Self {
pub fn new_with_headers(
base_url: String, retry_policy: R, header_provider: Arc<dyn VssHeaderProvider>,
) -> Self {
let client = Client::new();
Self { base_url, client, retry_policy, header_provider }
}
Expand All @@ -56,7 +63,9 @@ impl<R: RetryPolicy<E = VssError>> VssClient<R> {
/// Fetches a value against a given `key` in `request`.
/// Makes a service call to the `GetObject` endpoint of the VSS server.
/// For API contract/usage, refer to docs for [`GetObjectRequest`] and [`GetObjectResponse`].
pub async fn get_object(&self, request: &GetObjectRequest) -> Result<GetObjectResponse, VssError> {
pub async fn get_object(
&self, request: &GetObjectRequest,
) -> Result<GetObjectResponse, VssError> {
retry(
|| async {
let url = format!("{}/getObject", self.base_url);
Expand All @@ -79,7 +88,9 @@ impl<R: RetryPolicy<E = VssError>> VssClient<R> {
/// Makes a service call to the `PutObject` endpoint of the VSS server, with multiple items.
/// Items in the `request` are written in a single all-or-nothing transaction.
/// For API contract/usage, refer to docs for [`PutObjectRequest`] and [`PutObjectResponse`].
pub async fn put_object(&self, request: &PutObjectRequest) -> Result<PutObjectResponse, VssError> {
pub async fn put_object(
&self, request: &PutObjectRequest,
) -> Result<PutObjectResponse, VssError> {
retry(
|| async {
let url = format!("{}/putObjects", self.base_url);
Expand All @@ -93,7 +104,9 @@ impl<R: RetryPolicy<E = VssError>> VssClient<R> {
/// Deletes the given `key` and `value` in `request`.
/// Makes a service call to the `DeleteObject` endpoint of the VSS server.
/// For API contract/usage, refer to docs for [`DeleteObjectRequest`] and [`DeleteObjectResponse`].
pub async fn delete_object(&self, request: &DeleteObjectRequest) -> Result<DeleteObjectResponse, VssError> {
pub async fn delete_object(
&self, request: &DeleteObjectRequest,
) -> Result<DeleteObjectResponse, VssError> {
retry(
|| async {
let url = format!("{}/deleteObject", self.base_url);
Expand All @@ -120,7 +133,9 @@ impl<R: RetryPolicy<E = VssError>> VssClient<R> {
.await
}

async fn post_request<Rq: Message, Rs: Message + Default>(&self, request: &Rq, url: &str) -> Result<Rs, VssError> {
async fn post_request<Rq: Message, Rs: Message + Default>(
&self, request: &Rq, url: &str,
) -> Result<Rs, VssError> {
let request_body = request.encode_to_vec();
let headermap = self
.header_provider
Expand Down
200 changes: 115 additions & 85 deletions src/crypto/chacha20.rs

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/crypto/chacha20poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ mod real_chachapoly {
mac.input(aad);
ChaCha20Poly1305::pad_mac_16(&mut mac, aad.len());

ChaCha20Poly1305 { cipher, mac, finished: false, data_len: 0, aad_len: aad.len() as u64 }
ChaCha20Poly1305 {
cipher,
mac,
finished: false,
data_len: 0,
aad_len: aad.len() as u64,
}
}

pub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
Expand Down
73 changes: 51 additions & 22 deletions src/crypto/poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ pub struct Poly1305 {
impl Poly1305 {
pub fn new(key: &[u8]) -> Poly1305 {
assert!(key.len() == 32);
let mut poly =
Poly1305 { r: [0u32; 5], h: [0u32; 5], pad: [0u32; 4], leftover: 0, buffer: [0u8; 16], finalized: false };
let mut poly = Poly1305 {
r: [0u32; 5],
h: [0u32; 5],
pad: [0u32; 4],
leftover: 0,
buffer: [0u8; 16],
finalized: false,
};

// r &= 0xffffffc0ffffffc0ffffffc0fffffff
poly.r[0] = (u32::from_le_bytes(key[0..4].try_into().expect("len is 4"))) & 0x3ffffff;
poly.r[1] = (u32::from_le_bytes(key[3..7].try_into().expect("len is 4")) >> 2) & 0x3ffff03;
poly.r[2] = (u32::from_le_bytes(key[6..10].try_into().expect("len is 4")) >> 4) & 0x3ffc0ff;
poly.r[3] = (u32::from_le_bytes(key[9..13].try_into().expect("len is 4")) >> 6) & 0x3f03fff;
poly.r[4] = (u32::from_le_bytes(key[12..16].try_into().expect("len is 4")) >> 8) & 0x00fffff;
poly.r[4] =
(u32::from_le_bytes(key[12..16].try_into().expect("len is 4")) >> 8) & 0x00fffff;

poly.pad[0] = u32::from_le_bytes(key[16..20].try_into().expect("len is 4"));
poly.pad[1] = u32::from_le_bytes(key[20..24].try_into().expect("len is 4"));
Expand Down Expand Up @@ -272,22 +279,28 @@ mod test {
#[test]
fn test_nacl_vector() {
let key = [
0xee, 0xa6, 0xa7, 0x25, 0x1c, 0x1e, 0x72, 0x91, 0x6d, 0x11, 0xc2, 0xcb, 0x21, 0x4d, 0x3c, 0x25, 0x25, 0x39,
0x12, 0x1d, 0x8e, 0x23, 0x4e, 0x65, 0x2d, 0x65, 0x1f, 0xa4, 0xc8, 0xcf, 0xf8, 0x80,
0xee, 0xa6, 0xa7, 0x25, 0x1c, 0x1e, 0x72, 0x91, 0x6d, 0x11, 0xc2, 0xcb, 0x21, 0x4d,
0x3c, 0x25, 0x25, 0x39, 0x12, 0x1d, 0x8e, 0x23, 0x4e, 0x65, 0x2d, 0x65, 0x1f, 0xa4,
0xc8, 0xcf, 0xf8, 0x80,
];

let msg = [
0x8e, 0x99, 0x3b, 0x9f, 0x48, 0x68, 0x12, 0x73, 0xc2, 0x96, 0x50, 0xba, 0x32, 0xfc, 0x76, 0xce, 0x48, 0x33,
0x2e, 0xa7, 0x16, 0x4d, 0x96, 0xa4, 0x47, 0x6f, 0xb8, 0xc5, 0x31, 0xa1, 0x18, 0x6a, 0xc0, 0xdf, 0xc1, 0x7c,
0x98, 0xdc, 0xe8, 0x7b, 0x4d, 0xa7, 0xf0, 0x11, 0xec, 0x48, 0xc9, 0x72, 0x71, 0xd2, 0xc2, 0x0f, 0x9b, 0x92,
0x8f, 0xe2, 0x27,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, 0x3d, 0x14, 0xa6, 0x59, 0x9b, 0x1f, 0x65, 0x4c, 0xb4,
0x5a, 0x74, 0xe3, 0x55, 0xa5,
];

let expected = [0xf3, 0xff, 0xc7, 0x70, 0x3f, 0x94, 0x00, 0xe5, 0x2a, 0x7d, 0xfb, 0x4b, 0x3d, 0x33, 0x05, 0xd9];
let expected = [
0xf3, 0xff, 0xc7, 0x70, 0x3f, 0x94, 0x00, 0xe5, 0x2a, 0x7d, 0xfb, 0x4b, 0x3d, 0x33,
0x05, 0xd9,
];

let mut mac = [0u8; 16];
poly1305(&key, &msg, &mut mac);
Expand All @@ -312,25 +325,35 @@ mod test {
#[test]
fn donna_self_test() {
let wrap_key = [
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
];

let wrap_msg = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
let wrap_msg = [
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff,
];

let wrap_mac = [0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
let wrap_mac = [
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
];

let mut mac = [0u8; 16];
poly1305(&wrap_key, &wrap_msg, &mut mac);
assert_eq!(&mac[..], &wrap_mac[..]);

let total_key = [
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00,
];

let total_mac =
[0x64, 0xaf, 0xe2, 0xe8, 0xd6, 0xad, 0x7b, 0xbd, 0xd2, 0x87, 0xf9, 0x7c, 0x44, 0x62, 0x3d, 0x39];
let total_mac = [
0x64, 0xaf, 0xe2, 0xe8, 0xd6, 0xad, 0x7b, 0xbd, 0xd2, 0x87, 0xf9, 0x7c, 0x44, 0x62,
0x3d, 0x39,
];

let mut tpoly = Poly1305::new(&total_key);
for i in 0..256 {
Expand All @@ -349,13 +372,19 @@ mod test {
// from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
let key = b"this is 32-byte key for Poly1305";
let msg = [0u8; 32];
let expected = [0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6, 0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc, 0x03, 0x07];
let expected = [
0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6, 0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc,
0x03, 0x07,
];
let mut mac = [0u8; 16];
poly1305(key, &msg, &mut mac);
assert_eq!(&mac[..], &expected[..]);

let msg = b"Hello world!";
let expected = [0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16, 0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2, 0xb2, 0xf0];
let expected = [
0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16, 0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2,
0xb2, 0xf0,
];
poly1305(key, msg, &mut mac);
assert_eq!(&mac[..], &expected[..]);
}
Expand Down
28 changes: 17 additions & 11 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ impl VssError {
match ErrorResponse::decode(&payload[..]) {
Ok(error_response) => VssError::from(error_response),
Err(e) => {
let message =
format!("Unable to decode ErrorResponse from server, HttpStatusCode: {}, DecodeErr: {}", status, e);
let message = format!(
"Unable to decode ErrorResponse from server, HttpStatusCode: {}, DecodeErr: {}",
status, e
);
VssError::InternalError(message)
}
},
}
}
}
Expand All @@ -49,22 +51,22 @@ impl Display for VssError {
match self {
VssError::NoSuchKeyError(message) => {
write!(f, "Requested key does not exist: {}", message)
}
},
VssError::InvalidRequestError(message) => {
write!(f, "Request sent to VSS Storage was invalid: {}", message)
}
},
VssError::ConflictError(message) => {
write!(f, "Potential version conflict in write operation: {}", message)
}
},
VssError::AuthError(message) => {
write!(f, "Authentication or Authorization failure: {}", message)
}
},
VssError::InternalServerError(message) => {
write!(f, "InternalServerError: {}", message)
}
},
VssError::InternalError(message) => {
write!(f, "InternalError: {}", message)
}
},
}
}
}
Expand All @@ -75,10 +77,14 @@ impl From<ErrorResponse> for VssError {
fn from(error_response: ErrorResponse) -> Self {
match error_response.error_code() {
ErrorCode::NoSuchKeyException => VssError::NoSuchKeyError(error_response.message),
ErrorCode::InvalidRequestException => VssError::InvalidRequestError(error_response.message),
ErrorCode::InvalidRequestException => {
VssError::InvalidRequestError(error_response.message)
},
ErrorCode::ConflictException => VssError::ConflictError(error_response.message),
ErrorCode::AuthException => VssError::AuthError(error_response.message),
ErrorCode::InternalServerException => VssError::InternalServerError(error_response.message),
ErrorCode::InternalServerException => {
VssError::InternalServerError(error_response.message)
},
_ => VssError::InternalError(format!(
"VSS responded with an unknown error code: {}, message: {}",
error_response.error_code, error_response.message
Expand Down
Loading
Loading