Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/trunk/sys/src/libsec/port/salsa.c – Rev 26

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
26 7u83 1
#include "os.h"
2
#include <libsec.h>
3
 
4
/* little-endian data order */
5
#define	GET4(p)		((p)[0]|((p)[1]<<8)|((p)[2]<<16)|((p)[3]<<24))
6
#define	PUT4(p,v)	(p)[0]=(v);(p)[1]=(v)>>8;(p)[2]=(v)>>16;(p)[3]=(v)>>24
7
 
8
#define ROTATE(v,c) (t = v, (u32int)(t << (c)) | (t >> (32 - (c))))
9
 
10
#define ENCRYPT(s, x, y, d) {\
11
	u32int v; \
12
	v = GET4(s); \
13
	v ^= (x)+(y); \
14
	PUT4(d, v); \
15
}
16
 
17
static uchar sigma[16] = "expand 32-byte k";
18
static uchar tau[16] = "expand 16-byte k";
19
 
20
static void
21
load(u32int *d, uchar *s, int nw)
22
{
23
	int i;
24
 
25
	for(i = 0; i < nw; i++, s+=4)
26
		d[i] = GET4(s);
27
}
28
 
29
void
30
setupSalsastate(Salsastate *s, uchar *key, ulong keylen, uchar *iv, ulong ivlen, int rounds)
31
{
32
	if(keylen != 256/8 && keylen != 128/8)
33
		sysfatal("invalid salsa key length");
34
	if(ivlen != 64/8
35
	&& ivlen != 128/8 && ivlen != 192/8)	/* hsalsa, xsalsa */
36
		sysfatal("invalid salsa iv length");
37
	if(rounds == 0)
38
		rounds = 20;
39
	s->rounds = rounds;
40
	if(keylen == 256/8) { /* recommended */
41
		load(&s->input[0],  sigma+4*0, 1);
42
		load(&s->input[1],  key +16*0, 4);
43
		load(&s->input[5],  sigma+4*1, 1);
44
		load(&s->input[10], sigma+4*2, 1);
45
		load(&s->input[11], key +16*1, 4);
46
		load(&s->input[15], sigma+4*3, 1);
47
	}else{
48
		load(&s->input[0],  tau +4*0, 1);
49
		load(&s->input[1],  key, 4);
50
		load(&s->input[5],  tau +4*1, 1);
51
		load(&s->input[10], tau +4*2, 1);
52
		load(&s->input[11], key, 4);
53
		load(&s->input[15], tau +4*3, 1);
54
	}
55
	s->xkey[0] = s->input[1];
56
	s->xkey[1] = s->input[2];
57
	s->xkey[2] = s->input[3];
58
	s->xkey[3] = s->input[4];
59
	s->xkey[4] = s->input[11];
60
	s->xkey[5] = s->input[12];
61
	s->xkey[6] = s->input[13];
62
	s->xkey[7] = s->input[14];
63
 
64
	s->ivwords = ivlen/4;
65
	s->input[8] = 0;
66
	s->input[9] = 0;
67
	if(iv == nil){
68
		s->input[6] = 0;
69
		s->input[7] = 0;
70
	}else
71
		salsa_setiv(s, iv);
72
}
73
 
74
static void
75
dorounds(u32int x[16], int rounds)
76
{
77
	u32int t;
78
 
79
	for(; rounds > 0; rounds -= 2) {
80
	     x[4] ^= ROTATE( x[0]+x[12], 7);
81
	     x[8] ^= ROTATE( x[4]+ x[0], 9);
82
	    x[12] ^= ROTATE( x[8]+ x[4],13);
83
	     x[0] ^= ROTATE(x[12]+ x[8],18);
84
	     x[9] ^= ROTATE( x[5]+ x[1], 7);
85
	    x[13] ^= ROTATE( x[9]+ x[5], 9);
86
	     x[1] ^= ROTATE(x[13]+ x[9],13);
87
	     x[5] ^= ROTATE( x[1]+x[13],18);
88
	    x[14] ^= ROTATE(x[10]+ x[6], 7);
89
	     x[2] ^= ROTATE(x[14]+x[10], 9);
90
	     x[6] ^= ROTATE( x[2]+x[14],13);
91
	    x[10] ^= ROTATE( x[6]+ x[2],18);
92
	     x[3] ^= ROTATE(x[15]+x[11], 7);
93
	     x[7] ^= ROTATE( x[3]+x[15], 9);
94
	    x[11] ^= ROTATE( x[7]+ x[3],13);
95
	    x[15] ^= ROTATE(x[11]+ x[7],18);
96
	     x[1] ^= ROTATE( x[0]+ x[3], 7);
97
	     x[2] ^= ROTATE( x[1]+ x[0], 9);
98
	     x[3] ^= ROTATE( x[2]+ x[1],13);
99
	     x[0] ^= ROTATE( x[3]+ x[2],18);
100
	     x[6] ^= ROTATE( x[5]+ x[4], 7);
101
	     x[7] ^= ROTATE( x[6]+ x[5], 9);
102
	     x[4] ^= ROTATE( x[7]+ x[6],13);
103
	     x[5] ^= ROTATE( x[4]+ x[7],18);
104
	    x[11] ^= ROTATE(x[10]+ x[9], 7);
105
	     x[8] ^= ROTATE(x[11]+x[10], 9);
106
	     x[9] ^= ROTATE( x[8]+x[11],13);
107
	    x[10] ^= ROTATE( x[9]+ x[8],18);
108
	    x[12] ^= ROTATE(x[15]+x[14], 7);
109
	    x[13] ^= ROTATE(x[12]+x[15], 9);
110
	    x[14] ^= ROTATE(x[13]+x[12],13);
111
	    x[15] ^= ROTATE(x[14]+x[13],18);
112
	}
113
}
114
 
115
static void
116
hsalsablock(uchar h[32], Salsastate *s)
117
{
118
	u32int x[16];
119
 
120
	x[0] = s->input[0];
121
	x[1] = s->input[1];
122
	x[2] = s->input[2];
123
	x[3] = s->input[3];
124
	x[4] = s->input[4];
125
	x[5] = s->input[5];
126
	x[6] = s->input[6];
127
	x[7] = s->input[7];
128
	x[8] = s->input[8];
129
	x[9] = s->input[9];
130
	x[10] = s->input[10];
131
	x[11] = s->input[11];
132
	x[12] = s->input[12];
133
	x[13] = s->input[13];
134
	x[14] = s->input[14];
135
	x[15] = s->input[15];
136
 
137
	dorounds(x, s->rounds);
138
 
139
	PUT4(h+0*4, x[0]);
140
	PUT4(h+1*4, x[5]);
141
	PUT4(h+2*4, x[10]);
142
	PUT4(h+3*4, x[15]);
143
	PUT4(h+4*4, x[6]);
144
	PUT4(h+5*4, x[7]);
145
	PUT4(h+6*4, x[8]);
146
	PUT4(h+7*4, x[9]);
147
}
148
 
149
void
150
salsa_setiv(Salsastate *s, uchar *iv)
151
{
152
	if(s->ivwords == 128/32){
153
		/* hsalsa with 128-bit iv */
154
		load(&s->input[6], iv, 4);
155
		return;
156
	}
157
	if(s->ivwords == 192/32){
158
		/* xsalsa with 192-bit iv */
159
		u32int counter[2];
160
		uchar h[32];
161
 
162
		counter[0] = s->input[8];
163
		counter[1] = s->input[9];
164
 
165
		s->input[1] = s->xkey[0];
166
		s->input[2] = s->xkey[1];
167
		s->input[3] = s->xkey[2];
168
		s->input[4] = s->xkey[3];
169
		s->input[11] = s->xkey[4];
170
		s->input[12] = s->xkey[5];
171
		s->input[13] = s->xkey[6];
172
		s->input[14] = s->xkey[7];
173
 
174
		load(&s->input[6], iv, 4);
175
 
176
		hsalsablock(h, s);
177
		load(&s->input[1],  h+16*0, 4);
178
		load(&s->input[11], h+16*1, 4);
179
		memset(h, 0, 32);
180
 
181
		s->input[8] = counter[0];
182
		s->input[9] = counter[1];
183
 
184
		iv += 16;
185
	}
186
	/* 64-bit iv */
187
	load(&s->input[6], iv, 2);
188
}
189
 
190
void
191
salsa_setblock(Salsastate *s, u64int blockno)
192
{
193
	s->input[8] = blockno;
194
	s->input[9] = blockno>>32;
195
}
196
 
197
static void
198
encryptblock(Salsastate *s, uchar *src, uchar *dst)
199
{
200
	u32int x[16];
201
	int i;
202
 
203
	x[0] = s->input[0];
204
	x[1] = s->input[1];
205
	x[2] = s->input[2];
206
	x[3] = s->input[3];
207
	x[4] = s->input[4];
208
	x[5] = s->input[5];
209
	x[6] = s->input[6];
210
	x[7] = s->input[7];
211
	x[8] = s->input[8];
212
	x[9] = s->input[9];
213
	x[10] = s->input[10];
214
	x[11] = s->input[11];
215
	x[12] = s->input[12];
216
	x[13] = s->input[13];
217
	x[14] = s->input[14];
218
	x[15] = s->input[15];
219
 
220
	dorounds(x, s->rounds);
221
 
222
	for(i=0; i<nelem(x); i+=4){
223
		ENCRYPT(src, x[i], s->input[i], dst);
224
		ENCRYPT(src+4, x[i+1], s->input[i+1], dst+4);
225
		ENCRYPT(src+8, x[i+2], s->input[i+2], dst+8);
226
		ENCRYPT(src+12, x[i+3], s->input[i+3], dst+12);
227
		src += 16;
228
		dst += 16;
229
	}
230
 
231
	if(++s->input[8] == 0)
232
		s->input[9]++;
233
}
234
 
235
void
236
salsa_encrypt2(uchar *src, uchar *dst, ulong bytes, Salsastate *s)
237
{
238
	uchar tmp[SalsaBsize];
239
 
240
	for(; bytes >= SalsaBsize; bytes -= SalsaBsize){
241
		encryptblock(s, src, dst);
242
		src += SalsaBsize;
243
		dst += SalsaBsize;
244
	}
245
	if(bytes > 0){
246
		memmove(tmp, src, bytes);
247
		encryptblock(s, tmp, tmp);
248
		memmove(dst, tmp, bytes);
249
	}
250
}
251
 
252
void
253
salsa_encrypt(uchar *buf, ulong bytes, Salsastate *s)
254
{
255
	salsa_encrypt2(buf, buf, bytes, s);
256
}
257
 
258
void
259
salsa_core(u32int in[16], u32int out[16], int rounds)
260
{
261
	u32int x[16];
262
 
263
	x[0] = in[0];
264
	x[1] = in[1];
265
	x[2] = in[2];
266
	x[3] = in[3];
267
	x[4] = in[4];
268
	x[5] = in[5];
269
	x[6] = in[6];
270
	x[7] = in[7];
271
	x[8] = in[8];
272
	x[9] = in[9];
273
	x[10] = in[10];
274
	x[11] = in[11];
275
	x[12] = in[12];
276
	x[13] = in[13];
277
	x[14] = in[14];
278
	x[15] = in[15];
279
 
280
	dorounds(x, rounds);
281
 
282
	out[0] = x[0] + in[0];
283
	out[1] = x[1] + in[1];
284
	out[2] = x[2] + in[2];
285
	out[3] = x[3] + in[3];
286
	out[4] = x[4] + in[4];
287
	out[5] = x[5] + in[5];
288
	out[6] = x[6] + in[6];
289
	out[7] = x[7] + in[7];
290
	out[8] = x[8] + in[8];
291
	out[9] = x[9] + in[9];
292
	out[10] = x[10] + in[10];
293
	out[11] = x[11] + in[11];
294
	out[12] = x[12] + in[12];
295
	out[13] = x[13] + in[13];
296
	out[14] = x[14] + in[14];
297
	out[15] = x[15] + in[15];
298
}
299
 
300
void
301
hsalsa(uchar h[32], uchar *key, ulong keylen, uchar nonce[16], int rounds)
302
{
303
	Salsastate s[1];
304
 
305
	setupSalsastate(s, key, keylen, nonce, 16, rounds);
306
	hsalsablock(h, s);
307
	memset(s, 0, sizeof(s));
308
}