Skip to content

Commit a270e2a

Browse files
author
Tobias Waurick
committed
refactor: openssl feature flag handling
1 parent 81d78d9 commit a270e2a

File tree

8 files changed

+21
-19
lines changed

8 files changed

+21
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Currently two crypto libraries are supported:
3030
- Per default the OpenSSL library is locally compiled and then statically linked. The build process requires a C compiler, `perl` (and `perl-core`), and `make`. For further options see the [openssl crate documentation](https://docs.rs/openssl/0.10.55/openssl/).
3131
- Compilation to Wasm32 is [not yet supported](https://github.com/sfackler/rust-openssl/issues/1016)
3232

33-
Both cannot be enabled at the same time, thus on conflict `sframe` fallsback to using `ring`.
33+
Both cannot be enabled at the same time, thus on conflict `sframe` issues a compiler error.
3434
## License
3535
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
3636

src/crypto/aead.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ mod test {
156156
decrypt_test_vector(CipherSuiteVariant::AesGcm256Sha512);
157157
}
158158

159-
#[cfg(not(feature = "ring"))]
159+
#[cfg(feature = "openssl")]
160160
mod aes_ctr {
161161
use crate::CipherSuiteVariant;
162162

src/crypto/cipher_suite.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
#[cfg_attr(test, derive(strum_macros::Display))]
99
pub enum CipherSuiteVariant {
1010
// /// counter mode is [not implemented in ring](https://github.com/briansmith/ring/issues/656)
11-
#[cfg(not(feature = "ring"))]
11+
#[cfg(feature = "openssl")]
1212
AesCtr128HmacSha256_80,
13-
#[cfg(not(feature = "ring"))]
13+
#[cfg(feature = "openssl")]
1414
AesCtr128HmacSha256_64,
15-
#[cfg(not(feature = "ring"))]
15+
#[cfg(feature = "openssl")]
1616
AesCtr128HmacSha256_32,
1717
/// encryption: AES GCM 128, key expansion: HKDF with SHA256
1818
AesGcm128Sha256,
@@ -32,7 +32,7 @@ pub struct CipherSuite {
3232
impl From<CipherSuiteVariant> for CipherSuite {
3333
fn from(variant: CipherSuiteVariant) -> Self {
3434
match variant {
35-
#[cfg(not(feature = "ring"))]
35+
#[cfg(feature = "openssl")]
3636
CipherSuiteVariant::AesCtr128HmacSha256_80 => CipherSuite {
3737
variant,
3838
hash_len: 32,
@@ -41,15 +41,15 @@ impl From<CipherSuiteVariant> for CipherSuite {
4141
auth_tag_len: 10,
4242
},
4343

44-
#[cfg(not(feature = "ring"))]
44+
#[cfg(feature = "openssl")]
4545
CipherSuiteVariant::AesCtr128HmacSha256_64 => CipherSuite {
4646
variant,
4747
hash_len: 32,
4848
key_len: 16,
4949
nonce_len: 12,
5050
auth_tag_len: 8,
5151
},
52-
#[cfg(not(feature = "ring"))]
52+
#[cfg(feature = "openssl")]
5353
CipherSuiteVariant::AesCtr128HmacSha256_32 => CipherSuite {
5454
variant,
5555
hash_len: 32,
@@ -76,10 +76,10 @@ impl From<CipherSuiteVariant> for CipherSuite {
7676
}
7777

7878
impl CipherSuite {
79-
#[allow(dead_code)]
79+
#[cfg(any(feature = "openssl", test))]
8080
pub(crate) fn is_ctr_mode(&self) -> bool {
8181
match self.variant {
82-
#[cfg(not(feature = "ring"))]
82+
#[cfg(feature = "openssl")]
8383
CipherSuiteVariant::AesCtr128HmacSha256_80
8484
| CipherSuiteVariant::AesCtr128HmacSha256_64
8585
| CipherSuiteVariant::AesCtr128HmacSha256_32 => true,

src/crypto/key_expansion.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ pub const SFRAME_HKDF_SALT: &[u8] = "SFrame10".as_bytes();
1414
pub const SFRAME_HKDF_KEY_EXPAND_INFO: &[u8] = "key".as_bytes();
1515
pub const SFRAME_HDKF_SALT_EXPAND_INFO: &[u8] = "salt".as_bytes();
1616

17-
#[cfg(not(feature = "ring"))]
17+
#[cfg(feature = "openssl")]
1818
pub const SFRAME_HKDF_SUB_SALT: &[u8] = "SFrame10 AES CTR AEAD".as_bytes();
19-
#[cfg(not(feature = "ring"))]
19+
#[cfg(feature = "openssl")]
2020
pub const SFRAME_HKDF_SUB_ENC_EXPAND_INFO: &[u8] = "enc".as_bytes();
21-
#[cfg(not(feature = "ring"))]
21+
#[cfg(feature = "openssl")]
2222
pub const SFRAME_HDKF_SUB_AUTH_EXPAND_INFO: &[u8] = "auth".as_bytes();
2323

2424
#[cfg(test)]
@@ -50,7 +50,7 @@ mod test {
5050
derive_correct_base_keys(CipherSuiteVariant::AesGcm256Sha512);
5151
}
5252

53-
#[cfg(not(feature = "ring"))]
53+
#[cfg(feature = "openssl")]
5454
mod aes_ctr {
5555
use super::*;
5656

src/crypto/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if #[cfg(all(not(feature = "openssl"), feature = "ring"))]{
1313
else if #[cfg(all(feature = "openssl", not(feature = "ring")))] {
1414
mod openssl;
1515
} else {
16-
// fallback to ring
16+
compile_error!("Cannot configure multiple crypto backends at the same time.");
1717
mod ring;
1818
}
1919
}

src/crypto/ring/key_expansion.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ impl KeyExpansion for Secret {
2424
let key = expand_key(&prk, SFRAME_HKDF_KEY_EXPAND_INFO, cipher_suite.key_len)?;
2525
let salt = expand_key(&prk, SFRAME_HDKF_SALT_EXPAND_INFO, cipher_suite.nonce_len)?;
2626

27-
Ok(Secret { key, salt })
27+
Ok(Secret {
28+
key,
29+
salt,
30+
auth: None,
31+
})
2832
}
2933
}
3034

src/crypto/secret.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::header::FrameCount;
33
pub struct Secret {
44
pub key: Vec<u8>,
55
pub salt: Vec<u8>,
6-
#[cfg(not(feature = "ring"))]
76
pub auth: Option<Vec<u8>>,
87
}
98

@@ -27,7 +26,6 @@ impl Secret {
2726
Secret {
2827
key: test_vector.key.clone(),
2928
salt: test_vector.salt.clone(),
30-
#[cfg(not(feature = "ring"))]
3129
auth: None,
3230
}
3331
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! # Optional features
99
//!
1010
//! Using optional features `sframe` allows to configure different crypto libraries.
11-
//! Be aware that those features are mutually exlusive, if multiple `sframe` falls back to using `ring`.
11+
//! Be aware that those features are mutually exlusive, if multiple are configured `sframe` issues a compiler error.
1212
//!
1313
//! - **`ring`** *(enabled by default)* — Uses the [ring](https://crates.io/crates/ring) library which allows compilation to Wasm32.
1414
//! AES-CTR mode ciphers are not supported.

0 commit comments

Comments
 (0)