crypto_util.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. import 'dart:convert';
  2. import 'dart:io' as io;
  3. import 'dart:typed_data';
  4. import 'package:computer/computer.dart';
  5. import 'package:ente_auth/core/errors.dart';
  6. import 'package:ente_auth/models/derived_key_result.dart';
  7. import 'package:ente_auth/models/encryption_result.dart';
  8. import 'package:ente_auth/utils/device_info.dart';
  9. import 'package:flutter_sodium/flutter_sodium.dart';
  10. import 'package:logging/logging.dart';
  11. const int encryptionChunkSize = 4 * 1024 * 1024;
  12. final int decryptionChunkSize =
  13. encryptionChunkSize + Sodium.cryptoSecretstreamXchacha20poly1305Abytes;
  14. const int hashChunkSize = 4 * 1024 * 1024;
  15. const int loginSubKeyLen = 32;
  16. const int loginSubKeyId = 1;
  17. const String loginSubKeyContext = "loginctx";
  18. Uint8List cryptoSecretboxEasy(Map<String, dynamic> args) {
  19. return Sodium.cryptoSecretboxEasy(args["source"], args["nonce"], args["key"]);
  20. }
  21. Uint8List cryptoSecretboxOpenEasy(Map<String, dynamic> args) {
  22. return Sodium.cryptoSecretboxOpenEasy(
  23. args["cipher"],
  24. args["nonce"],
  25. args["key"],
  26. );
  27. }
  28. Uint8List cryptoPwHash(Map<String, dynamic> args) {
  29. return Sodium.cryptoPwhash(
  30. Sodium.cryptoSecretboxKeybytes,
  31. args["password"],
  32. args["salt"],
  33. args["opsLimit"],
  34. args["memLimit"],
  35. Sodium.cryptoPwhashAlgArgon2id13,
  36. );
  37. }
  38. Uint8List cryptoKdfDeriveFromKey(
  39. Map<String, dynamic> args,
  40. ) {
  41. return Sodium.cryptoKdfDeriveFromKey(
  42. args["subkeyLen"],
  43. args["subkeyId"],
  44. args["context"],
  45. args["key"],
  46. );
  47. }
  48. // Returns the hash for a given file, chunking it in batches of hashChunkSize
  49. Future<Uint8List> cryptoGenericHash(Map<String, dynamic> args) async {
  50. final sourceFile = io.File(args["sourceFilePath"]);
  51. final sourceFileLength = await sourceFile.length();
  52. final inputFile = sourceFile.openSync(mode: io.FileMode.read);
  53. final state =
  54. Sodium.cryptoGenerichashInit(null, Sodium.cryptoGenerichashBytesMax);
  55. var bytesRead = 0;
  56. bool isDone = false;
  57. while (!isDone) {
  58. var chunkSize = hashChunkSize;
  59. if (bytesRead + chunkSize >= sourceFileLength) {
  60. chunkSize = sourceFileLength - bytesRead;
  61. isDone = true;
  62. }
  63. final buffer = await inputFile.read(chunkSize);
  64. bytesRead += chunkSize;
  65. Sodium.cryptoGenerichashUpdate(state, buffer);
  66. }
  67. await inputFile.close();
  68. return Sodium.cryptoGenerichashFinal(state, Sodium.cryptoGenerichashBytesMax);
  69. }
  70. EncryptionResult chachaEncryptData(Map<String, dynamic> args) {
  71. final initPushResult =
  72. Sodium.cryptoSecretstreamXchacha20poly1305InitPush(args["key"]);
  73. final encryptedData = Sodium.cryptoSecretstreamXchacha20poly1305Push(
  74. initPushResult.state,
  75. args["source"],
  76. null,
  77. Sodium.cryptoSecretstreamXchacha20poly1305TagFinal,
  78. );
  79. return EncryptionResult(
  80. encryptedData: encryptedData,
  81. header: initPushResult.header,
  82. );
  83. }
  84. // Encrypts a given file, in chunks of encryptionChunkSize
  85. Future<EncryptionResult> chachaEncryptFile(Map<String, dynamic> args) async {
  86. final encryptionStartTime = DateTime.now().millisecondsSinceEpoch;
  87. final logger = Logger("ChaChaEncrypt");
  88. final sourceFile = io.File(args["sourceFilePath"]);
  89. final destinationFile = io.File(args["destinationFilePath"]);
  90. final sourceFileLength = await sourceFile.length();
  91. logger.info("Encrypting file of size " + sourceFileLength.toString());
  92. final inputFile = sourceFile.openSync(mode: io.FileMode.read);
  93. final key = args["key"] ?? Sodium.cryptoSecretstreamXchacha20poly1305Keygen();
  94. final initPushResult =
  95. Sodium.cryptoSecretstreamXchacha20poly1305InitPush(key);
  96. var bytesRead = 0;
  97. var tag = Sodium.cryptoSecretstreamXchacha20poly1305TagMessage;
  98. while (tag != Sodium.cryptoSecretstreamXchacha20poly1305TagFinal) {
  99. var chunkSize = encryptionChunkSize;
  100. if (bytesRead + chunkSize >= sourceFileLength) {
  101. chunkSize = sourceFileLength - bytesRead;
  102. tag = Sodium.cryptoSecretstreamXchacha20poly1305TagFinal;
  103. }
  104. final buffer = await inputFile.read(chunkSize);
  105. bytesRead += chunkSize;
  106. final encryptedData = Sodium.cryptoSecretstreamXchacha20poly1305Push(
  107. initPushResult.state,
  108. buffer,
  109. null,
  110. tag,
  111. );
  112. await destinationFile.writeAsBytes(encryptedData, mode: io.FileMode.append);
  113. }
  114. await inputFile.close();
  115. logger.info(
  116. "Encryption time: " +
  117. (DateTime.now().millisecondsSinceEpoch - encryptionStartTime)
  118. .toString(),
  119. );
  120. return EncryptionResult(key: key, header: initPushResult.header);
  121. }
  122. Future<void> chachaDecryptFile(Map<String, dynamic> args) async {
  123. final logger = Logger("ChaChaDecrypt");
  124. final decryptionStartTime = DateTime.now().millisecondsSinceEpoch;
  125. final sourceFile = io.File(args["sourceFilePath"]);
  126. final destinationFile = io.File(args["destinationFilePath"]);
  127. final sourceFileLength = await sourceFile.length();
  128. logger.info("Decrypting file of size " + sourceFileLength.toString());
  129. final inputFile = sourceFile.openSync(mode: io.FileMode.read);
  130. final pullState = Sodium.cryptoSecretstreamXchacha20poly1305InitPull(
  131. args["header"],
  132. args["key"],
  133. );
  134. var bytesRead = 0;
  135. var tag = Sodium.cryptoSecretstreamXchacha20poly1305TagMessage;
  136. while (tag != Sodium.cryptoSecretstreamXchacha20poly1305TagFinal) {
  137. var chunkSize = decryptionChunkSize;
  138. if (bytesRead + chunkSize >= sourceFileLength) {
  139. chunkSize = sourceFileLength - bytesRead;
  140. }
  141. final buffer = await inputFile.read(chunkSize);
  142. bytesRead += chunkSize;
  143. final pullResult =
  144. Sodium.cryptoSecretstreamXchacha20poly1305Pull(pullState, buffer, null);
  145. await destinationFile.writeAsBytes(pullResult.m, mode: io.FileMode.append);
  146. tag = pullResult.tag;
  147. }
  148. inputFile.closeSync();
  149. logger.info(
  150. "ChaCha20 Decryption time: " +
  151. (DateTime.now().millisecondsSinceEpoch - decryptionStartTime)
  152. .toString(),
  153. );
  154. }
  155. Uint8List chachaDecryptData(Map<String, dynamic> args) {
  156. final pullState = Sodium.cryptoSecretstreamXchacha20poly1305InitPull(
  157. args["header"],
  158. args["key"],
  159. );
  160. final pullResult = Sodium.cryptoSecretstreamXchacha20poly1305Pull(
  161. pullState,
  162. args["source"],
  163. null,
  164. );
  165. return pullResult.m;
  166. }
  167. class CryptoUtil {
  168. // Note: workers are turned on during app startup.
  169. static final Computer _computer = Computer.shared();
  170. static init() {
  171. Sodium.init();
  172. }
  173. static Uint8List base642bin(String b64, {
  174. String? ignore,
  175. int variant = Sodium.base64VariantOriginal,
  176. }) {
  177. return Sodium.base642bin(b64, ignore: ignore, variant: variant);
  178. }
  179. static String bin2base64(Uint8List bin, {
  180. bool urlSafe = false,
  181. }) {
  182. return Sodium.bin2base64(
  183. bin,
  184. variant:
  185. urlSafe ? Sodium.base64VariantUrlsafe : Sodium.base64VariantOriginal,
  186. );
  187. }
  188. static String bin2hex(Uint8List bin) {
  189. return Sodium.bin2hex(bin);
  190. }
  191. static Uint8List hex2bin(String hex) {
  192. return Sodium.hex2bin(hex);
  193. }
  194. // Encrypts the given source, with the given key and a randomly generated
  195. // nonce, using XSalsa20 (w Poly1305 MAC).
  196. // This function runs on the same thread as the caller, so should be used only
  197. // for small amounts of data where thread switching can result in a degraded
  198. // user experience
  199. static EncryptionResult encryptSync(Uint8List source, Uint8List key) {
  200. final nonce = Sodium.randombytesBuf(Sodium.cryptoSecretboxNoncebytes);
  201. final args = <String, dynamic>{};
  202. args["source"] = source;
  203. args["nonce"] = nonce;
  204. args["key"] = key;
  205. final encryptedData = cryptoSecretboxEasy(args);
  206. return EncryptionResult(
  207. key: key,
  208. nonce: nonce,
  209. encryptedData: encryptedData,
  210. );
  211. }
  212. // Decrypts the given cipher, with the given key and nonce using XSalsa20
  213. // (w Poly1305 MAC).
  214. static Future<Uint8List> decrypt(Uint8List cipher,
  215. Uint8List key,
  216. Uint8List nonce,) async {
  217. final args = <String, dynamic>{};
  218. args["cipher"] = cipher;
  219. args["nonce"] = nonce;
  220. args["key"] = key;
  221. return _computer.compute(
  222. cryptoSecretboxOpenEasy,
  223. param: args,
  224. taskName: "decrypt",
  225. );
  226. }
  227. // Decrypts the given cipher, with the given key and nonce using XSalsa20
  228. // (w Poly1305 MAC).
  229. // This function runs on the same thread as the caller, so should be used only
  230. // for small amounts of data where thread switching can result in a degraded
  231. // user experience
  232. static Uint8List decryptSync(Uint8List cipher,
  233. Uint8List key,
  234. Uint8List nonce,) {
  235. final args = <String, dynamic>{};
  236. args["cipher"] = cipher;
  237. args["nonce"] = nonce;
  238. args["key"] = key;
  239. return cryptoSecretboxOpenEasy(args);
  240. }
  241. // Encrypts the given source, with the given key and a randomly generated
  242. // nonce, using XChaCha20 (w Poly1305 MAC).
  243. // This function runs on the isolate pool held by `_computer`.
  244. // TODO: Remove "ChaCha", an implementation detail from the function name
  245. static Future<EncryptionResult> encryptChaCha(Uint8List source,
  246. Uint8List key,) async {
  247. final args = <String, dynamic>{};
  248. args["source"] = source;
  249. args["key"] = key;
  250. return _computer.compute(
  251. chachaEncryptData,
  252. param: args,
  253. taskName: "encryptChaCha",
  254. );
  255. }
  256. // Decrypts the given source, with the given key and header using XChaCha20
  257. // (w Poly1305 MAC).
  258. // TODO: Remove "ChaCha", an implementation detail from the function name
  259. static Future<Uint8List> decryptChaCha(Uint8List source,
  260. Uint8List key,
  261. Uint8List header,) async {
  262. final args = <String, dynamic>{};
  263. args["source"] = source;
  264. args["key"] = key;
  265. args["header"] = header;
  266. return _computer.compute(
  267. chachaDecryptData,
  268. param: args,
  269. taskName: "decryptChaCha",
  270. );
  271. }
  272. // Encrypts the file at sourceFilePath, with the key (if provided) and a
  273. // randomly generated nonce using XChaCha20 (w Poly1305 MAC), and writes it
  274. // to the destinationFilePath.
  275. // If a key is not provided, one is generated and returned.
  276. static Future<EncryptionResult> encryptFile(
  277. String sourceFilePath,
  278. String destinationFilePath, {
  279. Uint8List? key,
  280. }) {
  281. final args = <String, dynamic>{};
  282. args["sourceFilePath"] = sourceFilePath;
  283. args["destinationFilePath"] = destinationFilePath;
  284. args["key"] = key;
  285. return _computer.compute(
  286. chachaEncryptFile,
  287. param: args,
  288. taskName: "encryptFile",
  289. );
  290. }
  291. // Decrypts the file at sourceFilePath, with the given key and header using
  292. // XChaCha20 (w Poly1305 MAC), and writes it to the destinationFilePath.
  293. static Future<void> decryptFile(
  294. String sourceFilePath,
  295. String destinationFilePath,
  296. Uint8List header,
  297. Uint8List key,) {
  298. final args = <String, dynamic>{};
  299. args["sourceFilePath"] = sourceFilePath;
  300. args["destinationFilePath"] = destinationFilePath;
  301. args["header"] = header;
  302. args["key"] = key;
  303. return _computer.compute(
  304. chachaDecryptFile,
  305. param: args,
  306. taskName: "decryptFile",
  307. );
  308. }
  309. // Generates and returns a 256-bit key.
  310. static Uint8List generateKey() {
  311. return Sodium.cryptoSecretboxKeygen();
  312. }
  313. // Generates and returns a random byte buffer of length
  314. // crypto_pwhash_SALTBYTES (16)
  315. static Uint8List getSaltToDeriveKey() {
  316. return Sodium.randombytesBuf(Sodium.cryptoPwhashSaltbytes);
  317. }
  318. // Generates and returns a secret key and the corresponding public key.
  319. static Future<KeyPair> generateKeyPair() async {
  320. return Sodium.cryptoBoxKeypair();
  321. }
  322. // Decrypts the input using the given publicKey-secretKey pair
  323. static Uint8List openSealSync(
  324. Uint8List input,
  325. Uint8List publicKey,
  326. Uint8List secretKey,
  327. ) {
  328. return Sodium.cryptoBoxSealOpen(input, publicKey, secretKey);
  329. }
  330. // Encrypts the input using the given publicKey
  331. static Uint8List sealSync(Uint8List input, Uint8List publicKey) {
  332. return Sodium.cryptoBoxSeal(input, publicKey);
  333. }
  334. // Derives a key for a given password and salt using Argon2id, v1.3.
  335. // The function first attempts to derive a key with both memLimit and opsLimit
  336. // set to their Sensitive variants.
  337. // If this fails, say on a device with insufficient RAM, we retry by halving
  338. // the memLimit and doubling the opsLimit, while ensuring that we stay within
  339. // the min and max limits for both parameters.
  340. // At all points, we ensure that the product of these two variables (the area
  341. // under the graph that determines the amount of work required) is a constant.
  342. static Future<DerivedKeyResult> deriveSensitiveKey(
  343. Uint8List password,
  344. Uint8List salt,
  345. ) async {
  346. final logger = Logger("pwhash");
  347. int memLimit = Sodium.cryptoPwhashMemlimitSensitive;
  348. int opsLimit = Sodium.cryptoPwhashOpslimitSensitive;
  349. if (await isLowSpecDevice()) {
  350. logger.info("low spec device detected");
  351. // When sensitive memLimit (1 GB) is used, on low spec device the OS might
  352. // kill the app with OOM. To avoid that, start with 256 MB and
  353. // corresponding ops limit (16).
  354. // This ensures that the product of these two variables
  355. // (the area under the graph that determines the amount of work required)
  356. // stays the same
  357. // SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE: 1073741824
  358. // SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE: 268435456
  359. // SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE: 4
  360. memLimit = Sodium.cryptoPwhashMemlimitModerate;
  361. final factor = Sodium.cryptoPwhashMemlimitSensitive ~/
  362. Sodium.cryptoPwhashMemlimitModerate; // = 4
  363. opsLimit = opsLimit * factor; // = 16
  364. }
  365. Uint8List key;
  366. while (memLimit >= Sodium.cryptoPwhashMemlimitMin &&
  367. opsLimit <= Sodium.cryptoPwhashOpslimitMax) {
  368. try {
  369. key = await deriveKey(password, salt, memLimit, opsLimit);
  370. return DerivedKeyResult(key, memLimit, opsLimit);
  371. } catch (e, s) {
  372. logger.warning(
  373. "failed to deriveKey mem: $memLimit, ops: $opsLimit", e, s,);
  374. }
  375. memLimit = (memLimit / 2).round();
  376. opsLimit = opsLimit * 2;
  377. }
  378. throw UnsupportedError("Cannot perform this operation on this device");
  379. }
  380. // Derives a key for the given password and salt, using Argon2id, v1.3
  381. // with memory and ops limit hardcoded to their Interactive variants
  382. // NOTE: This is only used while setting passwords for shared links, as an
  383. // extra layer of authentication (atop the access token and collection key).
  384. // More details @ https://ente.io/blog/building-shareable-links/
  385. static Future<DerivedKeyResult> deriveInteractiveKey(
  386. Uint8List password,
  387. Uint8List salt,
  388. ) async {
  389. final int memLimit = Sodium.cryptoPwhashMemlimitInteractive;
  390. final int opsLimit = Sodium.cryptoPwhashOpslimitInteractive;
  391. final key = await deriveKey(password, salt, memLimit, opsLimit);
  392. return DerivedKeyResult(key, memLimit, opsLimit);
  393. }
  394. // Derives a key for a given password, salt, memLimit and opsLimit using
  395. // Argon2id, v1.3.
  396. static Future<Uint8List> deriveKey(
  397. Uint8List password,
  398. Uint8List salt,
  399. int memLimit,
  400. int opsLimit,
  401. ) {
  402. try {
  403. return _computer.compute(
  404. cryptoPwHash,
  405. param: {
  406. "password": password,
  407. "salt": salt,
  408. "memLimit": memLimit,
  409. "opsLimit": opsLimit,
  410. },
  411. taskName: "deriveKey",
  412. );
  413. } catch(e,s) {
  414. final String errMessage = 'failed to deriveKey memLimit: $memLimit and '
  415. 'opsLimit: $opsLimit';
  416. Logger("CryptoUtilDeriveKey").warning(errMessage, e, s);
  417. throw KeyDerivationError();
  418. }
  419. }
  420. // derives a Login key as subKey from the given key by applying KDF
  421. // (Key Derivation Function) with the `loginSubKeyId` and
  422. // `loginSubKeyLen` and `loginSubKeyContext` as context
  423. static Future<Uint8List> deriveLoginKey(
  424. Uint8List key,
  425. ) async {
  426. final Uint8List derivedKey = await _computer.compute(
  427. cryptoKdfDeriveFromKey,
  428. param: {
  429. "key": key,
  430. "subkeyId": loginSubKeyId,
  431. "subkeyLen": loginSubKeyLen,
  432. "context": utf8.encode(loginSubKeyContext),
  433. },
  434. taskName: "deriveLoginKey",
  435. );
  436. // return the first 16 bytes of the derived key
  437. return derivedKey.sublist(0, 16);
  438. }
  439. // Computes and returns the hash of the source file
  440. static Future<Uint8List> getHash(io.File source) {
  441. return _computer.compute(
  442. cryptoGenericHash,
  443. param: {
  444. "sourceFilePath": source.path,
  445. },
  446. taskName: "fileHash",
  447. );
  448. }
  449. }