|
| 1 | +import { test } from '../util'; |
| 2 | +import { argon2Sync, argon2 } from 'react-native-quick-crypto'; |
| 3 | +import { assert } from 'chai'; |
| 4 | +import { Buffer } from '@craftzdog/react-native-buffer'; |
| 5 | + |
| 6 | +const SUITE = 'argon2'; |
| 7 | + |
| 8 | +// RFC 9106 test vector for argon2id |
| 9 | +const RFC_PARAMS = { |
| 10 | + message: Buffer.from( |
| 11 | + '0101010101010101010101010101010101010101010101010101010101010101', |
| 12 | + 'hex', |
| 13 | + ), |
| 14 | + nonce: Buffer.from('02020202020202020202020202020202', 'hex'), |
| 15 | + parallelism: 4, |
| 16 | + tagLength: 32, |
| 17 | + memory: 32, // 32 KiB |
| 18 | + passes: 3, |
| 19 | + secret: Buffer.from('0303030303030303', 'hex'), |
| 20 | + associatedData: Buffer.from('040404040404040404040404', 'hex'), |
| 21 | + version: 0x13, |
| 22 | +}; |
| 23 | + |
| 24 | +test(SUITE, 'argon2Sync: argon2id produces expected output', () => { |
| 25 | + const result = argon2Sync('argon2id', RFC_PARAMS); |
| 26 | + assert.isOk(result); |
| 27 | + assert.strictEqual(result.length, 32); |
| 28 | +}); |
| 29 | + |
| 30 | +test(SUITE, 'argon2Sync: argon2i produces output', () => { |
| 31 | + const result = argon2Sync('argon2i', { |
| 32 | + message: Buffer.from('password'), |
| 33 | + nonce: Buffer.from('somesalt0000'), |
| 34 | + parallelism: 1, |
| 35 | + tagLength: 32, |
| 36 | + memory: 64, |
| 37 | + passes: 3, |
| 38 | + }); |
| 39 | + assert.isOk(result); |
| 40 | + assert.strictEqual(result.length, 32); |
| 41 | +}); |
| 42 | + |
| 43 | +test(SUITE, 'argon2Sync: argon2d produces output', () => { |
| 44 | + const result = argon2Sync('argon2d', { |
| 45 | + message: Buffer.from('password'), |
| 46 | + nonce: Buffer.from('somesalt0000'), |
| 47 | + parallelism: 1, |
| 48 | + tagLength: 32, |
| 49 | + memory: 64, |
| 50 | + passes: 3, |
| 51 | + }); |
| 52 | + assert.isOk(result); |
| 53 | + assert.strictEqual(result.length, 32); |
| 54 | +}); |
| 55 | + |
| 56 | +test(SUITE, 'argon2Sync: different algorithms produce different output', () => { |
| 57 | + const params = { |
| 58 | + message: Buffer.from('password'), |
| 59 | + nonce: Buffer.from('somesalt0000'), |
| 60 | + parallelism: 1, |
| 61 | + tagLength: 32, |
| 62 | + memory: 64, |
| 63 | + passes: 3, |
| 64 | + }; |
| 65 | + const d = argon2Sync('argon2d', params); |
| 66 | + const i = argon2Sync('argon2i', params); |
| 67 | + const id = argon2Sync('argon2id', params); |
| 68 | + assert.notDeepEqual(d, i); |
| 69 | + assert.notDeepEqual(i, id); |
| 70 | + assert.notDeepEqual(d, id); |
| 71 | +}); |
| 72 | + |
| 73 | +test(SUITE, 'argon2Sync: respects tagLength', () => { |
| 74 | + const result = argon2Sync('argon2id', { |
| 75 | + message: Buffer.from('password'), |
| 76 | + nonce: Buffer.from('somesalt0000'), |
| 77 | + parallelism: 1, |
| 78 | + tagLength: 64, |
| 79 | + memory: 64, |
| 80 | + passes: 3, |
| 81 | + }); |
| 82 | + assert.strictEqual(result.length, 64); |
| 83 | +}); |
| 84 | + |
| 85 | +test(SUITE, 'argon2Sync: throws on invalid algorithm', () => { |
| 86 | + assert.throws(() => { |
| 87 | + argon2Sync('argon2x', { |
| 88 | + message: Buffer.from('password'), |
| 89 | + nonce: Buffer.from('somesalt0000'), |
| 90 | + parallelism: 1, |
| 91 | + tagLength: 32, |
| 92 | + memory: 64, |
| 93 | + passes: 3, |
| 94 | + }); |
| 95 | + }, /Unknown argon2 algorithm/); |
| 96 | +}); |
| 97 | + |
| 98 | +test(SUITE, 'argon2: async produces same result as sync', () => { |
| 99 | + return new Promise<void>((resolve, reject) => { |
| 100 | + const params = { |
| 101 | + message: Buffer.from('password'), |
| 102 | + nonce: Buffer.from('somesalt0000'), |
| 103 | + parallelism: 1, |
| 104 | + tagLength: 32, |
| 105 | + memory: 64, |
| 106 | + passes: 3, |
| 107 | + }; |
| 108 | + const syncResult = argon2Sync('argon2id', params); |
| 109 | + argon2('argon2id', params, (err, asyncResult) => { |
| 110 | + try { |
| 111 | + assert.isNull(err); |
| 112 | + assert.deepEqual( |
| 113 | + Buffer.from(asyncResult).toString('hex'), |
| 114 | + Buffer.from(syncResult).toString('hex'), |
| 115 | + ); |
| 116 | + resolve(); |
| 117 | + } catch (e) { |
| 118 | + reject(e); |
| 119 | + } |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +test(SUITE, 'argon2Sync: deterministic with same inputs', () => { |
| 125 | + const params = { |
| 126 | + message: Buffer.from('password'), |
| 127 | + nonce: Buffer.from('somesalt0000'), |
| 128 | + parallelism: 1, |
| 129 | + tagLength: 32, |
| 130 | + memory: 64, |
| 131 | + passes: 3, |
| 132 | + }; |
| 133 | + const r1 = argon2Sync('argon2id', params); |
| 134 | + const r2 = argon2Sync('argon2id', params); |
| 135 | + assert.deepEqual( |
| 136 | + Buffer.from(r1).toString('hex'), |
| 137 | + Buffer.from(r2).toString('hex'), |
| 138 | + ); |
| 139 | +}); |
0 commit comments