-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
613 lines (549 loc) · 19.9 KB
/
Copy pathindex.html
File metadata and controls
613 lines (549 loc) · 19.9 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Secure Vault</title>
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net blob:; style-src 'unsafe-inline'; connect-src blob:">
<script src="https://cdn.jsdelivr.net/npm/argon2-browser@1.18.0/dist/argon2-bundled.min.js"
integrity="sha384-XOR3aNvHciLPIf6r+2glkrmbBbLmIJ1EChMXjw8eBKBf8gE0rDq1TyUNuRdorOqi"
crossorigin="anonymous"></script>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background: #f0f2f5;
color: #1a1a2e;
min-height: 100vh;
display: flex;
align-items: flex-start;
justify-content: center;
padding: 40px 16px;
}
.card {
background: #fff;
border-radius: 16px;
box-shadow: 0 4px 24px rgba(0,0,0,0.08);
width: 100%;
max-width: 600px;
overflow: hidden;
}
/* ── Header ── */
.header {
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
padding: 28px 32px 24px;
color: #fff;
}
.header h1 {
font-size: 1.4rem;
font-weight: 700;
letter-spacing: -0.3px;
display: flex;
align-items: center;
gap: 10px;
}
.header h1 svg { opacity: 0.9; }
.header p {
margin-top: 6px;
font-size: 0.85rem;
opacity: 0.6;
line-height: 1.5;
}
.about {
padding: 20px 32px;
border-bottom: 1px solid #e8eaed;
background: #fafbff;
}
.about-label {
color: #4f46e5;
font-size: 0.72rem;
font-weight: 700;
letter-spacing: 0;
text-transform: uppercase;
margin-bottom: 8px;
}
.about h2 {
color: #1a1a2e;
font-size: 1rem;
line-height: 1.35;
margin-bottom: 8px;
}
.about p {
color: #555;
font-size: 0.84rem;
line-height: 1.55;
margin-bottom: 12px;
}
.about-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 10px;
}
.about-grid div {
border: 1px solid #e5e7eb;
border-radius: 8px;
background: #fff;
padding: 10px;
}
.about-grid strong {
display: block;
color: #1a1a2e;
font-size: 0.78rem;
margin-bottom: 5px;
}
.about-grid span {
color: #666;
display: block;
font-size: 0.73rem;
line-height: 1.45;
}
/* ── Tabs ── */
.tabs {
display: flex;
border-bottom: 1px solid #e8eaed;
}
.tab {
flex: 1;
padding: 14px;
border: none;
background: none;
font-size: 0.9rem;
font-weight: 600;
color: #888;
cursor: pointer;
transition: color 0.15s, border-bottom 0.15s;
border-bottom: 3px solid transparent;
letter-spacing: 0.1px;
}
.tab:hover { color: #1a1a2e; }
.tab.active {
color: #1a1a2e;
border-bottom-color: #4f46e5;
}
/* ── Body ── */
.body { padding: 28px 32px 32px; }
.panel { display: none; }
.panel.active { display: block; }
label {
display: block;
font-size: 0.8rem;
font-weight: 600;
color: #555;
margin-bottom: 6px;
letter-spacing: 0.3px;
text-transform: uppercase;
}
textarea, input[type="password"], input[type="text"] {
width: 100%;
border: 1.5px solid #e0e3e8;
border-radius: 8px;
padding: 10px 14px;
font-size: 0.9rem;
font-family: inherit;
color: #1a1a2e;
background: #fafbfc;
transition: border-color 0.15s, box-shadow 0.15s;
outline: none;
resize: vertical;
}
textarea:focus, input:focus {
border-color: #4f46e5;
box-shadow: 0 0 0 3px rgba(79,70,229,0.1);
background: #fff;
}
textarea { min-height: 120px; line-height: 1.5; }
textarea.output {
font-family: "SF Mono", "Fira Code", Consolas, monospace;
font-size: 0.78rem;
background: #f8f9fa;
color: #444;
min-height: 100px;
}
.field { margin-bottom: 20px; }
/* ── Passphrase row ── */
.pass-row {
position: relative;
}
.pass-row input {
padding-right: 44px;
}
.toggle-vis {
position: absolute;
right: 12px;
top: 50%;
transform: translateY(-50%);
background: none;
border: none;
cursor: pointer;
color: #888;
padding: 2px;
display: flex;
align-items: center;
}
.toggle-vis:hover { color: #4f46e5; }
/* ── Action button ── */
.btn {
width: 100%;
padding: 13px;
border: none;
border-radius: 8px;
font-size: 0.95rem;
font-weight: 700;
cursor: pointer;
transition: transform 0.1s, opacity 0.15s;
letter-spacing: 0.2px;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn-primary {
background: #4f46e5;
color: #fff;
}
.btn-primary:hover:not(:disabled) { background: #4338ca; }
/* ── Status ── */
.status {
display: none;
font-size: 0.82rem;
border-radius: 8px;
padding: 10px 14px;
margin-bottom: 20px;
line-height: 1.5;
}
.status.error { background: #fef2f2; color: #b91c1c; display: block; }
.status.info { background: #eff6ff; color: #1d4ed8; display: block; }
/* ── Output row ── */
.output-section { display: none; margin-top: 20px; }
.output-section.visible { display: block; }
.output-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 6px;
}
.btn-copy {
background: none;
border: 1.5px solid #e0e3e8;
border-radius: 6px;
padding: 4px 10px;
font-size: 0.75rem;
font-weight: 600;
color: #555;
cursor: pointer;
transition: border-color 0.15s, color 0.15s;
}
.btn-copy:hover { border-color: #4f46e5; color: #4f46e5; }
.btn-copy.copied { border-color: #16a34a; color: #16a34a; }
/* ── Spinner ── */
@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
display: inline-block;
width: 16px;
height: 16px;
border: 2px solid rgba(255,255,255,0.4);
border-top-color: #fff;
border-radius: 50%;
animation: spin 0.7s linear infinite;
vertical-align: middle;
margin-right: 6px;
}
/* ── Footer ── */
.footer {
padding: 14px 32px;
border-top: 1px solid #f0f2f5;
font-size: 0.75rem;
color: #aaa;
display: flex;
align-items: center;
gap: 6px;
}
.footer svg { flex-shrink: 0; }
@media (max-width: 640px) {
.about-grid { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<div class="card">
<!-- Header -->
<div class="header">
<h1>
<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="3" y="11" width="18" height="11" rx="2" ry="2"/>
<path d="M7 11V7a5 5 0 0 1 10 0v4"/>
</svg>
Secure Vault
</h1>
<p>AES-256-GCM encryption with Argon2id key derivation. Runs entirely in your browser — nothing is sent anywhere.</p>
</div>
<section class="about" aria-labelledby="about-vault-title">
<div class="about-label">About this project</div>
<h2 id="about-vault-title">A compact authenticated-encryption tool with explicit security boundaries.</h2>
<p>
AES Secure Vault is a Python package, CLI, and browser UI for practicing
defensive crypto API design. It produces self-contained JSON payloads
using AES-256-GCM, Argon2id, random salt and nonce values, and AAD-bound
envelope metadata.
</p>
<p>
Built by Meidie Fei as part of a cybersecurity portfolio focused on small,
reviewable tools with explicit security boundaries.
</p>
<div class="about-grid" aria-label="AES Secure Vault project details">
<div><strong>Protects</strong><span>Confidentiality at rest, ciphertext tampering detection, metadata tampering detection, and KDF downgrade resistance.</span></div>
<div><strong>Interop</strong><span>Browser encrypted blobs are compatible with the Python CLI and library, so the same payload format can be reviewed both ways.</span></div>
<div><strong>Limits</strong><span>Passphrase strength, memory scraping, side channels, and key rotation remain outside the tool boundary.</span></div>
</div>
</section>
<!-- Tabs -->
<div class="tabs">
<button class="tab active" onclick="switchTab('encrypt')">🔒 Encrypt</button>
<button class="tab" onclick="switchTab('decrypt')">🔓 Decrypt</button>
</div>
<!-- Body -->
<div class="body">
<!-- Encrypt panel -->
<div class="panel active" id="panel-encrypt">
<div class="field">
<label for="enc-text">Your message</label>
<textarea id="enc-text" placeholder="Type or paste the text you want to encrypt…"></textarea>
</div>
<div class="field">
<label for="enc-pass">Passphrase</label>
<div class="pass-row">
<input type="password" id="enc-pass" placeholder="Choose a strong passphrase">
<button class="toggle-vis" onclick="toggleVis('enc-pass', this)" aria-label="Show passphrase">
<svg id="eye-enc" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>
</button>
</div>
</div>
<div class="status" id="enc-status"></div>
<button class="btn btn-primary" id="enc-btn" onclick="doEncrypt()">Encrypt</button>
<div class="output-section" id="enc-output">
<div class="output-header">
<label style="margin:0">Encrypted payload</label>
<button class="btn-copy" id="enc-copy" onclick="copyOutput('enc-result', this)">Copy</button>
</div>
<textarea class="output" id="enc-result" readonly></textarea>
</div>
</div>
<!-- Decrypt panel -->
<div class="panel" id="panel-decrypt">
<div class="field">
<label for="dec-text">Encrypted payload</label>
<textarea id="dec-text" placeholder="Paste the encrypted JSON blob here…"></textarea>
</div>
<div class="field">
<label for="dec-pass">Passphrase</label>
<div class="pass-row">
<input type="password" id="dec-pass" placeholder="Enter the passphrase used to encrypt">
<button class="toggle-vis" onclick="toggleVis('dec-pass', this)" aria-label="Show passphrase">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>
</button>
</div>
</div>
<div class="status" id="dec-status"></div>
<button class="btn btn-primary" id="dec-btn" onclick="doDecrypt()">Decrypt</button>
<div class="output-section" id="dec-output">
<div class="output-header">
<label style="margin:0">Decrypted message</label>
<button class="btn-copy" id="dec-copy" onclick="copyOutput('dec-result', this)">Copy</button>
</div>
<textarea class="output" id="dec-result" readonly></textarea>
</div>
</div>
</div><!-- /body -->
<!-- Footer -->
<div class="footer">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>
Encrypted payloads are compatible with the Python CLI tool.
</div>
</div><!-- /card -->
<script>
// =====================================================================
// UI helpers
// =====================================================================
function switchTab(name) {
document.querySelectorAll('.tab').forEach((t, i) => {
t.classList.toggle('active', (i === 0) === (name === 'encrypt'));
});
document.querySelectorAll('.panel').forEach(p => p.classList.remove('active'));
document.getElementById('panel-' + name).classList.add('active');
}
function toggleVis(inputId, btn) {
const inp = document.getElementById(inputId);
const isHidden = inp.type === 'password';
inp.type = isHidden ? 'text' : 'password';
btn.querySelector('svg').style.opacity = isHidden ? '0.5' : '1';
}
function setStatus(id, msg, type) {
const el = document.getElementById(id);
el.textContent = msg;
el.className = 'status ' + (type || '');
}
function setLoading(btnId, loading, label) {
const btn = document.getElementById(btnId);
btn.disabled = loading;
btn.innerHTML = loading
? '<span class="spinner"></span>Deriving key…'
: label;
}
function showOutput(sectionId, resultId, text) {
document.getElementById(resultId).value = text;
document.getElementById(sectionId).classList.add('visible');
}
function copyOutput(resultId, btn) {
const text = document.getElementById(resultId).value;
navigator.clipboard.writeText(text).then(() => {
btn.textContent = 'Copied!';
btn.classList.add('copied');
setTimeout(() => { btn.textContent = 'Copy'; btn.classList.remove('copied'); }, 2000);
});
}
// =====================================================================
// Crypto primitives
// =====================================================================
const KDF_CONFIG = { ops: 3, mem: 65536, p: 4, key_len: 32 };
const MAX_PAYLOAD_SIZE = 100 * 1024 * 1024; // 100 MiB — matches Python CLI limit
function toBase64(bytes) {
let binary = '';
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
return btoa(binary);
}
function fromBase64(str) {
return Uint8Array.from(atob(str), c => c.charCodeAt(0));
}
function buildAAD(version, ops, mem, p, key_len, salt_b64, nonce_b64) {
if (version === '1.0')
return `v=1.0;ops=${ops};mem=${mem};p=${p};salt=${salt_b64};nonce=${nonce_b64}`;
return `v=2.0;ops=${ops};mem=${mem};p=${p};key_len=${key_len};salt=${salt_b64};nonce=${nonce_b64}`;
}
async function deriveKey(passphrase, saltBytes, ops, mem, p, hashLen) {
const result = await argon2.hash({
pass: passphrase,
salt: saltBytes,
time: ops,
mem: mem,
hashLen: hashLen,
parallelism: p,
type: argon2.ArgonType.Argon2id,
});
return result.hash; // Uint8Array
}
async function encrypt(plaintext, passphrase) {
const encoded = new TextEncoder().encode(plaintext);
if (encoded.length > MAX_PAYLOAD_SIZE)
throw new Error(`Plaintext exceeds maximum allowed size (${MAX_PAYLOAD_SIZE} bytes).`);
const salt = crypto.getRandomValues(new Uint8Array(16));
const nonce = crypto.getRandomValues(new Uint8Array(12));
const cfg = KDF_CONFIG;
const salt_b64 = toBase64(salt);
const nonce_b64 = toBase64(nonce);
const keyBytes = await deriveKey(passphrase, salt, cfg.ops, cfg.mem, cfg.p, cfg.key_len);
const aad = new TextEncoder().encode(buildAAD('2.0', cfg.ops, cfg.mem, cfg.p, cfg.key_len, salt_b64, nonce_b64));
const cryptoKey = await crypto.subtle.importKey('raw', keyBytes, 'AES-GCM', false, ['encrypt']);
const ctBuffer = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv: nonce, additionalData: aad, tagLength: 128 },
cryptoKey,
new TextEncoder().encode(plaintext)
);
return JSON.stringify({
header: {
v: '2.0',
kdf: { ops: cfg.ops, mem: cfg.mem, p: cfg.p, key_len: cfg.key_len },
salt: salt_b64,
nonce: nonce_b64,
},
ciphertext: toBase64(new Uint8Array(ctBuffer)),
});
}
async function decrypt(blob, passphrase) {
if (blob.length > MAX_PAYLOAD_SIZE * 2)
throw new Error(`Payload input exceeds maximum allowed size (${MAX_PAYLOAD_SIZE * 2} bytes).`);
let packet;
try { packet = JSON.parse(blob); } catch { throw new Error('Not valid JSON. Paste the full encrypted payload.'); }
const { header, ciphertext: ct_b64 } = packet;
if (!header || !ct_b64) throw new Error('Malformed payload: missing header or ciphertext.');
const v = header.v;
if (!v || !['1.0', '2.0'].includes(v)) throw new Error(`Unsupported version: "${v}".`);
const kdf = header.kdf;
if (!kdf || typeof kdf !== 'object') throw new Error('Missing KDF parameters.');
const { ops, mem, p } = kdf;
if (!Number.isInteger(ops) || !Number.isInteger(mem) || !Number.isInteger(p))
throw new Error('KDF parameters must be integers.');
if (ops < 2 || ops > 10) throw new Error('KDF ops value is outside allowed range.');
if (mem < 32768 || mem > 262144) throw new Error('KDF memory value is outside allowed range.');
if (p < 1 || p > 16) throw new Error('KDF parallelism value is outside allowed range.');
let key_len;
if (v === '1.0') {
key_len = 32;
} else {
key_len = kdf.key_len;
if (![16, 24, 32].includes(key_len)) throw new Error('Invalid key_len in payload.');
}
const salt_b64 = header.salt;
const nonce_b64 = header.nonce;
if (typeof salt_b64 !== 'string' || typeof nonce_b64 !== 'string')
throw new Error('Salt and nonce must be strings.');
const salt = fromBase64(salt_b64);
const nonce = fromBase64(nonce_b64);
const ciphertextWithTag = fromBase64(ct_b64);
if (salt.length !== 16) throw new Error('Invalid salt length.');
if (nonce.length !== 12) throw new Error('Invalid nonce length.');
if (ciphertextWithTag.length < 16) throw new Error('Ciphertext too short to contain auth tag.');
const aad = new TextEncoder().encode(buildAAD(v, ops, mem, p, key_len, salt_b64, nonce_b64));
const keyBytes = await deriveKey(passphrase, salt, ops, mem, p, key_len);
const cryptoKey = await crypto.subtle.importKey('raw', keyBytes, 'AES-GCM', false, ['decrypt']);
let decrypted;
try {
decrypted = await crypto.subtle.decrypt(
{ name: 'AES-GCM', iv: nonce, additionalData: aad, tagLength: 128 },
cryptoKey,
ciphertextWithTag
);
} catch {
throw new Error('Decryption failed. Wrong passphrase or the payload has been tampered with.');
}
return new TextDecoder().decode(decrypted);
}
// =====================================================================
// Button handlers
// =====================================================================
async function doEncrypt() {
const text = document.getElementById('enc-text').value.trim();
const pass = document.getElementById('enc-pass').value;
setStatus('enc-status', '', '');
document.getElementById('enc-output').classList.remove('visible');
if (!text) return setStatus('enc-status', 'Please enter a message to encrypt.', 'error');
if (!pass) return setStatus('enc-status', 'Please enter a passphrase.', 'error');
setLoading('enc-btn', true);
try {
const blob = await encrypt(text, pass);
showOutput('enc-output', 'enc-result', blob);
setStatus('enc-status', '', '');
} catch (e) {
setStatus('enc-status', e.message, 'error');
} finally {
setLoading('enc-btn', false, 'Encrypt');
}
}
async function doDecrypt() {
const blob = document.getElementById('dec-text').value.trim();
const pass = document.getElementById('dec-pass').value;
setStatus('dec-status', '', '');
document.getElementById('dec-output').classList.remove('visible');
if (!blob) return setStatus('dec-status', 'Please paste an encrypted payload.', 'error');
if (!pass) return setStatus('dec-status', 'Please enter the passphrase.', 'error');
setLoading('dec-btn', true);
try {
const plaintext = await decrypt(blob, pass);
showOutput('dec-output', 'dec-result', plaintext);
setStatus('dec-status', '', '');
} catch (e) {
setStatus('dec-status', e.message, 'error');
} finally {
setLoading('dec-btn', false, 'Decrypt');
}
}
</script>
</body>
</html>