From e901dd2903f6df5780bc1438f1a8423c567140ba Mon Sep 17 00:00:00 2001 From: tabVersion Date: Mon, 22 Jan 2024 16:02:17 +0800 Subject: [PATCH] stash --- Cargo.lock | 104 ++++++++++++++++++++++++++++ src/expr/impl/Cargo.toml | 4 ++ src/expr/impl/src/scalar/encrypt.rs | 32 +++++++++ src/workspace-hack/Cargo.toml | 2 + 4 files changed, 142 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 090129a564581..4304adb50f4eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,41 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac1f845298e95f983ff1944b728ae08b8cebab80d684f0a832ed0fc74dfa27e2" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "aes-gcm" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + [[package]] name = "ahash" version = "0.7.7" @@ -2048,6 +2083,16 @@ dependencies = [ "half 1.8.2", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "clang-sys" version = "1.6.1" @@ -2708,6 +2753,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", + "rand_core", "typenum", ] @@ -2742,6 +2788,15 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + [[package]] name = "curve25519-dalek" version = "4.1.0" @@ -4577,6 +4632,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "ghash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d930750de5717d2dd0b8c0d42c076c0e884c81a73e6cab859bbd2339c71e3e40" +dependencies = [ + "opaque-debug", + "polyval", +] + [[package]] name = "gimli" version = "0.28.0" @@ -5202,6 +5267,15 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "inout" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" +dependencies = [ + "generic-array", +] + [[package]] name = "inquire" version = "0.6.2" @@ -6631,6 +6705,12 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + [[package]] name = "opendal" version = "0.44.0" @@ -7430,6 +7510,18 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "polyval" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52cff9d1d4dee5fe6d03729099f4a310a41179e0a10dbf542039873f2e826fb" +dependencies = [ + "cfg-if", + "cpufeatures", + "opaque-debug", + "universal-hash", +] + [[package]] name = "portable-atomic" version = "1.4.3" @@ -9143,6 +9235,7 @@ dependencies = [ name = "risingwave_expr_impl" version = "1.7.0-alpha" dependencies = [ + "aes-gcm", "aho-corasick", "anyhow", "arrow-schema 49.0.0", @@ -12507,6 +12600,16 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common", + "subtle", +] + [[package]] name = "unsafe-libyaml" version = "0.2.10" @@ -13644,6 +13747,7 @@ dependencies = [ "crossbeam-epoch", "crossbeam-utils", "crypto-bigint 0.5.5", + "crypto-common", "deranged", "digest", "either", diff --git a/src/expr/impl/Cargo.toml b/src/expr/impl/Cargo.toml index bee2e4eb10c04..683fcd1c5684f 100644 --- a/src/expr/impl/Cargo.toml +++ b/src/expr/impl/Cargo.toml @@ -16,6 +16,10 @@ ignored = ["workspace-hack", "ctor"] normal = ["workspace-hack", "ctor"] [dependencies] +aes-gcm = { version = "0.10.3", features = ["aes", + "alloc", + "getrandom" +] } aho-corasick = "1" anyhow = "1" arrow-schema = { workspace = true } diff --git a/src/expr/impl/src/scalar/encrypt.rs b/src/expr/impl/src/scalar/encrypt.rs index 691a35b9d9826..bb3304ddb4a6f 100644 --- a/src/expr/impl/src/scalar/encrypt.rs +++ b/src/expr/impl/src/scalar/encrypt.rs @@ -11,3 +11,35 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +use aes_gcm::{Aes128Gcm, Aes256Gcm, Key, KeyInit, OsRng}; +use risingwave_expr::{function, ExprError, Result}; + +enum CypherEnum { + Aes128(Aes128Gcm), + Aes256(Aes256Gcm), +} + +fn build_encrypt(key: &[u8], mode: &str) -> Result { + let cypher = match mode.to_lowercase().as_str() { + "aes128" => CypherEnum::Aes128(Aes128Gcm::new(Key::::from_slice(key))), + "aes256" => CypherEnum::Aes256(Aes256Gcm::new(Key::::from_slice(key))), + _ => { + return Err(ExprError::InvalidParam { + name: "mode", + reason: format!("invalid mode: {}, only accept aes128 or aes256 here", mode).into(), + }); + } + }; + + Ok(cypher) +} + +/// from pg doc https://www.postgresql.org/docs/current/pgcrypto.html#PGCRYPTO-RAW-ENC-FUNCS +#[function( + "decrypt(bytea, bytea, text) -> bytea", + prebuilt = "build_encrypt($1, $2)" +)] +pub fn decrypt(data: &[u8], cypher: CypherEnum) -> Result> { + Ok(data) +} diff --git a/src/workspace-hack/Cargo.toml b/src/workspace-hack/Cargo.toml index 8e7630d97f875..16befc5e1c1d3 100644 --- a/src/workspace-hack/Cargo.toml +++ b/src/workspace-hack/Cargo.toml @@ -42,6 +42,7 @@ combine = { version = "4", features = ["tokio"] } crossbeam-epoch = { version = "0.9" } crossbeam-utils = { version = "0.8" } crypto-bigint = { version = "0.5", features = ["generic-array", "zeroize"] } +crypto-common = { version = "0.1", default-features = false, features = ["getrandom", "std"] } deranged = { version = "0.3", default-features = false, features = ["powerfmt", "serde", "std"] } digest = { version = "0.10", features = ["mac", "oid", "std"] } either = { version = "1", features = ["serde"] } @@ -162,6 +163,7 @@ auto_enums = { version = "0.8", features = ["futures03", "tokio1"] } bitflags = { version = "2", default-features = false, features = ["serde", "std"] } bytes = { version = "1", features = ["serde"] } cc = { version = "1", default-features = false, features = ["parallel"] } +crypto-common = { version = "0.1", default-features = false, features = ["getrandom", "std"] } deranged = { version = "0.3", default-features = false, features = ["powerfmt", "serde", "std"] } digest = { version = "0.10", features = ["mac", "oid", "std"] } either = { version = "1", features = ["serde"] }