@immahq/aegis 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/e2ee.js ADDED
@@ -0,0 +1,207 @@
1
+ import { Logger } from "./logger.js";
2
+ import { IdentityManager } from "./identity-manager.js";
3
+ import { SessionManager } from "./session-manager.js";
4
+ import { CryptoManager } from "./crypto-manager.js";
5
+ import { RatchetManager } from "./ratchet-manager.js";
6
+ import { ReplayProtection } from "./replay-protection.js";
7
+ import { GroupManager } from "./group-manager.js";
8
+ export class E2EE {
9
+ constructor(storage) {
10
+ Object.defineProperty(this, "identityManager", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
16
+ Object.defineProperty(this, "sessionManager", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ Object.defineProperty(this, "cryptoManager", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ Object.defineProperty(this, "ratchetManager", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ Object.defineProperty(this, "replayProtection", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: void 0
39
+ });
40
+ Object.defineProperty(this, "groupManager", {
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true,
44
+ value: void 0
45
+ });
46
+ Object.defineProperty(this, "storage", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: void 0
51
+ });
52
+ this.storage = storage;
53
+ this.identityManager = new IdentityManager(storage);
54
+ this.sessionManager = new SessionManager(storage);
55
+ this.cryptoManager = new CryptoManager(storage);
56
+ this.ratchetManager = new RatchetManager();
57
+ this.replayProtection = new ReplayProtection();
58
+ this.groupManager = new GroupManager(storage);
59
+ Logger.log("E2EE", "Initialized with storage adapter");
60
+ }
61
+ async createIdentity() {
62
+ return this.identityManager.createIdentity();
63
+ }
64
+ async getIdentity() {
65
+ return this.identityManager.getIdentity();
66
+ }
67
+ async getPublicBundle() {
68
+ return this.identityManager.getPublicBundle();
69
+ }
70
+ async rotateIdentity() {
71
+ return this.identityManager.rotateIdentity();
72
+ }
73
+ async createSession(peerBundle) {
74
+ const identity = await this.identityManager.getIdentity();
75
+ return this.sessionManager.createSession(identity, peerBundle);
76
+ }
77
+ async createResponderSession(peerBundle, ciphertext, initiatorConfirmationMac) {
78
+ const identity = await this.identityManager.getIdentity();
79
+ return this.sessionManager.createResponderSession(identity, peerBundle, ciphertext, initiatorConfirmationMac);
80
+ }
81
+ async confirmSession(sessionId, responderConfirmationMac) {
82
+ return this.sessionManager.confirmSession(sessionId, responderConfirmationMac);
83
+ }
84
+ async getSessions() {
85
+ return this.sessionManager.getSessions();
86
+ }
87
+ async cleanupOldSessions(maxAge) {
88
+ return this.sessionManager.cleanupOldSessions(maxAge);
89
+ }
90
+ async encryptMessage(sessionId, plaintext) {
91
+ const session = await this.storage.getSession(sessionId);
92
+ if (!session)
93
+ throw new Error("Session not found");
94
+ const identity = await this.identityManager.getIdentity();
95
+ const shouldRatchet = (session) => {
96
+ return this.ratchetManager.shouldPerformSendingRatchet(session);
97
+ };
98
+ const performSendingRatchet = (session) => {
99
+ return this.ratchetManager.performSendingRatchet(session);
100
+ };
101
+ const updateSessionState = async (sessionId, session) => {
102
+ await this.storage.saveSession(sessionId, session);
103
+ };
104
+ return this.cryptoManager.encryptMessage(sessionId, plaintext, identity, shouldRatchet, performSendingRatchet, updateSessionState);
105
+ }
106
+ async decryptMessage(sessionId, encrypted) {
107
+ const session = await this.storage.getSession(sessionId);
108
+ if (!session)
109
+ throw new Error("Session not found");
110
+ const needsReceivingRatchet = (session, header) => {
111
+ return this.ratchetManager.needsReceivingRatchet(session, header);
112
+ };
113
+ const performReceivingRatchet = (session, kemCiphertext) => {
114
+ return this.ratchetManager.performReceivingRatchet(session, kemCiphertext);
115
+ };
116
+ const getSkippedKeyId = (ratchetPublicKey, messageNumber) => {
117
+ return this.replayProtection.getSkippedKeyId(ratchetPublicKey, messageNumber);
118
+ };
119
+ const storeReceivedMessageId = (session, messageId) => {
120
+ this.replayProtection.storeReceivedMessageId(session, messageId);
121
+ };
122
+ const cleanupSkippedKeys = (session) => {
123
+ this.replayProtection.cleanupSkippedKeys(session);
124
+ };
125
+ const applyPendingRatchet = (session) => {
126
+ return this.ratchetManager.applyPendingRatchet(session);
127
+ };
128
+ const getDecryptionChainForRatchetMessage = (session) => {
129
+ return this.ratchetManager.getDecryptionChainForRatchetMessage(session);
130
+ };
131
+ const updateSessionState = async (sessionId, session) => {
132
+ await this.storage.saveSession(sessionId, session);
133
+ };
134
+ return this.cryptoManager.decryptMessage(sessionId, encrypted, needsReceivingRatchet, performReceivingRatchet, getSkippedKeyId, storeReceivedMessageId, cleanupSkippedKeys, applyPendingRatchet, getDecryptionChainForRatchetMessage, updateSessionState);
135
+ }
136
+ async triggerRatchet(sessionId) {
137
+ const session = await this.storage.getSession(sessionId);
138
+ if (!session)
139
+ throw new Error("Session not found");
140
+ const updatedSession = await this.ratchetManager.triggerRatchet(sessionId, session);
141
+ await this.storage.saveSession(sessionId, updatedSession);
142
+ }
143
+ async getReplayProtectionStatus(sessionId) {
144
+ const session = await this.storage.getSession(sessionId);
145
+ if (!session)
146
+ throw new Error("Session not found");
147
+ return this.replayProtection.getReplayProtectionStatus(sessionId, session);
148
+ }
149
+ async getConfirmationMac(sessionId) {
150
+ const session = await this.storage.getSession(sessionId);
151
+ if (!session || !session.confirmationMac) {
152
+ return null;
153
+ }
154
+ return session.confirmationMac;
155
+ }
156
+ getStorage() {
157
+ return this.storage;
158
+ }
159
+ async createGroup(name, members, memberKemPublicKeys, memberDsaPublicKeys) {
160
+ const identity = await this.identityManager.getIdentity();
161
+ if (!identity)
162
+ throw new Error("Identity not found");
163
+ await this.groupManager.initialize(identity);
164
+ return this.groupManager.createGroup(name, members, memberKemPublicKeys, memberDsaPublicKeys);
165
+ }
166
+ async addGroupMember(groupId, userId, session, userPublicKey) {
167
+ const identity = await this.identityManager.getIdentity();
168
+ if (!identity)
169
+ throw new Error("Identity not found");
170
+ await this.groupManager.initialize(identity);
171
+ return this.groupManager.addMember(groupId, userId, session, userPublicKey);
172
+ }
173
+ async removeGroupMember(groupId, userId) {
174
+ const identity = await this.identityManager.getIdentity();
175
+ if (!identity)
176
+ throw new Error("Identity not found");
177
+ await this.groupManager.initialize(identity);
178
+ return this.groupManager.removeMember(groupId, userId);
179
+ }
180
+ async updateGroupKey(groupId) {
181
+ const identity = await this.identityManager.getIdentity();
182
+ if (!identity)
183
+ throw new Error("Identity not found");
184
+ await this.groupManager.initialize(identity);
185
+ return this.groupManager.updateGroupKey(groupId);
186
+ }
187
+ async encryptGroupMessage(groupId, message) {
188
+ const identity = await this.identityManager.getIdentity();
189
+ if (!identity)
190
+ throw new Error("Identity not found");
191
+ await this.groupManager.initialize(identity);
192
+ return this.groupManager.encryptMessage(groupId, message);
193
+ }
194
+ async decryptGroupMessage(groupId, encrypted) {
195
+ const identity = await this.identityManager.getIdentity();
196
+ if (!identity)
197
+ throw new Error("Identity not found");
198
+ await this.groupManager.initialize(identity);
199
+ return this.groupManager.decryptMessage(groupId, encrypted);
200
+ }
201
+ async getGroup(groupId) {
202
+ return this.groupManager.getGroup(groupId);
203
+ }
204
+ async getGroups() {
205
+ return this.groupManager.getGroups();
206
+ }
207
+ }