|
Name |
|
Date |
Size |
#Lines |
LOC |
| .. | | - | - |
| array_ref/ | | 03-May-2024 | - | 55 | 31 |
| array_view/ | | 03-May-2024 | - | 142 | 95 |
| ldt/ | | 03-May-2024 | - | 10,186 | 9,641 |
| ldt_np_adv/ | | 03-May-2024 | - | 10,494 | 10,172 |
| ldt_np_adv_ffi/ | | 03-May-2024 | - | 1,708 | 1,370 |
| ldt_np_adv_ffi_fuzz/ | | 03-May-2024 | - | 289 | 186 |
| ldt_np_c_sample/ | | 03-May-2024 | - | 667 | 483 |
| ldt_tbc/ | | 03-May-2024 | - | 112 | 57 |
| np_hkdf/ | | 03-May-2024 | - | 28,627 | 28,442 |
| rand_ext/ | | 03-May-2024 | - | 78 | 46 |
| test_helper/ | | 03-May-2024 | - | 76 | 41 |
| xts_aes/ | | 03-May-2024 | - | 34,957 | 26,485 |
| CMakeLists.txt | D | 03-May-2024 | 1.2 KiB | 40 | 30 |
| README.md | D | 03-May-2024 | 8.2 KiB | 242 | 174 |
README.md
1# What is this?
2
3Implementations of XTS and LDT for Nearby Presence "v0" advertisements.
4
5See the appendix below for more details on XTS and LDT.
6
7## Project structure
8*Note all new crates follow the convention of using underscore `_` instead of hyphen `-` in crate names
9
10### `ldt`
11
12An implementation
13of [`LDT`](https://luca-giuzzi.unibs.it/corsi/Support/papers-cryptography/1619-2007-NIST-Submission.pdf)
14 which can use `xts-aes` as its tweakable block cipher.
15
16 ### `ldt_tbc`
17
18The Tweakable Block Cipher traits for use in LDT. These traits have implementations in the `xts_aes`
19
20### `ldt_np_adv`
21
22Higher-level wrapper around the core LDT algorithm that does key derivation and
23payload validation the way Nearby Presence advertisements need.
24
25### `ldt_np_adv_ffi`
26
27C API for rust library, currently exposes C/C++ clients the needed API's to use the NP specific LDT rust implementation.
28For an example of how to integrate with these API's see program in `ldt_np_c_sample`
29
30### `ldt_np_c_sample`
31
32Sample c program which provides its own OpenSSL based AES implementation to encrypt data through the LDT rust implementation
33An example of how to interface with the `ldt_np_adv_ffi` API's
34
35### `np_hkdf`
36
37The Key Derivation functions used for creating keys used by nearby presence from a key_seed
38
39### `xts_aes`
40
41An implementation
42of [`XTS-AES`](https://luca-giuzzi.unibs.it/corsi/Support/papers-cryptography/1619-2007-NIST-Submission.pdf)
43
44
45## Setup for MacOS local development
46
47Dependencies:
48
49```
50brew install protobuf rapidjson google-benchmark
51```
52
53We depend on OpenSSL of version at least 3.0.5 being installed on your machine to build the fuzzers, for macOS run:
54
55```
56brew install openssl@3
57```
58
59Your build system may still be picking up an older version so you will have to symlink to the brew installed version:
60```
61brew link --force openssl
62```
63
64The in-box version of Clang which comes from XCode developer tools does not have a fuzzer runtime so we will have to use our own
65```
66brew install llvm
67```
68then to override the default version it needs to come before it in $PATH. first find your path:
69```
70$(brew --prefix llvm)/bin
71```
72then add this to the beginning of your path
73```
74echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.bash_profile
75export PATH="/opt/homebrew/opt/llvm/bin:$PATH"
76```
77verify success with:
78```
79clang --version
80```
81it should display the path to the homebrew version and not the xcode version.
82
83Some other dependencies you may need include:
84```
85brew install ninja bindgen
86```
87
88## Examples
89
90Examples use [clap](https://docs.rs/clap/latest/clap/) for nice CLIs, so try
91running with `--help` to see all args.
92
93*Note:* the examples are in the `ldt` crate, so `cd` into that first.
94
95### `ldt_prp`
96
97Confirm that LDT is, in fact, behaving as a PRP. That is, flipping one bit in
98the ciphertext is on average going to flip half of the bits in the decrypted
99plaintext, and that a change to the first `n` bytes of plaintext is increasingly
100likely to be detected as `n` increases.
101
102```
103cargo run --release --example ldt_prp -- \
104 --trials 1000000 \
105 --check-leading-bytes 16
106```
107
108### `ldt_benchmark`
109
110For interactive exploration of LDT performance looking for a needle in a
111haystack of ciphertexts.
112
113```
114cargo run --release --example ldt_benchmark -- \
115 --trials 500 \
116 --keys 1000
117```
118
119### `ldt_np_c_sample`
120From the root directory run the following commands to build and run the C sample.
121
122```
123mkdir -p cmake-build && cd cmake-build
124cmake ..
125make
126./ldt_np_c_sample/sample
127```
128
129### `ldt_np_c_sample/tests`
130Test cases for the ldt_np_adv_ffi C API which are built alongside the sample, use the following commands to run the tests, from root of repo:
131
132```
133mkdir -p cmake-build && cd cmake-build
134cmake .. -DENABLE_TESTS=TRUE
135make
136cd ldt_np_c_sample/tests && ctest
137```
138you can then view the output of the tests in `ldt_np_c_sample/tests/Testing/Temporary/LastTest.log`
139
140To run the benchmarks:
141
142`ldt_np_c_sample/tests/benchmarks`
143
144## Fuzzing
145
146To build all the fuzzers, run `scripts/build-fuzzers.sh`.
147
148### Rust
149
150Crates with fuzzers: `ldt`, `ldt_np_adv`, `xts_aes`
151
152- `cd` to a crate's directory
153- `cargo fuzz list` to list available fuzzers
154- `cargo fuzz run [fuzzer name]` to run a fuzzer
155
156### C
157
158Build w/ cmake as per `ldt_np_c_sample` instructions. Fuzzers will be in `np_ffi_fuzz` in the build directory.
159
160## Cross-compilation for Android
161
162- Add the 64bit ARM target to the stable and nightly toolchains:
163 - `rustup target add aarch64-linux-android`
164 - `rustup target add aarch64-linux-android --toolchain nightly`
165
166- Install the v22 NDK that still links against `libgcc` the way rust's stdlib
167 expects.
168 - Newer NDKs use `libunwind` instead, which can be used just fine if you
169 build your own rust stdlib, but for our purposes there's no problem with
170 just using NDK 22
171 - `./sdkmanager --install platform-tools 'ndk;22.0.7026061'`
172
173- Configure the linker used for the ARMv8 Android target to be the NDK's linker.
174 - `export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$ANDROID_HOME/ndk/22.0.7026061/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang`
175 - `export PKG_CONFIG_SYSROOT_DIR=$ANDROID_HOME/ndk/22.0.7026061/toolchains/llvm/prebuilt/linux-x86_64/sysroot`
176
177- See if everything builds, using the nightly toolchain for the moment to
178 convince the `aes` crate to use intrinsics on aarch64
179 - `cargo +nightly build --workspace --all-targets --target aarch64-linux-android`
180 - `cargo +nightly bench --workspace --no-run --target aarch64-linux-android`
181
182- Prepare a place for the benchmark to be on the phone
183
184 - `adb shell`
185 - then
186 - `mkdir -p /data/local/tmp/np && cd /data/local/tmp/np`
187 - Leave the shell on the phone open so you can use it to run the benchmark.
188
189- Find the benchmark binary in the build products
190
191 - Use whatever directory you configured as the `target-dir` in
192 `.cargo/config.toml` initially, and look for the file without the
193 trailing `.d`.
194 - `find TARGET_DIR -name 'ldt_scan*' | grep android`
195 - Copy the file to the phone
196 - `adb push FILE_FOUND_ABOVE /data/local/tmp/np/`
197
198- In your `adb shell`, run the benchmark
199
200 - `./ldt_scan-... --bench`
201### Building min-sized release cross-compiled for Android
202- Copy and paste the following into your `~/.cargo/config.toml`, replacing with a path to your NDK and Host OS
203```
204[target.aarch64-linux-android]
205# Replace this with a path to your ndk version and the prebuilt toolchain for your Host OS
206linker = "Library/Android/sdk/ndk/23.2.8568313/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang"
207```
208- then run:
209`cargo +nightly build -Z build-std=core,alloc -Z build-std-features=panic_immediate_abort --target aarch64-linux-android --profile release-min-size`
210
211## Appendix
212
213### XTS
214
215XTS-AES has a NIST
216spec: [The XTS-AES Tweakable Block Cipher - An Extract from IEEE Std 1619-2007](https://luca-giuzzi.unibs.it/corsi/Support/papers-cryptography/1619-2007-NIST-Submission.pdf)
217
218XTS is a scheme for turning a block cipher (AES in this case) into a _tweakable_
219block cipher. Tweakable block ciphers incorporate a _tweak_ which is cheaper to
220change than the key, with the assumption being that the tweak will change with
221each block or sequence of blocks. XTS-AES in particular is used in disk
222encryption, where the sector number or the like might be incorporated into the
223tweak to prevent the same data in different places on the disk being encrypted
224into the same ciphertext.
225
226### LDT
227
228LDT is the current state of the art in length
229doublers: [Efficient Length Doubling From Tweakable Block Ciphers](https://eprint.iacr.org/2017/841.pdf)
230. It builds on top of a tweakable block cipher, which is why we also have an XTS
231implementation.
232
233A length doubler is a way of adapting a block cipher to act as a secure PRP (
234pseudo random permutation) on data of lengths in `[block size, 2 * block size)`.
235For comparison, block ciphers act as PRPs on one block at a time rather than the
236whole message. Wide block modes would also work, but have higher overhead.
237
238We use a length doubler in Nearby Presence so that changing any ciphertext bit
239should flip each bit in the decrypted plaintext with 50% probability for each
240bit, making it possible to detect changes anywhere because it is very unlikely
241for none of the bit flips to affect the metadata key (which has a known digest).
242