Use same lock values as mobile clients
[ProtonMail-WebClient.git] / packages / shared / lib / calendar / crypto / encrypt.ts
blobafb9e251140c6d2c373b5b7aa29e841b2401873a
1 import type { PrivateKeyReference, PublicKeyReference, SessionKey } from '@proton/crypto';
2 import { CryptoProxy } from '@proton/crypto';
3 import { stringToUtf8Array } from '@proton/crypto/lib/utils';
5 import type { SimpleMap } from '../../interfaces';
6 import type { EncryptPartResult, SignPartResult } from '../../interfaces/calendar';
8 export function signPart(dataToSign: string, signingKey: PrivateKeyReference): Promise<SignPartResult>;
9 export function signPart(
10     dataToSign: string | undefined,
11     signingKey: PrivateKeyReference
12 ): Promise<SignPartResult | undefined>;
14 export async function signPart(
15     dataToSign: string | undefined,
16     signingKey: PrivateKeyReference
17 ): Promise<SignPartResult | undefined> {
18     if (!dataToSign) {
19         return;
20     }
22     const signature = await CryptoProxy.signMessage({
23         binaryData: stringToUtf8Array(dataToSign),
24         signingKeys: [signingKey],
25         detached: true,
26     });
27     return {
28         data: dataToSign,
29         signature,
30     };
33 export function encryptPart(
34     dataToEncrypt: string,
35     signingKey: PrivateKeyReference,
36     sessionKey: SessionKey
37 ): Promise<EncryptPartResult>;
38 export function encryptPart(
39     dataToEncrypt: string | undefined,
40     signingKey: PrivateKeyReference,
41     sessionKey: SessionKey
42 ): Promise<EncryptPartResult | undefined>;
44 export async function encryptPart(
45     dataToEncrypt: string | undefined,
46     signingKey: PrivateKeyReference,
47     sessionKey: SessionKey
48 ): Promise<EncryptPartResult | undefined> {
49     if (!dataToEncrypt) {
50         return;
51     }
52     const { message: encryptedData, signature: binarySignature } = await CryptoProxy.encryptMessage({
53         binaryData: stringToUtf8Array(dataToEncrypt),
54         signingKeys: [signingKey],
55         sessionKey,
56         format: 'binary',
57         detached: true,
58     });
60     return {
61         dataPacket: encryptedData,
62         signature: await CryptoProxy.getArmoredSignature({ binarySignature }),
63     };
66 export const getEncryptedSessionKey = async ({ data, algorithm }: SessionKey, publicKey: PublicKeyReference) => {
67     const encryptedSessionKey = await CryptoProxy.encryptSessionKey({
68         data,
69         algorithm,
70         encryptionKeys: [publicKey],
71         format: 'binary',
72     });
73     return encryptedSessionKey;
76 export const createSessionKey = async (publicKey: PublicKeyReference) =>
77     CryptoProxy.generateSessionKey({ recipientKeys: publicKey });
79 export const getEncryptedSessionKeysMap = async (
80     sessionKey: SessionKey,
81     publicKeyMap: SimpleMap<PublicKeyReference> = {}
82 ) => {
83     const emails = Object.keys(publicKeyMap);
84     if (!emails.length) {
85         return;
86     }
87     const result: SimpleMap<Uint8Array> = {};
88     await Promise.all(
89         emails.map(async (email) => {
90             const publicKey = publicKeyMap[email];
91             if (!publicKey) {
92                 return;
93             }
94             result[email] = await getEncryptedSessionKey(sessionKey, publicKey);
95         })
96     );
98     return result;