1*9d12795fSRobert Mustacchi /*
2*9d12795fSRobert Mustacchi chacha-merged.c version 20080118
3*9d12795fSRobert Mustacchi D. J. Bernstein
4*9d12795fSRobert Mustacchi Public domain.
5*9d12795fSRobert Mustacchi */
6*9d12795fSRobert Mustacchi 
7*9d12795fSRobert Mustacchi /* $OpenBSD: chacha_private.h,v 1.2 2013/10/04 07:02:27 djm Exp $ */
8*9d12795fSRobert Mustacchi 
9*9d12795fSRobert Mustacchi #include <chacha.h>
10*9d12795fSRobert Mustacchi #include <stddef.h>
11*9d12795fSRobert Mustacchi 
12*9d12795fSRobert Mustacchi typedef unsigned char u8;
13*9d12795fSRobert Mustacchi typedef unsigned int u32;
14*9d12795fSRobert Mustacchi typedef unsigned int u_int;
15*9d12795fSRobert Mustacchi 
16*9d12795fSRobert Mustacchi #define U8C(v) (v##U)
17*9d12795fSRobert Mustacchi #define U32C(v) (v##U)
18*9d12795fSRobert Mustacchi 
19*9d12795fSRobert Mustacchi #define U8V(v) ((u8)(v) & U8C(0xFF))
20*9d12795fSRobert Mustacchi #define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
21*9d12795fSRobert Mustacchi 
22*9d12795fSRobert Mustacchi #define ROTL32(v, n) \
23*9d12795fSRobert Mustacchi   (U32V((v) << (n)) | ((v) >> (32 - (n))))
24*9d12795fSRobert Mustacchi 
25*9d12795fSRobert Mustacchi #define U8TO32_LITTLE(p) \
26*9d12795fSRobert Mustacchi   (((u32)((p)[0])      ) | \
27*9d12795fSRobert Mustacchi    ((u32)((p)[1]) <<  8) | \
28*9d12795fSRobert Mustacchi    ((u32)((p)[2]) << 16) | \
29*9d12795fSRobert Mustacchi    ((u32)((p)[3]) << 24))
30*9d12795fSRobert Mustacchi 
31*9d12795fSRobert Mustacchi #define U32TO8_LITTLE(p, v) \
32*9d12795fSRobert Mustacchi   do { \
33*9d12795fSRobert Mustacchi     (p)[0] = U8V((v)      ); \
34*9d12795fSRobert Mustacchi     (p)[1] = U8V((v) >>  8); \
35*9d12795fSRobert Mustacchi     (p)[2] = U8V((v) >> 16); \
36*9d12795fSRobert Mustacchi     (p)[3] = U8V((v) >> 24); \
37*9d12795fSRobert Mustacchi   } while (0)
38*9d12795fSRobert Mustacchi 
39*9d12795fSRobert Mustacchi #define ROTATE(v,c) (ROTL32(v,c))
40*9d12795fSRobert Mustacchi #define XOR(v,w) ((v) ^ (w))
41*9d12795fSRobert Mustacchi #define PLUS(v,w) (U32V((v) + (w)))
42*9d12795fSRobert Mustacchi #define PLUSONE(v) (PLUS((v),1))
43*9d12795fSRobert Mustacchi 
44*9d12795fSRobert Mustacchi #define QUARTERROUND(a,b,c,d) \
45*9d12795fSRobert Mustacchi   a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
46*9d12795fSRobert Mustacchi   c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
47*9d12795fSRobert Mustacchi   a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
48*9d12795fSRobert Mustacchi   c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
49*9d12795fSRobert Mustacchi 
50*9d12795fSRobert Mustacchi static const char sigma[16] = "expand 32-byte k";
51*9d12795fSRobert Mustacchi static const char tau[16] = "expand 16-byte k";
52*9d12795fSRobert Mustacchi 
53*9d12795fSRobert Mustacchi void
chacha_keysetup(chacha_ctx_t * x,const u8 * k,u32 kbits,u32 ivbits)54*9d12795fSRobert Mustacchi chacha_keysetup(chacha_ctx_t *x,const u8 *k,u32 kbits,u32 ivbits)
55*9d12795fSRobert Mustacchi {
56*9d12795fSRobert Mustacchi   const char *constants;
57*9d12795fSRobert Mustacchi 
58*9d12795fSRobert Mustacchi   x->chacha_input[4] = U8TO32_LITTLE(k + 0);
59*9d12795fSRobert Mustacchi   x->chacha_input[5] = U8TO32_LITTLE(k + 4);
60*9d12795fSRobert Mustacchi   x->chacha_input[6] = U8TO32_LITTLE(k + 8);
61*9d12795fSRobert Mustacchi   x->chacha_input[7] = U8TO32_LITTLE(k + 12);
62*9d12795fSRobert Mustacchi   if (kbits == 256) { /* recommended */
63*9d12795fSRobert Mustacchi     k += 16;
64*9d12795fSRobert Mustacchi     constants = sigma;
65*9d12795fSRobert Mustacchi   } else { /* kbits == 128 */
66*9d12795fSRobert Mustacchi     constants = tau;
67*9d12795fSRobert Mustacchi   }
68*9d12795fSRobert Mustacchi   x->chacha_input[8] = U8TO32_LITTLE(k + 0);
69*9d12795fSRobert Mustacchi   x->chacha_input[9] = U8TO32_LITTLE(k + 4);
70*9d12795fSRobert Mustacchi   x->chacha_input[10] = U8TO32_LITTLE(k + 8);
71*9d12795fSRobert Mustacchi   x->chacha_input[11] = U8TO32_LITTLE(k + 12);
72*9d12795fSRobert Mustacchi   x->chacha_input[0] = U8TO32_LITTLE(constants + 0);
73*9d12795fSRobert Mustacchi   x->chacha_input[1] = U8TO32_LITTLE(constants + 4);
74*9d12795fSRobert Mustacchi   x->chacha_input[2] = U8TO32_LITTLE(constants + 8);
75*9d12795fSRobert Mustacchi   x->chacha_input[3] = U8TO32_LITTLE(constants + 12);
76*9d12795fSRobert Mustacchi }
77*9d12795fSRobert Mustacchi 
78*9d12795fSRobert Mustacchi void
chacha_ivsetup(chacha_ctx_t * x,const u8 * iv)79*9d12795fSRobert Mustacchi chacha_ivsetup(chacha_ctx_t *x,const u8 *iv)
80*9d12795fSRobert Mustacchi {
81*9d12795fSRobert Mustacchi   x->chacha_input[12] = 0;
82*9d12795fSRobert Mustacchi   x->chacha_input[13] = 0;
83*9d12795fSRobert Mustacchi   x->chacha_input[14] = U8TO32_LITTLE(iv + 0);
84*9d12795fSRobert Mustacchi   x->chacha_input[15] = U8TO32_LITTLE(iv + 4);
85*9d12795fSRobert Mustacchi }
86*9d12795fSRobert Mustacchi 
87*9d12795fSRobert Mustacchi void
chacha_encrypt_bytes(chacha_ctx_t * x,const u8 * m,u8 * c,u32 bytes)88*9d12795fSRobert Mustacchi chacha_encrypt_bytes(chacha_ctx_t *x,const u8 *m,u8 *c,u32 bytes)
89*9d12795fSRobert Mustacchi {
90*9d12795fSRobert Mustacchi   u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
91*9d12795fSRobert Mustacchi   u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
92*9d12795fSRobert Mustacchi   u8 *ctarget = NULL;
93*9d12795fSRobert Mustacchi   u8 tmp[64];
94*9d12795fSRobert Mustacchi   u_int i;
95*9d12795fSRobert Mustacchi 
96*9d12795fSRobert Mustacchi   if (!bytes) return;
97*9d12795fSRobert Mustacchi 
98*9d12795fSRobert Mustacchi   j0 = x->chacha_input[0];
99*9d12795fSRobert Mustacchi   j1 = x->chacha_input[1];
100*9d12795fSRobert Mustacchi   j2 = x->chacha_input[2];
101*9d12795fSRobert Mustacchi   j3 = x->chacha_input[3];
102*9d12795fSRobert Mustacchi   j4 = x->chacha_input[4];
103*9d12795fSRobert Mustacchi   j5 = x->chacha_input[5];
104*9d12795fSRobert Mustacchi   j6 = x->chacha_input[6];
105*9d12795fSRobert Mustacchi   j7 = x->chacha_input[7];
106*9d12795fSRobert Mustacchi   j8 = x->chacha_input[8];
107*9d12795fSRobert Mustacchi   j9 = x->chacha_input[9];
108*9d12795fSRobert Mustacchi   j10 = x->chacha_input[10];
109*9d12795fSRobert Mustacchi   j11 = x->chacha_input[11];
110*9d12795fSRobert Mustacchi   j12 = x->chacha_input[12];
111*9d12795fSRobert Mustacchi   j13 = x->chacha_input[13];
112*9d12795fSRobert Mustacchi   j14 = x->chacha_input[14];
113*9d12795fSRobert Mustacchi   j15 = x->chacha_input[15];
114*9d12795fSRobert Mustacchi 
115*9d12795fSRobert Mustacchi   for (;;) {
116*9d12795fSRobert Mustacchi     if (bytes < 64) {
117*9d12795fSRobert Mustacchi       for (i = 0;i < bytes;++i) tmp[i] = m[i];
118*9d12795fSRobert Mustacchi       m = tmp;
119*9d12795fSRobert Mustacchi       ctarget = c;
120*9d12795fSRobert Mustacchi       c = tmp;
121*9d12795fSRobert Mustacchi     }
122*9d12795fSRobert Mustacchi     x0 = j0;
123*9d12795fSRobert Mustacchi     x1 = j1;
124*9d12795fSRobert Mustacchi     x2 = j2;
125*9d12795fSRobert Mustacchi     x3 = j3;
126*9d12795fSRobert Mustacchi     x4 = j4;
127*9d12795fSRobert Mustacchi     x5 = j5;
128*9d12795fSRobert Mustacchi     x6 = j6;
129*9d12795fSRobert Mustacchi     x7 = j7;
130*9d12795fSRobert Mustacchi     x8 = j8;
131*9d12795fSRobert Mustacchi     x9 = j9;
132*9d12795fSRobert Mustacchi     x10 = j10;
133*9d12795fSRobert Mustacchi     x11 = j11;
134*9d12795fSRobert Mustacchi     x12 = j12;
135*9d12795fSRobert Mustacchi     x13 = j13;
136*9d12795fSRobert Mustacchi     x14 = j14;
137*9d12795fSRobert Mustacchi     x15 = j15;
138*9d12795fSRobert Mustacchi     for (i = 20;i > 0;i -= 2) {
139*9d12795fSRobert Mustacchi       QUARTERROUND( x0, x4, x8,x12)
140*9d12795fSRobert Mustacchi       QUARTERROUND( x1, x5, x9,x13)
141*9d12795fSRobert Mustacchi       QUARTERROUND( x2, x6,x10,x14)
142*9d12795fSRobert Mustacchi       QUARTERROUND( x3, x7,x11,x15)
143*9d12795fSRobert Mustacchi       QUARTERROUND( x0, x5,x10,x15)
144*9d12795fSRobert Mustacchi       QUARTERROUND( x1, x6,x11,x12)
145*9d12795fSRobert Mustacchi       QUARTERROUND( x2, x7, x8,x13)
146*9d12795fSRobert Mustacchi       QUARTERROUND( x3, x4, x9,x14)
147*9d12795fSRobert Mustacchi     }
148*9d12795fSRobert Mustacchi     x0 = PLUS(x0,j0);
149*9d12795fSRobert Mustacchi     x1 = PLUS(x1,j1);
150*9d12795fSRobert Mustacchi     x2 = PLUS(x2,j2);
151*9d12795fSRobert Mustacchi     x3 = PLUS(x3,j3);
152*9d12795fSRobert Mustacchi     x4 = PLUS(x4,j4);
153*9d12795fSRobert Mustacchi     x5 = PLUS(x5,j5);
154*9d12795fSRobert Mustacchi     x6 = PLUS(x6,j6);
155*9d12795fSRobert Mustacchi     x7 = PLUS(x7,j7);
156*9d12795fSRobert Mustacchi     x8 = PLUS(x8,j8);
157*9d12795fSRobert Mustacchi     x9 = PLUS(x9,j9);
158*9d12795fSRobert Mustacchi     x10 = PLUS(x10,j10);
159*9d12795fSRobert Mustacchi     x11 = PLUS(x11,j11);
160*9d12795fSRobert Mustacchi     x12 = PLUS(x12,j12);
161*9d12795fSRobert Mustacchi     x13 = PLUS(x13,j13);
162*9d12795fSRobert Mustacchi     x14 = PLUS(x14,j14);
163*9d12795fSRobert Mustacchi     x15 = PLUS(x15,j15);
164*9d12795fSRobert Mustacchi 
165*9d12795fSRobert Mustacchi #ifndef KEYSTREAM_ONLY
166*9d12795fSRobert Mustacchi     x0 = XOR(x0,U8TO32_LITTLE(m + 0));
167*9d12795fSRobert Mustacchi     x1 = XOR(x1,U8TO32_LITTLE(m + 4));
168*9d12795fSRobert Mustacchi     x2 = XOR(x2,U8TO32_LITTLE(m + 8));
169*9d12795fSRobert Mustacchi     x3 = XOR(x3,U8TO32_LITTLE(m + 12));
170*9d12795fSRobert Mustacchi     x4 = XOR(x4,U8TO32_LITTLE(m + 16));
171*9d12795fSRobert Mustacchi     x5 = XOR(x5,U8TO32_LITTLE(m + 20));
172*9d12795fSRobert Mustacchi     x6 = XOR(x6,U8TO32_LITTLE(m + 24));
173*9d12795fSRobert Mustacchi     x7 = XOR(x7,U8TO32_LITTLE(m + 28));
174*9d12795fSRobert Mustacchi     x8 = XOR(x8,U8TO32_LITTLE(m + 32));
175*9d12795fSRobert Mustacchi     x9 = XOR(x9,U8TO32_LITTLE(m + 36));
176*9d12795fSRobert Mustacchi     x10 = XOR(x10,U8TO32_LITTLE(m + 40));
177*9d12795fSRobert Mustacchi     x11 = XOR(x11,U8TO32_LITTLE(m + 44));
178*9d12795fSRobert Mustacchi     x12 = XOR(x12,U8TO32_LITTLE(m + 48));
179*9d12795fSRobert Mustacchi     x13 = XOR(x13,U8TO32_LITTLE(m + 52));
180*9d12795fSRobert Mustacchi     x14 = XOR(x14,U8TO32_LITTLE(m + 56));
181*9d12795fSRobert Mustacchi     x15 = XOR(x15,U8TO32_LITTLE(m + 60));
182*9d12795fSRobert Mustacchi #endif
183*9d12795fSRobert Mustacchi 
184*9d12795fSRobert Mustacchi     j12 = PLUSONE(j12);
185*9d12795fSRobert Mustacchi     if (!j12) {
186*9d12795fSRobert Mustacchi       j13 = PLUSONE(j13);
187*9d12795fSRobert Mustacchi       /* stopping at 2^70 bytes per nonce is user's responsibility */
188*9d12795fSRobert Mustacchi     }
189*9d12795fSRobert Mustacchi 
190*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 0,x0);
191*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 4,x1);
192*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 8,x2);
193*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 12,x3);
194*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 16,x4);
195*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 20,x5);
196*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 24,x6);
197*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 28,x7);
198*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 32,x8);
199*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 36,x9);
200*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 40,x10);
201*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 44,x11);
202*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 48,x12);
203*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 52,x13);
204*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 56,x14);
205*9d12795fSRobert Mustacchi     U32TO8_LITTLE(c + 60,x15);
206*9d12795fSRobert Mustacchi 
207*9d12795fSRobert Mustacchi     if (bytes <= 64) {
208*9d12795fSRobert Mustacchi       if (bytes < 64) {
209*9d12795fSRobert Mustacchi         for (i = 0;i < bytes;++i) ctarget[i] = c[i];
210*9d12795fSRobert Mustacchi       }
211*9d12795fSRobert Mustacchi       x->chacha_input[12] = j12;
212*9d12795fSRobert Mustacchi       x->chacha_input[13] = j13;
213*9d12795fSRobert Mustacchi       return;
214*9d12795fSRobert Mustacchi     }
215*9d12795fSRobert Mustacchi     bytes -= 64;
216*9d12795fSRobert Mustacchi     c += 64;
217*9d12795fSRobert Mustacchi #ifndef KEYSTREAM_ONLY
218*9d12795fSRobert Mustacchi     m += 64;
219*9d12795fSRobert Mustacchi #endif
220*9d12795fSRobert Mustacchi   }
221*9d12795fSRobert Mustacchi }
222