1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
10 /* The implementation is based on:
11 * chacha-ref.c version 20080118
12 * Public domain from D. J. Bernstein
19 #define QUARTERROUND(a,b,c,d) \
20 x[a] += x[b]; x[d] = ROL(x[d] ^ x[a], 16); \
21 x[c] += x[d]; x[b] = ROL(x[b] ^ x[c], 12); \
22 x[a] += x[b]; x[d] = ROL(x[d] ^ x[a], 8); \
23 x[c] += x[d]; x[b] = ROL(x[b] ^ x[c], 7);
25 static void _chacha_block(unsigned char *output, const ulong32 *input, int rounds)
29 XMEMCPY(x, input, sizeof(x));
30 for (i = rounds; i > 0; i -= 2) {
31 QUARTERROUND(0, 4, 8,12)
32 QUARTERROUND(1, 5, 9,13)
33 QUARTERROUND(2, 6,10,14)
34 QUARTERROUND(3, 7,11,15)
35 QUARTERROUND(0, 5,10,15)
36 QUARTERROUND(1, 6,11,12)
37 QUARTERROUND(2, 7, 8,13)
38 QUARTERROUND(3, 4, 9,14)
40 for (i = 0; i < 16; ++i) {
42 STORE32L(x[i], output + 4 * i);
47 Encrypt (or decrypt) bytes of ciphertext (or plaintext) with ChaCha
48 @param st The ChaCha state
49 @param in The plaintext (or ciphertext)
50 @param inlen The length of the input (octets)
51 @param out [out] The ciphertext (or plaintext), length inlen
52 @return CRYPT_OK if successful
54 int chacha_crypt(chacha_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out)
56 unsigned char buf[64];
59 if (inlen == 0) return CRYPT_OK; /* nothing to do */
61 LTC_ARGCHK(st != NULL);
62 LTC_ARGCHK(in != NULL);
63 LTC_ARGCHK(out != NULL);
64 LTC_ARGCHK(st->ivlen != 0);
67 j = MIN(st->ksleft, inlen);
68 for (i = 0; i < j; ++i, st->ksleft--) out[i] = in[i] ^ st->kstream[64 - st->ksleft];
70 if (inlen == 0) return CRYPT_OK;
75 _chacha_block(buf, st->input, st->rounds);
77 /* IV-64bit, increment 64bit counter */
78 if (0 == ++st->input[12] && 0 == ++st->input[13]) return CRYPT_OVERFLOW;
81 /* IV-96bit, increment 32bit counter */
82 if (0 == ++st->input[12]) return CRYPT_OVERFLOW;
85 for (i = 0; i < inlen; ++i) out[i] = in[i] ^ buf[i];
86 st->ksleft = 64 - inlen;
87 for (i = inlen; i < 64; ++i) st->kstream[i] = buf[i];
90 for (i = 0; i < 64; ++i) out[i] = in[i] ^ buf[i];
99 /* ref: $Format:%D$ */
100 /* git commit: $Format:%H$ */
101 /* commit time: $Format:%ai$ */