From f64af0a248696b7ebe39590b787b3151621abfae Mon Sep 17 00:00:00 2001 From: Kirill Shakirov <38155247+Nyanraltotlapun@users.noreply.github.com> Date: Sat, 14 Mar 2026 14:13:18 +0100 Subject: [PATCH] Romove big files. Wrapping things up. --- .gitignore | 4 + nyash_client/Cargo.lock | 1084 ++++++++++++++++- nyash_client/Cargo.toml | 20 +- nyash_client/build.rs | 55 + nyash_client/nyash_conf.json | 12 + nyash_client/proto/nyash.proto | 80 ++ nyash_client/src/client.rs | 344 ++++++ nyash_client/src/client_config.rs | 130 +- nyash_client/src/num_utils.rs | 571 +++++---- nyash_client/src/ocl_utils.rs | 320 +++++ nyash_client/src/{main.rs => old_main.rs} | 25 - nyash_client/src/open_cl/build_spirv.sh | 8 + .../src/open_cl/nyash_aes_xts256_plain.cl | 68 +- nyash_client/src/search_params.rs | 38 + nyash_client/src/test_cl.rs | 79 ++ .../__pycache__/utils.cpython-314.pyc | Bin 0 -> 2178 bytes nyash_client/src/test_data/create_test.sh | 16 + .../src/test_data/extract_encrypted_block.py | 44 + nyash_client/src/test_data/utils.py | 25 + nyash_client/src/test_nums.rs | 3 - nyash_server/Cargo.lock | 48 - nyash_server/Cargo.toml | 2 +- nyash_server/src/database.rs | 69 +- nyash_server/src/num_utils.rs | 256 ++-- .../src/{launch.json => old_launch.json} | 0 nyash_server/src/{main.rs => old_main.rs} | 0 nyash_server/src/server.rs | 72 +- 27 files changed, 2840 insertions(+), 533 deletions(-) create mode 100644 nyash_client/build.rs create mode 100644 nyash_client/nyash_conf.json create mode 100644 nyash_client/proto/nyash.proto create mode 100644 nyash_client/src/client.rs create mode 100644 nyash_client/src/ocl_utils.rs rename nyash_client/src/{main.rs => old_main.rs} (92%) create mode 100755 nyash_client/src/open_cl/build_spirv.sh create mode 100644 nyash_client/src/search_params.rs create mode 100644 nyash_client/src/test_cl.rs create mode 100644 nyash_client/src/test_data/__pycache__/utils.cpython-314.pyc create mode 100644 nyash_client/src/test_data/create_test.sh create mode 100644 nyash_client/src/test_data/extract_encrypted_block.py create mode 100644 nyash_client/src/test_data/utils.py rename nyash_server/src/{launch.json => old_launch.json} (100%) rename nyash_server/src/{main.rs => old_main.rs} (100%) diff --git a/.gitignore b/.gitignore index ad67955..746635e 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,7 @@ target # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +# ignore test data +*.img +*.key diff --git a/nyash_client/Cargo.lock b/nyash_client/Cargo.lock index 2a90c69..ae24894 100644 --- a/nyash_client/Cargo.lock +++ b/nyash_client/Cargo.lock @@ -2,18 +2,123 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + +[[package]] +name = "async-trait" +version = "0.1.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + [[package]] name = "autocfg" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +[[package]] +name = "axum" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b52af3cb4058c895d37317bb27508dccc8e5f2d39454016b297bf4a400597b8" +dependencies = [ + "axum-core", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "serde_core", + "sync_wrapper", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "sync_wrapper", + "tower-layer", + "tower-service", +] + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af" + +[[package]] +name = "bytes" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + [[package]] name = "cl-sys" version = "0.4.3" @@ -23,6 +128,15 @@ dependencies = [ "libc", ] +[[package]] +name = "crc32fast" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" +dependencies = [ + "cfg-if", +] + [[package]] name = "crossbeam" version = "0.8.4" @@ -79,6 +193,12 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + [[package]] name = "enum_primitive" version = "0.1.1" @@ -88,30 +208,363 @@ dependencies = [ "num-traits 0.1.43", ] +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "fixedbitset" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" + +[[package]] +name = "flate2" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "futures" version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678" +[[package]] +name = "futures-channel" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-sink" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c39754e157331b013978ec91992bde1ac089843443c49cbc7f46150b0fad0893" + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", + "wasip3", +] + +[[package]] +name = "h2" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f44da3a8150a6703ed5d34e164b875fd14c2cdab9af1252a9a1020bde2bdc54" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "http" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab2d4f250c3d7b1c9fcdff1cece94ea4e2dfbec68614f7b87cb205f24ca9d11" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "pin-utils", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-timeout" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0" +dependencies = [ + "hyper", + "hyper-util", + "pin-project-lite", + "tokio", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "libc", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + +[[package]] +name = "indexmap" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" +dependencies = [ + "equivalent", + "hashbrown 0.16.1", + "serde", + "serde_core", +] + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + [[package]] name = "libc" version = "0.2.178" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091" +[[package]] +name = "linux-raw-sys" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + [[package]] name = "memchr" version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "mio" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "multimap" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" + [[package]] name = "nodrop" version = "0.1.14" @@ -149,9 +602,16 @@ dependencies = [ name = "nyash_client" version = "0.1.0" dependencies = [ + "flate2", "ocl", + "ocl-include", + "prost", "serde", "serde_json", + "tokio", + "tonic", + "tonic-prost", + "tonic-prost-build", ] [[package]] @@ -174,7 +634,7 @@ version = "0.11.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c145dd9f205b86611a5df15eb89517417b03005441cf6cec245c65a4b9248c52" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cl-sys", "enum_primitive", "num-complex", @@ -193,6 +653,81 @@ dependencies = [ "num-traits 0.2.19", ] +[[package]] +name = "ocl-include" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca5e3e4ddb71f9c8b8fecc714863290951ebbb5a55a6bc1953e36d22b1977d7f" +dependencies = [ + "lazy_static", + "regex", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "petgraph" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8701b58ea97060d5e5b155d383a69952a60943f0e6dfe30b04c287beb0b27455" +dependencies = [ + "fixedbitset", + "hashbrown 0.15.5", + "indexmap", +] + +[[package]] +name = "pin-project" +version = "1.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1749c7ed4bcaf4c3d0a3efc28538844fb29bcdd7d2b67b2be7e20ba861ff517" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b20ed30f105399776b9c883e68e536ef602a16ae6f596d2c473591d6ad64c6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn", +] + [[package]] name = "proc-macro2" version = "1.0.103" @@ -202,6 +737,79 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "prost" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ea70524a2f82d518bce41317d0fae74151505651af45faf1ffbd6fd33f0568" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-build" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "343d3bd7056eda839b03204e68deff7d1b13aba7af2b2fd16890697274262ee7" +dependencies = [ + "heck", + "itertools", + "log", + "multimap", + "petgraph", + "prettyplease", + "prost", + "prost-types", + "pulldown-cmark", + "pulldown-cmark-to-cmark", + "regex", + "syn", + "tempfile", +] + +[[package]] +name = "prost-derive" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27c6023962132f4b30eb4c172c91ce92d933da334c59c23cddee82358ddafb0b" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "prost-types" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8991c4cbdb8bc5b11f0b074ffe286c30e523de90fee5ba8132f1399f23cb3dd7" +dependencies = [ + "prost", +] + +[[package]] +name = "pulldown-cmark" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83c41efbf8f90ac44de7f3a868f0867851d261b56291732d0cbf7cceaaeb55a6" +dependencies = [ + "bitflags 2.11.0", + "memchr", + "unicase", +] + +[[package]] +name = "pulldown-cmark-to-cmark" +version = "22.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50793def1b900256624a709439404384204a5dc3a6ec580281bfaac35e882e90" +dependencies = [ + "pulldown-cmark", +] + [[package]] name = "quote" version = "1.0.42" @@ -221,6 +829,41 @@ dependencies = [ "futures", ] +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "regex" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" + [[package]] name = "rustc_version" version = "0.4.1" @@ -230,6 +873,19 @@ dependencies = [ "semver", ] +[[package]] +name = "rustix" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34" +dependencies = [ + "bitflags 2.11.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "semver" version = "1.0.27" @@ -279,6 +935,44 @@ dependencies = [ "zmij", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "simd-adler32" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "socket2" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "syn" version = "2.0.111" @@ -290,6 +984,25 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" + +[[package]] +name = "tempfile" +version = "3.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0136791f7c95b1f6dd99f9cc786b91bb81c3800b639b3478e561ddb7be95e5f1" +dependencies = [ + "fastrand", + "getrandom", + "once_cell", + "rustix", + "windows-sys", +] + [[package]] name = "thiserror" version = "1.0.69" @@ -310,12 +1023,381 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio" +version = "1.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27ad5e34374e03cfffefc301becb44e9dc3c17584f414349ebe29ed26661822d" +dependencies = [ + "bytes", + "libc", + "mio", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c55a2eff8b69ce66c84f85e1da1c233edc36ceb85a2058d11b0d6a3c7e7569c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-stream" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32da49809aab5c3bc678af03902d4ccddea2a87d028d86392a4b1560c6906c70" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ae9cec805b01e8fc3fd2fe289f89149a9b66dd16786abd8b19cfa7b48cb0098" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tonic" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fec7c61a0695dc1887c1b53952990f3ad2e3a31453e1f49f10e75424943a93ec" +dependencies = [ + "async-trait", + "axum", + "base64", + "bytes", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-timeout", + "hyper-util", + "percent-encoding", + "pin-project", + "socket2", + "sync_wrapper", + "tokio", + "tokio-stream", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tonic-build" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1882ac3bf5ef12877d7ed57aad87e75154c11931c2ba7e6cde5e22d63522c734" +dependencies = [ + "prettyplease", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tonic-prost" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a55376a0bbaa4975a3f10d009ad763d8f4108f067c7c2e74f3001fb49778d309" +dependencies = [ + "bytes", + "prost", + "tonic", +] + +[[package]] +name = "tonic-prost-build" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3144df636917574672e93d0f56d7edec49f90305749c668df5101751bb8f95a" +dependencies = [ + "prettyplease", + "proc-macro2", + "prost-build", + "prost-types", + "quote", + "syn", + "tempfile", + "tonic-build", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "indexmap", + "pin-project-lite", + "slab", + "sync_wrapper", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "unicase" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142" + [[package]] name = "unicode-ident" version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.2+wasi-0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9517f9239f02c069db75e65f174b3da828fe5f5b945c4dd26bd25d89c03ebcf5" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags 2.11.0", + "hashbrown 0.15.5", + "indexmap", + "semver", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "wit-bindgen" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +dependencies = [ + "wit-bindgen-rust-macro", +] + +[[package]] +name = "wit-bindgen-core" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" +dependencies = [ + "anyhow", + "heck", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn", + "wit-bindgen-core", + "wit-bindgen-rust", +] + +[[package]] +name = "wit-component" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" +dependencies = [ + "anyhow", + "bitflags 2.11.0", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser", +] + [[package]] name = "zmij" version = "1.0.13" diff --git a/nyash_client/Cargo.toml b/nyash_client/Cargo.toml index 31c320a..120cbb5 100644 --- a/nyash_client/Cargo.toml +++ b/nyash_client/Cargo.toml @@ -2,8 +2,26 @@ name = "nyash_client" version = "0.1.0" edition = "2024" +build = "build.rs" + +[[bin]] # Bin to run the HelloWorld gRPC client +name = "nyash-client" +path = "src/client.rs" [dependencies] -ocl = "0.19" +flate2 = "1.1.9" +ocl = { version = "0.19" } +prost = "0.14.3" serde = { version = "1.0.228", features = ["derive"] } serde_json = "1.0.149" +tokio = { version = "1.50.0", features = ["rt", "macros", "signal"] } +tonic = "0.14.5" +tonic-prost = "0.14.5" + +[build-dependencies] +tonic-prost-build = "0.14.5" +ocl-include = "0.6" +flate2 = "1.1.9" + + + diff --git a/nyash_client/build.rs b/nyash_client/build.rs new file mode 100644 index 0000000..f6f1c84 --- /dev/null +++ b/nyash_client/build.rs @@ -0,0 +1,55 @@ +use std::env; +use std::io::Write; +use std::path::Path; +use ocl_include; +use flate2::write::GzEncoder; +use flate2::Compression; + +fn main() -> Result<(), Box> { + //compile gRPC + tonic_prost_build::compile_protos("proto/nyash.proto")?; + + + //******************* compile ocl into spirv64 ********* + // let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + // let ocl_src_dir = Path::new(&manifest_dir).join("src/open_cl"); + // let spirv_build_script = ocl_src_dir.clone().join("build_spirv.sh"); + + + // let out_dir = env::var("OUT_DIR").unwrap(); + // let spirv_bin_path = Path::new(&out_dir).join("nyash_aes_xts256_plain.spv"); + // let str_spirv_bit_p = spirv_bin_path.to_str().expect("Error converting spirv out path to str!"); + // let _output = Command::new(spirv_build_script) + // .current_dir(ocl_src_dir) + // .arg(str_spirv_bit_p) + // .output() + // .expect("Failed to execute spirv build script!"); + + // ***************** concat ocl sources to one file and zip it ********************** + let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let ocl_src_dir = Path::new(&manifest_dir).join("src/open_cl"); + let ocl_src_file = ocl_src_dir.clone().join("nyash_aes_xts256_plain.cl"); + + let ocl_parser = ocl_include::Parser::builder() + .add_source( + ocl_include::source::Fs::builder() + .include_dir(ocl_src_dir) + .expect("Error adding ocl include dir!") + .build(), + ) + .build(); + println!("Concatenating and compressing ocl sources..."); + let ocl_node = ocl_parser.parse(&ocl_src_file).expect("Error parsing ocl source!"); + let full_src = ocl_node.collect().0; + let mut gz_encoder = GzEncoder::new(Vec::new(), Compression::best()); + + gz_encoder.write_all(full_src.as_bytes()).expect("Error compressing src file!"); + + let compressed_bytes = gz_encoder.finish().expect("Error compressing src!"); + + let out_dir = env::var("OUT_DIR").unwrap(); + let ocl_concat_src_path = Path::new(&out_dir).join("nyash_aes_full.cl.gz"); + std::fs::write(ocl_concat_src_path, compressed_bytes).expect("Error writing compressed src file!"); + + Ok(()) +} \ No newline at end of file diff --git a/nyash_client/nyash_conf.json b/nyash_client/nyash_conf.json new file mode 100644 index 0000000..5c6ba84 --- /dev/null +++ b/nyash_client/nyash_conf.json @@ -0,0 +1,12 @@ +{ + "devices": [ + { + "dev_name": "gfx1103", + "platform_name": "AMD Accelerated Parallel Processing", + "id": 0, + "work_size": 4096, + "batch_size": 5412812 + } + ], + "dev_fill": 100 +} \ No newline at end of file diff --git a/nyash_client/proto/nyash.proto b/nyash_client/proto/nyash.proto new file mode 100644 index 0000000..2772e84 --- /dev/null +++ b/nyash_client/proto/nyash.proto @@ -0,0 +1,80 @@ +syntax = "proto3"; +package nyash_proto; + +// serving key brutforcing for aes xts +service NyashLuks { + // Requesting work, gettin in response work data, error, or message that there is no work currently awaylable. + rpc RequestWork (WorkRequest) returns (WorkReply); + // Commiting work, with work ID. If key was found, sending also discowered key + rpc CommitWork (WorkCommit) returns (CommitReply); + // Request owerall key search progress + rpc RequestProgress (ProgressRequest) returns (ProgressReply); +} + +message WorkRequest { + // Request work from server + // Preffered work size in number of keys + uint64 pref_work_size = 1; +} + +message WorkReply { + oneof result { + WorkData work_data = 1; + bool no_work = 2; + string error = 3; + } +} + +message WorkData { + // work id + uint64 work_id =1; + // work size - number of keys to check + uint64 work_size= 2; + // tweak key is 128 bit. We send it as two uint64 values + uint64 tweak_key0 =3; + uint64 tweak_key1 =4; + // start key is 128 bit. We send it as two uint64 values + uint64 start_key0 =5; + uint64 start_key1 =6; +} + +message WorkCommit { + // work id + uint64 work_id =1; + oneof result { + bool no_key = 2; + KeyData found_key = 3; + } + +} + +message KeyData { + // tweak key is 128 bit. We send it as two uint64 values + uint64 tweak_key0 =3; + uint64 tweak_key1 =4; + // start key is 128 bit. We send it as two uint64 values + uint64 start_key0 =5; + uint64 start_key1 =6; +} + + +message CommitReply { + // Status code + // 0 - means ok + // 1 - something went wrong on server + // 2 - something wrong with request + uint32 status_code = 1; + // Brutforce progress as ratio from 0 to 1(completed) + double progress = 2; +} + +// request total progress +message ProgressRequest { + +} + +message ProgressReply { + // Brutforce progress as ratio from 0 to 1(completed) + bool key_found = 1; + double progress = 2; +} \ No newline at end of file diff --git a/nyash_client/src/client.rs b/nyash_client/src/client.rs new file mode 100644 index 0000000..8b06ca8 --- /dev/null +++ b/nyash_client/src/client.rs @@ -0,0 +1,344 @@ +use std::time::Duration; + +use tokio; +use tonic; +use tonic::transport::Channel; +//use tonic::{Request, Response, Status}; +pub mod nyash_proto { + tonic::include_proto!("nyash_proto"); // The string specified here must match the proto package name +} + +use nyash_proto::nyash_luks_client::NyashLuksClient; +use nyash_proto::{ + CommitReply, KeyData, ProgressReply, ProgressRequest, WorkCommit, WorkReply, + WorkRequest, work_commit, work_reply, +}; + +use std::sync::{Arc}; +use tokio::sync::RwLock as AsyncRwLock; + +use crate::ocl_utils::ExecData; + +pub mod client_config; +pub mod num_utils; +pub mod ocl_utils; +mod search_params; +//mod test_cl; + +const S_ADDR: &str = "http://127.0.0.1:37939"; +// const SRC_PATH: &str = "src/open_cl/nyash_aes_xts256_plain.cl"; +// const OCL_COMP_OPT: &str = "-I src/open_cl"; +const CONF_RILE_NAME: &str = "nyash_conf.json"; + + +async fn shutdown_signal() { + let ctrl_c = async { + tokio::signal::ctrl_c() + .await + .expect("Failed to install Ctrl+C handler"); + }; + + #[cfg(unix)] + let terminate = async { + tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) + .expect("Failed to install SIGTERM handler") + .recv() + .await; + }; + + #[cfg(not(unix))] + let terminate = std::future::pending::<()>(); + + tokio::select! { + _ = ctrl_c => println!("Received Ctrl+C, initiating shutdown"), + _ = terminate => println!("Received SIGTERM, initiating shutdown"), + } +} + +fn key_dat_from_exec_dat(ex_dat: &ExecData) -> KeyData { + let t_k = num_utils::u128_to_u64arr(num_utils::u32arr_to_u128(num_utils::vec_to_u32_4arr( + &ex_dat.tweak_key, + 0, + ))); + let e_k = num_utils::u128_to_u64arr(ex_dat.get_found_key()); + + KeyData { + start_key0: e_k[0], + start_key1: e_k[1], + tweak_key0: t_k[0], + tweak_key1: t_k[1], + } +} + +async fn get_progress(chanel: Channel) -> Result { + let mut client = NyashLuksClient::new(chanel); + + let request = tonic::Request::new(ProgressRequest {}); + let response = client.request_progress(request).await?; + return Ok(response.into_inner()); +} + +async fn get_work(chanel: Channel, work_size: u64) -> Result { + let mut client = NyashLuksClient::new(chanel); + + println!("Requesting work {} keys...", work_size); + let request = tonic::Request::new(WorkRequest { + pref_work_size: work_size, + }); + let response = client.request_work(request).await?; + return Ok(response.into_inner()); +} + +async fn commit_work( + chanel: Channel, + commit_data: WorkCommit, +) -> Result { + let mut client = NyashLuksClient::new(chanel); + + let request = tonic::Request::new(commit_data); + let response = client.commit_work(request).await?; + return Ok(response.into_inner()); +} + +fn benchmark(exec_context: &mut ocl_utils::ExecContext) -> (u64, usize) { + let mut nyan_exec_dat = ocl_utils::ExecData { + start_key: vec![1u32; 4], + tweak_key: vec![2u32; 4], + uenc_data: vec![0u32; 4], + target_data: vec![0u32; 4], + tweak_i: 0, + tweak_j: 0, + key_found: vec![0u32; 5], + batch_size: 10000000, + work_size: 128, + }; + + let total_work: u64 = 1280000000; + let work_sizes: [usize; 8] = [128, 256, 512, 1024, 2048, 4096, 8192, 16384]; + let mut work_time = [0f64; 8]; + + ocl_utils::set_target_data(exec_context, &mut nyan_exec_dat).expect("Error set target data!"); + + let mut preffered_work_size: usize = work_sizes[0]; + let mut preffered_batch_size: u64 = 0; + for i in 0..8 { + let test_work_s = work_sizes[i]; + let batch_size: u64 = total_work / test_work_s as u64; + nyan_exec_dat.work_size = test_work_s; + nyan_exec_dat.batch_size = batch_size; + println!("Benchmarking work size {}", test_work_s); + for _j in 0..3 { + let (_, exec_time) = + ocl_utils::do_work(exec_context, &mut nyan_exec_dat).expect("Error running tests!"); + work_time[i] += exec_time; + } + work_time[i] = work_time[i] / 3.0; + println!("Average time {}", work_time[i]); + if i > 0 { + //giving 5% error for speed mesure + if (work_time[i]*1.05) > work_time[i - 1] { + break; + } + } + preffered_work_size = work_sizes[i]; + // calculate batch size so it correspond to 30 sec job + preffered_batch_size = (batch_size as f64 * (20.0 / work_time[i])) as u64; + println!("batch_size {}, work_time {}, preffered_batch_size {}, preffered_work_size {}", + batch_size, + work_time[i], + preffered_batch_size, + preffered_work_size); + } + + return (preffered_batch_size, preffered_work_size); +} + +#[tokio::main(flavor = "current_thread")] +async fn main() -> Result<(), Box> { + const OCL_NYAS_GZ_SRC: &[u8] = + include_bytes!(concat!(env!("OUT_DIR"), "/nyash_aes_full.cl.gz")); + + println!("OCL bin src lenght {}", OCL_NYAS_GZ_SRC.len()); + println!("Hello, world nya!"); + //use ocl::{Buffer, Context, Device, Kernel, Platform, Program, Queue, flags}; + let (devices, mut app_conf) = + client_config::get_devices_conf(CONF_RILE_NAME).expect("Error loading config!"); + + let nyash_dev = devices[0].0; + let nyash_plt = devices[0].1; +// + println!( + "Platform: {:?}, Device: {:?}", + nyash_plt.name().unwrap(), + nyash_dev.name().unwrap() + ); + + // reading ocl program sources + //let prog_src = std::fs::read_to_string(SRC_PATH).expect("Error reading program sources!"); + + let mut exec_context: ocl_utils::ExecContext = ocl_utils::ExecContext::new( + nyash_dev, + nyash_plt, + OCL_NYAS_GZ_SRC, + 1024 + ) + .expect("Error creating exec context!"); + + // need to train in order to learn optimal params + + // need to train in order to learn optimal params + if (app_conf.devices[0].batch_size == 0) || (app_conf.devices[0].work_size == 0) { + println!("Performing banchmark to determine optimal GPU parameters..."); + let (batch_size, work_size) = benchmark(&mut exec_context); + println!("batch_size {}, work_size {}", batch_size, work_size); + app_conf.devices[0].batch_size = batch_size; + app_conf.devices[0].work_size = work_size; + client_config::save_config(CONF_RILE_NAME, &app_conf).expect("Error saving config!"); + } + + let nyash_dev_cfg = &app_conf.devices[0]; + println!( + "Preffered Work size: {}, Batch size {}", + nyash_dev_cfg.work_size, nyash_dev_cfg.batch_size + ); + + let (_, _, encrypted_data) = search_params::get_params(); + //setting data + let mut nyan_exec_dat = ocl_utils::ExecData { + start_key: Vec::new(), + tweak_key: Vec::new(), + uenc_data: vec![0u32; 4], + target_data: encrypted_data.to_vec(), + tweak_i: 0, + tweak_j: 0, + key_found: vec![0u32; 5], + batch_size: nyash_dev_cfg.batch_size, + work_size: nyash_dev_cfg.work_size, + }; + println!( + "nyan_exec_dat Work size: {}, Batch size {}", + nyan_exec_dat.work_size, nyan_exec_dat.batch_size + ); + + + ocl_utils::set_target_data(&mut exec_context, &mut nyan_exec_dat) + .expect("Error setting target data!"); + + // Don't keep connection alive when idle + let nya_channel: tonic::transport::Channel = tonic::transport::Endpoint::from_static(S_ADDR) + .keep_alive_while_idle(false) + .keep_alive_timeout(Duration::from_secs(10)) + .connect_timeout(Duration::from_secs(10)) + .connect() + .await + .expect("Error connecting to server!"); + + let key_found = match get_progress(nya_channel.clone()).await { + Err(_) => { + println!("Error getting progress!"); + false + } + Ok(p_r) => { + println!("Current progress {:.8}%", p_r.progress * 100.0); + p_r.key_found + } + }; + + let mut giga_keys_per_second: f64 = 0f64; + let req_work_size: u64 = nyash_dev_cfg.batch_size as u64 * nyash_dev_cfg.work_size as u64; + + + let shared_key_found = Arc::new(AsyncRwLock::new(key_found)); + // handling program termination + let sh_k_f_clone = shared_key_found.clone(); + tokio::spawn(async move { + shutdown_signal().await; + //signaling that we should stop + let mut guard = sh_k_f_clone.write().await; + *guard = true; + }); + + while *shared_key_found.read().await == false { + let mut work = get_work(nya_channel.clone(), req_work_size).await; + while work.is_err() { + println!("Error getting work, waiting 10 seconds and trying again..."); + tokio::time::sleep(Duration::from_secs(10)).await; + work = get_work(nya_channel.clone(), req_work_size).await; + } + let work = work.expect("Error getting work!"); + + let work_data = match work.result.expect("Error! Expected WorkResult!") { + work_reply::Result::NoWork(_) => { + println!("No work right now, try again later..."); + continue; + } + work_reply::Result::Error(ex) => { + println!("Erro getting work: {}", ex); + continue; + } + work_reply::Result::WorkData(wd) => wd, + }; + println!("Got work, {} keys...", work_data.work_size); + nyan_exec_dat.start_key = num_utils::u128_to_u32arr(num_utils::u64arr_to_u128([ + work_data.start_key0, + work_data.start_key1, + ])) + .to_vec(); + nyan_exec_dat.tweak_key = num_utils::u128_to_u32arr(num_utils::u64arr_to_u128([ + work_data.tweak_key0, + work_data.tweak_key1, + ])) + .to_vec(); + + let mut batch_size = work_data.work_size / nyan_exec_dat.work_size as u64; + if (work_data.work_size % nyan_exec_dat.work_size as u64) != 0 { + batch_size += 1; + } + println!("Setting batch size to {}", batch_size); + nyan_exec_dat.batch_size = batch_size; + + println!("Crunching numbers..."); + match ocl_utils::do_work(&mut exec_context, &mut nyan_exec_dat) { + Err(_) => println!("Error doing work!"), + Ok((k_f, work_time)) => { + let mut w_k = WorkCommit { + work_id: work_data.work_id, + result: Some(work_commit::Result::NoKey(true)), + }; + + let g_k_p_s = (work_data.work_size as f64 / work_time) / 1000000000.0; + if giga_keys_per_second != 0f64 { + giga_keys_per_second -= giga_keys_per_second / 10.0; + giga_keys_per_second += g_k_p_s / 10.0; + } else { + giga_keys_per_second = g_k_p_s; + } + println!("Average speed: {:.3}GigaKeys/Sec", giga_keys_per_second); + if k_f == true { + println!( + "We found the key! {:?} {:?}", + nyan_exec_dat.key_found, nyan_exec_dat.tweak_key + ); + + //signaling that key found + let mut guard = shared_key_found.write().await; + *guard = true; + + w_k.result = Some(work_commit::Result::FoundKey(key_dat_from_exec_dat( + &nyan_exec_dat, + ))); + } + + let resp = commit_work(nya_channel.clone(), w_k).await; + match resp { + Ok(c_r) => println!("Work commited. Progress: {:.8}%", c_r.progress * 100.0), + Err(_) => println!("Error commiting work..."), + }; + } + }; + } + + println!("Exiting!"); + + Ok(()) +} diff --git a/nyash_client/src/client_config.rs b/nyash_client/src/client_config.rs index 43a3720..d6d31e6 100644 --- a/nyash_client/src/client_config.rs +++ b/nyash_client/src/client_config.rs @@ -1,7 +1,8 @@ -use ocl::{Device, Platform}; +use ocl::{Device, Platform, DeviceType, flags}; use serde::{Deserialize, Serialize}; use serde_json; use std::error::Error; +use std::{io}; #[derive(Clone, Deserialize, Serialize, Debug)] pub struct DevConf { @@ -9,7 +10,7 @@ pub struct DevConf { pub platform_name: String, pub id: usize, pub work_size: usize, - pub batch_size: u32, + pub batch_size: u64, } impl DevConf { @@ -68,7 +69,130 @@ pub fn load_config(file_name: &str) -> Result> { } pub fn save_config(file_name: &str, app_conf: &AppConfig) -> Result<(), Box> { - let conf_str = serde_json::to_string(app_conf)?; + let conf_str = serde_json::to_string_pretty(app_conf)?; std::fs::write(file_name, conf_str)?; return Ok(()); } + +fn dev_type_from_str(s: &str) -> Result { + match s { + "CPU" => Ok(flags::DeviceType::CPU), + "GPU" => Ok(flags::DeviceType::GPU), + "ALL" => Ok(flags::DeviceType::ALL), + "CUSTOM" => Ok(flags::DeviceType::CUSTOM), + "ACCELERATOR" => Ok(flags::DeviceType::ACCELERATOR), + "DEFAULT" => Ok(flags::DeviceType::DEFAULT), + _ => Err(()), + } +} + +fn str_or_empty(r: ocl::error::Result) -> String { + match r { + Ok(s) => s, + Err(_) => "".to_string(), + } +} + +fn print_devices(dev_list: &Vec<(Device, Platform)>) { + let mut i = 0; + for (dev, plt) in dev_list.iter() { + let dev_name = str_or_empty(dev.name()); + let plt_name = str_or_empty(plt.name()); + println!("({i}) device: \"{dev_name}\" ----- platorm: \"{plt_name}\""); + i += 1; + } +} + +fn choose_devices(devices_num: usize) -> Result, String> { + println!("Please input desired device to use as a number and press Enter."); + let mut result: Vec = Vec::new(); + + let mut s_devs_nums = String::new(); + + io::stdin() + .read_line(&mut s_devs_nums) + .expect("Failed to read line"); + + for s_dev_num in s_devs_nums.split(' ') { + let dev_num: usize = match s_dev_num.trim().parse() { + Ok(num) => num, + Err(_) => return Err("You must input a number from device list.".to_string()), + }; + if dev_num >= devices_num { + return Err("You must input a number from device list.".to_string()); + }; + result.push(dev_num); + } + return Ok(result); +} + +fn list_devices(dev_type: DeviceType) -> Vec<(Device, Platform)> { + let platforms = Platform::list(); + let mut devices: Vec<(Device, Platform)> = Vec::new(); + for plt in platforms.iter() { + //let plat_name = str_or_empty(plt.name()); + let list_res = Device::list(plt, Some(dev_type)); + match list_res { + Ok(dev_l) => devices.extend(dev_l.iter().map(|dev| (*dev, plt.clone()))), + Err(_) => {} + } + } + return devices; +} + + +fn dev_sel_dialog(all_devices: &Vec<(Device, Platform)>) -> Vec { + let devs_nums = loop { + print_devices(&all_devices); + match choose_devices(all_devices.len()) { + Ok(value) => break value, + Err(exc) => { + println!("Error! {exc}\n") + } + } + }; + return devs_nums; +} + +pub fn get_devices_conf(file_name: &str) -> Result<(Vec<(Device, Platform)>, AppConfig), String> { + let dev_type = dev_type_from_str("GPU").expect("Unexpected device type!"); + + // Get devices to be used for key search + let all_devices: Vec<(Device, Platform)> = list_devices(dev_type); + if all_devices.len() == 0 { + return Err("Cannot find any usable devices.".to_string()); + }; + + let app_conf = match load_config(file_name) { + Ok(readed_config) => { + let dev_found = all_devices + .iter() + .filter(|dp| readed_config.device_exist(&dp.0)) + .count(); + if dev_found < readed_config.devices.len() { + println!("Devices from config not found in the system!"); + let devs_nums = dev_sel_dialog(&all_devices); + let res = AppConfig::from_dev_list(&all_devices, devs_nums); + save_config(file_name, &res).expect("Error saving config!"); + res + } else { + readed_config + } + } + Err(_) => { + println!("Cannot find config file {}", file_name); + let devs_nums = dev_sel_dialog(&all_devices); + let res = AppConfig::from_dev_list(&all_devices, devs_nums); + save_config(file_name, &res).expect("Error saving config!"); + res + } + }; + + let selected_devs = all_devices + .iter() + .filter(|dp| app_conf.device_exist(&dp.0)) + .cloned() + .collect(); + + return Ok((selected_devs, app_conf)); +} \ No newline at end of file diff --git a/nyash_client/src/num_utils.rs b/nyash_client/src/num_utils.rs index 6a153d8..93eeba4 100644 --- a/nyash_client/src/num_utils.rs +++ b/nyash_client/src/num_utils.rs @@ -3,265 +3,334 @@ // i: [u32; 2], // } -fn add_u32_to_u256(a: &[u32; 8], b: u32) -> ([u32; 8], bool) { - let mut res: [u32; 8] = [0; 8]; - let mut carry = false; - (res[0], carry) = a[0].carrying_add(b, carry); - - for idx in 1..8 { - (res[idx], carry) = a[idx].carrying_add(0, carry); +pub fn vec_to_u32_4arr(in_v: &Vec, start_idx: usize) -> [u32; 4] { + let mut u32_arr_k = [0u32; 4]; + for i in 0..4 { + u32_arr_k[0] = in_v[start_idx + i]; } - - return (res, carry); + return u32_arr_k; } -fn add_u32_to_u256_(a: &mut [u32; 8], b: u32) -> bool { - let mut carry = false; - (a[0], carry) = a[0].carrying_add(b, carry); - - for idx in 1..8 { - (a[idx], carry) = a[idx].carrying_add(0, carry); +pub fn u128_to_u64arr(a: u128) -> [u64; 2] { + let mut res = [0u64; 2]; + let a_bytes = a.to_le_bytes(); + let chunks = a_bytes.as_chunks::<8>().0; + for i in 0..2 { + res[i] = u64::from_le_bytes(chunks[i]); } - - return carry; -} - -fn bytes_from_chars(chars_chunk: &[char]) -> [u8; 4] { - let mut res: [u8; 4] = [0; 4]; - let mut idx: usize = 0; - chars_chunk.chunks_exact(2).for_each(|b_c| { - if idx < 4 { - match u8::from_str_radix(&b_c.iter().collect::(), 16) { - Ok(n) => res[idx] = n, - Err(_) => (), - } - idx += 1; - } - }); return res; } -fn bignum_from_hex(hex: &str) -> [u32; 8] { - let mut res: [u32; 8] = [0; 8]; - let mut idx: usize = 0; - let chars_hex = hex.chars().collect::>(); - chars_hex - .chunks_exact(8) - .rev() - .map(|chunk| bytes_from_chars(chunk)) - .for_each(|b_arr| { - if idx < 8 { - res[idx] = u32::from_be_bytes(b_arr); - idx += 1; - } - }); - return res; +pub fn u64arr_to_u128(a: [u64; 2]) -> u128 { + let mut bytes_data: [u8; 16] = [0u8; 16]; + bytes_data[0..8].copy_from_slice(a[0].to_le_bytes().as_slice()); + bytes_data[8..].copy_from_slice(a[1].to_le_bytes().as_slice()); + + return u128::from_le_bytes(bytes_data); } -fn hex_fmt_byte(n: u32) -> String { - let res: String = n - .to_be_bytes() - .iter() - .map(|b| format!("{:02x}", b)) - .collect(); - return res; -} - -fn bignum_to_hex(a: &[u32; 8]) -> String { - let res: String = a - .iter() - .rev() - .map(|n| hex_fmt_byte(*n)) - .collect::>() - .join(""); - return res; -} - -#[cfg(test)] -mod num_utils_tests { - use std::io::Read; - - use super::*; - - #[test] - fn test_add() { - use std::io::{BufRead, BufReader}; - use std::process::{Command, Stdio}; - - let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py"; - let mut child = Command::new(test_gen_cmd) - .stdout(Stdio::piped()) - .spawn() - .unwrap(); - - let gen_stdout = child - .stdout - .take() - .ok_or("Failed to capture stdout") - .unwrap(); - let gen_reader = BufReader::new(gen_stdout); - - for r_line in gen_reader.lines() { - let test_line: String = r_line.unwrap(); // Handle any I/O errors - let test_data_line = test_line.split(' ').collect::>(); - let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap(); - let t0 = bignum_from_hex(test_data_line[1]); - - let t1_test = add_u32_to_u256(&t0, 1).0; - let t2_test = add_u32_to_u256(&t0, num_to_add).0; - - let res_actual = format!( - "{} {} {} {}", - num_to_add, - bignum_to_hex(&t0), - bignum_to_hex(&t1_test), - bignum_to_hex(&t2_test) - ); - assert_eq!(test_line, res_actual); - } - - let _ = child.wait().unwrap(); - - } - - #[test] - fn test_cl_add() { - extern crate ocl; - use ocl::{ - Buffer, Context, Device, DeviceType, Kernel, Platform, Program, Queue, SpatialDims, - flags, - }; - use std::fs::File; - use std::io::{BufRead, BufReader}; - use std::process::{Command, Stdio}; - - let cl_test_path = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/open_cl/test_num_utils.cl"; - let cl_include_opt = - "-I /home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/open_cl"; - let mut cl_src = String::new(); - // read ocl source - BufReader::new(File::open(cl_test_path).unwrap()).read_to_string(&mut cl_src); - - const G_WORK_SIZE: usize = 4096; - - let cl_platform = Platform::default(); - let cl_device = Device::first(cl_platform).unwrap(); - let cl_context = Context::builder() - .platform(cl_platform) - .devices(cl_device.clone()) - .build() - .unwrap(); - let cl_program = Program::builder() - .devices(cl_device) - .src(cl_src) - .cmplr_opt(cl_include_opt) - .build(&cl_context) - .unwrap(); - let cl_queue = Queue::new(&cl_context, cl_device, None).unwrap(); - - let cl_buffer_num = Buffer::::builder() - .queue(cl_queue.clone()) - .flags(flags::MEM_READ_ONLY) - .len(G_WORK_SIZE) - .fill_val(0u32) - .build() - .unwrap(); - - let cl_buffer_t0 = Buffer::::builder() - .queue(cl_queue.clone()) - .flags(flags::MEM_READ_ONLY) - .len(G_WORK_SIZE * 8) - .fill_val(0u32) - .build() - .unwrap(); - - let cl_buffer_t1 = Buffer::::builder() - .queue(cl_queue.clone()) - .flags(flags::MEM_WRITE_ONLY) - .len(G_WORK_SIZE * 8) - .fill_val(0u32) - .build() - .unwrap(); - - let cl_buffer_t2 = Buffer::::builder() - .queue(cl_queue.clone()) - .flags(flags::MEM_WRITE_ONLY) - .len(G_WORK_SIZE * 8) - .fill_val(0u32) - .build() - .unwrap(); - - // (3) Create a kernel with arguments matching those in the source above: - let kernel = Kernel::builder() - .program(&cl_program) - .name("test_add") - .queue(cl_queue.clone()) - .global_work_size(G_WORK_SIZE) - .arg(&cl_buffer_num) - .arg(&cl_buffer_t0) - .arg(&cl_buffer_t1) - .arg(&cl_buffer_t2) - .build() - .unwrap(); - - let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py"; - let mut child = Command::new(test_gen_cmd) - .stdout(Stdio::piped()) - .spawn() - .unwrap(); - - let gen_stdout = child - .stdout - .take() - .ok_or("Failed to capture stdout") - .unwrap(); - let gen_reader = BufReader::new(gen_stdout); - - - let mut buffer_num: Vec = vec![0u32; G_WORK_SIZE]; - let mut buffer_t0: Vec = vec![0u32; G_WORK_SIZE*8]; - let mut exp_buffer_t1: Vec = vec![0u32; G_WORK_SIZE*8]; - let mut exp_buffer_t2: Vec = vec![0u32; G_WORK_SIZE*8]; - - let mut act_buffer_t1: Vec = vec![0u32; G_WORK_SIZE*8]; - let mut act_buffer_t2: Vec = vec![0u32; G_WORK_SIZE*8]; - - let mut w_id: usize = 0; - - for r_line in gen_reader.lines() { - let test_line: String = r_line.unwrap(); // Handle any I/O errors - let test_data_line = test_line.split(' ').collect::>(); - let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap(); - buffer_num[w_id] = num_to_add; - let slise_id = w_id*8; - buffer_t0[slise_id..slise_id+8].copy_from_slice(&bignum_from_hex(test_data_line[1])); - exp_buffer_t1[slise_id..slise_id+8].copy_from_slice(&bignum_from_hex(test_data_line[2])); - exp_buffer_t2[slise_id..slise_id+8].copy_from_slice(&bignum_from_hex(test_data_line[3])); - - w_id += 1; - if w_id >= G_WORK_SIZE { - w_id = 0; // reset counter - cl_buffer_num.cmd().queue(&cl_queue).offset(0).write(&buffer_num).enq().unwrap(); - cl_buffer_t0.cmd().queue(&cl_queue).offset(0).write(&buffer_t0).enq().unwrap(); - - // (4) Run the kernel - unsafe { - kernel - .cmd() - .queue(&cl_queue) - .global_work_size(G_WORK_SIZE) - .enq().unwrap(); - } - - cl_buffer_t1.cmd().queue(&cl_queue).offset(0).read(&mut act_buffer_t1).enq().unwrap(); - cl_buffer_t2.cmd().queue(&cl_queue).offset(0).read(&mut act_buffer_t2).enq().unwrap(); - - assert_eq!(exp_buffer_t1, act_buffer_t1); - assert_eq!(exp_buffer_t2, act_buffer_t2); - } - - } - - let _ = child.wait().unwrap(); +pub fn u128_to_u32arr(a: u128) -> [u32; 4] { + let mut res = [0u32; 4]; + let a_bytes = a.to_le_bytes(); + let chunks = a_bytes.as_chunks::<4>().0; + for i in 0..4 { + res[i] = u32::from_le_bytes(chunks[i]); } + return res; } + +pub fn u32arr_to_u128(a: [u32; 4]) -> u128 { + let mut bytes_data: [u8; 16] = [0u8; 16]; + bytes_data[0..4].copy_from_slice(a[0].to_le_bytes().as_slice()); + bytes_data[4..8].copy_from_slice(a[1].to_le_bytes().as_slice()); + bytes_data[8..12].copy_from_slice(a[2].to_le_bytes().as_slice()); + bytes_data[12..16].copy_from_slice(a[3].to_le_bytes().as_slice()); + + return u128::from_le_bytes(bytes_data); +} + +// fn add_u32_to_u256(a: &[u32; 8], b: u32) -> ([u32; 8], bool) { +// let mut res: [u32; 8] = [0; 8]; +// let mut carry = false; +// (res[0], carry) = a[0].carrying_add(b, carry); + +// for idx in 1..8 { +// (res[idx], carry) = a[idx].carrying_add(0, carry); +// } + +// return (res, carry); +// } + +// fn add_u32_to_u256_(a: &mut [u32; 8], b: u32) -> bool { +// let mut carry = false; +// (a[0], carry) = a[0].carrying_add(b, carry); + +// for idx in 1..8 { +// (a[idx], carry) = a[idx].carrying_add(0, carry); +// } + +// return carry; +// } + +// fn bytes_from_chars(chars_chunk: &[char]) -> [u8; 4] { +// let mut res: [u8; 4] = [0; 4]; +// let mut idx: usize = 0; +// chars_chunk.chunks_exact(2).for_each(|b_c| { +// if idx < 4 { +// match u8::from_str_radix(&b_c.iter().collect::(), 16) { +// Ok(n) => res[idx] = n, +// Err(_) => (), +// } +// idx += 1; +// } +// }); +// return res; +// } + +// fn bignum_from_hex(hex: &str) -> [u32; 8] { +// let mut res: [u32; 8] = [0; 8]; +// let mut idx: usize = 0; +// let chars_hex = hex.chars().collect::>(); +// chars_hex +// .chunks_exact(8) +// .rev() +// .map(|chunk| bytes_from_chars(chunk)) +// .for_each(|b_arr| { +// if idx < 8 { +// res[idx] = u32::from_be_bytes(b_arr); +// idx += 1; +// } +// }); +// return res; +// } + +// fn hex_fmt_byte(n: u32) -> String { +// let res: String = n +// .to_be_bytes() +// .iter() +// .map(|b| format!("{:02x}", b)) +// .collect(); +// return res; +// } + +// fn bignum_to_hex(a: &[u32; 8]) -> String { +// let res: String = a +// .iter() +// .rev() +// .map(|n| hex_fmt_byte(*n)) +// .collect::>() +// .join(""); +// return res; +// } + +// #[cfg(test)] +// mod num_utils_tests { +// use std::io::Read; + +// use super::*; + +// #[test] +// fn test_add() { +// use std::io::{BufRead, BufReader}; +// use std::process::{Command, Stdio}; + +// let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py"; +// let mut child = Command::new(test_gen_cmd) +// .stdout(Stdio::piped()) +// .spawn() +// .unwrap(); + +// let gen_stdout = child +// .stdout +// .take() +// .ok_or("Failed to capture stdout") +// .unwrap(); +// let gen_reader = BufReader::new(gen_stdout); + +// for r_line in gen_reader.lines() { +// let test_line: String = r_line.unwrap(); // Handle any I/O errors +// let test_data_line = test_line.split(' ').collect::>(); +// let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap(); +// let t0 = bignum_from_hex(test_data_line[1]); + +// let t1_test = add_u32_to_u256(&t0, 1).0; +// let t2_test = add_u32_to_u256(&t0, num_to_add).0; + +// let res_actual = format!( +// "{} {} {} {}", +// num_to_add, +// bignum_to_hex(&t0), +// bignum_to_hex(&t1_test), +// bignum_to_hex(&t2_test) +// ); +// assert_eq!(test_line, res_actual); +// } + +// let _ = child.wait().unwrap(); +// } + +// #[test] +// fn test_cl_add() { +// extern crate ocl; +// use ocl::{Buffer, Context, Device, Kernel, Platform, Program, Queue, flags}; +// use std::fs::File; +// use std::io::{BufRead, BufReader}; +// use std::process::{Command, Stdio}; + +// let cl_test_path = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/open_cl/test_num_utils.cl"; +// let cl_include_opt = +// "-I /home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/open_cl"; +// let mut cl_src = String::new(); +// // read ocl source +// BufReader::new(File::open(cl_test_path).unwrap()) +// .read_to_string(&mut cl_src) +// .expect("Error reading cl_src!"); + +// const G_WORK_SIZE: usize = 4096; + +// let cl_platform = Platform::default(); +// let cl_device = Device::first(cl_platform).unwrap(); +// let cl_context = Context::builder() +// .platform(cl_platform) +// .devices(cl_device.clone()) +// .build() +// .unwrap(); +// let cl_program = Program::builder() +// .devices(cl_device) +// .src(cl_src) +// .cmplr_opt(cl_include_opt) +// .build(&cl_context) +// .unwrap(); +// let cl_queue = Queue::new(&cl_context, cl_device, None).unwrap(); + +// let cl_buffer_num = Buffer::::builder() +// .queue(cl_queue.clone()) +// .flags(flags::MEM_READ_ONLY) +// .len(G_WORK_SIZE) +// .fill_val(0u32) +// .build() +// .unwrap(); + +// let cl_buffer_t0 = Buffer::::builder() +// .queue(cl_queue.clone()) +// .flags(flags::MEM_READ_ONLY) +// .len(G_WORK_SIZE * 8) +// .fill_val(0u32) +// .build() +// .unwrap(); + +// let cl_buffer_t1 = Buffer::::builder() +// .queue(cl_queue.clone()) +// .flags(flags::MEM_WRITE_ONLY) +// .len(G_WORK_SIZE * 8) +// .fill_val(0u32) +// .build() +// .unwrap(); + +// let cl_buffer_t2 = Buffer::::builder() +// .queue(cl_queue.clone()) +// .flags(flags::MEM_WRITE_ONLY) +// .len(G_WORK_SIZE * 8) +// .fill_val(0u32) +// .build() +// .unwrap(); + +// // (3) Create a kernel with arguments matching those in the source above: +// let kernel = Kernel::builder() +// .program(&cl_program) +// .name("test_add") +// .queue(cl_queue.clone()) +// .global_work_size(G_WORK_SIZE) +// .arg(&cl_buffer_num) +// .arg(&cl_buffer_t0) +// .arg(&cl_buffer_t1) +// .arg(&cl_buffer_t2) +// .build() +// .unwrap(); + +// let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py"; +// let mut child = Command::new(test_gen_cmd) +// .stdout(Stdio::piped()) +// .spawn() +// .unwrap(); + +// let gen_stdout = child +// .stdout +// .take() +// .ok_or("Failed to capture stdout") +// .unwrap(); +// let gen_reader = BufReader::new(gen_stdout); + +// let mut buffer_num: Vec = vec![0u32; G_WORK_SIZE]; +// let mut buffer_t0: Vec = vec![0u32; G_WORK_SIZE * 8]; +// let mut exp_buffer_t1: Vec = vec![0u32; G_WORK_SIZE * 8]; +// let mut exp_buffer_t2: Vec = vec![0u32; G_WORK_SIZE * 8]; + +// let mut act_buffer_t1: Vec = vec![0u32; G_WORK_SIZE * 8]; +// let mut act_buffer_t2: Vec = vec![0u32; G_WORK_SIZE * 8]; + +// let mut w_id: usize = 0; + +// for r_line in gen_reader.lines() { +// let test_line: String = r_line.unwrap(); // Handle any I/O errors +// let test_data_line = test_line.split(' ').collect::>(); +// let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap(); +// buffer_num[w_id] = num_to_add; +// let slise_id = w_id * 8; +// buffer_t0[slise_id..slise_id + 8].copy_from_slice(&bignum_from_hex(test_data_line[1])); +// exp_buffer_t1[slise_id..slise_id + 8] +// .copy_from_slice(&bignum_from_hex(test_data_line[2])); +// exp_buffer_t2[slise_id..slise_id + 8] +// .copy_from_slice(&bignum_from_hex(test_data_line[3])); + +// w_id += 1; +// if w_id >= G_WORK_SIZE { +// w_id = 0; // reset counter +// cl_buffer_num +// .cmd() +// .queue(&cl_queue) +// .offset(0) +// .write(&buffer_num) +// .enq() +// .unwrap(); +// cl_buffer_t0 +// .cmd() +// .queue(&cl_queue) +// .offset(0) +// .write(&buffer_t0) +// .enq() +// .unwrap(); + +// // (4) Run the kernel +// unsafe { +// kernel +// .cmd() +// .queue(&cl_queue) +// .global_work_size(G_WORK_SIZE) +// .enq() +// .unwrap(); +// } + +// cl_buffer_t1 +// .cmd() +// .queue(&cl_queue) +// .offset(0) +// .read(&mut act_buffer_t1) +// .enq() +// .unwrap(); +// cl_buffer_t2 +// .cmd() +// .queue(&cl_queue) +// .offset(0) +// .read(&mut act_buffer_t2) +// .enq() +// .unwrap(); + +// assert_eq!(exp_buffer_t1, act_buffer_t1); +// assert_eq!(exp_buffer_t2, act_buffer_t2); +// } +// } + +// let _ = child.wait().unwrap(); +// } +// } diff --git a/nyash_client/src/ocl_utils.rs b/nyash_client/src/ocl_utils.rs new file mode 100644 index 0000000..d30bb7d --- /dev/null +++ b/nyash_client/src/ocl_utils.rs @@ -0,0 +1,320 @@ + +use std::io::Read; + +use ocl::{Buffer, Context, Device, Kernel, Platform, Program, Queue, flags}; + +use crate::num_utils; + +pub struct CtxBuffers { + tweak_params: Buffer, + batch_size: Buffer, + start_key: Buffer, + tweak_key: Buffer, + uenc_data: Buffer, + target_data: Buffer, + key_found: Buffer, +} + +pub struct ExecData { + pub start_key: Vec, + pub tweak_key: Vec, + pub uenc_data: Vec, + pub target_data: Vec, + pub tweak_i: u64, + pub tweak_j: u32, + pub key_found: Vec, + pub batch_size: u64, + pub work_size: usize, +} + impl ExecData { + // g_params[uint4] + // g_params[0-1] - ulog g_Ti + // g_params[2] - g_Tj + pub fn tweak_params(&self) -> Vec { + let mut res: Vec = Vec::with_capacity(4); + + // the sector number (S) is first converted into a little-endian byte array + // before being encrypted using the second AES key (K₂) + let tweak_i_b: [u8;8] = self.tweak_i.to_le_bytes(); + let (tweak_i_cnk,_) = tweak_i_b.as_chunks::<4>(); + res.push(u32::from_le_bytes(tweak_i_cnk[0])); + res.push(u32::from_le_bytes(tweak_i_cnk[1])); + + //last enc block number (tweak_j) + res.push(self.tweak_j); + + return res; + } + + pub fn get_found_key(&self) -> u128 { + let mut u32_arr_k = [0u32;4]; + u32_arr_k[0] = self.key_found[1]; + u32_arr_k[1] = self.key_found[2]; + u32_arr_k[2] = self.key_found[3]; + u32_arr_k[3] = self.key_found[4]; + return num_utils::u32arr_to_u128(u32_arr_k); + } + + } + +pub struct ExecContext { + _ctx: Context, + kernel: Kernel, + _prog: Program, + queue: Queue, + buffers: CtxBuffers, +} + +pub fn init_program( + cl_device: Device, + cl_platform: Platform, + cl_src_gz_bytes: &[u8], +) -> Result<(Context, Program, Queue), ocl::Error> { + use flate2::read::GzDecoder; + let mut gz_decoder = GzDecoder::new(cl_src_gz_bytes); + let mut decompressed_src = String::new(); + gz_decoder.read_to_string(&mut decompressed_src).expect("Error decompressing OCL sources!"); + + let cl_context = Context::builder() + .platform(cl_platform) + .devices(cl_device.clone()) + .build()?; + + let cl_program = Program::builder() + .devices(cl_device) + .src(decompressed_src) + .build(&cl_context) + .unwrap(); + + let cl_queue: Queue = Queue::new(&cl_context, cl_device, None)?; + + return Ok((cl_context, cl_program, cl_queue)); +} + +pub fn init_buffers(cl_queue: &Queue) -> Result { + let cl_buffer_tweak_params = Buffer::::builder() + .queue(cl_queue.clone()) + .flags(flags::MEM_READ_ONLY) + .len(3) + .fill_val(0u32) + .build()?; + + let cl_buffer_batch_size = Buffer::::builder() + .queue(cl_queue.clone()) + .flags(flags::MEM_READ_ONLY) + .len(1) + .fill_val(0u64) + .build()?; + + let cl_buffer_start_key = Buffer::::builder() + .queue(cl_queue.clone()) + .flags(flags::MEM_READ_ONLY) + .len(4) + .fill_val(0u32) + .build()?; + + let cl_buffer_tweak_key = Buffer::::builder() + .queue(cl_queue.clone()) + .flags(flags::MEM_READ_ONLY) + .len(4) + .fill_val(0u32) + .build()?; + + let cl_buffer_uenc_data = Buffer::::builder() + .queue(cl_queue.clone()) + .flags(flags::MEM_READ_ONLY) + .len(4) + .fill_val(0u32) + .build()?; + + let cl_buffer_target_data = Buffer::::builder() + .queue(cl_queue.clone()) + .flags(flags::MEM_READ_ONLY) + .len(4) + .fill_val(0u32) + .build()?; + + let cl_buffer_key_found = Buffer::::builder() + .queue(cl_queue.clone()) + .flags(flags::MEM_WRITE_ONLY) + .len(5) + .fill_val(0u32) + .build()?; + + + Ok(CtxBuffers { + tweak_params: cl_buffer_tweak_params, + batch_size: cl_buffer_batch_size, + start_key: cl_buffer_start_key, + tweak_key: cl_buffer_tweak_key, + uenc_data: cl_buffer_uenc_data, + target_data: cl_buffer_target_data, + key_found: cl_buffer_key_found, + }) +} + +fn init_kernel( + work_size: usize, + cl_program: &Program, + cl_queue: &Queue, + buffs: &CtxBuffers, +) -> Result { + Kernel::builder() + .program(cl_program) + .name("search_key") + .queue(cl_queue.clone()) + .global_work_size(work_size) + .arg(&buffs.tweak_params) + .arg(&buffs.batch_size) + .arg(&buffs.start_key) + .arg(&buffs.tweak_key) + .arg(&buffs.uenc_data) + .arg(&buffs.target_data) + .arg(&buffs.key_found) + .build() +} + +impl ExecContext { + // Constructor with parameters + pub fn new( + cl_device: Device, + cl_platform: Platform, + cl_src_gz_bytes: &[u8], + global_work_size: usize, + ) -> Result { + let (nya_cl_context, nya_cl_program, nya_cl_queue) = + init_program(cl_device, cl_platform, cl_src_gz_bytes)?; + + let nya_cl_buffers = init_buffers(&nya_cl_queue)?; + + let nya_cl_kernel = init_kernel( + global_work_size, + &nya_cl_program, + &nya_cl_queue, + &nya_cl_buffers, + )?; + Ok(Self { + _ctx: nya_cl_context, + kernel: nya_cl_kernel, + _prog: nya_cl_program, + queue: nya_cl_queue, + buffers: nya_cl_buffers, + }) + } +} + +pub fn set_target_data(ex_ctx: &mut ExecContext, ex_data: &mut ExecData) -> Result<(), ocl::Error> { + // transfer tweaks + let t_p = ex_data.tweak_params(); + ex_ctx + .buffers + .tweak_params + .cmd() + .queue(&ex_ctx.queue) + .offset(0) + .write(&t_p) + .enq()?; + + // transfen unencrypted data to device + ex_ctx + .buffers + .uenc_data + .cmd() + .queue(&ex_ctx.queue) + .offset(0) + .write(&ex_data.uenc_data) + .enq()?; + + // transfet target data + ex_ctx + .buffers + .target_data + .cmd() + .queue(&ex_ctx.queue) + .offset(0) + .write(&ex_data.target_data) + .enq()?; + + ex_ctx.queue.finish()?; + + return Ok(()); +} + +pub fn do_work(ex_ctx: &mut ExecContext, ex_data: &mut ExecData) -> Result<(bool, f64), ocl::Error> { + + let b_s = vec![ex_data.batch_size]; + + let start_time = std::time::Instant::now(); + + // tranfer batch_size + ex_ctx + .buffers + .batch_size + .cmd() + .queue(&ex_ctx.queue) + .offset(0) + .write(&b_s) + .enq()?; + + + // transfer start key to device + ex_ctx + .buffers + .start_key + .cmd() + .queue(&ex_ctx.queue) + .offset(0) + .write(&ex_data.start_key) + .enq()?; + + // transfet tweak key + ex_ctx + .buffers + .tweak_key + .cmd() + .queue(&ex_ctx.queue) + .offset(0) + .write(&ex_data.tweak_key) + .enq()?; + + // zero out key_found buffer + // ex_ctx + // .buffers + // .key_found + // .cmd() + // .queue(&ex_ctx.queue) + // .offset(0) + // .fill(0u32, None) + // .enq()?; + + + // (4) Run the kernel + unsafe { + ex_ctx + .kernel + .cmd() + .queue(&ex_ctx.queue) + .global_work_size(ex_data.work_size) + .enq()?; + } + + // read key_foun buffer + ex_ctx + .buffers + .key_found + .cmd() + .queue(&ex_ctx.queue) + .offset(0) + .read(&mut ex_data.key_found) + .enq()?; + + let exec_duration = start_time.elapsed().as_secs_f64(); + + //ex_ctx.queue.finish()?; + + if ex_data.key_found[0] == 0 { + Ok((false, exec_duration)) + } else { + Ok((true, exec_duration)) + } +} diff --git a/nyash_client/src/main.rs b/nyash_client/src/old_main.rs similarity index 92% rename from nyash_client/src/main.rs rename to nyash_client/src/old_main.rs index cbea613..cb5c2c7 100644 --- a/nyash_client/src/main.rs +++ b/nyash_client/src/old_main.rs @@ -12,32 +12,7 @@ use crate::client_config::{AppConfig, DevConf}; mod client_config; mod num_utils; -/// Exploded version. Boom! -/// -/// The functions above use `ProQue` and other abstractions to greatly reduce -/// the amount of boilerplate and configuration necessary to do basic work. -/// Many tasks, however, will require more configuration and will necessitate -/// doing away with `ProQue` altogether. Enqueuing kernels and reading/writing -/// from buffers and images usually requires a more explicit interface. -/// -/// The following function performs the exact same steps that the above -/// functions did, with many of the convenience abstractions peeled away. -/// -/// See the function below this to take things a step deeper... -/// -// trait FromStr { -// fn from_str(&self); -// } -// // Define a trait with a constructor method -// trait NewFile { -// fn new>(path: P) -> std::io::Result where Self: Sized; -// } -// impl Foo for ocl::flags::DeviceType { -// fn foo(&self) { -// println!("foo"); -// } -// } fn dev_type_from_str(s: &str) -> Result { match s { diff --git a/nyash_client/src/open_cl/build_spirv.sh b/nyash_client/src/open_cl/build_spirv.sh new file mode 100755 index 0000000..7149f29 --- /dev/null +++ b/nyash_client/src/open_cl/build_spirv.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +out_file="$1" + +clang -c -target spir64 -O0 -finclude-default-header -I ./ -emit-llvm -o nyash_aes_xts256_plain.bc nyash_aes_xts256_plain.cl + +#llc -march=spir64 nyash_aes_xts256_plain.bc -filetype=obj -o $out_file +llvm-spirv -o $out_file nyash_aes_xts256_plain.bc \ No newline at end of file diff --git a/nyash_client/src/open_cl/nyash_aes_xts256_plain.cl b/nyash_client/src/open_cl/nyash_aes_xts256_plain.cl index 582a0be..4f5c98d 100644 --- a/nyash_client/src/open_cl/nyash_aes_xts256_plain.cl +++ b/nyash_client/src/open_cl/nyash_aes_xts256_plain.cl @@ -32,15 +32,25 @@ // g_key_found uint[9] - 0 element - flag that sets to 1 if key found. // Other 8 elements is found key -__kernel void search_key(const uint batch_size, const ulong g_Ti, const uint g_Tj, - __global const uint8* g_start_enc_key, + +// in current implementation tweak key is not changing by kernel +// So changing it a bit + +// g_params[uint4] +// g_params[0] - batch_size +// g_params[1-2] - ulong g_Ti +// g_params[3] - g_Tj +__kernel void search_key(__global const uint* g_tweak_params, + __global const ulong* g_batch_size, + __global const uint4* g_start_enc_key, + __global const uint4* g_tweak_key, __global const uint4* g_uenc_data, __global const uint4* g_target_data, __global uint* g_key_found) { const uint g_id = get_global_id(0); - uint enc_key[8]; + uint enc_key[4]; uint tweak[4]; uint uenc_data[4]; uint4 target_data = *g_target_data; @@ -48,32 +58,34 @@ __kernel void search_key(const uint batch_size, const ulong g_Ti, const uint g_T uint d_ks[44]; // data expanded key uint t_ks[44]; // tweak expanded key + //set batch_size + ulong batch_size = g_batch_size[0]; + // set disk sector number uint sec_n[4] = {0}; - sec_n[0] = ((uint*)&g_Ti)[0]; - sec_n[1] = ((uint*)&g_Ti)[1]; + sec_n[0] = g_tweak_params[0]; + sec_n[1] = g_tweak_params[1]; - uint Tj = g_Tj; // AES block number + uint Tj = g_tweak_params[2]; // AES block number vstore4(*g_uenc_data, 0, uenc_data); - vstore8(*g_start_enc_key, 0, enc_key); - + vstore4(*g_start_enc_key, 0, enc_key); + vstore4(*g_tweak_key, 0, tweak); // Set initial start key for every work thread uint k_data_carry = add_uint_to_bigint4_ (enc_key, (g_id*batch_size)); - // uint k_tweak_carry = add_uint_to_bigint4_ (&enc_key[4], k_data_carry); - // No need to store tweak carry - if (add_uint_to_bigint4_ (&enc_key[4], k_data_carry) != 0u) return; // if reached max key value exit thread + if (k_data_carry != 0u) return; // if reached max key value exit thread // Generate tweak - aes128_set_encrypt_key (t_ks, &enc_key[4]); + aes128_set_encrypt_key (t_ks, tweak); aes_xts256_gen_tweak (t_ks, sec_n, Tj, tweak); + //if (g_id == 0) g_key_found[1] = 1; - for (uint batch_id = 0u; (batch_id < batch_size); batch_id++) + for (ulong batch_id = 0ul; batch_id < batch_size; batch_id++) { - // Data encrypt key always changing because we increment from 0 index to 8 + //if (g_id == 0) g_key_found[1] = 2; + // Set encrypt key aes128_set_encrypt_key (d_ks, enc_key); - // encrypt data aes_xts256_enc_block (d_ks, tweak, uenc_data, (uint*)&enc_data); @@ -81,29 +93,17 @@ __kernel void search_key(const uint batch_size, const ulong g_Ti, const uint g_T if (all(enc_data==target_data)) { g_key_found[0] = 1; - vstore8(vload8(0, enc_key), 0, &g_key_found[1]); + g_key_found[1] = enc_key[0]; + g_key_found[2] = enc_key[1]; + g_key_found[3] = enc_key[2]; + g_key_found[4] = enc_key[3]; return; } - // Increment data key part by 1. + // Increment data key by 1. k_data_carry = add_one_to_bigint4_ (enc_key); - - // Tweak changes only once in 2^128 times - if (k_data_carry != 0u) { - - // Increment tweak part - // k_tweak_carry = add_one_to_bigint4_ (&enc_key[4]); - add_one_to_bigint4_ (&enc_key[4]); // no need to store tweak carry - - // *** I commented next line because, its a really!! lol! rare event, - // *** and in a worse case we just do a bit of noneed work. - // *** But additional check on every itaration actually mesurable cost. - // if (k_tweak_carry != 0u) return; // if reached max key value exit thread - - // Gen new tweak - aes128_set_encrypt_key (t_ks, &enc_key[4]); - aes_xts256_gen_tweak (t_ks, sec_n, Tj, tweak); - } + // if reached max key value exit thread + if (k_data_carry != 0u) return; } } diff --git a/nyash_client/src/search_params.rs b/nyash_client/src/search_params.rs new file mode 100644 index 0000000..077b76a --- /dev/null +++ b/nyash_client/src/search_params.rs @@ -0,0 +1,38 @@ + + +pub fn get_params() ->([u32; 4],[u32; 4],[u32; 4]) { + use crate::num_utils; + + const ENCRYPTED_DATA: [u8; 16] = [ + 198, 255, 55, 185, 15, 226, 223, 174, 119, 8, 36, 239, 242, 89, 126, 230 + ]; + + const KEY_DATA: [u8; 32] = [ + 206, 193, 83, 54, 46, 234, 185, 41, 146, 244, 130, 6, 212, 68, 106, 162, 165, 97, 188, + 218, 39, 111, 141, 236, 67, 159, 157, 157, 166, 79, 89, 134 + ]; + + // let key_bytes_reversed: Vec = KEY_DATA.iter().rev().map(|e| *e).collect(); + // let data_bytes_reversed: Vec = ENCRYPTED_DATA.iter().rev().map(|e| *e).collect(); + + let mut tweak_key_b: [u8;16] = [0u8;16]; + let mut data_key_b: [u8;16] = [0u8;16]; + + data_key_b.copy_from_slice(&KEY_DATA[0..16]); + tweak_key_b.copy_from_slice(&KEY_DATA[16..32]); + + // getting keys + let data_key = u128::from_le_bytes(data_key_b); + let tweak_key = u128::from_le_bytes(tweak_key_b); + let data_key = num_utils::u128_to_u32arr(data_key); + let tweak_key = num_utils::u128_to_u32arr(tweak_key); + + // converting bytes raw data to u32 arr + let mut encrypted_data: [u32; 4] = [0u32; 4]; + let (enc_dat_bytes_chunks, _) = ENCRYPTED_DATA.as_chunks::<4>(); + for i in 0..4 { + encrypted_data[i] = u32::from_le_bytes(enc_dat_bytes_chunks[i]); + } + + return (data_key, tweak_key, encrypted_data); +} \ No newline at end of file diff --git a/nyash_client/src/test_cl.rs b/nyash_client/src/test_cl.rs new file mode 100644 index 0000000..df8162c --- /dev/null +++ b/nyash_client/src/test_cl.rs @@ -0,0 +1,79 @@ + +#[cfg(test)] +mod test_cl { + #[test] + fn test_encryption() { + use ocl::{Device, Platform}; + use crate::ocl_utils; + use crate::num_utils; + + + const SRC_PATH: &str = "src/open_cl/nyash_aes_xts256_plain.cl"; + const OCL_COMP_OPT: &str = "-I src/open_cl"; + const ENCRYPTED_DATA: [u8; 16] = [ + 198, 255, 55, 185, 15, 226, 223, 174, 119, 8, 36, 239, 242, 89, 126, 230 + ]; + const KEY_DATA: [u8; 32] = [ + 206, 193, 83, 54, 46, 234, 185, 41, 146, 244, 130, 6, 212, 68, 106, 162, 165, 97, 188, + 218, 39, 111, 141, 236, 67, 159, 157, 157, 166, 79, 89, 134 + ]; + + // let key_bytes_reversed: Vec = KEY_DATA.iter().rev().map(|e| *e).collect(); + // let data_bytes_reversed: Vec = ENCRYPTED_DATA.iter().rev().map(|e| *e).collect(); + + let mut tweak_key_b: [u8;16] = [0u8;16]; + let mut data_key_b: [u8;16] = [0u8;16]; + + data_key_b.copy_from_slice(&KEY_DATA[0..16]); + tweak_key_b.copy_from_slice(&KEY_DATA[16..32]); + + // getting keys + let data_key = u128::from_le_bytes(data_key_b); + let tweak_key = u128::from_le_bytes(tweak_key_b); + let data_key = num_utils::u128_to_u32arr(data_key); + let tweak_key = num_utils::u128_to_u32arr(tweak_key); + + + // converting bytes raw data to u32 arr + let mut encrypted_data: [u32; 4] = [0u32; 4]; + let (enc_dat_bytes_chunks, _) = ENCRYPTED_DATA.as_chunks::<4>(); + for i in 0..4 { + encrypted_data[i] = u32::from_le_bytes(enc_dat_bytes_chunks[i]); + } + + // init devices + let platform = Platform::first().expect("Error getting platform!"); + let device = Device::first(platform).expect("Error getting device!"); + + println!("Platform: {:?}, Device: {:?}", platform.name().unwrap(), device.name().unwrap()); + + // reading ocl program sources + let prog_src = std::fs::read_to_string(SRC_PATH).expect("Error reading program sources!"); + + let mut nyan_context = + ocl_utils::ExecContext::new(device, platform, prog_src.as_str(), OCL_COMP_OPT, 256) + .expect("Error creating execution nyan context!"); + + + //setting data + let mut nyan_exec_dat = ocl_utils::ExecData { + start_key: data_key.to_vec(), + tweak_key: tweak_key.to_vec(), + uenc_data: vec![0u32;4], + target_data: encrypted_data.to_vec(), + tweak_i: 0, + tweak_j: 0, + key_found: vec![0u32;5], + batch_size: 1000000, + work_size: 256, + }; + + println!("Set target data"); + ocl_utils::set_target_data(&mut nyan_context, &mut nyan_exec_dat).expect("Error set target data!"); + + let found_flag = ocl_utils::do_work(&mut nyan_context, &mut nyan_exec_dat).expect("Error do work!"); + println!("Found?: {}", found_flag); + println!("Key found: {:?}", nyan_exec_dat.key_found); + assert_eq!(true, found_flag); + } +} diff --git a/nyash_client/src/test_data/__pycache__/utils.cpython-314.pyc b/nyash_client/src/test_data/__pycache__/utils.cpython-314.pyc new file mode 100644 index 0000000000000000000000000000000000000000..687fe785204209069731fe7425e1159311f54b85 GIT binary patch literal 2178 zcmbtVO>7fK6rTO@Ux@7l5RURg3=v5UIOZo%N>D2V38_FdEWuF8%4%be$u{=xYGzGw z4%CMpLN7$U)RmA*m8hjsRprnFhaTvU)E?6&F44dNsa&`PP!*}Cz8QN%h-?2vjzNN#jq^h2kD#*rqyjof-GxsoQwkfy{?p~aOo z75YBWS2U>yCHznMsoYAFInRzygWL@D3+G z{30^Xp;JK`L|T*6s9$w9L}YY59(+ojcA>PSDVnPJUgz_H962qI@{Mewadhko#QGqQ zHCS+un+W%CT_)%akBZFjqvqX? zN-hgxD{JRW>+1BLAwp~t$JcQdF3h?b?|bQ>FFs*f1}X1~CxWbrmL?oxXECKrAtj47 z)w2rLfefQ=4whyr-vIL!Y$HcmXy|$tJFW>MT#pG!sKf_nt8ZM-7IQo`X{i7k@-|KR zmGs8ral)g@!UP*R-BXe-7~=+X5?BR2?&!PQzIf#9FYhk4U#^Ti3CC`Ye>7elSs1&2 zW;uMWGQ1K9KM1t_8fdGYTn=>I>HRYKdGfd2gO9ewJ~{sJ@oN6F;-|%>ZTn|}E790Y z^hqSP*#6di|Igv?!}mT|YT~=kVpuI>OdB86DSj`g)3eI z#-SFJTAp!so38Ta>zru51K5G|(MeDNg$>BrlXBNkjYFg+{Q9SGFMK zy;R+(*Q8e@d86bgnRQS9d0l1WN}9S+^7i4V|8rH}##J3(hNrKfG}8P7jY2ah=~789 zDLNWxXdQprKg#2_iPEaKvu*?y{MI(n<%|2*eIVpnI{{661C40G>_88SqkX8aG5;#; zr4yNQH|j#&;=2MCgGv`NB|vYmkw=utMxF)W88978nQ8#OnBUIlp`r*Qoto2_$z*b7R8c6_SAzqwx{s_T?1J#J~tnKaP z(DuN+Re&2x5ie;tr^6lzfAU1megT~dT8ZqKovuZ8&mX??{@jULTY1}TrGNV zp|=)&#7ho&8JE z)Qr03LrtBle$?DjIlC5s{_mlVhWHod_2GwYiBo>xkNz#EVj;lkRv02XIO8Qtoa!qi spjEGH!4pM0S1RB)$sy2l1W|6isz{Rb5Vbx;TmDq}!?@N4$Q1Mb2I8B!3jhEB literal 0 HcmV?d00001 diff --git a/nyash_client/src/test_data/create_test.sh b/nyash_client/src/test_data/create_test.sh new file mode 100644 index 0000000..c68c5bc --- /dev/null +++ b/nyash_client/src/test_data/create_test.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +truncate -s 255M test_btrfs.img +dd if=/dev/urandom of=master.key bs=32 count=1 + +cryptsetup luksFormat --type=luks2 --sector-size 512 --pbkdf=pbkdf2 --pbkdf-force-iterations=1000 --hash=sha256 --key-size=256 --cipher=aes-xts-plain64 --master-key-file ./test_master.key ./test.img + +cryptsetup luksFormat --type=luks2 --pbkdf=pbkdf2 --pbkdf-force-iterations=1000 --hash=sha256 --key-size=256 --cipher=aes-xts-plain64 ./luks-container.img + +sudo cryptsetup luksOpen ./test.img luks-container-crypt + +sudo mkfs.btrfs /dev/mapper/luks-container-crypt + +sudo dd if=/dev/mapper/luks-container-crypt of=./test_btrfs_luks_unencrypt.img bs=1M count=255 + +sudo cryptsetup close luks-container-crypt diff --git a/nyash_client/src/test_data/extract_encrypted_block.py b/nyash_client/src/test_data/extract_encrypted_block.py new file mode 100644 index 0000000..4181b19 --- /dev/null +++ b/nyash_client/src/test_data/extract_encrypted_block.py @@ -0,0 +1,44 @@ +from utils import read_metadata + + +# Init logger + +LUKS_FILE_NAME = "test.img" +KEY_FILE_NAME = "master.key" + + + + +def main(): + + metadat = read_metadata(LUKS_FILE_NAME) + print(f"metadata:\n{metadat}") + + + segments_offset_bytes = int(metadat["segments"]["0"]["offset"]) + superblock_start_bytes = 0x00010000 + superblock_start_sector = superblock_start_bytes//512 + magic_offset = 0x40 + superblock_lenght_bytes = 0x1000 + sector_size = 512 + + with open(LUKS_FILE_NAME, 'rb') as luks_file: + luks_file.seek(segments_offset_bytes) + enc_data = luks_file.read(16) + + + print("ENC DATA:") + print("[" + ",".join([format(a, 'd') for a in enc_data])+"]") + + print() + print("KEY DATA:") + with open(KEY_FILE_NAME, 'rb') as key_file: + key_data = key_file.read(32) + print("[" + ",".join([format(a, 'd') for a in key_data])+"]") + + + + + +if __name__ == '__main__': + main() diff --git a/nyash_client/src/test_data/utils.py b/nyash_client/src/test_data/utils.py new file mode 100644 index 0000000..8fd40fd --- /dev/null +++ b/nyash_client/src/test_data/utils.py @@ -0,0 +1,25 @@ +import subprocess +import json + +def read_metadata(file_name: str) -> dict: + #cryptsetup luksDump --dump-json-metadata /dev/loop0 + luks_cmd: list[str] = ["cryptsetup", "luksDump", "--dump-json-metadata", file_name] + + result = subprocess.run(luks_cmd, capture_output=True, encoding="UTF-8") + if result.returncode == 0 and result.stdout is not None: + metadata = json.loads(result.stdout) + return metadata + else: + raise Exception(f"Error executing 'cryptsetup' binary! {result.stderr}") + + +def read_encrypted_key(f_name: str, metadata: dict, keyslot: int) -> bytes: + stripes = metadata["keyslots"][str(keyslot)]["af"]["stripes"] + offset = int(metadata["keyslots"][str(keyslot)]["area"]["offset"]) + # size = int(metadata["keyslots"][str(keyslot)]["area"]["size"]) + key_size = metadata["keyslots"][str(keyslot)]["area"]["key_size"] + with open(f_name, 'rb') as luks_file: + luks_file.seek(offset) + data = luks_file.read(key_size*stripes) + return data + diff --git a/nyash_client/src/test_nums.rs b/nyash_client/src/test_nums.rs index d953976..9b16fa2 100644 --- a/nyash_client/src/test_nums.rs +++ b/nyash_client/src/test_nums.rs @@ -1,9 +1,6 @@ - - - #[cfg(test)] mod cl_num_utils_tests { use super::*; diff --git a/nyash_server/Cargo.lock b/nyash_server/Cargo.lock index 28dbf80..3d93879 100644 --- a/nyash_server/Cargo.lock +++ b/nyash_server/Cargo.lock @@ -581,15 +581,6 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" -[[package]] -name = "lock_api" -version = "0.4.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" -dependencies = [ - "scopeguard", -] - [[package]] name = "log" version = "0.4.29" @@ -662,29 +653,6 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" -[[package]] -name = "parking_lot" -version = "0.12.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-link", -] - [[package]] name = "percent-encoding" version = "2.3.2" @@ -888,15 +856,6 @@ dependencies = [ "libc", ] -[[package]] -name = "redox_syscall" -version = "0.5.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" -dependencies = [ - "bitflags", -] - [[package]] name = "regex" version = "1.12.3" @@ -939,12 +898,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - [[package]] name = "serde" version = "1.0.228" @@ -1092,7 +1045,6 @@ dependencies = [ "bytes", "libc", "mio", - "parking_lot", "pin-project-lite", "signal-hook-registry", "socket2", diff --git a/nyash_server/Cargo.toml b/nyash_server/Cargo.toml index b1dfedc..60e75ba 100644 --- a/nyash_server/Cargo.toml +++ b/nyash_server/Cargo.toml @@ -17,7 +17,7 @@ prost = "0.14.3" rand = "0.9.2" redb = "3.1.0" serde = { version = "1.0.228", features = ["derive"] } -tokio = { version = "1.49.0", features = ["full"] } +tokio = { version = "1.49.0", features = ["rt", "macros", "signal"] } tokio-tungstenite = "0.28.0" toml = "1.0.1" tonic = "0.14.5" diff --git a/nyash_server/src/database.rs b/nyash_server/src/database.rs index 8d29b32..1c4b319 100644 --- a/nyash_server/src/database.rs +++ b/nyash_server/src/database.rs @@ -10,7 +10,7 @@ use std::{path::Path, u64, u128}; // const RANGE_STATUS: MultimapTableDefinition = // MultimapTableDefinition::new("range_status"); -const JOBS_TABLE: TableDefinition = TableDefinition::new("jobs"); +const JOBS_TABLE: TableDefinition = TableDefinition::new("jobs"); const JOBS_FREE_IDS: TableDefinition = TableDefinition::new("jobs_free_ids"); const FOUND_KEYS_TABLE: TableDefinition<(u128, u128), u64> = TableDefinition::new("found_keys"); @@ -24,7 +24,7 @@ fn get_timestump() -> u64 { #[derive(Copy, Clone, Debug)] pub struct JobRecord { pub id: u64, - pub range_id: u16, + pub range_id: u32, pub tweak_key: u128, pub start_key: u128, pub len: u64, @@ -32,7 +32,7 @@ pub struct JobRecord { } impl JobRecord { - pub fn from_data(k: u64, v: &(u16, u128, u128, u64, u64)) -> Self { + pub fn from_data(k: u64, v: &(u32, u128, u128, u64, u64)) -> Self { Self { id: k, range_id: v.0, @@ -47,7 +47,7 @@ impl JobRecord { item: Result< ( redb::AccessGuard<'_, u64>, - redb::AccessGuard<'_, (u16, u128, u128, u64, u64)>, + redb::AccessGuard<'_, (u32, u128, u128, u64, u64)>, ), redb::StorageError, >, @@ -56,7 +56,7 @@ impl JobRecord { Ok(Self::from_data(ag.0.value(), &ag.1.value())) } - pub fn get_value(&self) -> (u16, u128, u128, u64, u64) { + pub fn get_value(&self) -> (u32, u128, u128, u64, u64) { return ( self.range_id, self.tweak_key, @@ -69,7 +69,7 @@ impl JobRecord { // search for abandoned job // returns first job older than 3 hours pub fn get_staled_job( - jobs_table: &mut redb::Table<'_, u64, (u16, u128, u128, u64, u64)>, + jobs_table: &mut redb::Table<'_, u64, (u32, u128, u128, u64, u64)>, timeout: u64, ) -> Result, redb::Error> { let current_time = get_timestump(); @@ -103,7 +103,7 @@ impl JobRecord { // What we want is to check max and min ids // and return id lower than min or grater than max fn get_new_job_id( - jobs_table: &redb::Table<'_, u64, (u16, u128, u128, u64, u64)>, + jobs_table: &redb::Table<'_, u64, (u32, u128, u128, u64, u64)>, ) -> Result { // If table is empty return 0 ID if jobs_table.len()? == 0 { @@ -132,9 +132,9 @@ impl JobRecord { } } -const RANGES: TableDefinition = TableDefinition::new("ranges"); -const RANGES_TO_USE: TableDefinition = TableDefinition::new("ranges_to_use"); - +const RANGES: TableDefinition = TableDefinition::new("ranges"); +const RANGES_TO_USE: TableDefinition = TableDefinition::new("ranges_to_use"); + // to not owerflow u128 we calculate it in range 0..max and then substruct // const SUB_DIV: u128 = 0x1000100010001000100010001; // TWEAKS_PER_RANGE: u128 = 5192296858534827628530496329220096 @@ -147,7 +147,7 @@ const TWEAKS_PER_RANGE_ID_VICE: u128 = TWEAKS_PER_RANGE - 1; #[derive(Copy, Clone, Debug)] struct RangeRecord { - id: u16, + id: u32, tweak_current: u128, tweak_end: u128, @@ -157,7 +157,7 @@ struct RangeRecord { impl RangeRecord { - fn from_value(k: u16, v: &(u128, u128, u128, u128)) -> Self { + fn from_value(k: u32, v: &(u128, u128, u128, u128)) -> Self { Self { id: k, tweak_current: v.0, @@ -167,7 +167,7 @@ impl RangeRecord { } } - fn new(id: u16) -> Self { + fn new(id: u32) -> Self { let tweak_start: u128 = id as u128 * TWEAKS_PER_RANGE; Self { @@ -259,8 +259,8 @@ impl RangeRecord { #[derive(Debug)] struct RangesTable<'a> { - ranges: redb::Table<'a, u16, (u128, u128, u128, u128)>, - ranges_to_use: redb::Table<'a, u16, ()>, + ranges: redb::Table<'a, u32, (u128, u128, u128, u128)>, + ranges_to_use: redb::Table<'a, u32, ()>, } impl<'a> RangesTable<'a> { @@ -281,7 +281,7 @@ impl<'a> RangesTable<'a> { }) } - fn get_range(&self, k: u16) -> Result, redb::Error> { + fn get_range(&self, k: u32) -> Result, redb::Error> { Ok(self .ranges .get(k)? @@ -501,30 +501,43 @@ pub fn db_get_progress(db: &Database) -> Result { fn db_init(db: &Database) -> Result<(), redb::Error> { - let transct: redb::WriteTransaction = db.begin_write()?; + println!("Start DB init."); + + let trx: redb::WriteTransaction = db.begin_write()?; { - let mut ranges = transct.open_table(RANGES)?; - let mut ranges_to_use = transct.open_table(RANGES_TO_USE)?; - - // create new ranges - for r_id in 0..=u16::MAX { - let r = RangeRecord::new(r_id); - ranges.insert(r_id, r.value())?; - ranges_to_use.insert(r_id, ())?; + let mut ranges = trx.open_table(RANGES)?; + let mut ranges_to_use = trx.open_table(RANGES_TO_USE)?; + for k in 0..=u16::MAX { + // create new ranges + let r = RangeRecord::new(k as u32); + ranges.insert(k as u32, r.value())?; + ranges_to_use.insert(k as u32, ())?; } + + println!("ensure that all tables is created"); + // ensure that all tables is created + trx.open_table(FOUND_KEYS_TABLE)?.len()?; + trx.open_table(JOBS_TABLE)?.len()?; + trx.open_table(JOBS_FREE_IDS)?.len()?; } - transct.commit()?; + println!("Commit transaction"); + trx.commit()?; + println!("Done!"); + Ok(()) } pub fn db_open(db_file_path: &Path) -> Result { let db = Database::create(db_file_path)?; - let trx = db.begin_read()?; + let trx = db.begin_write()?; let ranges_len = { + println!("Opening RANGES to get len"); let ranges_table = trx.open_table(RANGES)?; + println!("Try to get len"); ranges_table.len()? }; - trx.close()?; + println!("Commit transaction"); + trx.commit()?; if ranges_len == 0 { db_init(&db)?; } diff --git a/nyash_server/src/num_utils.rs b/nyash_server/src/num_utils.rs index b7dbb2f..1cbb560 100644 --- a/nyash_server/src/num_utils.rs +++ b/nyash_server/src/num_utils.rs @@ -4,15 +4,15 @@ // } -fn u128_to_u32arr(a: u128) -> [u32;4] { - let mut res = [0u32;4]; - let a_bytes = a.to_le_bytes(); - let chunks = a_bytes.as_chunks::<4>().0; - for i in 0..4 { - res[i] = u32::from_le_bytes(chunks[i]); - } - return res; -} +// fn u128_to_u32arr(a: u128) -> [u32;4] { +// let mut res = [0u32;4]; +// let a_bytes = a.to_le_bytes(); +// let chunks = a_bytes.as_chunks::<4>().0; +// for i in 0..4 { +// res[i] = u32::from_le_bytes(chunks[i]); +// } +// return res; +// } pub fn u128_to_u64arr(a: u128) -> [u64;2] { let mut res = [0u64;2]; @@ -32,143 +32,143 @@ pub fn u64arr_to_u128(a:(u64,u64)) -> u128 { return u128::from_le_bytes(bytes_data); } -pub fn add_u128_to_u256(a: &[u32; 8], b: u128) -> ([u32; 8], bool) { - let mut res: [u32; 8] = [0; 8]; - let mut carry = false; +// pub fn add_u128_to_u256(a: &[u32; 8], b: u128) -> ([u32; 8], bool) { +// let mut res: [u32; 8] = [0; 8]; +// let mut carry = false; - // convert b u128 value to bytes then to u32 and then add it to a - b.to_le_bytes() - .as_chunks::<4>() - .0 - .iter() - .enumerate() - .for_each(|(i, sl)| { - (res[i], carry) = a[i].carrying_add(u32::from_le_bytes(*sl), carry); - }); +// // convert b u128 value to bytes then to u32 and then add it to a +// b.to_le_bytes() +// .as_chunks::<4>() +// .0 +// .iter() +// .enumerate() +// .for_each(|(i, sl)| { +// (res[i], carry) = a[i].carrying_add(u32::from_le_bytes(*sl), carry); +// }); - // propagate carry till the end of a - for idx in 4..8 { - (res[idx], carry) = a[idx].carrying_add(0, carry); - } +// // propagate carry till the end of a +// for idx in 4..8 { +// (res[idx], carry) = a[idx].carrying_add(0, carry); +// } - return (res, carry); -} +// return (res, carry); +// } -fn add_u32_to_u256(a: &[u32; 8], b: u32) -> ([u32; 8], bool) { - let mut res: [u32; 8] = [0; 8]; - let mut carry = false; - (res[0], carry) = a[0].carrying_add(b, carry); +// fn add_u32_to_u256(a: &[u32; 8], b: u32) -> ([u32; 8], bool) { +// let mut res: [u32; 8] = [0; 8]; +// let mut carry = false; +// (res[0], carry) = a[0].carrying_add(b, carry); - for idx in 1..8 { - (res[idx], carry) = a[idx].carrying_add(0, carry); - } +// for idx in 1..8 { +// (res[idx], carry) = a[idx].carrying_add(0, carry); +// } - return (res, carry); -} +// return (res, carry); +// } -fn add_u32_to_u256_(a: &mut [u32; 8], b: u32) -> bool { - let mut carry = false; - (a[0], carry) = a[0].carrying_add(b, carry); +// fn add_u32_to_u256_(a: &mut [u32; 8], b: u32) -> bool { +// let mut carry = false; +// (a[0], carry) = a[0].carrying_add(b, carry); - for idx in 1..8 { - (a[idx], carry) = a[idx].carrying_add(0, carry); - } +// for idx in 1..8 { +// (a[idx], carry) = a[idx].carrying_add(0, carry); +// } - return carry; -} +// return carry; +// } -fn bytes_from_chars(chars_chunk: &[char]) -> [u8; 4] { - let mut res: [u8; 4] = [0; 4]; - let mut idx: usize = 0; - chars_chunk.chunks_exact(2).for_each(|b_c| { - if idx < 4 { - match u8::from_str_radix(&b_c.iter().collect::(), 16) { - Ok(n) => res[idx] = n, - Err(_) => (), - } - idx += 1; - } - }); - return res; -} +// fn bytes_from_chars(chars_chunk: &[char]) -> [u8; 4] { +// let mut res: [u8; 4] = [0; 4]; +// let mut idx: usize = 0; +// chars_chunk.chunks_exact(2).for_each(|b_c| { +// if idx < 4 { +// match u8::from_str_radix(&b_c.iter().collect::(), 16) { +// Ok(n) => res[idx] = n, +// Err(_) => (), +// } +// idx += 1; +// } +// }); +// return res; +// } -fn bignum_from_hex(hex: &str) -> [u32; 8] { - let mut res: [u32; 8] = [0; 8]; - let mut idx: usize = 0; - let chars_hex = hex.chars().collect::>(); - chars_hex - .chunks_exact(8) - .rev() - .map(|chunk| bytes_from_chars(chunk)) - .for_each(|b_arr| { - if idx < 8 { - res[idx] = u32::from_be_bytes(b_arr); - idx += 1; - } - }); - return res; -} +// fn bignum_from_hex(hex: &str) -> [u32; 8] { +// let mut res: [u32; 8] = [0; 8]; +// let mut idx: usize = 0; +// let chars_hex = hex.chars().collect::>(); +// chars_hex +// .chunks_exact(8) +// .rev() +// .map(|chunk| bytes_from_chars(chunk)) +// .for_each(|b_arr| { +// if idx < 8 { +// res[idx] = u32::from_be_bytes(b_arr); +// idx += 1; +// } +// }); +// return res; +// } -fn hex_fmt_byte(n: u32) -> String { - let res: String = n - .to_be_bytes() - .iter() - .map(|b| format!("{:02x}", b)) - .collect(); - return res; -} +// fn hex_fmt_byte(n: u32) -> String { +// let res: String = n +// .to_be_bytes() +// .iter() +// .map(|b| format!("{:02x}", b)) +// .collect(); +// return res; +// } -fn bignum_to_hex(a: &[u32; 8]) -> String { - let res: String = a - .iter() - .rev() - .map(|n| hex_fmt_byte(*n)) - .collect::>() - .join(""); - return res; -} +// fn bignum_to_hex(a: &[u32; 8]) -> String { +// let res: String = a +// .iter() +// .rev() +// .map(|n| hex_fmt_byte(*n)) +// .collect::>() +// .join(""); +// return res; +// } -#[cfg(test)] -mod num_utils_tests { - use super::*; +// #[cfg(test)] +// mod num_utils_tests { +// use super::*; - #[test] - fn test_add() { - use std::io::{BufRead, BufReader}; - use std::process::{Command, Stdio}; +// #[test] +// fn test_add() { +// use std::io::{BufRead, BufReader}; +// use std::process::{Command, Stdio}; - let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py"; - let mut child = Command::new(test_gen_cmd) - .stdout(Stdio::piped()) - .spawn() - .unwrap(); +// let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py"; +// let mut child = Command::new(test_gen_cmd) +// .stdout(Stdio::piped()) +// .spawn() +// .unwrap(); - let gen_stdout = child - .stdout - .take() - .ok_or("Failed to capture stdout") - .unwrap(); - let gen_reader = BufReader::new(gen_stdout); +// let gen_stdout = child +// .stdout +// .take() +// .ok_or("Failed to capture stdout") +// .unwrap(); +// let gen_reader = BufReader::new(gen_stdout); - for r_line in gen_reader.lines() { - let test_line: String = r_line.unwrap(); // Handle any I/O errors - let test_data_line = test_line.split(' ').collect::>(); - let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap(); - let t0 = bignum_from_hex(test_data_line[1]); +// for r_line in gen_reader.lines() { +// let test_line: String = r_line.unwrap(); // Handle any I/O errors +// let test_data_line = test_line.split(' ').collect::>(); +// let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap(); +// let t0 = bignum_from_hex(test_data_line[1]); - let t1_test = add_u32_to_u256(&t0, 1).0; - let t2_test = add_u32_to_u256(&t0, num_to_add).0; +// let t1_test = add_u32_to_u256(&t0, 1).0; +// let t2_test = add_u32_to_u256(&t0, num_to_add).0; - let res_actual = format!( - "{} {} {} {}", - num_to_add, - bignum_to_hex(&t0), - bignum_to_hex(&t1_test), - bignum_to_hex(&t2_test) - ); - assert_eq!(test_line, res_actual); - } +// let res_actual = format!( +// "{} {} {} {}", +// num_to_add, +// bignum_to_hex(&t0), +// bignum_to_hex(&t1_test), +// bignum_to_hex(&t2_test) +// ); +// assert_eq!(test_line, res_actual); +// } - let _ = child.wait().unwrap(); - } -} +// let _ = child.wait().unwrap(); +// } +// } diff --git a/nyash_server/src/launch.json b/nyash_server/src/old_launch.json similarity index 100% rename from nyash_server/src/launch.json rename to nyash_server/src/old_launch.json diff --git a/nyash_server/src/main.rs b/nyash_server/src/old_main.rs similarity index 100% rename from nyash_server/src/main.rs rename to nyash_server/src/old_main.rs diff --git a/nyash_server/src/server.rs b/nyash_server/src/server.rs index 9023fb9..1842d56 100644 --- a/nyash_server/src/server.rs +++ b/nyash_server/src/server.rs @@ -25,6 +25,31 @@ mod database; mod num_utils; mod config; +async fn shutdown_signal() { + let ctrl_c = async { + tokio::signal::ctrl_c() + .await + .expect("Failed to install Ctrl+C handler"); + }; + + #[cfg(unix)] + let terminate = async { + tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) + .expect("Failed to install SIGTERM handler") + .recv() + .await; + }; + + #[cfg(not(unix))] + let terminate = std::future::pending::<()>(); + + tokio::select! { + _ = ctrl_c => println!("Received Ctrl+C, initiating shutdown"), + _ = terminate => println!("Received SIGTERM, initiating shutdown"), + } +} + + fn work_rec_to_work_data(work_rec: database::JobRecord) -> WorkData { let tw_key = num_utils::u128_to_u64arr(work_rec.tweak_key); let st_key = num_utils::u128_to_u64arr(work_rec.start_key); @@ -42,8 +67,8 @@ fn work_rec_to_work_data(work_rec: database::JobRecord) -> WorkData { // Background task to update progress from DB -async fn update_service_progress(progress: Arc>, db: Arc) { - loop { +async fn update_service_progress(progress: Arc>, db: Arc, term: Arc>) { + while *term.read().await == false { tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; match database::db_get_progress(&db) { Ok(p) => { @@ -66,7 +91,11 @@ pub struct NyashService { #[tonic::async_trait] impl NyashLuks for NyashService { async fn request_work(&self, request: Request) -> Result, Status> { - println!("Got a request: {:?}", request); + let rem_addr = match request.remote_addr() { + Some(v) => v.to_string(), + None => "None".to_string() + }; + println!("Remoute: {}, request_work.", rem_addr); if *self.key_found.read().await == true { @@ -98,7 +127,12 @@ impl NyashLuks for NyashService { } async fn commit_work(&self, request: Request) -> Result, Status> { - println!("Got a request: {:?}", request); + let rem_addr = match request.remote_addr() { + Some(v) => v.to_string(), + None => "None".to_string() + }; + println!("Remoute: {}, commit_work.", rem_addr); + let work_commit = request.into_inner(); let put_k_no_err: bool = match work_commit.result { @@ -133,7 +167,12 @@ impl NyashLuks for NyashService { } } - async fn request_progress(&self, _request: Request) -> Result, Status> { + async fn request_progress(&self, request: Request) -> Result, Status> { + let rem_addr = match request.remote_addr() { + Some(v) => v.to_string(), + None => "None".to_string() + }; + println!("Remoute: {}, request_progress.", rem_addr); Ok(Response::new(ProgressReply { key_found: *self.key_found.read().await, progress: *self.progress.read().await, @@ -153,7 +192,7 @@ struct ProgArgs { -#[tokio::main] +#[tokio::main(flavor = "current_thread")] async fn main() -> Result<(), Box> { let args = ProgArgs::parse(); @@ -165,7 +204,9 @@ async fn main() -> Result<(), Box> { let db_file = format!("{}data.rdb",db_dir); let db_path = PathBuf::from_str(db_file.as_str()).unwrap(); - let db = database::db_open(&db_path).expect("Error Opening database"); + let mut db = database::db_open(&db_path).expect("Error Opening database"); + println!("Compact db"); + db.compact()?; // Get the address to bind to let addr = format!("{}:{}",server_config.bind_addr, server_config.listen_port); @@ -190,22 +231,33 @@ async fn main() -> Result<(), Box> { let shared_progress = Arc::new(AsyncRwLock::new(progress)); let shared_db = Arc::new(db); let shared_key_found = Arc::new(AsyncRwLock::new(false)); - + let shared_termination: Arc> = Arc::new(AsyncRwLock::new(false)); // let service_state = Arc::new(AsyncRwLock::new(ServiceState { // db: Arc::new(db), // key_found: false, // progress: progress, // })); + let term_clone = shared_termination.clone(); + let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel(); + tokio::spawn(async move { + shutdown_signal().await; + //signaling that we should stop + let mut guard = term_clone.write().await; + *guard = true; + let _ = shutdown_tx.send(()); + }); + // Spawn the periodic updater - tokio::spawn(update_service_progress(shared_progress.clone(), shared_db.clone())); + tokio::spawn(update_service_progress(shared_progress.clone(), shared_db.clone(), shared_termination.clone())); let nyash_service = NyashService{db:shared_db, progress:shared_progress, key_found:shared_key_found}; Server::builder() .add_service(NyashLuksServer::new(nyash_service)) - .serve(addr) + .serve_with_shutdown(addr, async { shutdown_rx.await.ok(); }) .await?; + Ok(()) }