-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathargon2_tests.ts
More file actions
204 lines (185 loc) · 5.5 KB
/
argon2_tests.ts
File metadata and controls
204 lines (185 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { test } from '../util';
import { argon2Sync, argon2, Buffer } from 'react-native-quick-crypto';
import { assert } from 'chai';
const SUITE = 'argon2';
// RFC 9106 test vector for argon2id
const RFC_PARAMS = {
message: Buffer.from(
'0101010101010101010101010101010101010101010101010101010101010101',
'hex',
),
nonce: Buffer.from('02020202020202020202020202020202', 'hex'),
parallelism: 4,
tagLength: 32,
memory: 32, // 32 KiB
passes: 3,
secret: Buffer.from('0303030303030303', 'hex'),
associatedData: Buffer.from('040404040404040404040404', 'hex'),
version: 0x13,
};
test(SUITE, 'argon2Sync: argon2id produces expected output', () => {
const result = argon2Sync('argon2id', RFC_PARAMS);
assert.isOk(result);
assert.strictEqual(result.length, 32);
});
test(SUITE, 'argon2Sync: argon2i produces output', () => {
const result = argon2Sync('argon2i', {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
});
assert.isOk(result);
assert.strictEqual(result.length, 32);
});
test(SUITE, 'argon2Sync: argon2d produces output', () => {
const result = argon2Sync('argon2d', {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
});
assert.isOk(result);
assert.strictEqual(result.length, 32);
});
test(SUITE, 'argon2Sync: different algorithms produce different output', () => {
const params = {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
};
const d = argon2Sync('argon2d', params);
const i = argon2Sync('argon2i', params);
const id = argon2Sync('argon2id', params);
assert.notDeepEqual(d, i);
assert.notDeepEqual(i, id);
assert.notDeepEqual(d, id);
});
test(SUITE, 'argon2Sync: respects tagLength', () => {
const result = argon2Sync('argon2id', {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 64,
memory: 64,
passes: 3,
});
assert.strictEqual(result.length, 64);
});
test(SUITE, 'argon2Sync: throws on invalid algorithm', () => {
assert.throws(() => {
argon2Sync('argon2x', {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
});
}, /Unknown argon2 algorithm/);
});
test(SUITE, 'argon2: async produces same result as sync', () => {
return new Promise<void>((resolve, reject) => {
const params = {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
};
const syncResult = argon2Sync('argon2id', params);
argon2('argon2id', params, (err, asyncResult) => {
try {
assert.isNull(err);
assert.deepEqual(
Buffer.from(asyncResult).toString('hex'),
Buffer.from(syncResult).toString('hex'),
);
resolve();
} catch (e) {
reject(e);
}
});
});
});
test(SUITE, 'argon2Sync: deterministic with same inputs', () => {
const params = {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
};
const r1 = argon2Sync('argon2id', params);
const r2 = argon2Sync('argon2id', params);
assert.deepEqual(
Buffer.from(r1).toString('hex'),
Buffer.from(r2).toString('hex'),
);
});
// --- Numeric parameter validation (Phase 1.1: validateUInt) ---
//
// `static_cast<uint32_t>(NaN | +/-Infinity | -1)` is undefined behavior in
// C++. The C++ layer used to do these casts naked; the validateUInt helper
// now rejects them with a descriptive error before the cast.
const baseParams = {
message: Buffer.from('password'),
nonce: Buffer.from('somesalt0000'),
parallelism: 1,
tagLength: 32,
memory: 64,
passes: 3,
};
test(SUITE, 'argon2Sync: rejects NaN parallelism', () => {
assert.throws(() => {
argon2Sync('argon2id', { ...baseParams, parallelism: NaN });
}, /parallelism.*NaN/i);
});
test(SUITE, 'argon2Sync: rejects +Infinity memory', () => {
assert.throws(() => {
argon2Sync('argon2id', { ...baseParams, memory: Infinity });
}, /memory.*infinity/i);
});
test(SUITE, 'argon2Sync: rejects -Infinity passes', () => {
assert.throws(() => {
argon2Sync('argon2id', { ...baseParams, passes: -Infinity });
}, /passes.*infinity/i);
});
test(SUITE, 'argon2Sync: rejects negative tagLength', () => {
assert.throws(() => {
argon2Sync('argon2id', { ...baseParams, tagLength: -1 });
}, /tagLength.*non-negative/i);
});
test(SUITE, 'argon2Sync: rejects fractional passes', () => {
assert.throws(() => {
argon2Sync('argon2id', { ...baseParams, passes: 3.5 });
}, /passes.*integer/i);
});
test(SUITE, 'argon2Sync: rejects out-of-range memory', () => {
// memory is uint32_t — anything beyond UINT32_MAX must be rejected.
assert.throws(() => {
argon2Sync('argon2id', { ...baseParams, memory: 2 ** 32 });
}, /memory.*out of range/i);
});
test(SUITE, 'argon2: async path also rejects NaN parallelism', () => {
return new Promise<void>((resolve, reject) => {
argon2('argon2id', { ...baseParams, parallelism: NaN }, err => {
try {
assert.isNotNull(err);
assert.match(err!.message, /parallelism.*NaN/i);
resolve();
} catch (e) {
reject(e);
}
});
});
});