-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from InfoHunter/crypto
Support cryptography module
- Loading branch information
Showing
18 changed files
with
1,742 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,52 @@ | ||
use std::env; | ||
|
||
// This is not going to happen any more since we have a default feature definition in Cargo.toml | ||
//#[cfg(not(any(feature = "crypto_adaptor_openssl", feature = "crypto_adaptor_tongsuo")))] | ||
//compile_error! { | ||
// r#" | ||
// No cryptography adaptor is enabled! | ||
// | ||
// In RustyVault, the real cryptographic operations are done via "crypto_adaptor"s. | ||
// | ||
// A crypto adaptor is a module that conveys and translates high level cryptography | ||
// operations like encryption, signing into the APIs provided by underlying cryptography | ||
// libraries such as OpenSSL, Tongsuo and so forth. | ||
// | ||
// At current stage, only one crypto_adaptor can be enabled at compilation phase and later | ||
// be used at run-time. "crypto_adaptor"s are configured as 'feature's in the Cargo context. | ||
// | ||
// Currently, the supported feature names of crypto adaptors are as follows, you can enable | ||
// them by adding one '--features crypto_adaptor_name' option when running "cargo build": | ||
// 1. the OpenSSL adaptor: crypto_adaptor_openssl | ||
// 2. the Tongsuo adaptor: crypto_adaptor_tongsuo | ||
// "# | ||
//} | ||
|
||
#[cfg(all(feature = "crypto_adaptor_openssl", feature = "crypto_adaptor_tongsuo"))] | ||
compile_error! { | ||
r#" | ||
Only one cryptography adapator can be enabled! | ||
In RustyVault, the real cryptographic operations are done via "crypto_adaptor"s. | ||
A crypto adaptor is a module that conveys and translates high level cryptography | ||
operations like encryption, signing into the APIs provided by underlying cryptography | ||
libraries such as OpenSSL, Tongsuo and so forth. | ||
At current stage, only one crypto_adaptor can be enabled at compilation phase and later | ||
be used at run-time. "crypto_adaptor"s are configured as 'feature's in the Cargo context. | ||
Currently, the supported feature names of crypto adaptors are as follows, you can enable | ||
them by adding one '--features crypto_adaptor_name' option when running "cargo build": | ||
1. the OpenSSL adaptor: crypto_adaptor_openssl | ||
2. the Tongsuo adaptor: crypto_adaptor_tongsuo | ||
"# | ||
} | ||
|
||
fn main() { | ||
if let Ok(_) = env::var("DEP_OPENSSL_TONGSUO") { | ||
println!("cargo:rustc-cfg=tongsuo"); | ||
} else if cfg!(feature = "crypto_adaptor_tongsuo") { | ||
println!("cargo:rustc-cfg=tongsuo"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# RustyVault Crypto Adaptor | ||
|
||
In RustyVault, we provide a mechanism for the users to build with selectable underlying cryptography libraries. This is the "crypto adaptor" mechanism. | ||
|
||
Currently, only two adaptors are supported: | ||
|
||
* OpenSSL crypto adaptor | ||
* Tongsuo crypto adaptor | ||
|
||
## The OpenSSL Crypto Adaptor | ||
|
||
The following steps require a properly installed OpenSSL library. There are many ways of installing an OpenSSL on various platforms, so in this docuemnt we don't discuss that part. | ||
|
||
The OpenSSL crypto adaptor is configured by default in RustyVault, so you can simply build RustyVault to enable it: | ||
|
||
~~~ | ||
cargo build | ||
~~~ | ||
|
||
Otherwise if you want to explicitly configure it, you can still use something like: | ||
|
||
~~~ | ||
cargo build --features crypto_adaptor_openssl | ||
~~~ | ||
|
||
But this is not necessary. | ||
|
||
## The Tongsuo Crypto Adaptor | ||
|
||
Tongsuo is a fork of OpenSSL aiming to have a better support on Chinese cryptography algorithms and standards. To use Tongsuo as the cryptography functionality provider in RustyVault, typically you need to build RustyVault as follows. | ||
|
||
### Download and Install Tongsuo | ||
|
||
Firstly, you need to have a copy of Tongsuo code and successfully build it into libraires and finally install it into somewhere in your machine. | ||
|
||
Go to [https://tongsuo.net/docs/compilation/compile-and-install](https://tongsuo.net/docs/compilation/compile-and-install) for more detailed information. | ||
|
||
### Configure RustyVault to use Tongsuo | ||
|
||
RustyVault uses rust-tongsuo crate to call C APIs provided by Tongsuo. So we need to configure Cargo to use it, let's assume Tongsuo is successfully installed to `/path/to/tongsuo` directory: | ||
|
||
~~~ | ||
OPENSSL_DIR=/path/to/tongsuo cargo build \ | ||
--features crypto_adaptor_tongsuo \ | ||
--no-default-features \ | ||
--config 'patch.crates-io.openssl.git="https://github.com/Tongsuo-Project/rust-tongsuo.git"' \ | ||
--config 'patch.crates-io.openssl-sys.git="https://github.com/Tongsuo-Project/rust-tongsuo.git"' | ||
~~~ | ||
|
||
Furthermore, if you choose to use a local copy of rust-tongsuo crate, you can use the file path form as well. Assume the local rust-tongsuo crate is located in `/path/to/rust-tongsuo` directory: | ||
|
||
~~~ | ||
OPENSSL_DIR=/path/to/tongsuo cargo build \ | ||
--features crypto_adaptor_tongsuo \ | ||
--no-default-features \ | ||
--config 'patch.crates-io.openssl.path="/path/to/rust-tongsuo/openssl"' \ | ||
--config 'patch.crates-io.openssl-sys.path="/path/to/rust-tongsuo/openssl-sys"' | ||
~~~ | ||
|
||
### The `LD_LIBRARY_PATH` Variable | ||
|
||
If you are using Linux, then you may need to specify which path for RustyVault to look for the Tongsuo libraries. There are many ways of having this done, but in this document we demonstrate with the global environment variable way. | ||
|
||
~~~ | ||
export LD_LIBRARY_PATH=/path/to/tongsuol/lib | ||
~~~ | ||
|
||
Then you can run RustyVault smoothly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.