}
}
-static void chacha20_core(u32 input[16], u8 *cipher_text, const u8 *plain_text,
- size_t text_size)
+static u64 chacha20_core(u32 input[16], u8 *cipher_text, const u8 *plain_text,
+ size_t text_size)
{
// Whole blocks
u32 pool[16];
WIPE_BUFFER(tmp);
}
WIPE_BUFFER(pool);
+ return input[12] + ((u64)input[13] << 32) + (text_size > 0);
}
void crypto_hchacha20(u8 out[32], const u8 key[32], const u8 in [16])
WIPE_BUFFER(block);
}
-void crypto_chacha20_ctr(u8 *cipher_text, const u8 *plain_text,
- size_t text_size, const u8 key[32], const u8 nonce[8],
- u64 ctr)
+u64 crypto_chacha20_ctr(u8 *cipher_text, const u8 *plain_text,
+ size_t text_size, const u8 key[32], const u8 nonce[8],
+ u64 ctr)
{
u32 input[16];
chacha20_init_key(input, key);
input[13] = (u32)(ctr >> 32);
input[14] = load32_le(nonce);
input[15] = load32_le(nonce + 4);
- chacha20_core(input, cipher_text, plain_text, text_size);
+ ctr = chacha20_core(input, cipher_text, plain_text, text_size);
WIPE_BUFFER(input);
+ return ctr;
}
-void crypto_ietf_chacha20_ctr(u8 *cipher_text, const u8 *plain_text,
- size_t text_size,
- const u8 key[32], const u8 nonce[12], u32 ctr)
+u32 crypto_ietf_chacha20_ctr(u8 *cipher_text, const u8 *plain_text,
+ size_t text_size,
+ const u8 key[32], const u8 nonce[12], u32 ctr)
{
u32 input[16];
chacha20_init_key(input, key);
input[13] = load32_le(nonce);
input[14] = load32_le(nonce + 4);
input[15] = load32_le(nonce + 8);
- chacha20_core(input, cipher_text, plain_text, text_size);
+ ctr = chacha20_core(input, cipher_text, plain_text, text_size);
WIPE_BUFFER(input);
+ return ctr;
}
-void crypto_xchacha20_ctr(u8 *cipher_text, const u8 *plain_text,
- size_t text_size,
- const u8 key[32], const u8 nonce[24], u64 ctr)
+u64 crypto_xchacha20_ctr(u8 *cipher_text, const u8 *plain_text,
+ size_t text_size,
+ const u8 key[32], const u8 nonce[24], u64 ctr)
{
u8 sub_key[32];
crypto_hchacha20(sub_key, key, nonce);
- crypto_chacha20_ctr(cipher_text, plain_text, text_size,
- sub_key, nonce+16, ctr);
+ ctr = crypto_chacha20_ctr(cipher_text, plain_text, text_size,
+ sub_key, nonce+16, ctr);
WIPE_BUFFER(sub_key);
+ return ctr;
}
void crypto_chacha20(u8 *cipher_text, const u8 *plain_text, size_t text_size,
size_t text_size,
const uint8_t key[32],
const uint8_t nonce[12]);
-void crypto_chacha20_ctr(uint8_t *cipher_text,
- const uint8_t *plain_text,
- size_t text_size,
- const uint8_t key[32],
- const uint8_t nonce[8],
- uint64_t ctr);
-void crypto_xchacha20_ctr(uint8_t *cipher_text,
- const uint8_t *plain_text,
- size_t text_size,
- const uint8_t key[32],
- const uint8_t nonce[24],
- uint64_t ctr);
-void crypto_ietf_chacha20_ctr(uint8_t *cipher_text,
+uint64_t crypto_chacha20_ctr(uint8_t *cipher_text,
+ const uint8_t *plain_text,
+ size_t text_size,
+ const uint8_t key[32],
+ const uint8_t nonce[8],
+ uint64_t ctr);
+uint64_t crypto_xchacha20_ctr(uint8_t *cipher_text,
const uint8_t *plain_text,
size_t text_size,
const uint8_t key[32],
- const uint8_t nonce[12],
- uint32_t ctr);
+ const uint8_t nonce[24],
+ uint64_t ctr);
+uint32_t crypto_ietf_chacha20_ctr(uint8_t *cipher_text,
+ const uint8_t *plain_text,
+ size_t text_size,
+ const uint8_t key[32],
+ const uint8_t nonce[12],
+ uint32_t ctr);
// Poly 1305
const vector *key = in;
const vector *nonce = in + 1;
const vector *plain = in + 2;
- u64 ctr = load64_le(in[3].buf);
- crypto_chacha20_ctr(out->buf, plain->buf, plain->size,
- key->buf, nonce->buf, ctr);
+ u64 ctr = load64_le(in[3].buf);
+ u64 new_ctr = crypto_chacha20_ctr(out->buf, plain->buf, plain->size,
+ 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: ");
+ }
}
static void hchacha20(const vector in[], vector *out)
const vector *key = in;
const vector *nonce = in + 1;
const vector *plain = in + 2;
- u64 ctr = load64_le(in[3].buf);
- crypto_xchacha20_ctr(out->buf, plain->buf, plain->size,
- key->buf, nonce->buf, ctr);
+ u64 ctr = load64_le(in[3].buf);
+ u64 new_ctr = crypto_xchacha20_ctr(out->buf, plain->buf, plain->size,
+ 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: ");
+ }
}
static void poly1305(const vector in[], vector *out)