tomSecretsOpenBundle function

Future<Uint8List> tomSecretsOpenBundle({
  1. required List<int> bundleKey,
  2. required String vault,
  3. required int serviceStart,
  4. required Map<String, Object?> bundle,
})

Opens the bundle object of unlock for vault: the plaintext bytes, which the caller wipes after use. Throws TomSecretsBundleException when it is malformed or does not open (another key, vault or service start).

Implementation

Future<Uint8List> tomSecretsOpenBundle({
  required List<int> bundleKey,
  required String vault,
  required int serviceStart,
  required Map<String, Object?> bundle,
}) async {
  final Uint8List nonce;
  final Uint8List sealed;
  try {
    nonce = base64.decode(bundle['nonce']! as String);
    sealed = base64.decode(bundle['ciphertext']! as String);
  } on Object {
    throw const TomSecretsBundleException('no sealed bundle');
  }
  const tag = 16;
  if (nonce.length != tomSecretsBundleNonceBytes || sealed.length < tag) {
    throw const TomSecretsBundleException('no sealed bundle');
  }
  try {
    final plain = await _aesGcm.decrypt(
      SecretBox(
        sealed.sublist(0, sealed.length - tag),
        nonce: nonce,
        mac: Mac(sealed.sublist(sealed.length - tag)),
      ),
      secretKey: SecretKeyData(bundleKey),
      aad: tomSecretsBundleAad(vault, serviceStart),
    );
    return Uint8List.fromList(plain);
  } on SecretBoxAuthenticationError {
    throw const TomSecretsBundleException('the bundle does not open');
  }
}