Skip to content

Commit

Permalink
don't use cpuid-bool in fortanix sgx target
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Jul 14, 2020
1 parent afc167f commit 49b786d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sha2/Cargo.toml
Expand Up @@ -21,7 +21,7 @@ opaque-debug = "0.3"
cfg-if = "0.1"
sha2-asm = { version = "0.5", optional = true }

[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dependencies]
[target.'cfg(all(any(target_arch = "x86", target_arch = "x86_64"), not(target_env = "sgx")))'.dependencies]
cpuid-bool = "0.1"

[target.'cfg(all(target_arch = "aarch64", target_os = "linux"))'.dependencies]
Expand Down
23 changes: 20 additions & 3 deletions sha2/src/sha256/x86.rs
@@ -1,9 +1,9 @@
#![allow(clippy::many_single_char_names)]

#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;

unsafe fn schedule(v0: __m128i, v1: __m128i, v2: __m128i, v3: __m128i) -> __m128i {
let t1 = _mm_sha256msg1_epu32(v0, v1);
Expand Down Expand Up @@ -61,7 +61,7 @@ unsafe fn digest_blocks(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
let mut w0 = _mm_shuffle_epi8(_mm_loadu_si128(data_ptr.add(0)), MASK);
let mut w1 = _mm_shuffle_epi8(_mm_loadu_si128(data_ptr.add(1)), MASK);
let mut w2 = _mm_shuffle_epi8(_mm_loadu_si128(data_ptr.add(2)), MASK);
let mut w3 = _mm_shuffle_epi8( _mm_loadu_si128(data_ptr.add(3)), MASK);
let mut w3 = _mm_shuffle_epi8(_mm_loadu_si128(data_ptr.add(3)), MASK);
let mut w4;

rounds4!(abef, cdgh, w0, 0);
Expand Down Expand Up @@ -95,6 +95,7 @@ unsafe fn digest_blocks(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
_mm_storeu_si128(state_ptr_mut.add(1), hgef);
}

#[cfg(feature = "cpuid_bool")]
pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
// TODO: Replace with https://github.com/rust-lang/rfcs/pull/2725
// after stabilization
Expand All @@ -106,3 +107,19 @@ pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
super::soft::compress(state, blocks);
}
}

/// Use `is_x86_feature_detected` in sgx environment in the future
#[cfg(not(feature = "cpuid_bool"))]
pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
if core::detect::is_target_feature_detected("sha")
&& core::detect::is_target_feature_detected("sse2")
&& core::detect::is_target_feature_detected("ssse3")
&& core::detect::is_target_feature_detected("sse4.1")
{
unsafe {
digest_blocks(state, blocks);
}
} else {
super::soft::compress(state, blocks);
}
}

0 comments on commit 49b786d

Please sign in to comment.