@@ -59,8 +59,10 @@ pub trait Sha256 {
5959}
6060
6161/// Implementation of SHA256 using the `ring` crate (fastest on CPUs without SHA extensions).
62+ #[ cfg( feature = "ring" ) ]
6263pub struct RingImpl ;
6364
65+ #[ cfg( feature = "ring" ) ]
6466impl Sha256Context for ring:: digest:: Context {
6567 fn new ( ) -> Self {
6668 Self :: new ( & ring:: digest:: SHA256 )
@@ -77,6 +79,7 @@ impl Sha256Context for ring::digest::Context {
7779 }
7880}
7981
82+ #[ cfg( feature = "ring" ) ]
8083impl Sha256 for RingImpl {
8184 type Context = ring:: digest:: Context ;
8285
@@ -97,6 +100,7 @@ impl Sha256 for RingImpl {
97100pub enum DynamicImpl {
98101 #[ cfg( target_arch = "x86_64" ) ]
99102 Sha2 ,
103+ #[ cfg( feature = "ring" ) ]
100104 Ring ,
101105}
102106
@@ -119,15 +123,27 @@ impl DynamicImpl {
119123 /// Choose the best available implementation based on the currently executing CPU.
120124 #[ inline( always) ]
121125 pub fn best ( ) -> Self {
122- #[ cfg( target_arch = "x86_64" ) ]
126+ #[ cfg( all( not( feature = "ring" ) , not( target_arch = "x86_64" ) ) ) ]
127+ {
128+ compile_error ! ( "Ring must be enabled on non-x86_64 architectures" ) ;
129+ }
130+
131+ #[ cfg( all( not( feature = "ring" ) , target_arch = "x86_64" ) ) ]
132+ {
133+ Self :: Sha2
134+ }
135+
136+ #[ cfg( all( feature = "ring" , target_arch = "x86_64" ) ) ]
123137 if have_sha_extensions ( ) {
124138 Self :: Sha2
125139 } else {
126140 Self :: Ring
127141 }
128142
129- #[ cfg( not( target_arch = "x86_64" ) ) ]
130- Self :: Ring
143+ #[ cfg( all( feature = "ring" , not( target_arch = "x86_64" ) ) ) ]
144+ {
145+ Self :: Ring
146+ }
131147 }
132148}
133149
@@ -139,6 +155,7 @@ impl Sha256 for DynamicImpl {
139155 match self {
140156 #[ cfg( target_arch = "x86_64" ) ]
141157 Self :: Sha2 => Sha2CrateImpl . hash ( input) ,
158+ #[ cfg( feature = "ring" ) ]
142159 Self :: Ring => RingImpl . hash ( input) ,
143160 }
144161 }
@@ -148,6 +165,7 @@ impl Sha256 for DynamicImpl {
148165 match self {
149166 #[ cfg( target_arch = "x86_64" ) ]
150167 Self :: Sha2 => Sha2CrateImpl . hash_fixed ( input) ,
168+ #[ cfg( feature = "ring" ) ]
151169 Self :: Ring => RingImpl . hash_fixed ( input) ,
152170 }
153171 }
@@ -159,6 +177,7 @@ impl Sha256 for DynamicImpl {
159177pub enum DynamicContext {
160178 #[ cfg( target_arch = "x86_64" ) ]
161179 Sha2 ( sha2:: Sha256 ) ,
180+ #[ cfg( feature = "ring" ) ]
162181 Ring ( ring:: digest:: Context ) ,
163182}
164183
@@ -167,6 +186,7 @@ impl Sha256Context for DynamicContext {
167186 match DynamicImpl :: best ( ) {
168187 #[ cfg( target_arch = "x86_64" ) ]
169188 DynamicImpl :: Sha2 => Self :: Sha2 ( Sha256Context :: new ( ) ) ,
189+ #[ cfg( feature = "ring" ) ]
170190 DynamicImpl :: Ring => Self :: Ring ( Sha256Context :: new ( ) ) ,
171191 }
172192 }
@@ -175,6 +195,7 @@ impl Sha256Context for DynamicContext {
175195 match self {
176196 #[ cfg( target_arch = "x86_64" ) ]
177197 Self :: Sha2 ( ctxt) => Sha256Context :: update ( ctxt, bytes) ,
198+ #[ cfg( feature = "ring" ) ]
178199 Self :: Ring ( ctxt) => Sha256Context :: update ( ctxt, bytes) ,
179200 }
180201 }
@@ -183,6 +204,7 @@ impl Sha256Context for DynamicContext {
183204 match self {
184205 #[ cfg( target_arch = "x86_64" ) ]
185206 Self :: Sha2 ( ctxt) => Sha256Context :: finalize ( ctxt) ,
207+ #[ cfg( feature = "ring" ) ]
186208 Self :: Ring ( ctxt) => Sha256Context :: finalize ( ctxt) ,
187209 }
188210 }
0 commit comments