Skip to content

Commit 49b786d

Browse files
committed
don't use cpuid-bool in fortanix sgx target
1 parent afc167f commit 49b786d

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

sha2/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ opaque-debug = "0.3"
2121
cfg-if = "0.1"
2222
sha2-asm = { version = "0.5", optional = true }
2323

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

2727
[target.'cfg(all(target_arch = "aarch64", target_os = "linux"))'.dependencies]

sha2/src/sha256/x86.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![allow(clippy::many_single_char_names)]
22

3-
#[cfg(target_arch = "x86_64")]
4-
use core::arch::x86_64::*;
53
#[cfg(target_arch = "x86")]
64
use core::arch::x86::*;
5+
#[cfg(target_arch = "x86_64")]
6+
use core::arch::x86_64::*;
77

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

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

98+
#[cfg(feature = "cpuid_bool")]
9899
pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
99100
// TODO: Replace with https://github.com/rust-lang/rfcs/pull/2725
100101
// after stabilization
@@ -106,3 +107,19 @@ pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
106107
super::soft::compress(state, blocks);
107108
}
108109
}
110+
111+
/// Use `is_x86_feature_detected` in sgx environment in the future
112+
#[cfg(not(feature = "cpuid_bool"))]
113+
pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
114+
if core::detect::is_target_feature_detected("sha")
115+
&& core::detect::is_target_feature_detected("sse2")
116+
&& core::detect::is_target_feature_detected("ssse3")
117+
&& core::detect::is_target_feature_detected("sse4.1")
118+
{
119+
unsafe {
120+
digest_blocks(state, blocks);
121+
}
122+
} else {
123+
super::soft::compress(state, blocks);
124+
}
125+
}

0 commit comments

Comments
 (0)