|
| 1 | +import { test } from '../util'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { subtle, getRandomValues } from 'react-native-quick-crypto'; |
| 4 | +import { CryptoKey } from 'react-native-quick-crypto'; |
| 5 | +import type { CryptoKeyPair } from 'react-native-quick-crypto'; |
| 6 | + |
| 7 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 8 | +const subtleAny = subtle as any; |
| 9 | + |
| 10 | +const SUITE = 'subtle.deriveKey'; |
| 11 | + |
| 12 | +// Test 1: PBKDF2 deriveKey |
| 13 | +test(SUITE, 'PBKDF2 deriveKey to AES-GCM', async () => { |
| 14 | + const password = new TextEncoder().encode('my-password'); |
| 15 | + const salt = getRandomValues(new Uint8Array(16)); |
| 16 | + |
| 17 | + const baseKey = await subtle.importKey( |
| 18 | + 'raw', |
| 19 | + password, |
| 20 | + { name: 'PBKDF2' }, |
| 21 | + false, |
| 22 | + ['deriveKey'], |
| 23 | + ); |
| 24 | + |
| 25 | + const derivedKey = await subtleAny.deriveKey( |
| 26 | + { |
| 27 | + name: 'PBKDF2', |
| 28 | + salt, |
| 29 | + iterations: 100000, |
| 30 | + hash: 'SHA-256', |
| 31 | + }, |
| 32 | + baseKey as CryptoKey, |
| 33 | + { name: 'AES-GCM', length: 256 }, |
| 34 | + true, |
| 35 | + ['encrypt', 'decrypt'], |
| 36 | + ); |
| 37 | + |
| 38 | + // Verify key can encrypt/decrypt |
| 39 | + const plaintext = new Uint8Array([1, 2, 3, 4]); |
| 40 | + const iv = getRandomValues(new Uint8Array(12)); |
| 41 | + |
| 42 | + const ciphertext = await subtle.encrypt( |
| 43 | + { name: 'AES-GCM', iv }, |
| 44 | + derivedKey as CryptoKey, |
| 45 | + plaintext, |
| 46 | + ); |
| 47 | + |
| 48 | + const decrypted = await subtle.decrypt( |
| 49 | + { name: 'AES-GCM', iv }, |
| 50 | + derivedKey as CryptoKey, |
| 51 | + ciphertext, |
| 52 | + ); |
| 53 | + |
| 54 | + expect(Buffer.from(decrypted).toString('hex')).to.equal( |
| 55 | + Buffer.from(plaintext).toString('hex'), |
| 56 | + ); |
| 57 | +}); |
| 58 | + |
| 59 | +// Test 2: X25519 deriveKey |
| 60 | +test(SUITE, 'X25519 deriveKey to AES-GCM', async () => { |
| 61 | + const aliceKeyPair = await subtle.generateKey({ name: 'X25519' }, false, [ |
| 62 | + 'deriveKey', |
| 63 | + 'deriveBits', |
| 64 | + ]); |
| 65 | + |
| 66 | + const bobKeyPair = await subtle.generateKey({ name: 'X25519' }, false, [ |
| 67 | + 'deriveKey', |
| 68 | + 'deriveBits', |
| 69 | + ]); |
| 70 | + |
| 71 | + const aliceDerivedKey = await subtleAny.deriveKey( |
| 72 | + { |
| 73 | + name: 'X25519', |
| 74 | + public: (bobKeyPair as CryptoKeyPair).publicKey, |
| 75 | + }, |
| 76 | + (aliceKeyPair as CryptoKeyPair).privateKey, |
| 77 | + { name: 'AES-GCM', length: 256 }, |
| 78 | + true, |
| 79 | + ['encrypt', 'decrypt'], |
| 80 | + ); |
| 81 | + |
| 82 | + const bobDerivedKey = await subtleAny.deriveKey( |
| 83 | + { |
| 84 | + name: 'X25519', |
| 85 | + public: (aliceKeyPair as CryptoKeyPair).publicKey, |
| 86 | + }, |
| 87 | + (bobKeyPair as CryptoKeyPair).privateKey, |
| 88 | + { name: 'AES-GCM', length: 256 }, |
| 89 | + true, |
| 90 | + ['encrypt', 'decrypt'], |
| 91 | + ); |
| 92 | + |
| 93 | + // Both should derive the same key |
| 94 | + const aliceRaw = await subtle.exportKey('raw', aliceDerivedKey as CryptoKey); |
| 95 | + const bobRaw = await subtle.exportKey('raw', bobDerivedKey as CryptoKey); |
| 96 | + |
| 97 | + expect(Buffer.from(aliceRaw as ArrayBuffer).toString('hex')).to.equal( |
| 98 | + Buffer.from(bobRaw as ArrayBuffer).toString('hex'), |
| 99 | + ); |
| 100 | +}); |
0 commit comments