Simplify sticker upload

This commit is contained in:
Fedor Indutny 2022-12-15 13:47:38 -08:00 committed by GitHub
parent 47b0ee6135
commit d608b81292
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 17 deletions

View file

@ -207,7 +207,6 @@ window.encryptAndUpload = async (
const packKey = getRandomBytes(32); const packKey = getRandomBytes(32);
const encryptionKey = deriveStickerPackKey(packKey); const encryptionKey = deriveStickerPackKey(packKey);
const iv = getRandomBytes(16);
const server = WebAPI.connect({ const server = WebAPI.connect({
username, username,
@ -259,15 +258,14 @@ window.encryptAndUpload = async (
const encryptedManifest = await encrypt( const encryptedManifest = await encrypt(
Proto.StickerPack.encode(manifestProto).finish(), Proto.StickerPack.encode(manifestProto).finish(),
encryptionKey, encryptionKey
iv
); );
const encryptedStickers = uniqueStickers.map(({ imageData }) => { const encryptedStickers = uniqueStickers.map(({ imageData }) => {
if (!imageData?.buffer) { if (!imageData?.buffer) {
throw new Error('encryptStickers: Missing image data on sticker'); throw new Error('encryptStickers: Missing image data on sticker');
} }
return encrypt(imageData.buffer, encryptionKey, iv); return encrypt(imageData.buffer, encryptionKey);
}); });
const packId = await server.putStickers( const packId = await server.putStickers(
@ -283,12 +281,8 @@ window.encryptAndUpload = async (
return { packId, key: hexKey }; return { packId, key: hexKey };
}; };
function encrypt( function encrypt(data: Uint8Array, key: Uint8Array): Uint8Array {
data: Uint8Array, const { ciphertext } = encryptAttachment(data, key);
key: Uint8Array,
iv: Uint8Array
): Uint8Array {
const { ciphertext } = encryptAttachment(data, key, iv);
return ciphertext; return ciphertext;
} }

View file

@ -455,8 +455,7 @@ export function decryptAttachment(
export function encryptAttachment( export function encryptAttachment(
plaintext: Uint8Array, plaintext: Uint8Array,
keys: Uint8Array, keys: Uint8Array
iv: Uint8Array
): EncryptedAttachment { ): EncryptedAttachment {
if (!(plaintext instanceof Uint8Array)) { if (!(plaintext instanceof Uint8Array)) {
throw new TypeError( throw new TypeError(
@ -467,9 +466,7 @@ export function encryptAttachment(
if (keys.byteLength !== 64) { if (keys.byteLength !== 64) {
throw new Error('Got invalid length attachment keys'); throw new Error('Got invalid length attachment keys');
} }
if (iv.byteLength !== 16) { const iv = getRandomBytes(16);
throw new Error('Got invalid length attachment iv');
}
const aesKey = keys.slice(0, 32); const aesKey = keys.slice(0, 32);
const macKey = keys.slice(32, 64); const macKey = keys.slice(32, 64);

View file

@ -700,9 +700,8 @@ export default class MessageSender {
const padded = this.getPaddedAttachment(data); const padded = this.getPaddedAttachment(data);
const key = getRandomBytes(64); const key = getRandomBytes(64);
const iv = getRandomBytes(16);
const result = encryptAttachment(padded, key, iv); const result = encryptAttachment(padded, key);
const id = await this.server.putAttachment(result.ciphertext); const id = await this.server.putAttachment(result.ciphertext);
const proto = new Proto.AttachmentPointer(); const proto = new Proto.AttachmentPointer();