Replacement for #3940
Improve valid MAC detection

Implement constant time string comparison as well as double HMAC
verification for encryption MACs.
Merge. Fix conflicts.
Add security to encryption's composer file.
Merge.