|
1 | 1 | import { expect } from 'chai'; |
| 2 | +import type { WebCryptoKeyPair } from 'react-native-quick-crypto'; |
2 | 3 | import crypto, { |
3 | 4 | Buffer, |
4 | 5 | subtle, |
@@ -215,4 +216,193 @@ test(SUITE, 'x25519 - error handling', () => { |
215 | 216 | }).to.throw(); |
216 | 217 | }); |
217 | 218 |
|
218 | | -// ecdh deriveBits |
| 219 | +// --- ECDH subtle.deriveBits Tests --- |
| 220 | + |
| 221 | +import type { NamedCurve } from 'react-native-quick-crypto'; |
| 222 | + |
| 223 | +const ecdhCurves: Array<{ curve: NamedCurve; bitLen: number }> = [ |
| 224 | + { curve: 'P-256', bitLen: 256 }, |
| 225 | + { curve: 'P-384', bitLen: 384 }, |
| 226 | + { curve: 'P-521', bitLen: 528 }, |
| 227 | +]; |
| 228 | + |
| 229 | +for (const { curve, bitLen } of ecdhCurves) { |
| 230 | + test(SUITE, `ECDH deriveBits - ${curve}`, async () => { |
| 231 | + const alice = (await subtle.generateKey( |
| 232 | + { name: 'ECDH', namedCurve: curve }, |
| 233 | + true, |
| 234 | + ['deriveBits'], |
| 235 | + )) as WebCryptoKeyPair; |
| 236 | + const bob = (await subtle.generateKey( |
| 237 | + { name: 'ECDH', namedCurve: curve }, |
| 238 | + true, |
| 239 | + ['deriveBits'], |
| 240 | + )) as WebCryptoKeyPair; |
| 241 | + |
| 242 | + const bits = await subtle.deriveBits( |
| 243 | + { name: 'ECDH', public: bob.publicKey }, |
| 244 | + alice.privateKey, |
| 245 | + bitLen, |
| 246 | + ); |
| 247 | + expect(bits).to.be.an.instanceOf(ArrayBuffer); |
| 248 | + expect(bits.byteLength).to.equal(bitLen / 8); |
| 249 | + }); |
| 250 | + |
| 251 | + test(SUITE, `ECDH deriveBits symmetry - ${curve}`, async () => { |
| 252 | + const alice = (await subtle.generateKey( |
| 253 | + { name: 'ECDH', namedCurve: curve }, |
| 254 | + true, |
| 255 | + ['deriveBits'], |
| 256 | + )) as WebCryptoKeyPair; |
| 257 | + const bob = (await subtle.generateKey( |
| 258 | + { name: 'ECDH', namedCurve: curve }, |
| 259 | + true, |
| 260 | + ['deriveBits'], |
| 261 | + )) as WebCryptoKeyPair; |
| 262 | + |
| 263 | + const aliceBits = await subtle.deriveBits( |
| 264 | + { name: 'ECDH', public: bob.publicKey }, |
| 265 | + alice.privateKey, |
| 266 | + bitLen, |
| 267 | + ); |
| 268 | + const bobBits = await subtle.deriveBits( |
| 269 | + { name: 'ECDH', public: alice.publicKey }, |
| 270 | + bob.privateKey, |
| 271 | + bitLen, |
| 272 | + ); |
| 273 | + |
| 274 | + expect(Buffer.from(aliceBits).equals(Buffer.from(bobBits))).to.equal(true); |
| 275 | + }); |
| 276 | +} |
| 277 | + |
| 278 | +// --- X448 diffieHellman Tests --- |
| 279 | + |
| 280 | +test(SUITE, 'x448 - shared secret', () => { |
| 281 | + const alice = crypto.generateKeyPairSync('x448', {}); |
| 282 | + const bob = crypto.generateKeyPairSync('x448', {}); |
| 283 | + |
| 284 | + const privateKey = KeyObject.createKeyObject( |
| 285 | + 'private', |
| 286 | + alice.privateKey as ArrayBuffer, |
| 287 | + ); |
| 288 | + const publicKey = KeyObject.createKeyObject( |
| 289 | + 'public', |
| 290 | + bob.publicKey as ArrayBuffer, |
| 291 | + ); |
| 292 | + |
| 293 | + const sharedSecret = crypto.diffieHellman({ privateKey, publicKey }); |
| 294 | + expect(Buffer.isBuffer(sharedSecret)).to.equal(true); |
| 295 | +}); |
| 296 | + |
| 297 | +test(SUITE, 'x448 - shared secret symmetry', () => { |
| 298 | + const alice = crypto.generateKeyPairSync('x448', {}); |
| 299 | + const bob = crypto.generateKeyPairSync('x448', {}); |
| 300 | + |
| 301 | + const alicePrivate = KeyObject.createKeyObject( |
| 302 | + 'private', |
| 303 | + alice.privateKey as ArrayBuffer, |
| 304 | + ); |
| 305 | + const alicePublic = KeyObject.createKeyObject( |
| 306 | + 'public', |
| 307 | + alice.publicKey as ArrayBuffer, |
| 308 | + ); |
| 309 | + const bobPrivate = KeyObject.createKeyObject( |
| 310 | + 'private', |
| 311 | + bob.privateKey as ArrayBuffer, |
| 312 | + ); |
| 313 | + const bobPublic = KeyObject.createKeyObject( |
| 314 | + 'public', |
| 315 | + bob.publicKey as ArrayBuffer, |
| 316 | + ); |
| 317 | + |
| 318 | + const sharedSecretAlice = crypto.diffieHellman({ |
| 319 | + privateKey: alicePrivate, |
| 320 | + publicKey: bobPublic, |
| 321 | + }) as Buffer; |
| 322 | + |
| 323 | + const sharedSecretBob = crypto.diffieHellman({ |
| 324 | + privateKey: bobPrivate, |
| 325 | + publicKey: alicePublic, |
| 326 | + }) as Buffer; |
| 327 | + |
| 328 | + expect(Buffer.isBuffer(sharedSecretAlice)).to.equal(true); |
| 329 | + expect(Buffer.isBuffer(sharedSecretBob)).to.equal(true); |
| 330 | + expect(sharedSecretAlice.equals(sharedSecretBob)).to.equal(true); |
| 331 | +}); |
| 332 | + |
| 333 | +test(SUITE, 'x448 - shared secret properties', () => { |
| 334 | + const alice = crypto.generateKeyPairSync('x448', {}); |
| 335 | + const bob = crypto.generateKeyPairSync('x448', {}); |
| 336 | + |
| 337 | + const alicePrivate = KeyObject.createKeyObject( |
| 338 | + 'private', |
| 339 | + alice.privateKey as ArrayBuffer, |
| 340 | + ); |
| 341 | + const bobPublic = KeyObject.createKeyObject( |
| 342 | + 'public', |
| 343 | + bob.publicKey as ArrayBuffer, |
| 344 | + ); |
| 345 | + |
| 346 | + const sharedSecret = crypto.diffieHellman({ |
| 347 | + privateKey: alicePrivate, |
| 348 | + publicKey: bobPublic, |
| 349 | + }) as Buffer; |
| 350 | + |
| 351 | + expect(sharedSecret.length).to.equal(56); |
| 352 | + |
| 353 | + const allZeros = Buffer.alloc(56, 0); |
| 354 | + expect(sharedSecret.equals(allZeros)).to.equal(false); |
| 355 | + |
| 356 | + const sharedSecret2 = crypto.diffieHellman({ |
| 357 | + privateKey: alicePrivate, |
| 358 | + publicKey: bobPublic, |
| 359 | + }) as Buffer; |
| 360 | + expect(sharedSecret.equals(sharedSecret2)).to.equal(true); |
| 361 | +}); |
| 362 | + |
| 363 | +test(SUITE, 'x448 - different key pairs produce different secrets', () => { |
| 364 | + const alice = crypto.generateKeyPairSync('x448', {}); |
| 365 | + const bob = crypto.generateKeyPairSync('x448', {}); |
| 366 | + const charlie = crypto.generateKeyPairSync('x448', {}); |
| 367 | + |
| 368 | + const alicePrivate = KeyObject.createKeyObject( |
| 369 | + 'private', |
| 370 | + alice.privateKey as ArrayBuffer, |
| 371 | + ); |
| 372 | + const bobPublic = KeyObject.createKeyObject( |
| 373 | + 'public', |
| 374 | + bob.publicKey as ArrayBuffer, |
| 375 | + ); |
| 376 | + const charliePublic = KeyObject.createKeyObject( |
| 377 | + 'public', |
| 378 | + charlie.publicKey as ArrayBuffer, |
| 379 | + ); |
| 380 | + |
| 381 | + const secretAliceBob = crypto.diffieHellman({ |
| 382 | + privateKey: alicePrivate, |
| 383 | + publicKey: bobPublic, |
| 384 | + }) as Buffer; |
| 385 | + |
| 386 | + const secretAliceCharlie = crypto.diffieHellman({ |
| 387 | + privateKey: alicePrivate, |
| 388 | + publicKey: charliePublic, |
| 389 | + }) as Buffer; |
| 390 | + |
| 391 | + expect(secretAliceBob.equals(secretAliceCharlie)).to.equal(false); |
| 392 | +}); |
| 393 | + |
| 394 | +test(SUITE, 'x448 - error handling', () => { |
| 395 | + const alice = crypto.generateKeyPairSync('x448', {}); |
| 396 | + |
| 397 | + const alicePrivate = KeyObject.createKeyObject( |
| 398 | + 'private', |
| 399 | + alice.privateKey as ArrayBuffer, |
| 400 | + ); |
| 401 | + |
| 402 | + expect(() => { |
| 403 | + crypto.diffieHellman({ |
| 404 | + privateKey: alicePrivate, |
| 405 | + publicKey: {} as KeyObject, |
| 406 | + }); |
| 407 | + }).to.throw(); |
| 408 | +}); |
0 commit comments