mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Fix ChaCha20 Alignment Issues
This commit is contained in:
committed by
Christoffer Lerno
parent
436af4dbca
commit
97a9cab218
@@ -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);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user