summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/padlock/padlock_rng.c
blob: 8ff46081bfe32c239536753e2b514f41b0051bdb (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
/*
 * Copyright (C) 2008 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * 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 "padlock_rng.h"

typedef struct private_padlock_rng_t private_padlock_rng_t;
typedef enum padlock_quality_factor_t padlock_quality_factor_t;

/**
 * Padlock RNG quality factors
 */
enum padlock_quality_factor_t {
	/* Lowest quality: Reads 8 bytes */
	PADLOCK_QF0 = 0x00,
	/* Medium quality: Reads 4 bytes */
	PADLOCK_QF1 = 0x01,
	/* Better quality: Reads 2 bytes */
	PADLOCK_QF2 = 0x10,
	/* Highest quality: Reads 1 byte */
	PADLOCK_QF3 = 0x11,
};

/**
 * Private data of an padlock_rng_t object.
 */
struct private_padlock_rng_t {

	/**
	 * Public padlock_rng_t interface.
	 */
	padlock_rng_t public;

	/**
	 * Padlock quality factor
	 */
	padlock_quality_factor_t quality;
};

/**
 * Get bytes from Padlock RNG. buf should have space for (len + 7)
 */
static void rng(char *buf, int len, int quality)
{
	while (len > 0)
	{
		int status;

		/* run XSTORE until we have all bytes needed. We do not use REP, as
		 * this should not be performance critical and it's easier this way. */
		asm volatile (
			".byte 0x0F,0xA7,0xC0 \n\t"
			: "=D"(buf), "=a"(status)
			: "d"(quality), "D"(buf));

		/* bits[0..4] of status word contains the number of bytes read */
		len -= status & 0x1F;
	}
}

/**
 * Implementation of padlock_rng_t.allocate_bytes.
 */
static void allocate_bytes(private_padlock_rng_t *this, size_t bytes,
						   chunk_t *chunk)
{
	chunk->len = bytes;
	/* padlock requires some additional bytes */
	chunk->ptr = malloc(bytes + 7);

	rng(chunk->ptr, chunk->len, this->quality);
}

/**
 * Implementation of padlock_rng_t.get_bytes.
 */
static void get_bytes(private_padlock_rng_t *this, size_t bytes,
					  u_int8_t *buffer)
{
	chunk_t chunk;

	/* Padlock needs a larger buffer than "bytes", we need a new buffer */
	allocate_bytes(this, bytes, &chunk);
	memcpy(buffer, chunk.ptr, bytes);
	chunk_clear(&chunk);
}

/**
 * Implementation of padlock_rng_t.destroy.
 */
static void destroy(private_padlock_rng_t *this)
{
	free(this);
}

/*
 * Described in header.
 */
padlock_rng_t *padlock_rng_create(rng_quality_t quality)
{
	private_padlock_rng_t *this = malloc_thing(private_padlock_rng_t);

	this->public.rng.get_bytes = (void (*) (rng_t *, size_t, u_int8_t*)) get_bytes;
	this->public.rng.allocate_bytes = (void (*) (rng_t *, size_t, chunk_t*)) allocate_bytes;
	this->public.rng.destroy = (void (*) (rng_t *))destroy;

	/* map RNG quality to Padlock quality factor */
	switch (quality)
	{
		case RNG_WEAK:
			this->quality = PADLOCK_QF0;
			break;
		case RNG_STRONG:
			this->quality = PADLOCK_QF1;
			break;
		case RNG_TRUE:
			this->quality = PADLOCK_QF3;
			break;
	}

	return &this->public;
}