Skip to content

Commit 7775e15

Browse files
committed
feat(web-server/sgx-wallet-impl): serde_bytes_array hack
This works around serde-rs/bytes#26
1 parent 70249c4 commit 7775e15

File tree

7 files changed

+40
-3
lines changed

7 files changed

+40
-3
lines changed

web-server/sgx-wallet-impl/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web-server/sgx-wallet-impl/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ thiserror = { git = "https://github.com/mesalock-linux/thiserror-sgx" }
2929

3030
# Our SGX forks
3131
algonaut = { git = "https://github.com/registreerocks/algonaut-sgx", branch = "v0.3.0-sgx" }
32+
serde_bytes = { version = "0.11.4", git = "https://github.com/registreerocks/serde-bytes-sgx" } # SGX: registreerocks fork for 0.11.4
3233

3334
[patch.'https://github.com/apache/teaclave-sgx-sdk.git']
3435
sgx_libc = { git = "https://github.com/apache/incubator-teaclave-sgx-sdk", rev = "d107bd0718f723221750a4f2973451b386cbf9d2" }

web-server/sgx-wallet-impl/src/schema/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ pub mod entities;
1111
mod macros;
1212
pub mod msgpack;
1313
pub mod sealing;
14+
pub(crate) mod serde_bytes_array;
1415
pub mod types;

web-server/sgx-wallet-impl/src/schema/sealing.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
//! [`SealedMessage`] sealing and unsealing.
22
33
use std::error::Error;
4-
use std::prelude::v1::{Box, Vec};
4+
use std::prelude::v1::Box;
55

66
use secrecy::{ExposeSecret, Secret};
77
use serde::{Deserialize, Serialize};
88
use zeroize::Zeroize;
99

1010
use crate::ported::crypto::{CryptoError, Nonce, PublicKey, SecretBytes, SodaBoxCrypto};
1111
use crate::schema::msgpack::{FromMessagePack, FromMessagePackOwned, ToMessagePack};
12+
use crate::schema::serde_bytes_array;
13+
use crate::schema::types::Bytes;
1214

1315
/// A sealed message
1416
#[derive(Debug)] // core
1517
#[derive(Deserialize, Serialize)] // serde
1618
pub struct SealedMessage {
17-
pub ciphertext: Vec<u8>,
19+
#[serde(with = "serde_bytes")]
20+
pub ciphertext: Bytes,
21+
#[serde(with = "serde_bytes_array")]
1822
pub nonce: Nonce,
23+
#[serde(with = "serde_bytes_array")]
1924
pub sender_public_key: PublicKey,
2025
}
2126

@@ -27,7 +32,7 @@ pub fn seal(
2732
) -> Result<SealedMessage, CryptoError> {
2833
let encrypted_message = sender_crypto.encrypt_message(message_bytes, &receiver_public_key)?;
2934
Ok(SealedMessage {
30-
ciphertext: encrypted_message.ciphertext.into_vec(),
35+
ciphertext: encrypted_message.ciphertext,
3136
nonce: encrypted_message.nonce,
3237
sender_public_key: sender_crypto.get_pubkey(),
3338
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! Work around
2+
3+
use core::convert::TryInto;
4+
5+
use serde::de::Error;
6+
use serde::{Deserializer, Serializer};
7+
8+
/// This just specializes [`serde_bytes::serialize`] to `<T = [u8]>`.
9+
pub(crate) fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
10+
where
11+
S: Serializer,
12+
{
13+
serde_bytes::serialize(bytes, serializer)
14+
}
15+
16+
/// This takes the result of [`serde_bytes::deserialize`] from `[u8]` to `[u8; N]`.
17+
pub(crate) fn deserialize<'de, D, const N: usize>(deserializer: D) -> Result<[u8; N], D::Error>
18+
where
19+
D: Deserializer<'de>,
20+
{
21+
let slice: &[u8] = serde_bytes::deserialize(deserializer)?;
22+
let array: [u8; N] = slice.try_into().map_err(|_| {
23+
let expected = format!("[u8; {}]", N);
24+
D::Error::invalid_length(slice.len(), &expected.as_str())
25+
})?;
26+
Ok(array)
27+
}

web-server/sgx-wallet-test/enclave/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web-server/sgx-wallet/enclave/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)