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