@@ -3,25 +3,10 @@ use openssl::derive::Deriver;
33use openssl:: ec:: { EcGroup , EcKey , EcPoint , PointConversionForm } ;
44use openssl:: error:: ErrorStack ;
55use openssl:: nid:: Nid ;
6- #[ cfg( not( feature = "fips" ) ) ]
7- use openssl:: pkey:: Id ;
86use openssl:: pkey:: { PKey , Private , Public } ;
97use rustls:: crypto:: { ActiveKeyExchange , SharedSecret , SupportedKxGroup } ;
108use rustls:: { Error , NamedGroup } ;
119
12- /// [Supported KeyExchange groups](SupportedKxGroup).
13- /// * [SECP384R1]
14- /// * [SECP256R1]
15- /// * [X25519]
16- ///
17- /// If the `fips` feature is enabled, only [SECP384R1] and [SECP256R1] are available.
18- pub const ALL_KX_GROUPS : & [ & dyn SupportedKxGroup ] = & [
19- SECP256R1 ,
20- SECP384R1 ,
21- #[ cfg( not( feature = "fips" ) ) ]
22- X25519 ,
23- ] ;
24-
2510/// `KXGroup`'s that use `openssl::ec` module with Nid's for key exchange.
2611#[ derive( Debug ) ]
2712struct EcKxGroup {
@@ -36,21 +21,6 @@ struct EcKeyExchange {
3621 pub_key : Vec < u8 > ,
3722}
3823
39- #[ cfg( not( feature = "fips" ) ) ]
40- /// `KXGroup`` for X25519
41- #[ derive( Debug ) ]
42- struct X25519KxGroup { }
43-
44- #[ cfg( not( feature = "fips" ) ) ]
45- #[ derive( Debug ) ]
46- struct X25519KeyExchange {
47- private_key : PKey < Private > ,
48- public_key : Vec < u8 > ,
49- }
50-
51- #[ cfg( not( feature = "fips" ) ) ]
52- /// X25519 key exchange group as registered with [IANA](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8).
53- pub const X25519 : & dyn SupportedKxGroup = & X25519KxGroup { } ;
5424/// secp256r1 key exchange group as registered with [IANA](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8)
5525pub const SECP256R1 : & dyn SupportedKxGroup = & EcKxGroup {
5626 name : NamedGroup :: secp256r1,
@@ -132,67 +102,22 @@ impl ActiveKeyExchange for EcKeyExchange {
132102 }
133103}
134104
135- #[ cfg( not( feature = "fips" ) ) ]
136- impl SupportedKxGroup for X25519KxGroup {
137- fn start ( & self ) -> Result < Box < dyn ActiveKeyExchange > , Error > {
138- PKey :: generate_x25519 ( )
139- . and_then ( |private_key| {
140- let public_key = private_key. raw_public_key ( ) ?;
141- Ok ( Box :: new ( X25519KeyExchange {
142- private_key,
143- public_key,
144- } ) as Box < dyn ActiveKeyExchange > )
145- } )
146- . map_err ( |e| Error :: General ( format ! ( "OpenSSL error: {e}" ) ) )
147- }
148-
149- fn name ( & self ) -> NamedGroup {
150- NamedGroup :: X25519
151- }
152- }
153-
154- #[ cfg( not( feature = "fips" ) ) ]
155- impl ActiveKeyExchange for X25519KeyExchange {
156- fn complete ( self : Box < Self > , peer_pub_key : & [ u8 ] ) -> Result < SharedSecret , Error > {
157- PKey :: public_key_from_raw_bytes ( peer_pub_key, Id :: X25519 )
158- . and_then ( |peer_pub_key| {
159- let mut deriver = Deriver :: new ( & self . private_key ) ?;
160- deriver. set_peer ( & peer_pub_key) ?;
161- let secret = deriver. derive_to_vec ( ) ?;
162- Ok ( SharedSecret :: from ( secret. as_slice ( ) ) )
163- } )
164- . map_err ( |e| Error :: General ( format ! ( "OpenSSL error: {e}" ) ) )
165- }
166-
167- fn pub_key ( & self ) -> & [ u8 ] {
168- & self . public_key
169- }
170-
171- fn group ( & self ) -> NamedGroup {
172- NamedGroup :: X25519
173- }
174- }
175-
176105#[ cfg( test) ]
177106mod test {
178107 use openssl:: {
179108 bn:: BigNum ,
180109 ec:: { EcGroup , EcKey , EcPoint } ,
181110 nid:: Nid ,
182- pkey:: { Id , PKey } ,
183111 } ;
184- use rustls:: { crypto:: ActiveKeyExchange , NamedGroup } ;
185- use wycheproof:: { ecdh:: TestName , TestResult } ;
112+ use rustls:: { NamedGroup , crypto:: ActiveKeyExchange } ;
113+ use wycheproof:: { TestResult , ecdh:: TestName } ;
186114
187- use crate :: kx:: EcKeyExchange ;
188-
189- #[ cfg( not( feature = "fips" ) ) ]
190- use super :: X25519KeyExchange ;
115+ use super :: EcKeyExchange ;
191116
192117 #[ rstest:: rstest]
193118 #[ case:: secp256r1( TestName :: EcdhSecp256r1 , NamedGroup :: secp256r1, Nid :: X9_62_PRIME256V1 ) ]
194119 #[ case:: secp384r1( TestName :: EcdhSecp384r1 , NamedGroup :: secp384r1, Nid :: SECP384R1 ) ]
195- fn ec ( #[ case] test_name : TestName , #[ case] rustls_group : NamedGroup , #[ case] nid : Nid ) {
120+ fn test_ec_kx ( #[ case] test_name : TestName , #[ case] rustls_group : NamedGroup , #[ case] nid : Nid ) {
196121 let test_set = wycheproof:: ecdh:: TestSet :: load ( test_name) . unwrap ( ) ;
197122 let ctx = openssl:: bn:: BigNumContext :: new ( ) . unwrap ( ) ;
198123
@@ -231,45 +156,4 @@ mod test {
231156 }
232157 }
233158 }
234-
235- #[ cfg( not( feature = "fips" ) ) ]
236- #[ test]
237- fn x25519 ( ) {
238- let test_set = wycheproof:: xdh:: TestSet :: load ( wycheproof:: xdh:: TestName :: X25519 ) . unwrap ( ) ;
239- for test_group in & test_set. test_groups {
240- for test in & test_group. tests {
241- let kx = X25519KeyExchange {
242- private_key : PKey :: private_key_from_raw_bytes ( & test. private_key , Id :: X25519 )
243- . unwrap ( ) ,
244- public_key : Vec :: new ( ) ,
245- } ;
246-
247- let res = Box :: new ( kx) . complete ( & test. public_key ) ;
248-
249- // OpenSSL does not support producing a zero shared secret
250- let zero_shared_secret = test
251- . flags
252- . contains ( & wycheproof:: xdh:: TestFlag :: ZeroSharedSecret ) ;
253-
254- match ( & test. result , zero_shared_secret) {
255- ( TestResult :: Acceptable , false ) | ( TestResult :: Valid , _) => match res {
256- Ok ( sharedsecret) => {
257- assert_eq ! (
258- sharedsecret. secret_bytes( ) ,
259- & test. shared_secret[ ..] ,
260- "Derived incorrect secret: {:?}" ,
261- test
262- ) ;
263- }
264- Err ( e) => {
265- panic ! ( "Test failed: {:?}. Error {:?}" , test, e) ;
266- }
267- } ,
268- _ => {
269- assert ! ( res. is_err( ) , "Expected error: {:?}" , test) ;
270- }
271- }
272- }
273- }
274- }
275159}
0 commit comments