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!: avoid Encryptable domain collisions #6275

Merged
merged 1 commit into from
Apr 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use chrono::{NaiveDateTime, Utc};
use diesel::{prelude::*, SqliteConnection};
use tari_common_sqlite::util::diesel_ext::ExpectedRowsExtension;
use tari_common_types::encryption::{decrypt_bytes_integral_nonce, encrypt_bytes_integral_nonce};
use tari_utilities::Hidden;
use tari_utilities::{ByteArray, Hidden};

use crate::{
key_manager_service::{
Expand Down Expand Up @@ -147,9 +147,15 @@ impl KeyManagerStateSql {

impl Encryptable<XChaCha20Poly1305> for KeyManagerStateSql {
fn domain(&self, field_name: &'static str) -> Vec<u8> {
[Self::KEY_MANAGER, self.branch_seed.as_bytes(), field_name.as_bytes()]
.concat()
.to_vec()
// Because there are two variable-length inputs in the concatenation, we prepend the length of the first
[
Self::KEY_MANAGER,
(self.branch_seed.len() as u64).to_le_bytes().as_bytes(),
self.branch_seed.as_bytes(),
field_name.as_bytes(),
]
.concat()
.to_vec()
}

fn encrypt(mut self, cipher: &XChaCha20Poly1305) -> Result<Self, String> {
Expand All @@ -172,9 +178,15 @@ impl Encryptable<XChaCha20Poly1305> for KeyManagerStateSql {

impl Encryptable<XChaCha20Poly1305> for NewKeyManagerStateSql {
fn domain(&self, field_name: &'static str) -> Vec<u8> {
[Self::KEY_MANAGER, self.branch_seed.as_bytes(), field_name.as_bytes()]
.concat()
.to_vec()
// Because there are two variable-length inputs in the concatenation, we prepend the length of the first
[
Self::KEY_MANAGER,
(self.branch_seed.len() as u64).to_le_bytes().as_bytes(),
self.branch_seed.as_bytes(),
field_name.as_bytes(),
]
.concat()
.to_vec()
}

fn encrypt(mut self, cipher: &XChaCha20Poly1305) -> Result<Self, String> {
Expand Down
12 changes: 9 additions & 3 deletions base_layer/wallet/src/storage/sqlite_db/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,9 +954,15 @@ impl ClientKeyValueSql {

impl Encryptable<XChaCha20Poly1305> for ClientKeyValueSql {
fn domain(&self, field_name: &'static str) -> Vec<u8> {
[Self::CLIENT_KEY_VALUE, self.key.as_bytes(), field_name.as_bytes()]
.concat()
.to_vec()
// Because there are two variable-length inputs in the concatenation, we prepend the length of the first
[
Self::CLIENT_KEY_VALUE,
(self.key.len() as u64).to_le_bytes().as_bytes(),
self.key.as_bytes(),
field_name.as_bytes(),
]
.concat()
.to_vec()
}

#[allow(unused_assignments)]
Expand Down
Loading