1
+ function define_tests_25519 ( ) {
2
+ return define_tests ( "X25519" ) ;
3
+ }
4
+
5
+ function define_tests_448 ( ) {
6
+ return define_tests ( "X448" ) ;
7
+ }
1
8
2
- function define_tests ( ) {
9
+ function define_tests ( algorithmName ) {
3
10
// May want to test prefixed implementations.
4
11
var subtle = self . crypto . subtle ;
5
12
@@ -8,7 +15,7 @@ function define_tests() {
8
15
// https://www.rfc-editor.org/rfc/rfc7748#section-6.1
9
16
// TODO: The spec states that the check must be done on use, but there is discussion about doing it on import.
10
17
// https://github.com/WICG/webcrypto-secure-curves/pull/13
11
- Object . keys ( kSmallOrderPoint ) . forEach ( function ( algorithmName ) {
18
+ {
12
19
kSmallOrderPoint [ algorithmName ] . forEach ( function ( test ) {
13
20
promise_test ( async ( ) => {
14
21
let derived ;
@@ -32,10 +39,10 @@ function define_tests() {
32
39
assert_equals ( derived , undefined , "Operation succeeded, but should not have." ) ;
33
40
} , algorithmName + " deriveBits checks for all-zero value result with a key of order " + test . order ) ;
34
41
} ) ;
35
- } ) ;
42
+ }
36
43
37
44
// Ensure the keys generated by each algorithm are valid for key derivation.
38
- Object . keys ( sizes ) . forEach ( function ( algorithmName ) {
45
+ {
39
46
promise_test ( async ( ) => {
40
47
let derived ;
41
48
try {
@@ -46,15 +53,16 @@ function define_tests() {
46
53
}
47
54
assert_false ( derived === undefined , "Key derivation failed." ) ;
48
55
} , "Key derivation using a " + algorithmName + " generated keys." ) ;
49
- } ) ;
56
+ }
50
57
51
58
return importKeys ( pkcs8 , spki , sizes )
52
59
. then ( function ( results ) {
53
60
publicKeys = results . publicKeys ;
54
61
privateKeys = results . privateKeys ;
55
62
noDeriveKeyKeys = results . noDeriveKeyKeys ;
63
+ ecdhKeys = results . ecdhKeys ;
56
64
57
- Object . keys ( sizes ) . forEach ( function ( algorithmName ) {
65
+ {
58
66
// Basic success case
59
67
promise_test ( function ( test ) {
60
68
return subtle . deriveKey ( { name : algorithmName , public : publicKeys [ algorithmName ] } , privateKeys [ algorithmName ] , { name : "HMAC" , hash : "SHA-256" , length : 256 } , true , [ "sign" , "verify" ] )
@@ -102,11 +110,7 @@ function define_tests() {
102
110
103
111
// - wrong algorithm
104
112
promise_test ( function ( test ) {
105
- publicKey = publicKeys [ "X25519" ] ;
106
- if ( algorithmName === "X25519" ) {
107
- publicKey = publicKeys [ "X448" ] ;
108
- }
109
- return subtle . deriveKey ( { name : algorithmName , public : publicKey } , privateKeys [ algorithmName ] , { name : "HMAC" , hash : "SHA-256" , length : 256 } , true , [ "sign" , "verify" ] )
113
+ return subtle . deriveKey ( { name : algorithmName , public : ecdhKeys [ algorithmName ] } , privateKeys [ algorithmName ] , { name : "HMAC" , hash : "SHA-256" , length : 256 } , true , [ "sign" , "verify" ] )
110
114
. then ( function ( key ) { return crypto . subtle . exportKey ( "raw" , key ) ; } )
111
115
. then ( function ( exportedKey ) {
112
116
assert_unreached ( "deriveKey succeeded but should have failed with InvalidAccessError" ) ;
@@ -161,16 +165,17 @@ function define_tests() {
161
165
} ) ;
162
166
} ) ;
163
167
} , algorithmName + " public property value is a secret key" ) ;
164
- } ) ;
168
+ }
165
169
} ) ;
166
170
167
171
function importKeys ( pkcs8 , spki , sizes ) {
168
172
var privateKeys = { } ;
169
173
var publicKeys = { } ;
170
174
var noDeriveKeyKeys = { } ;
175
+ var ecdhPublicKeys = { } ;
171
176
172
177
var promises = [ ] ;
173
- Object . keys ( pkcs8 ) . forEach ( function ( algorithmName ) {
178
+ {
174
179
var operation = subtle . importKey ( "pkcs8" , pkcs8 [ algorithmName ] ,
175
180
{ name : algorithmName } ,
176
181
false , [ "deriveBits" , "deriveKey" ] )
@@ -180,8 +185,8 @@ function define_tests() {
180
185
privateKeys [ algorithmName ] = null ;
181
186
} ) ;
182
187
promises . push ( operation ) ;
183
- } ) ;
184
- Object . keys ( pkcs8 ) . forEach ( function ( algorithmName ) {
188
+ }
189
+ {
185
190
var operation = subtle . importKey ( "pkcs8" , pkcs8 [ algorithmName ] ,
186
191
{ name : algorithmName } ,
187
192
false , [ "deriveBits" ] )
@@ -191,8 +196,8 @@ function define_tests() {
191
196
noDeriveKeyKeys [ algorithmName ] = null ;
192
197
} ) ;
193
198
promises . push ( operation ) ;
194
- } ) ;
195
- Object . keys ( spki ) . forEach ( function ( algorithmName ) {
199
+ }
200
+ {
196
201
var operation = subtle . importKey ( "spki" , spki [ algorithmName ] ,
197
202
{ name : algorithmName } ,
198
203
false , [ ] )
@@ -202,10 +207,18 @@ function define_tests() {
202
207
publicKeys [ algorithmName ] = null ;
203
208
} ) ;
204
209
promises . push ( operation ) ;
205
- } ) ;
210
+ }
211
+ {
212
+ var operation = subtle . importKey ( "spki" , ecSPKI ,
213
+ { name : "ECDH" , namedCurve : "P-256" } ,
214
+ false , [ ] )
215
+ . then ( function ( key ) {
216
+ ecdhPublicKeys [ algorithmName ] = key ;
217
+ } ) ;
218
+ }
206
219
207
220
return Promise . all ( promises )
208
- . then ( function ( results ) { return { privateKeys : privateKeys , publicKeys : publicKeys , noDeriveKeyKeys : noDeriveKeyKeys } } ) ;
221
+ . then ( function ( results ) { return { privateKeys : privateKeys , publicKeys : publicKeys , noDeriveKeyKeys : noDeriveKeyKeys , ecdhKeys : ecdhPublicKeys } } ) ;
209
222
}
210
223
211
224
// Compares two ArrayBuffer or ArrayBufferView objects. If bitCount is
0 commit comments