summaryrefslogtreecommitdiffstats
path: root/src/protocols/ec25519_fhmqvc/state.c
blob: fc5527690e0854578ff0707323d4bc9d59f66db8 (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
/*
  Copyright (c) 2012-2016, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
   \file

   ec25519-fhmqvc protocol: state handling
*/


#include "handshake.h"
#include "../../crypto.h"


/** Allocates the protocol-specific state */
static void init_protocol_state(void) {
	if (!ctx.protocol_state) {
		ctx.protocol_state = fastd_new0(fastd_protocol_state_t);

		ctx.protocol_state->prev_handshake_key.preferred_till = ctx.now;
		ctx.protocol_state->handshake_key.preferred_till = ctx.now;
	}
}

/** Generates a new ephemeral keypair */
static void new_handshake_key(keypair_t *key) {
	fastd_random_bytes(key->secret.p, SECRETKEYBYTES, false);
	ecc_25519_gf_sanitize_secret(&key->secret, &key->secret);

	ecc_25519_work_t work;
	ecc_25519_scalarmult(&work, &key->secret, &ecc_25519_work_base_legacy);
	ecc_25519_store_packed_legacy(&key->public.int256, &work);

	if (!divide_key(&key->secret))
		exit_bug("generated invalid ephemeral key");
}

/**
   Performs maintenance tasks on the protocol state

   If there is currently no preferred ephemeral keypair, a new one
   will be generated.
*/
void fastd_protocol_ec25519_fhmqvc_maintenance(void) {
	init_protocol_state();

	if (!is_handshake_key_preferred(&ctx.protocol_state->handshake_key)) {
		pr_debug("generating new handshake key");

		ctx.protocol_state->prev_handshake_key = ctx.protocol_state->handshake_key;

		ctx.protocol_state->handshake_key.serial++;

		new_handshake_key(&ctx.protocol_state->handshake_key.key);

		ctx.protocol_state->handshake_key.preferred_till = ctx.now + 15000;
		ctx.protocol_state->handshake_key.valid_till = ctx.now + 30000;
	}
}

/** Allocated protocol-specific peer state */
void fastd_protocol_ec25519_fhmqvc_init_peer_state(fastd_peer_t *peer) {
	init_protocol_state();

	if (peer->protocol_state)
		exit_bug("tried to reinit peer state");

	peer->protocol_state = fastd_new0(fastd_protocol_peer_state_t);
	peer->protocol_state->last_serial = ctx.protocol_state->handshake_key.serial;
}

/** Resets a the state of a session, freeing method-specific state */
static void reset_session(protocol_session_t *session) {
	if (session->method)
		session->method->provider->session_free(session->method_state);
	secure_memzero(session, sizeof(protocol_session_t));
}

/** Resets all protocol-specific state of a peer */
void fastd_protocol_ec25519_fhmqvc_reset_peer_state(fastd_peer_t *peer) {
	if (!peer->protocol_state)
		return;

	reset_session(&peer->protocol_state->old_session);
	reset_session(&peer->protocol_state->session);
}

/** Frees the protocol-specific state */
void fastd_protocol_ec25519_fhmqvc_free_peer_state(fastd_peer_t *peer) {
	if (peer->protocol_state) {
		reset_session(&peer->protocol_state->old_session);
		reset_session(&peer->protocol_state->session);

		free(peer->protocol_state);
	}
}