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

Fix ui issues #29

Merged
merged 6 commits into from
Oct 14, 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
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ file(GLOB_RECURSE LIB_SRC
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl_common.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper/chacha.c

# ###
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref/blake2b-ref.c
Expand All @@ -150,7 +149,6 @@ target_include_directories(app_lib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/include
${CMAKE_CURRENT_SOURCE_DIR}/app/src
${CMAKE_CURRENT_SOURCE_DIR}/app/src/lib
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper
${CMAKE_CURRENT_SOURCE_DIR}/app/src/common
${CMAKE_CURRENT_SOURCE_DIR}/app/rust/include
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref
Expand Down
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ APPVERSION_M=0
# This is the minor version
APPVERSION_N=1
# This is the patch version
APPVERSION_P=0
APPVERSION_P=1
141 changes: 141 additions & 0 deletions app/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ crate-type = ["staticlib"]
[dependencies]
jubjub = { version = "0.10.0", default-features = false }
blake2b_simd = { version = "1.0.0", default-features = false }
chacha20poly1305 = { version = "0.10.1", default-features = false }

[target.thumbv6m-none-eabi.dev-dependencies]
panic-halt = "0.2.0"
Expand Down
6 changes: 4 additions & 2 deletions app/rust/include/rslib.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ parser_error_t scalar_multiplication(const uint8_t input[32], constant_key_t key
parser_error_t randomizeKey(const uint8_t key[KEY_LENGTH], const uint8_t randomness[KEY_LENGTH], uint8_t output[KEY_LENGTH]);
parser_error_t compute_sbar(const uint8_t s[KEY_LENGTH], const uint8_t r[KEY_LENGTH], const uint8_t rsk[KEY_LENGTH],
uint8_t sbar[32]);
parser_error_t shared_secret(uint8_t secret_key[32], uint8_t other_public_key[32], const uint8_t reference_public_key[32],
uint8_t output[32]);
parser_error_t decrypt_note(const uint8_t *note, uint8_t secret_key[32], uint8_t other_public_key[32],
const uint8_t reference_public_key[32], uint8_t output[32]);
parser_error_t decrypt_note_encryption_keys(const uint8_t *ovk, const uint8_t *note, uint8_t output[ENCRYPTED_SHARED_KEY_SIZE]);

#ifdef __cplusplus
}
#endif
16 changes: 14 additions & 2 deletions app/rust/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,22 @@ pub const MEMO_SIZE: usize = 32;
pub const AMOUNT_VALUE_SIZE: usize = 8;
pub const ASSET_ID_LENGTH: usize = 32;
pub const PUBLIC_ADDRESS_SIZE: usize = 32;

pub const EPHEMEREAL_PUBLIC_KEY_SIZE: usize = 32;
pub const NOTE_COMMITMENT_SIZE: usize = 32;
pub const ENCRYPTED_NOTE_SIZE: usize =
SCALAR_SIZE + MEMO_SIZE + AMOUNT_VALUE_SIZE + ASSET_ID_LENGTH + PUBLIC_ADDRESS_SIZE;

pub const ENCRYPTED_NOTE_OFFSET: usize =
VALUE_COMMITMENT_SIZE + NOTE_COMMITMENT_SIZE + EPHEMEREAL_PUBLIC_KEY_SIZE;
pub const ENCRYPTED_SHARED_KEY_SIZE: usize = 64;

pub const NOTE_ENCRYPTION_KEY_SIZE: usize = ENCRYPTED_SHARED_KEY_SIZE + MAC_SIZE;
pub const NOTE_LEN_TO_HASH: usize =
VALUE_COMMITMENT_SIZE + NOTE_COMMITMENT_SIZE + EPHEMEREAL_PUBLIC_KEY_SIZE;
pub const VALUE_COMMITMENT_SIZE: usize = 32;
pub const MERKLE_NOTE_SIZE: usize = VALUE_COMMITMENT_SIZE
+ NOTE_COMMITMENT_SIZE
+ EPHEMEREAL_PUBLIC_KEY_SIZE
+ ENCRYPTED_NOTE_SIZE
+ MAC_SIZE
+ NOTE_ENCRYPTION_KEY_SIZE;
pub const ENCRYPTED_NOTE_SIZE_WITH_MAC: usize = ENCRYPTED_NOTE_SIZE + MAC_SIZE;
97 changes: 91 additions & 6 deletions app/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

use core::panic::PanicInfo;

use chacha20poly1305::{aead::generic_array::GenericArray, ChaCha20Poly1305, Key, KeyInit, Nonce};
use constants::{
DIFFIE_HELLMAN_PERSONALIZATION, ENCRYPTED_NOTE_SIZE, ENCRYPTED_SHARED_KEY_SIZE, MAC_SIZE,
NOTE_ENCRYPTION_KEY_SIZE, SHARED_KEY_PERSONALIZATION, SPENDING_KEY_GENERATOR,
DIFFIE_HELLMAN_PERSONALIZATION, ENCRYPTED_NOTE_OFFSET, ENCRYPTED_NOTE_SIZE,
ENCRYPTED_NOTE_SIZE_WITH_MAC, ENCRYPTED_SHARED_KEY_SIZE, EPHEMEREAL_PUBLIC_KEY_SIZE, MAC_SIZE,
MERKLE_NOTE_SIZE, NOTE_COMMITMENT_SIZE, NOTE_ENCRYPTION_KEY_SIZE, NOTE_LEN_TO_HASH,
SHARED_KEY_PERSONALIZATION, SPENDING_KEY_GENERATOR, VALUE_COMMITMENT_SIZE,
};
mod constants;

Expand Down Expand Up @@ -123,12 +126,90 @@ fn hash_shared_secret(shared_secret: &[u8; 32], reference_public_key: &AffinePoi
hash_result
}

#[inline(never)]
pub fn calculate_key_for_encryption_keys(
outgoing_view_key: &[u8; 32],
note: &[u8; NOTE_LEN_TO_HASH],
) -> Result<[u8; 32], ParserError> {
let mut key_input = [0u8; 128];

key_input[0..32].copy_from_slice(outgoing_view_key);
key_input[32..128].copy_from_slice(note);

// Store the hash state in a variable
let hash_state = Blake2b::new()
.hash_length(32)
.personal(SHARED_KEY_PERSONALIZATION)
.hash(&key_input);

// Get the hash result as bytes
let hash_result = hash_state.as_bytes();

// Attempt to convert the hash result into an array and handle the error
hash_result
.try_into()
.map_err(|_| ParserError::ParserUnexpectedError) // Return error if conversion fails
}

fn decrypt<const SIZE: usize>(
key: &[u8; 32],
ciphertext: &[u8],
plaintext: &mut [u8; SIZE],
) -> ParserError {
use chacha20poly1305::AeadInPlace;

// Check if the ciphertext length is sufficient
if ciphertext.len() < SIZE {
return ParserError::ParserUnexpectedError; // Return an error if insufficient data
}

let decryptor = ChaCha20Poly1305::new(Key::from_slice(key));

plaintext.copy_from_slice(&ciphertext[..SIZE]);

// Attempt decryption
match decryptor.decrypt_in_place_detached(
&Nonce::default(),
&[],
plaintext,
ciphertext[SIZE..].into(),
) {
Ok(_) => ParserError::ParserOk,
Err(_) => ParserError::ParserUnexpectedError, // Handle decryption failure
}
}

#[no_mangle]
pub extern "C" fn shared_secret(
pub extern "C" fn decrypt_note_encryption_keys(
ovk: &[u8; 32],
note: &[u8; MERKLE_NOTE_SIZE],
output: &mut [u8; ENCRYPTED_SHARED_KEY_SIZE],
) -> ParserError {
let key_result =
calculate_key_for_encryption_keys(ovk, &note[..NOTE_LEN_TO_HASH].try_into().unwrap());

// Handle the result of calculate_key_for_encryption_keys
let key = match key_result {
Ok(k) => k,
Err(_) => return ParserError::ParserUnexpectedError, // Return error if key calculation fails
};

let ciphertext: &[u8; NOTE_ENCRYPTION_KEY_SIZE] = note
[MERKLE_NOTE_SIZE - NOTE_ENCRYPTION_KEY_SIZE..]
.try_into()
.unwrap();
decrypt::<ENCRYPTED_SHARED_KEY_SIZE>(&key, ciphertext, output);

ParserError::ParserOk
}

#[no_mangle]
pub extern "C" fn decrypt_note(
note: &[u8; MERKLE_NOTE_SIZE],
secret_key: &[u8; 32],
other_public_key: &[u8; 32],
reference_public_key: &[u8; 32],
output: &mut [u8; 32],
output: &mut [u8; ENCRYPTED_NOTE_SIZE],
) -> ParserError {
let secret_key = Fr::from_bytes(secret_key);
if secret_key.is_none().into() {
Expand All @@ -145,9 +226,13 @@ pub extern "C" fn shared_secret(

let shared_secret = other_public_key.unwrap() * secret_key.unwrap();
let affine = AffinePoint::from(&shared_secret).to_bytes();
*output = hash_shared_secret(&affine, &reference_public_key.unwrap());
let hash = hash_shared_secret(&affine, &reference_public_key.unwrap());

ParserError::ParserOk
let ciphertext: &[u8; ENCRYPTED_NOTE_SIZE_WITH_MAC] = note
[ENCRYPTED_NOTE_OFFSET..ENCRYPTED_NOTE_OFFSET + ENCRYPTED_NOTE_SIZE_WITH_MAC]
.try_into()
.unwrap();
decrypt::<ENCRYPTED_NOTE_SIZE>(&hash, ciphertext, output)
}

#[cfg(not(test))]
Expand Down
Loading
Loading