srp.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import ComlinkCryptoWorker from "@ente/shared/crypto";
  2. import { generateLoginSubKey } from "@ente/shared/crypto/helpers";
  3. import { KeyAttributes } from "@ente/shared/user/types";
  4. import { generateSRPSetupAttributes } from "../services/srp";
  5. import { SRPSetupAttributes } from "../types/srp";
  6. export async function generateKeyAndSRPAttributes(passphrase: string): Promise<{
  7. keyAttributes: KeyAttributes;
  8. masterKey: string;
  9. srpSetupAttributes: SRPSetupAttributes;
  10. }> {
  11. const cryptoWorker = await ComlinkCryptoWorker.getInstance();
  12. const masterKey = await cryptoWorker.generateEncryptionKey();
  13. const recoveryKey = await cryptoWorker.generateEncryptionKey();
  14. const kekSalt = await cryptoWorker.generateSaltToDeriveKey();
  15. const kek = await cryptoWorker.deriveSensitiveKey(passphrase, kekSalt);
  16. const masterKeyEncryptedWithKek = await cryptoWorker.encryptToB64(
  17. masterKey,
  18. kek.key,
  19. );
  20. const masterKeyEncryptedWithRecoveryKey = await cryptoWorker.encryptToB64(
  21. masterKey,
  22. recoveryKey,
  23. );
  24. const recoveryKeyEncryptedWithMasterKey = await cryptoWorker.encryptToB64(
  25. recoveryKey,
  26. masterKey,
  27. );
  28. const keyPair = await cryptoWorker.generateKeyPair();
  29. const encryptedKeyPairAttributes = await cryptoWorker.encryptToB64(
  30. keyPair.privateKey,
  31. masterKey,
  32. );
  33. const loginSubKey = await generateLoginSubKey(kek.key);
  34. const srpSetupAttributes = await generateSRPSetupAttributes(loginSubKey);
  35. const keyAttributes: KeyAttributes = {
  36. kekSalt,
  37. encryptedKey: masterKeyEncryptedWithKek.encryptedData,
  38. keyDecryptionNonce: masterKeyEncryptedWithKek.nonce,
  39. publicKey: keyPair.publicKey,
  40. encryptedSecretKey: encryptedKeyPairAttributes.encryptedData,
  41. secretKeyDecryptionNonce: encryptedKeyPairAttributes.nonce,
  42. opsLimit: kek.opsLimit,
  43. memLimit: kek.memLimit,
  44. masterKeyEncryptedWithRecoveryKey:
  45. masterKeyEncryptedWithRecoveryKey.encryptedData,
  46. masterKeyDecryptionNonce: masterKeyEncryptedWithRecoveryKey.nonce,
  47. recoveryKeyEncryptedWithMasterKey:
  48. recoveryKeyEncryptedWithMasterKey.encryptedData,
  49. recoveryKeyDecryptionNonce: recoveryKeyEncryptedWithMasterKey.nonce,
  50. };
  51. return {
  52. keyAttributes,
  53. masterKey,
  54. srpSetupAttributes,
  55. };
  56. }