-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcryptoHandler.ts
More file actions
166 lines (147 loc) · 5.17 KB
/
cryptoHandler.ts
File metadata and controls
166 lines (147 loc) · 5.17 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
// Copyright 2025 Flower Labs GmbH. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================
import getRandomValues from 'get-random-values';
import { FailureCode, Message, Result } from '../../typing';
import { KeyManager } from './keyManager';
import { NetworkService } from './networkService';
const webCrypto = (globalThis as { crypto?: Crypto }).crypto;
const requireWebCrypto = () => {
if (!webCrypto) {
throw new Error('Web Crypto API is not available.');
}
return webCrypto;
};
const GCM_IV_LENGTH = 12;
const BIT_TAG_LENGTH = 8 * 16; // 128 bits or 16 bytes
const CRYPTO_ALG = 'AES-GCM';
/** Orchestrates key management, key exchange, and message encryption/decryption */
export class CryptographyHandler {
private keyManager: KeyManager;
private networkService: NetworkService;
private sharedSecretKey: ArrayBuffer | null = null;
#encryptionId: string | null = null;
constructor(serverUrl: string, apiKey: string) {
this.keyManager = new KeyManager();
this.networkService = new NetworkService(serverUrl, apiKey);
}
get encryptionId() {
return this.#encryptionId;
}
async initializeKeysAndExchange(): Promise<Result<void>> {
if (this.networkService.isClientKeyExpired()) {
await this.keyManager.generateKeyPair();
const clientPublicKey = await this.keyManager.exportPublicKey();
if (!clientPublicKey.ok) {
return clientPublicKey;
}
const encryptionId = await this.networkService.submitClientPublicKey(clientPublicKey.value);
if (!encryptionId.ok) {
return encryptionId;
}
this.#encryptionId = encryptionId.value;
}
const serverPublicKey = await this.networkService.getServerPublicKey();
if (!serverPublicKey.ok) {
return serverPublicKey;
}
const sharedSecretKey = await this.keyManager.deriveSharedSecret(serverPublicKey.value);
if (!sharedSecretKey.ok) {
return sharedSecretKey;
}
this.sharedSecretKey = sharedSecretKey.value;
return { ok: true, value: undefined };
}
async encryptMessage(message: string): Promise<Result<string>> {
if (!this.sharedSecretKey) {
return {
ok: false,
failure: {
code: FailureCode.EncryptionError,
description: 'Shared secret is not derived.',
},
};
}
try {
const iv = getRandomValues(new Uint8Array(GCM_IV_LENGTH));
const aesKey = await requireWebCrypto().subtle.importKey(
'raw',
this.sharedSecretKey,
{ name: CRYPTO_ALG },
false,
['encrypt']
);
const encodedMessage = new TextEncoder().encode(message);
const encryptedData = await requireWebCrypto().subtle.encrypt(
{ name: CRYPTO_ALG, iv, tagLength: BIT_TAG_LENGTH },
aesKey,
encodedMessage
);
const encryptedBytes = new Uint8Array(encryptedData);
const combined = new Uint8Array(iv.length + encryptedBytes.length);
combined.set(iv, 0);
combined.set(encryptedBytes, iv.length);
return { ok: true, value: btoa(String.fromCharCode(...combined)) };
} catch (error) {
return {
ok: false,
failure: { code: FailureCode.EncryptionError, description: String(error) },
};
}
}
async encryptMessages(messages: Message[]): Promise<Result<void>> {
for (const message of messages) {
const encryptedContent = await this.encryptMessage(message.content);
if (!encryptedContent.ok) {
return encryptedContent;
}
message.content = encryptedContent.value;
}
return { ok: true, value: undefined };
}
async decryptMessage(encryptedMessage: string): Promise<Result<string>> {
if (!this.sharedSecretKey) {
return {
ok: false,
failure: {
code: FailureCode.EncryptionError,
description: 'Shared secret is not derived.',
},
};
}
try {
const data = Uint8Array.from(atob(encryptedMessage), (char) => char.charCodeAt(0));
const iv = data.slice(0, GCM_IV_LENGTH);
const ciphertext = data.slice(GCM_IV_LENGTH);
const aesKey = await requireWebCrypto().subtle.importKey(
'raw',
this.sharedSecretKey,
{ name: CRYPTO_ALG },
false,
['decrypt']
);
const plaintext = await requireWebCrypto().subtle.decrypt(
{ name: CRYPTO_ALG, iv },
aesKey,
ciphertext
);
return { ok: true, value: new TextDecoder().decode(plaintext) };
} catch (error: unknown) {
return {
ok: false,
failure: { code: FailureCode.EncryptionError, description: String(error) },
};
}
}
}