1+ function define_tests_25519 ( ) {
2+ return define_tests ( "X25519" ) ;
3+ }
4+
5+ function define_tests_448 ( ) {
6+ return define_tests ( "X448" ) ;
7+ }
18
2- function define_tests ( ) {
9+ function define_tests ( algorithmName ) {
310 // May want to test prefixed implementations.
411 var subtle = self . crypto . subtle ;
512
613 // Verify the derive functions perform checks against the all-zero value results,
714 // ensuring small-order points are rejected.
815 // https://www.rfc-editor.org/rfc/rfc7748#section-6.1
9- // TODO: The spec states that the check must be done on use, but there is discussion about doing it on import.
10- // https://github.com/WICG/webcrypto-secure-curves/pull/13
11- Object . keys ( kSmallOrderPoint ) . forEach ( function ( algorithmName ) {
16+ {
1217 kSmallOrderPoint [ algorithmName ] . forEach ( function ( test ) {
1318 promise_test ( async ( ) => {
1419 let derived ;
@@ -23,22 +28,23 @@ function define_tests() {
2328 false , [ ] )
2429 derived = await subtle . deriveBits ( { name : algorithmName , public : publicKey } , privateKey , 8 * sizes [ algorithmName ] ) ;
2530 } catch ( err ) {
26- assert_false ( privateKey = == undefined , "Private key should be valid." ) ;
27- assert_false ( publicKey = == undefined , "Public key should be valid." ) ;
31+ assert_true ( privateKey ! == undefined , "Private key should be valid." ) ;
32+ assert_true ( publicKey ! == undefined , "Public key should be valid." ) ;
2833 assert_equals ( err . name , "OperationError" , "Should throw correct error, not " + err . name + ": " + err . message + "." ) ;
2934 }
3035 assert_equals ( derived , undefined , "Operation succeeded, but should not have." ) ;
3136 } , algorithmName + " key derivation checks for all-zero value result with a key of order " + test . order ) ;
3237 } ) ;
33- } ) ;
38+ }
3439
3540 return importKeys ( pkcs8 , spki , sizes )
3641 . then ( function ( results ) {
3742 publicKeys = results . publicKeys ;
3843 privateKeys = results . privateKeys ;
3944 noDeriveBitsKeys = results . noDeriveBitsKeys ;
45+ ecdhKeys = results . ecdhKeys ;
4046
41- Object . keys ( sizes ) . forEach ( function ( algorithmName ) {
47+ {
4248 // Basic success case
4349 promise_test ( function ( test ) {
4450 return subtle . deriveBits ( { name : algorithmName , public : publicKeys [ algorithmName ] } , privateKeys [ algorithmName ] , 8 * sizes [ algorithmName ] )
@@ -59,25 +65,6 @@ function define_tests() {
5965 } ) ;
6066 } , algorithmName + " mixed case parameters" ) ;
6167
62- // Null length
63- // "Null" is not valid per the current spec
64- // - https://github.com/w3c/webcrypto/issues/322
65- // - https://github.com/w3c/webcrypto/issues/329
66- //
67- // Proposal for a spec change:
68- // - https://github.com/w3c/webcrypto/pull/345
69- //
70- // This test case may be replaced by these new tests:
71- // - https://github.com/web-platform-tests/wpt/pull/43400
72- promise_test ( function ( test ) {
73- return subtle . deriveBits ( { name : algorithmName , public : publicKeys [ algorithmName ] } , privateKeys [ algorithmName ] , null )
74- . then ( function ( derivation ) {
75- assert_true ( equalBuffers ( derivation , derivations [ algorithmName ] ) , "Derived correct bits" ) ;
76- } , function ( err ) {
77- assert_unreached ( "deriveBits failed with error " + err . name + ": " + err . message ) ;
78- } ) ;
79- } , algorithmName + " with null length" ) ;
80-
8168 // Shorter than entire derivation per algorithm
8269 promise_test ( function ( test ) {
8370 return subtle . deriveBits ( { name : algorithmName , public : publicKeys [ algorithmName ] } , privateKeys [ algorithmName ] , 8 * sizes [ algorithmName ] - 32 )
@@ -122,11 +109,7 @@ function define_tests() {
122109
123110 // - wrong algorithm
124111 promise_test ( function ( test ) {
125- publicKey = publicKeys [ "X25519" ] ;
126- if ( algorithmName === "X25519" ) {
127- publicKey = publicKeys [ "X448" ] ;
128- }
129- return subtle . deriveBits ( { name : algorithmName , public : publicKey } , privateKeys [ algorithmName ] , 8 * sizes [ algorithmName ] )
112+ return subtle . deriveBits ( { name : algorithmName , public : ecdhKeys [ algorithmName ] } , privateKeys [ algorithmName ] , 8 * sizes [ algorithmName ] )
130113 . then ( function ( derivation ) {
131114 assert_unreached ( "deriveBits succeeded but should have failed with InvalidAccessError" ) ;
132115 } , function ( err ) {
@@ -186,16 +169,17 @@ function define_tests() {
186169 assert_equals ( err . name , "OperationError" , "Should throw correct error, not " + err . name + ": " + err . message ) ;
187170 } ) ;
188171 } , algorithmName + " asking for too many bits" ) ;
189- } ) ;
172+ }
190173 } ) ;
191174
192175 function importKeys ( pkcs8 , spki , sizes ) {
193176 var privateKeys = { } ;
194177 var publicKeys = { } ;
195178 var noDeriveBitsKeys = { } ;
179+ var ecdhPublicKeys = { } ;
196180
197181 var promises = [ ] ;
198- Object . keys ( pkcs8 ) . forEach ( function ( algorithmName ) {
182+ {
199183 var operation = subtle . importKey ( "pkcs8" , pkcs8 [ algorithmName ] ,
200184 { name : algorithmName } ,
201185 false , [ "deriveBits" , "deriveKey" ] )
@@ -205,8 +189,8 @@ function define_tests() {
205189 privateKeys [ algorithmName ] = null ;
206190 } ) ;
207191 promises . push ( operation ) ;
208- } ) ;
209- Object . keys ( pkcs8 ) . forEach ( function ( algorithmName ) {
192+ }
193+ {
210194 var operation = subtle . importKey ( "pkcs8" , pkcs8 [ algorithmName ] ,
211195 { name : algorithmName } ,
212196 false , [ "deriveKey" ] )
@@ -216,8 +200,8 @@ function define_tests() {
216200 noDeriveBitsKeys [ algorithmName ] = null ;
217201 } ) ;
218202 promises . push ( operation ) ;
219- } ) ;
220- Object . keys ( spki ) . forEach ( function ( algorithmName ) {
203+ }
204+ {
221205 var operation = subtle . importKey ( "spki" , spki [ algorithmName ] ,
222206 { name : algorithmName } ,
223207 false , [ ] )
@@ -227,10 +211,17 @@ function define_tests() {
227211 publicKeys [ algorithmName ] = null ;
228212 } ) ;
229213 promises . push ( operation ) ;
230- } ) ;
231-
214+ }
215+ {
216+ var operation = subtle . importKey ( "spki" , ecSPKI ,
217+ { name : "ECDH" , namedCurve : "P-256" } ,
218+ false , [ ] )
219+ . then ( function ( key ) {
220+ ecdhPublicKeys [ algorithmName ] = key ;
221+ } ) ;
222+ }
232223 return Promise . all ( promises )
233- . then ( function ( results ) { return { privateKeys : privateKeys , publicKeys : publicKeys , noDeriveBitsKeys : noDeriveBitsKeys } } ) ;
224+ . then ( function ( results ) { return { privateKeys : privateKeys , publicKeys : publicKeys , noDeriveBitsKeys : noDeriveBitsKeys , ecdhKeys : ecdhPublicKeys } } ) ;
234225 }
235226
236227 // Compares two ArrayBuffer or ArrayBufferView objects. If bitCount is
0 commit comments