--- /dev/null
+#include <sodium.h>
+#include "utils.h"
+
+static void test(size_t size, u32 ctr)
+{
+ RANDOM_INPUT(key , 32);
+ RANDOM_INPUT(nonce, 12);
+ RANDOM_INPUT(in , 128); // size <= 128
+ u8 out[128]; // size <= 128
+
+ crypto_stream_chacha20_ietf_xor_ic(out, in, size, nonce, ctr, key);
+
+ print_vector(key , 32);
+ print_vector(nonce, 12);
+ print_vector(in , size);
+ print_number(ctr );
+ print_vector(out , size);
+ printf("\n");
+}
+
+int main(void)
+{
+ SODIUM_INIT;
+ FOR (size, 0, 128) { test(size, (u32)rand64()); }
+ return 0;
+}
.PHONY: all clean
-VEC = chacha20 hchacha20 xchacha20 aead_ietf poly1305 \
- blake2b sha512 hmac_sha512 argon2i \
- edDSA edDSA_pk ed_25519 ed_25519_check \
- x25519 x25519_pk key_exchange
+VEC = chacha20 hchacha20 xchacha20 ietf_chacha20 aead_ietf \
+ poly1305 blake2b sha512 hmac_sha512 argon2i \
+ edDSA edDSA_pk ed_25519 ed_25519_check \
+ x25519 x25519_pk key_exchange
# monokex_xk1 monokex_x
VEC2 = $(patsubst %, %.all.vec, $(VEC))
HEADERS = $(patsubst %, %.h.vec , $(VEC))
x25519_pk.all.vec : x25519_pk.vec
hchacha20.all.vec : hchacha20.vec
xchacha20.all.vec : xchacha20.vec
+ietf_chacha20.all.vec : ietf_chacha20.vec
aead_ietf.all.vec : aead_ietf.vec
blake2b.all.vec : blake2b.vec
sha512.all.vec : sha512.vec
}
}
+static void ietf_chacha20(const vector in[], vector *out)
+{
+ const vector *key = in;
+ const vector *nonce = in + 1;
+ const vector *plain = in + 2;
+ u32 ctr = load32_le(in[3].buf);
+ u32 new_ctr = crypto_ietf_chacha20_ctr(out->buf, plain->buf, plain->size,
+ key->buf, nonce->buf, ctr);
+ u32 nb_blocks = plain->size / 64 + (plain->size % 64 != 0);
+ if (new_ctr - ctr != nb_blocks) {
+ printf("FAILURE: IETF Chacha20 returned counter not correct: ");
+ }
+}
+
static void hchacha20(const vector in[], vector *out)
{
const vector *key = in;
key->buf, nonce->buf, ctr);
u64 nb_blocks = plain->size / 64 + (plain->size % 64 != 0);
if (new_ctr - ctr != nb_blocks) {
- printf("FAILURE: Chacha20 returned counter not correct: ");
+ printf("FAILURE: XChacha20 returned counter not correct: ");
}
}
printf("\nTest against vectors");
printf("\n--------------------\n");
status |= TEST(chacha20 , 4);
+ status |= TEST(ietf_chacha20 , 4);
status |= TEST(hchacha20 , 2);
status |= TEST(xchacha20 , 4);
status |= TEST(poly1305 , 2);
out[7] = (in >> 56) & 0xff;
}
-u64 load64_le(const u8 s[8])
+u32 load32_le(const u8 s[4])
{
return (u64)s[0]
| ((u64)s[1] << 8)
| ((u64)s[2] << 16)
- | ((u64)s[3] << 24)
- | ((u64)s[4] << 32)
- | ((u64)s[5] << 40)
- | ((u64)s[6] << 48)
- | ((u64)s[7] << 56);
+ | ((u64)s[3] << 24);
+}
+
+u64 load64_le(const u8 s[8])
+{
+ return load32_le(s) | ((u64)load32_le(s+4) << 32);
}
// Must be seeded with a nonzero value.
void store64_le(u8 out[8], u64 in);
u64 load64_le(const u8 s[8]);
+u32 load32_le(const u8 s[8]);
u64 rand64(); // Pseudo-random 64 bit number, based on xorshift*
void p_random(u8 *stream, size_t size);
void print_vector(const u8 *buf, size_t size);