aeadEncrypt function
Performs AES-GCM encryption following the QUIC nonce construction.
Implementation
Uint8List? aeadEncrypt(
Uint8List key,
Uint8List iv,
int packetNumber,
Uint8List plaintext,
Uint8List aad,
) {
try {
// 1. Determine Key length
if (key.length != 16 && key.length != 32) {
throw Exception(
"Unsupported key length: ${key.length}. Must be 16 or 32 bytes.",
);
}
// 2. Compute Nonce
final nonce = computeNonce(iv, packetNumber);
// 3. Perform Encryption (This needs a proper AEAD encrypt primitive)
// Since the JS function returns the combined ciphertext+tag, we need a helper
// that produces both the ciphertext and the 16-byte tag.
// This part is highly dependent on the Dart crypto library used.
// For simplicity, we assume an encrypt primitive that returns a combined result.
// NOTE: For GCM, the primitive must return (ciphertext + tag).
// --- Placeholder for combined encryption primitive ---
Uint8List combinedCiphertextTag(
Uint8List key,
Uint8List nonce,
Uint8List plaintext,
Uint8List aad,
) {
final encrypted = encrypt(
encryptionKey: key,
message: plaintext,
nonce: nonce,
aead: aad,
);
// return Uint8List.fromList([...aad, ...encrypted]);
return encrypted;
}
// --- End Placeholder ---
final combined = combinedCiphertextTag(key, nonce, plaintext, aad);
return combined;
} catch (e, st) {
print("AEAD Encryption failed: $e");
print(st);
return null;
}
}