summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/rdrand/rdrand_rng.c
blob: fa66f3ad78a7e236eedd865c5d42b9a8c52a9a5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * Copyright (C) 2012 Martin Willi
 * Copyright (C) 2012 revosec AG
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "rdrand_rng.h"

#include <unistd.h>

typedef struct private_rdrand_rng_t private_rdrand_rng_t;

/**
 * Private data of an rdrand_rng_t object.
 */
struct private_rdrand_rng_t {

	/**
	 * Public rdrand_rng_t interface.
	 */
	rdrand_rng_t public;

	/**
	 * Quality we produce RNG data
	 */
	rng_quality_t quality;
};

/**
 * Retries for failed RDRAND instructions
 */
#define MAX_TRIES 16

/**
 * After how many bytes should we reseed for RNG_STRONG
 * (must be a power of two >= 8)
 */
#define FORCE_RESEED 16

/**
 * How many times we mix reseeded RDRAND output when using RNG_TRUE
 */
#define MIX_ROUNDS 32

/**
 * Get a two byte word using RDRAND
 */
static bool rdrand16(u_int16_t *out)
{
	u_char res;
	int i;

	for (i = 0; i < MAX_TRIES; i++)
	{
		asm(".byte 0x66;.byte 0x0f;.byte 0xc7;.byte 0xf0; " /* rdrand */
			"setc %1;"
			: "=a"(*out), "=qm"(res));

		if (res)
		{
			return TRUE;
		}
	}
	return FALSE;
}

/**
 * Get a four byte word using RDRAND
 */
static bool rdrand32(u_int32_t *out)
{
	u_char res;
	int i;

	for (i = 0; i < MAX_TRIES; i++)
	{
		asm(".byte 0x0f;.byte 0xc7;.byte 0xf0;" /* rdrand */
			"setc %1;"
			: "=a"(*out), "=qm"(res));

		if (res)
		{
			return TRUE;
		}
	}
	return FALSE;
}

#ifdef __x86_64__
/**
 * Get a eight byte word using RDRAND
 */
static bool rdrand64(u_int64_t *out)
{
	u_char res;
	int i;

	for (i = 0; i < MAX_TRIES; i++)
	{
		asm(".byte 0x48;.byte 0x0f;.byte 0xc7;.byte 0xf0;" /* rdrand */
			"setc %1;"
			: "=a"(*out), "=qm"(res));

		if (res)
		{
			return TRUE;
		}
	}
	return FALSE;
}
#endif /* __x86_64__ */

/**
 * Get a one byte word using RDRAND
 */
static bool rdrand8(u_int8_t *out)
{
	u_int16_t u16;

	if (!rdrand16(&u16))
	{
		return FALSE;
	}
	*out = u16;
	return TRUE;
}

/**
 * Get a 16 byte word using RDRAND
 */
static bool rdrand128(void *out)
{
#ifdef __x86_64__
	if (!rdrand64(out) ||
		!rdrand64(out + sizeof(u_int64_t)))
	{
		return FALSE;
	}
#else /* __i386__ */
	if (!rdrand32(out) ||
		!rdrand32(out + 1 * sizeof(u_int32_t)) ||
		!rdrand32(out + 2 * sizeof(u_int32_t)) ||
		!rdrand32(out + 3 * sizeof(u_int32_t)))
	{
		return FALSE;
	}
#endif /* __x86_64__ / __i386__ */
	return TRUE;
}

/**
 * Enforce a DRNG reseed by reading 511 128-bit samples
 */
static bool reseed()
{
	int i;

#ifdef __x86_64__
	u_int64_t tmp;

	for (i = 0; i < 511 * 16 / sizeof(u_int64_t); i++)
	{
		if (!rdrand64(&tmp))
		{
			return FALSE;
		}
	}
#else /* __i386__ */
	u_int32_t tmp;

	for (i = 0; i < 511 * 16 / sizeof(u_int32_t); i++)
	{
		if (!rdrand32(&tmp))
		{
			return FALSE;
		}
	}
#endif /* __x86_64__ / __i386__ */
	return TRUE;
}

/**
 * Fill a preallocated chunk of data with random bytes
 */
static bool rdrand_chunk(private_rdrand_rng_t *this, chunk_t chunk)
{
	if (this->quality == RNG_STRONG)
	{
		if (!reseed())
		{
			return FALSE;
		}
	}

	/* align to 2 byte */
	if (chunk.len >= sizeof(u_int8_t))
	{
		if ((uintptr_t)chunk.ptr % 2)
		{
			if (!rdrand8((u_int8_t*)chunk.ptr))
			{
				return FALSE;
			}
			chunk = chunk_skip(chunk, sizeof(u_int8_t));
		}
	}

	/* align to 4 byte */
	if (chunk.len >= sizeof(u_int16_t))
	{
		if ((uintptr_t)chunk.ptr % 4)
		{
			if (!rdrand16((u_int16_t*)chunk.ptr))
			{
				return FALSE;
			}
			chunk = chunk_skip(chunk, sizeof(u_int16_t));
		}
	}

#ifdef __x86_64__

	/* align to 8 byte */
	if (chunk.len >= sizeof(u_int32_t))
	{
		if ((uintptr_t)chunk.ptr % 8)
		{
			if (!rdrand32((u_int32_t*)chunk.ptr))
			{
				return FALSE;
			}
			chunk = chunk_skip(chunk, sizeof(u_int32_t));
		}
	}

	/* fill with 8 byte words */
	while (chunk.len >= sizeof(u_int64_t))
	{
		if (this->quality == RNG_STRONG && chunk.len % FORCE_RESEED == 0)
		{
			if (!reseed())
			{
				return FALSE;
			}
		}
		if (!rdrand64((u_int64_t*)chunk.ptr))
		{
			return FALSE;
		}
		chunk = chunk_skip(chunk, sizeof(u_int64_t));
	}

	/* append 4 byte word */
	if (chunk.len >= sizeof(u_int32_t))
	{
		if (!rdrand32((u_int32_t*)chunk.ptr))
		{
			return FALSE;
		}
		chunk = chunk_skip(chunk, sizeof(u_int32_t));
	}

#else /* __i386__ */

	/* fill with 4 byte words */
	while (chunk.len >= sizeof(u_int32_t))
	{
		if (this->quality == RNG_STRONG && chunk.len % FORCE_RESEED == 0)
		{
			if (!reseed())
			{
				return FALSE;
			}
		}
		if (!rdrand32((u_int32_t*)chunk.ptr))
		{
			return FALSE;
		}
		chunk = chunk_skip(chunk, sizeof(u_int32_t));
	}

#endif /* __x86_64__ / __i386__ */

	if (this->quality == RNG_STRONG)
	{
		if (!reseed())
		{
			return FALSE;
		}
	}

	/* append 2 byte word */
	if (chunk.len >= sizeof(u_int16_t))
	{
		if (!rdrand16((u_int16_t*)chunk.ptr))
		{
			return FALSE;
		}
		chunk = chunk_skip(chunk, sizeof(u_int16_t));
	}

	/* append 1 byte word */
	if (chunk.len >= sizeof(u_int8_t))
	{
		if (!rdrand8((u_int8_t*)chunk.ptr))
		{
			return FALSE;
		}
		chunk = chunk_skip(chunk, sizeof(u_int8_t));
	}

	return TRUE;
}

/**
 * Stronger variant mixing reseeded results of rdrand output
 *
 * This is based on the Intel DRNG "Software Implementation Guide", using
 * AES-CBC to mix several reseeded RDRAND outputs.
 */
static bool rdrand_mixed(private_rdrand_rng_t *this, chunk_t chunk)
{
	u_char block[16], forward[16], key[16], iv[16];
	crypter_t *crypter;
	int i, len;

	memset(iv, 0, sizeof(iv));
	crypter = lib->crypto->create_crypter(lib->crypto, ENCR_AES_CBC, 16);
	if (!crypter)
	{
		return FALSE;
	}
	for (i = 0; i < sizeof(key); i++)
	{
		key[i] = i;
	}
	if (!crypter->set_key(crypter, chunk_from_thing(key)))
	{
		crypter->destroy(crypter);
		return FALSE;
	}
	while (chunk.len > 0)
	{
		memset(forward, 0, sizeof(forward));
		for (i = 0; i < MIX_ROUNDS; i++)
		{
			/* sleep to reseed PRNG */
			usleep(10);
			if (!rdrand128(block))
			{
				crypter->destroy(crypter);
				return FALSE;
			}
			memxor(forward, block, sizeof(block));
			if (!crypter->encrypt(crypter, chunk_from_thing(forward),
								  chunk_from_thing(iv), NULL))
			{
				crypter->destroy(crypter);
				return FALSE;
			}
		}
		len = min(chunk.len, sizeof(forward));
		memcpy(chunk.ptr, forward, len);
		chunk = chunk_skip(chunk, len);
	}
	crypter->destroy(crypter);

	return TRUE;
}

METHOD(rng_t, get_bytes, bool,
	private_rdrand_rng_t *this, size_t bytes, u_int8_t *buffer)
{
	switch (this->quality)
	{
		case RNG_WEAK:
		case RNG_STRONG:
			return rdrand_chunk(this, chunk_create(buffer, bytes));
		case RNG_TRUE:
			return rdrand_mixed(this, chunk_create(buffer, bytes));
		default:
			return FALSE;
	}
}

METHOD(rng_t, allocate_bytes, bool,
	private_rdrand_rng_t *this, size_t bytes, chunk_t *chunk)
{
	*chunk = chunk_alloc(bytes);
	if (get_bytes(this, bytes, chunk->ptr))
	{
		return TRUE;
	}
	free(chunk->ptr);
	return FALSE;
}

METHOD(rng_t, destroy, void,
	private_rdrand_rng_t *this)
{
	free(this);
}

/*
 * Described in header.
 */
rdrand_rng_t *rdrand_rng_create(rng_quality_t quality)
{
	private_rdrand_rng_t *this;

	switch (quality)
	{
		case RNG_WEAK:
		case RNG_STRONG:
		case RNG_TRUE:
			break;
		default:
			return NULL;
	}

	INIT(this,
		.public = {
			.rng = {
				.get_bytes = _get_bytes,
				.allocate_bytes = _allocate_bytes,
				.destroy = _destroy,
			},
		},
		.quality = quality,
	);

	return &this->public;
}