chacha20
This commit is contained in:
174
chacha20.h
Normal file
174
chacha20.h
Normal file
@@ -0,0 +1,174 @@
|
||||
/* define SLOWGRAPH_IMPL */
|
||||
|
||||
#ifndef SLOWCRYPT_CHACHA20_H
|
||||
#define SLOWCRYPT_CHACHA20_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef SLOWCRYPT_CHACHA20_FUNC
|
||||
#define SLOWCRYPT_CHACHA20_FUNC /**/
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint32_t state[16];
|
||||
} slowcrypt_chacha20;
|
||||
|
||||
/*
|
||||
* initialize state, run 20 iterations, serialize, xor data inplace
|
||||
*
|
||||
* does NOT zeroize states! zeroize manually when done.
|
||||
*/
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_block(slowcrypt_chacha20 state[2], char const key[32],
|
||||
uint32_t block_ctr, char const nonce[12],
|
||||
char data[64]);
|
||||
|
||||
/* call this to zero out memory */
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_deinit(slowcrypt_chacha20 *state);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(slowcrypt_chacha20 *state,
|
||||
char const key[32],
|
||||
uint32_t block_ctr,
|
||||
char const nonce[12]);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_serialize(char buf[64], slowcrypt_chacha20 const *state);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_serialize_xor(char buf[64], slowcrypt_chacha20 const *state);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20 *state,
|
||||
slowcrypt_chacha20 *swap,
|
||||
int num_rounds);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC uint32_t slowcrypt_chacha20_read_ul32(char const *buf);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_write_ul32(char *buf,
|
||||
uint32_t val);
|
||||
|
||||
#define SLOWCRYPT_CHACHA20_LAST32(n, bits) (((uint32_t)(n)) >> (32 - (bits)))
|
||||
|
||||
#define SLOWCRYPT_CHACHA20_ROL32(n, by) \
|
||||
((((uint32_t)(n)) << (by)) | SLOWCRYPT_CHACHA20_LAST32((n), (by)))
|
||||
|
||||
#define SLOWCRYPT_CHACHA20_QROUND(state, a, b, c, d) \
|
||||
do { \
|
||||
state[a] += state[b]; \
|
||||
state[d] ^= state[a]; \
|
||||
state[d] = SLOWCRYPT_CHACHA20_ROL32(state[d], 16); \
|
||||
\
|
||||
state[c] += state[d]; \
|
||||
state[b] ^= state[c]; \
|
||||
state[b] = SLOWCRYPT_CHACHA20_ROL32(state[b], 12); \
|
||||
\
|
||||
state[a] += state[b]; \
|
||||
state[d] ^= state[a]; \
|
||||
state[d] = SLOWCRYPT_CHACHA20_ROL32(state[d], 8); \
|
||||
\
|
||||
state[c] += state[d]; \
|
||||
state[b] ^= state[c]; \
|
||||
state[b] = SLOWCRYPT_CHACHA20_ROL32(state[b], 7); \
|
||||
} while (0)
|
||||
|
||||
#ifdef SLOWCRYPT_CHACHA20_IMPL
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_deinit(slowcrypt_chacha20 *state) {
|
||||
int i;
|
||||
for (i = 0; i < 16; i++)
|
||||
*(volatile int *)&state->state[i] = 0;
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC uint32_t slowcrypt_chacha20_read_ul32(char const *buf) {
|
||||
uint32_t o = (uint32_t)((uint8_t const *)buf)[0];
|
||||
o |= (uint32_t)((uint8_t const *)buf)[1] << 8;
|
||||
o |= (uint32_t)((uint8_t const *)buf)[2] << 16;
|
||||
o |= (uint32_t)((uint8_t const *)buf)[3] << 24;
|
||||
return o;
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_write_ul32(char *buf,
|
||||
uint32_t val) {
|
||||
((uint8_t *)buf)[0] = (uint8_t)(val & 0xFF);
|
||||
((uint8_t *)buf)[1] = (uint8_t)((val >> 8) & 0xFF);
|
||||
((uint8_t *)buf)[2] = (uint8_t)((val >> 16) & 0xFF);
|
||||
((uint8_t *)buf)[3] = (uint8_t)((val >> 24) & 0xFF);
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(slowcrypt_chacha20 *state,
|
||||
char const key[32],
|
||||
uint32_t block_ctr,
|
||||
char const nonce[12]) {
|
||||
int i;
|
||||
state->state[0] = 0x61707865;
|
||||
state->state[1] = 0x3320646e;
|
||||
state->state[2] = 0x79622d32;
|
||||
state->state[3] = 0x6b206574;
|
||||
for (i = 0; i < 8; i++)
|
||||
state->state[4 + i] = slowcrypt_chacha20_read_ul32(&key[i * 4]);
|
||||
state->state[12] = block_ctr;
|
||||
for (i = 0; i < 3; i++)
|
||||
state->state[13 + i] = slowcrypt_chacha20_read_ul32(&nonce[i * 4]);
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_serialize(char buf[64], slowcrypt_chacha20 const *state) {
|
||||
int i;
|
||||
for (i = 0; i < 16; i++)
|
||||
slowcrypt_chacha20_write_ul32(&buf[i * 4], state->state[i]);
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_serialize_xor(char buf[64],
|
||||
slowcrypt_chacha20 const *state) {
|
||||
char swp[4];
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
slowcrypt_chacha20_write_ul32(swp, state->state[i]);
|
||||
for (j = 0; j < 4; j++)
|
||||
buf[i * 4 + j] ^= swp[j];
|
||||
}
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20 *state,
|
||||
slowcrypt_chacha20 *swap,
|
||||
int num_rounds) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i++)
|
||||
swap->state[i] = state->state[i];
|
||||
|
||||
for (i = 0; i < num_rounds; i++) {
|
||||
if (i % 2 == 0) {
|
||||
/* column round */
|
||||
SLOWCRYPT_CHACHA20_QROUND(state->state, 0, 4, 8, 12);
|
||||
SLOWCRYPT_CHACHA20_QROUND(state->state, 1, 5, 9, 13);
|
||||
SLOWCRYPT_CHACHA20_QROUND(state->state, 2, 6, 10, 14);
|
||||
SLOWCRYPT_CHACHA20_QROUND(state->state, 3, 7, 11, 15);
|
||||
} else {
|
||||
/* diagonal round */
|
||||
SLOWCRYPT_CHACHA20_QROUND(state->state, 0, 5, 10, 15);
|
||||
SLOWCRYPT_CHACHA20_QROUND(state->state, 1, 6, 11, 12);
|
||||
SLOWCRYPT_CHACHA20_QROUND(state->state, 2, 7, 8, 13);
|
||||
SLOWCRYPT_CHACHA20_QROUND(state->state, 3, 4, 9, 14);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 16; i++)
|
||||
state->state[i] += swap->state[i];
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_block(slowcrypt_chacha20 state[2], char const key[32],
|
||||
uint32_t block_ctr, char const nonce[12],
|
||||
char data[64]) {
|
||||
slowcrypt_chacha20_init(state, key, block_ctr, nonce);
|
||||
slowcrypt_chacha20_run(state, &state[1], 20);
|
||||
slowcrypt_chacha20_serialize_xor(data, state);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
63
chacha20/nostd0.c
Normal file
63
chacha20/nostd0.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#define SLOWCRYPT_CHACHA20_IMPL
|
||||
#include "../chacha20.h"
|
||||
|
||||
static char key[] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
|
||||
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
|
||||
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
|
||||
};
|
||||
|
||||
static char nonce[] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
static char text[] =
|
||||
"Ladies and Gentlemen of the class of '99: If I could offer you only one "
|
||||
"tip for the future, sunscreen would be it.";
|
||||
|
||||
static char expected[] = {
|
||||
0x6E, 0x2E, 0x35, 0x9A, 0x25, 0x68, 0xF9, 0x80, 0x41, 0xBA, 0x07, 0x28,
|
||||
0xDD, 0x0D, 0x69, 0x81, 0xE9, 0x7E, 0x7A, 0xEC, 0x1D, 0x43, 0x60, 0xC2,
|
||||
0x0A, 0x27, 0xAF, 0xCC, 0xFD, 0x9F, 0xAE, 0x0B, 0xF9, 0x1B, 0x65, 0xC5,
|
||||
0x52, 0x47, 0x33, 0xAB, 0x8F, 0x59, 0x3D, 0xAB, 0xCD, 0x62, 0xB3, 0x57,
|
||||
0x16, 0x39, 0xD6, 0x24, 0xE6, 0x51, 0x52, 0xAB, 0x8F, 0x53, 0x0C, 0x35,
|
||||
0x9F, 0x08, 0x61, 0xD8, 0x07, 0xCA, 0x0D, 0xBF, 0x50, 0x0D, 0x6A, 0x61,
|
||||
0x56, 0xA3, 0x8E, 0x08, 0x8A, 0x22, 0xB6, 0x5E, 0x52, 0xBC, 0x51, 0x4D,
|
||||
0x16, 0xCC, 0xF8, 0x06, 0x81, 0x8C, 0xE9, 0x1A, 0xB7, 0x79, 0x37, 0x36,
|
||||
0x5A, 0xF9, 0x0B, 0xBF, 0x74, 0xA3, 0x5B, 0xE6, 0xB4, 0x0B, 0x8E, 0xED,
|
||||
0xF2, 0x78, 0x5E, 0x42, 0x87, 0x4D,
|
||||
};
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
slowcrypt_chacha20 state[2];
|
||||
char buf[64];
|
||||
int i, j, blksz;
|
||||
uint32_t ctr = 1;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
for (i = 0; i < (int)(sizeof(text) - 1); i += 64) {
|
||||
blksz = (sizeof(text) - 1) - i;
|
||||
if (blksz > 64)
|
||||
blksz = 64;
|
||||
|
||||
for (j = 0; j < blksz; j++) {
|
||||
buf[j] = text[i + j];
|
||||
}
|
||||
|
||||
slowcrypt_chacha20_block(state, key, ctr, nonce, buf);
|
||||
ctr++;
|
||||
|
||||
for (j = 0; j < blksz; j++)
|
||||
if (buf[j] != expected[i + j])
|
||||
return 1;
|
||||
}
|
||||
|
||||
slowcrypt_chacha20_deinit(&state[0]);
|
||||
slowcrypt_chacha20_deinit(&state[1]);
|
||||
for (i = 0; i < 64; i++)
|
||||
buf[i] = 0;
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user