-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hybrid asymmetric/symmetric encryption for streams (not enabled) (#2016)
We're getting lots of groups wanting to expand their towns. This should allow us to have much bigger towns. just pass around a small number of aes keys instead of N asymmetric keys per stream this sets the default to the new decryption, probably want to run tests against both for the time being
- Loading branch information
Showing
19 changed files
with
1,411 additions
and
720 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export async function generateNewAesGcmKey(): Promise<CryptoKey> { | ||
return crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, true, ['encrypt', 'decrypt']) | ||
} | ||
|
||
export async function exportAesGsmKeyBytes(key: CryptoKey): Promise<Uint8Array> { | ||
const exportedKey = await crypto.subtle.exportKey('raw', key) | ||
return new Uint8Array(exportedKey) | ||
} | ||
|
||
export async function importAesGsmKeyBytes(key: Uint8Array): Promise<CryptoKey> { | ||
return crypto.subtle.importKey('raw', key, 'AES-GCM', true, ['encrypt', 'decrypt']) | ||
} | ||
|
||
export async function encryptAesGcm( | ||
key: CryptoKey, | ||
data: Uint8Array, | ||
): Promise<{ ciphertext: Uint8Array; iv: Uint8Array }> { | ||
// If data is empty, it's obvious what the message is from the result length. | ||
if (data.length === 0) { | ||
throw new Error('Data to encrypt cannot be empty') | ||
} | ||
const iv = crypto.getRandomValues(new Uint8Array(12)) | ||
const encrypted = await crypto.subtle.encrypt( | ||
{ name: 'AES-GCM', iv, tagLength: 128 }, | ||
key, | ||
data, | ||
) | ||
return { ciphertext: new Uint8Array(encrypted), iv } | ||
} | ||
|
||
export async function decryptAesGcm( | ||
key: CryptoKey, | ||
ciphertext: Uint8Array, | ||
iv: Uint8Array, | ||
): Promise<Uint8Array> { | ||
if (iv.length !== 12) { | ||
throw new Error('IV must be 12 bytes') | ||
} | ||
if (ciphertext.length < 17) { | ||
throw new Error('Ciphertext can not be this short') | ||
} | ||
const decrypted = await crypto.subtle.decrypt( | ||
{ name: 'AES-GCM', iv, tagLength: 128 }, | ||
key, | ||
ciphertext, | ||
) | ||
return new Uint8Array(decrypted) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.