Fix ChaCha20 Alignment Issues

This commit is contained in:
Zack Puhl
2025-12-17 22:57:34 +00:00
committed by Christoffer Lerno
parent 436af4dbca
commit 97a9cab218
5 changed files with 48 additions and 42 deletions

View File

@@ -6,7 +6,7 @@ fn void pointer_add_sub_diff()
int*[<2>] y;
double*[<2>] z = y; // #error: 'int*[<2>]' to 'double*[<2>]'
y / y; // #error: Cannot divide
y % y; // #error: Cannot calculate the reminder
y % y; // #error: Cannot calculate the remainder
y * y; // #error: multiply
y ^ y; // #error: not defined
iptr[<2>] g = (iptr[<2>])y;

View File

@@ -412,28 +412,31 @@ fn void scrolling_input_unaligned_permutations_with_random_chunks()
{
// Paranoia, honestly... Use a known test vector a couple blocks long, and - no matter the alignment started from - ensure the same result.
Lcg64Random rand;
random::seed(&rand, 0x1337_83fb_c1ac_1a20);
random::seed(&rand, 0x1337_83fb_c1ac_eeee);
char[*] key = sha256::hash("dance with me");
char[*] nonce = "123456789abc";
for (usz i = 1; i < ulong.sizeof + 1; i++)
{
for (usz j = 1; j < LARGE_INPUT.len; j++) @pool()
{
char[] unaligned @align(ulong.sizeof) = mem::talloc_array(char, j + ulong.sizeof);
char[] encrypt_me = unaligned[i:j];
encrypt_me[..] = LARGE_INPUT[:j];
for (usz j = 1; j < LARGE_INPUT.len; j++)
{
for (usz k = 1; k < 128; k++) @pool()
{
char[] unaligned @align(ulong.sizeof) = mem::talloc_array(char, 1 + j + ulong.sizeof);
unaligned[i:j] = LARGE_INPUT[:j];
test::@check(chacha20::tencrypt(unaligned[i:j], key, nonce) == LARGE_EXPECTED[:j], "Mismatched permutation of hash on index (%d, %d).", i, j);
ChaCha20 c @noinit;
defer c.destroy();
c.init(key, nonce);
for (usz x = 1; encrypt_me.len; encrypt_me = encrypt_me[x..], x = (rand.next_byte() % min(20, encrypt_me.len ?: 1)) ?: 1) c.transform(encrypt_me[:x]);
test::@check(unaligned[i:j] == LARGE_EXPECTED[:j], "Mismatched permutation of hash on index (%d, %d).", i, j);
char[] encrypt_me = unaligned[i:j];
ChaCha20 c @noinit;
defer c.destroy();
c.init(key, nonce);
for (usz x = 1; encrypt_me.len; encrypt_me = encrypt_me[x..], x = (rand.next_byte() % min(k, encrypt_me.len ?: 1)) ?: 1) c.transform(encrypt_me[:x]);
test::@check(unaligned[i:j] == LARGE_EXPECTED[:j], "Mismatched permutation of hash on index (%d, %d; %d).", i, j, k);
// test::@check(chacha20::tencrypt(unaligned[i:j], key, nonce) == LARGE_EXPECTED[:j], "Mismatched permutation of hash on index (%d, %d).", i, j);
test::@check(chacha20::tencrypt(LARGE_INPUT[:j], key, nonce) == LARGE_EXPECTED[:j], "Mismatched permutation of hash on index (%d, %d).", i, j);
};
test::@check(chacha20::tencrypt(LARGE_INPUT[:j], key, nonce) == LARGE_EXPECTED[:j], "Mismatched permutation of hash on index (%d, %d).", i, j);
};
}
}
}