summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/curve25519/curve25519_dh.c
blob: c550263d4e8706d018bcdbfa0928e08730d3d09f (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
/*
 * Copyright (C) 2015 Martin Willi
 * Copyright (C) 2015 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 <string.h>
#include <stdint.h>

#include "curve25519_dh.h"
#include "curve25519_drv.h"

typedef struct private_curve25519_dh_t private_curve25519_dh_t;

/**
 * Private data of an curve25519_dh_t object.
 */
struct private_curve25519_dh_t {

	/**
	 * Public curve25519_dh_t interface.
	 */
	curve25519_dh_t public;

	/**
	 * Shared key, if computed
	 */
	u_char shared[CURVE25519_KEY_SIZE];

	/**
	 * TRUE if shared secret is computed
	 */
	bool computed;

	/**
	 * Curve25519 backend
	 */
	curve25519_drv_t *drv;
};

/**
 * Generate a valid Curve25519 key
 */
static bool generate_key(private_curve25519_dh_t *this)
{
	u_char key[CURVE25519_KEY_SIZE];
	rng_t *rng;

	rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
	if (!rng)
	{
		DBG1(DBG_LIB, "no RNG found for quality %N",
			 rng_quality_names, RNG_STRONG);
		return FALSE;
	}
	if (!rng->get_bytes(rng, CURVE25519_KEY_SIZE, key))
	{
		rng->destroy(rng);
		return FALSE;
	}
	rng->destroy(rng);

	return this->drv->set_key(this->drv, key);
}

METHOD(diffie_hellman_t, set_other_public_value, bool,
	private_curve25519_dh_t *this, chunk_t value)
{
	if (value.len == CURVE25519_KEY_SIZE)
	{
		if (this->drv->curve25519(this->drv, value.ptr, this->shared))
		{
			this->computed = TRUE;
			return TRUE;
		}
	}
	return FALSE;
}

METHOD(diffie_hellman_t, get_my_public_value, bool,
	private_curve25519_dh_t *this, chunk_t *value)
{
	u_char basepoint[CURVE25519_KEY_SIZE] = { 9 };

	*value = chunk_alloc(CURVE25519_KEY_SIZE);
	if (this->drv->curve25519(this->drv, basepoint, value->ptr))
	{
		return TRUE;
	}
	free(value->ptr);
	return FALSE;
}

METHOD(diffie_hellman_t, set_private_value, bool,
	private_curve25519_dh_t *this, chunk_t value)
{
	if (value.len != CURVE25519_KEY_SIZE)
	{
		return FALSE;
	}
	return this->drv->set_key(this->drv, value.ptr);
}

METHOD(diffie_hellman_t, get_shared_secret, bool,
	private_curve25519_dh_t *this, chunk_t *secret)
{
	if (!this->computed)
	{
		return FALSE;
	}
	*secret = chunk_clone(chunk_from_thing(this->shared));
	return TRUE;
}

METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
	private_curve25519_dh_t *this)
{
	return CURVE_25519;
}

METHOD(diffie_hellman_t, destroy, void,
	private_curve25519_dh_t *this)
{
	this->drv->destroy(this->drv);
	free(this);
}

/*
 * Described in header.
 */
curve25519_dh_t *curve25519_dh_create(diffie_hellman_group_t group)
{
	private_curve25519_dh_t *this;

	if (group != CURVE_25519)
	{
		return FALSE;
	}

	INIT(this,
		.public = {
			.dh = {
				.get_shared_secret = _get_shared_secret,
				.set_other_public_value = _set_other_public_value,
				.get_my_public_value = _get_my_public_value,
				.set_private_value = _set_private_value,
				.get_dh_group = _get_dh_group,
				.destroy = _destroy,
			},
		},
		.drv = curve25519_drv_probe(),
	);

	if (!this->drv)
	{
		free(this);
		return NULL;
	}
	if (!generate_key(this))
	{
		destroy(this);
		return NULL;
	}
	return &this->public;
}