summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/chapoly/chapoly_xof.c
blob: 2740a55b4cca70d8abb3f2be9e813c6baa32577a (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
/*
 * Copyright (C) 2016 Andreas Steffen
 * HSR 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 "chapoly_xof.h"
#include "chapoly_drv.h"

typedef struct private_chapoly_xof_t private_chapoly_xof_t;

/**
 * Private data of an chapoly_xof_t object.
 */
struct private_chapoly_xof_t {

	/**
	 * Public chapoly_xof_t interface.
	 */
	chapoly_xof_t public;

	/**
	 * Latest block of the ChaCha20 stream.
	 */
	uint8_t stream[CHACHA_BLOCK_SIZE];

	/**
	 * Index pointing to the current position in the stream
	 */
	u_int stream_index;

	/**
	 * Driver backend
	 */
	chapoly_drv_t *drv;
};

METHOD(xof_t, get_type, ext_out_function_t,
	private_chapoly_xof_t *this)
{
	return XOF_CHACHA20;
}

METHOD(xof_t, get_bytes, bool,
	private_chapoly_xof_t *this, size_t out_len, uint8_t *buffer)
{
	size_t index = 0, len, blocks;

	/* empty the stream buffer first */
	len = min(out_len, CHACHA_BLOCK_SIZE - this->stream_index);
	if (len)
	{
		memcpy(buffer, this->stream + this->stream_index, len);
		index += len;
		this->stream_index += len;
	}

	/* copy whole stream blocks directly to output buffer */
	blocks = (out_len - index) / CHACHA_BLOCK_SIZE;	
	while (blocks--)
	{
		if (!this->drv->chacha(this->drv, buffer + index))
		{
			return FALSE;
		}
		index += CHACHA_BLOCK_SIZE;
	}	
	
	/* refill the stream buffer if some more output bytes are needed */
	len = out_len - index;
	if (len)
	{
		if (!this->drv->chacha(this->drv, this->stream))
		{
			return FALSE;
		}
		memcpy(buffer + index, this->stream, len);
		this->stream_index = len;
	}		
	
	return TRUE;
}

METHOD(xof_t, allocate_bytes, bool,
	private_chapoly_xof_t *this, size_t out_len, chunk_t *chunk)
{
	*chunk = chunk_alloc(out_len);

	if (!get_bytes(this, out_len, chunk->ptr))
	{
		chunk_free(chunk);
		return FALSE;
	}

	return TRUE;
}

METHOD(xof_t, get_block_size, size_t,
	private_chapoly_xof_t *this)
{
	return CHACHA_BLOCK_SIZE;
}

METHOD(xof_t, get_seed_size, size_t,
	private_chapoly_xof_t *this)
{
	return CHACHA_KEY_SIZE + CHACHA_SALT_SIZE + CHACHA_IV_SIZE;
}

METHOD(xof_t, set_seed, bool,
	private_chapoly_xof_t *this, chunk_t seed)
{
	this->stream_index = CHACHA_BLOCK_SIZE;

	return  seed.len == get_seed_size(this) &&
			this->drv->set_key(this->drv, "expand 32-byte k",
							seed.ptr, seed.ptr + CHACHA_KEY_SIZE) &&
			this->drv->init(this->drv,
						 	seed.ptr + CHACHA_KEY_SIZE + CHACHA_SALT_SIZE);
}

METHOD(xof_t, destroy, void,
	private_chapoly_xof_t *this)
{
	this->drv->destroy(this->drv);
	free(this);
}

/**
 * See header
 */
chapoly_xof_t *chapoly_xof_create(ext_out_function_t algorithm)
{
	private_chapoly_xof_t *this;
	chapoly_drv_t *drv;

	if (algorithm != XOF_CHACHA20)
	{
		return NULL;
	}

	drv = chapoly_drv_probe();
	if (!drv)
	{
		return NULL;
	}

	INIT(this,
		.public = {
			.xof_interface = {
				.get_type = _get_type,
				.get_bytes = _get_bytes,
				.allocate_bytes = _allocate_bytes,
				.get_block_size = _get_block_size,
				.get_seed_size = _get_seed_size,
				.set_seed = _set_seed,
				.destroy = _destroy,
			},
		},
		.drv = drv,
	);

	return &this->public;
}