Skip to content

Commit ee6c652

Browse files
fix: update extension to 0.2.2
1 parent 9c1859e commit ee6c652

File tree

4 files changed

+58
-19
lines changed

4 files changed

+58
-19
lines changed

deps/extension/Cargo.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deps/extension/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[package]
77
name = "signal-sqlcipher-extension"
8-
version = "0.2.1"
8+
version = "0.2.2"
99
edition = "2021"
1010
license = "AGPL-3.0-only"
1111

@@ -23,6 +23,7 @@ cbc = "0.1.2"
2323
hmac = "0.12.1"
2424
pbkdf2 = "0.12.2"
2525
rand_core = { version = "0.6.4", "default-features" = false, features = ["getrandom"] }
26+
sha1 = { version = "0.10.6", "default-features" = false }
2627
sha2 = { version = "0.10.8", "default-features" = false }
2728
signal-tokenizer = { git = "https://github.com/signalapp/Signal-FTS5-Extension" }
2829

deps/extension/src/lib.rs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use core::ffi::{c_char, c_int, c_uchar, c_void};
1010
use hmac::{Hmac, Mac};
1111
use pbkdf2::pbkdf2_hmac;
1212
use rand_core::{OsRng, RngCore};
13+
use sha1::Sha1;
1314
use sha2::Sha512;
1415

1516
pub use signal_tokenizer;
@@ -70,6 +71,7 @@ extern "C" fn random(_ctx: *mut c_void, buf: *mut c_void, length: c_int) -> c_in
7071
extern "C" fn get_hmac_sz(_ctx: *mut c_void, algorithm: c_int) -> c_int {
7172
match algorithm {
7273
SQLCIPHER_HMAC_SHA512 => 64,
74+
SQLCIPHER_HMAC_SHA1 => 20,
7375
_ => 0,
7476
}
7577
}
@@ -85,9 +87,6 @@ extern "C" fn hmac(
8587
in2_sz: c_int,
8688
out: *mut c_uchar,
8789
) -> c_int {
88-
if algorithm != SQLCIPHER_HMAC_SHA512 {
89-
return SQLITE_ERROR;
90-
}
9190
if hmac_key.is_null() || in1.is_null() || out.is_null() {
9291
return SQLITE_ERROR;
9392
}
@@ -99,16 +98,34 @@ extern "C" fn hmac(
9998
Some(unsafe { core::slice::from_raw_parts(in2 as *mut c_uchar, in2_sz as usize) })
10099
};
101100

102-
let Ok(mut mac) = Hmac::<Sha512>::new_from_slice(key) else {
103-
return SQLITE_ERROR;
104-
};
105-
mac.update(in1);
106-
if let Some(in2) = in2 {
107-
mac.update(in2);
108-
}
109-
let digest = mac.finalize().into_bytes();
110-
unsafe {
111-
out.copy_from(digest.as_ptr(), digest.len());
101+
match algorithm {
102+
SQLCIPHER_HMAC_SHA512 => {
103+
let Ok(mut mac) = Hmac::<Sha512>::new_from_slice(key) else {
104+
return SQLITE_ERROR;
105+
};
106+
mac.update(in1);
107+
if let Some(in2) = in2 {
108+
mac.update(in2);
109+
}
110+
let digest = mac.finalize().into_bytes();
111+
unsafe {
112+
out.copy_from(digest.as_ptr(), digest.len());
113+
};
114+
}
115+
SQLCIPHER_HMAC_SHA1 => {
116+
let Ok(mut mac) = Hmac::<Sha1>::new_from_slice(key) else {
117+
return SQLITE_ERROR;
118+
};
119+
mac.update(in1);
120+
if let Some(in2) = in2 {
121+
mac.update(in2);
122+
}
123+
let digest = mac.finalize().into_bytes();
124+
unsafe {
125+
out.copy_from(digest.as_ptr(), digest.len());
126+
};
127+
}
128+
_ => return SQLITE_ERROR,
112129
};
113130
SQLITE_OK
114131
}
@@ -124,16 +141,21 @@ extern "C" fn pbkdf(
124141
key_sz: c_int,
125142
key: *mut c_uchar,
126143
) -> c_int {
127-
if algorithm != SQLCIPHER_PBKDF2_HMAC_SHA512 {
128-
return SQLITE_ERROR;
129-
}
130144
if pass.is_null() || salt.is_null() || key.is_null() {
131145
return SQLITE_ERROR;
132146
}
133147
let password = unsafe { core::slice::from_raw_parts(pass as *const c_uchar, pass_sz as usize) };
134148
let salt = unsafe { core::slice::from_raw_parts(salt as *const c_uchar, salt_sz as usize) };
135149
let buf = unsafe { core::slice::from_raw_parts_mut(key as *mut c_uchar, key_sz as usize) };
136-
pbkdf2_hmac::<Sha512>(password, salt, workfactor as u32, buf);
150+
match algorithm {
151+
SQLCIPHER_PBKDF2_HMAC_SHA512 => {
152+
pbkdf2_hmac::<Sha512>(password, salt, workfactor as u32, buf);
153+
}
154+
SQLCIPHER_PBKDF2_HMAC_SHA1 => {
155+
pbkdf2_hmac::<Sha1>(password, salt, workfactor as u32, buf);
156+
}
157+
_ => return SQLITE_ERROR,
158+
};
137159
SQLITE_OK
138160
}
139161

deps/extension/src/sqlcipher.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use core::ffi::{c_char, c_int, c_uchar, c_void};
1010
pub const SQLCIPHER_HMAC_SHA512: c_int = 2;
1111
pub const SQLCIPHER_PBKDF2_HMAC_SHA512: c_int = 2;
1212

13+
// Legacy encryption primitives
14+
pub const SQLCIPHER_HMAC_SHA1: c_int = 0;
15+
pub const SQLCIPHER_PBKDF2_HMAC_SHA1: c_int = 0;
16+
1317
pub const CIPHER_ENCRYPT: c_int = 1;
1418

1519
#[repr(C)]

0 commit comments

Comments
 (0)