askarKeyAeadDecrypt function

AskarResult<Uint8List> askarKeyAeadDecrypt(
  1. LocalKeyHandle handle,
  2. Uint8List ciphertext,
  3. Uint8List nonce, {
  4. Uint8List? tag,
  5. Uint8List? aad,
})

Implementation

AskarResult<Uint8List> askarKeyAeadDecrypt(
  LocalKeyHandle handle,
  Uint8List ciphertext,
  Uint8List nonce, {
  Uint8List? tag,
  Uint8List? aad,
}) {
  Pointer<NativeSecretBuffer> secretBufferPointer = calloc<NativeSecretBuffer>();

  Pointer<NativeByteBuffer> ciphertextPtr = nullptr;
  Pointer<NativeByteBuffer> noncePtr = nullptr;
  Pointer<NativeByteBuffer> tagPtr = nullptr;
  Pointer<NativeByteBuffer> aadPtr = nullptr;

  try {
    ciphertextPtr = bytesListToByteBuffer(ciphertext);
    noncePtr = bytesListToByteBuffer(nonce);
    tagPtr = bytesListToByteBuffer(tag);
    aadPtr = bytesListToByteBuffer(aad);

    final funcResult = nativeAskarKeyAeadDecrypt(
      handle.toInt(),
      ciphertextPtr.ref,
      noncePtr.ref,
      tagPtr.ref,
      aadPtr.ref,
      secretBufferPointer,
    );

    final errorCode = ErrorCode.fromInt(funcResult);

    final decryptedData =
        (errorCode == ErrorCode.success)
            ? secretBufferToBytesList(secretBufferPointer.ref)
            : Uint8List(0);

    return AskarResult<Uint8List>(errorCode, decryptedData);
  } finally {
    freeSecretBufferPointer(secretBufferPointer);
    freeByteBufferPointer(ciphertextPtr);
    freeByteBufferPointer(noncePtr);
    freeByteBufferPointer(tagPtr);
    freeByteBufferPointer(aadPtr);
  }
}