upstream: https://github.com/mirage/mirage-crypto
at main 32 lines 1.0 kB view raw
1/* Based on https://github.com/abeaumont/ocaml-chacha.git */ 2 3#include "crypto.h" 4 5static inline void mc_chacha_quarterround(uint32_t *x, int a, int b, int c, int d) { 6 x[a] += x[b]; x[d] = rol32(x[d] ^ x[a], 16); 7 x[c] += x[d]; x[b] = rol32(x[b] ^ x[c], 12); 8 x[a] += x[b]; x[d] = rol32(x[d] ^ x[a], 8); 9 x[c] += x[d]; x[b] = rol32(x[b] ^ x[c], 7); 10} 11 12void mc_chacha_core_generic(int count, const uint32_t *src, uint32_t *dst) { 13 uint32_t x[16]; 14 cpu_to_le32_array(x, src, 16); 15 for (int i = 0; i < count; i++) { 16 mc_chacha_quarterround(x, 0, 4, 8, 12); 17 mc_chacha_quarterround(x, 1, 5, 9, 13); 18 mc_chacha_quarterround(x, 2, 6, 10, 14); 19 mc_chacha_quarterround(x, 3, 7, 11, 15); 20 21 mc_chacha_quarterround(x, 0, 5, 10, 15); 22 mc_chacha_quarterround(x, 1, 6, 11, 12); 23 mc_chacha_quarterround(x, 2, 7, 8, 13); 24 mc_chacha_quarterround(x, 3, 4, 9, 14); 25 } 26 for (int i = 0; i < 16; i++) { 27 uint32_t xi = x[i]; 28 uint32_t hj = cpu_to_le32(src[i]); 29 dst[i] = le32_to_cpu(xi + hj); 30 } 31} 32