From 77dc8fb9a1c04e124335ec3fe0015fd352d68dad Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sat, 15 Sep 2018 15:55:50 -0500 Subject: [PATCH] Fix bug in updating the AES-CTR counter --- LibHac/Aes128CtrStream.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/LibHac/Aes128CtrStream.cs b/LibHac/Aes128CtrStream.cs index 18a9e588..10402f89 100644 --- a/LibHac/Aes128CtrStream.cs +++ b/LibHac/Aes128CtrStream.cs @@ -85,11 +85,15 @@ namespace LibHac private void UpdateCounter(long offset) { ulong off = (ulong)offset >> 4; - for (uint j = 0; j < 0x8; j++) + for (uint j = 0; j < 0x7; j++) { Counter[0x10 - j - 1] = (byte)(off & 0xFF); off >>= 8; } + + // Because the value stored in the counter is offset >> 4, the top 4 bits + // of byte 8 need to have their original value preserved + Counter[8] = (byte)((Counter[8] & 0xF0) | (int)(off & 0x0F)); } public override void Flush()