summaryrefslogtreecommitdiffhomepage
path: root/chachapoly.c
blob: c065fac4b3a24f97ebd6d9d3ac45f75e0fde3db0 (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
/*
 * Dropbear SSH
 * 
 * Copyright (c) 2002,2003 Matt Johnston
 * Copyright (c) 2020 by Vladislav Grishenko
 * All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. */

#include "includes.h"
#include "algo.h"
#include "dbutil.h"
#include "chachapoly.h"

#if DROPBEAR_CHACHA20POLY1305

#define CHACHA20_KEY_LEN 32
#define CHACHA20_BLOCKSIZE 8
#define POLY1305_KEY_LEN 32
#define POLY1305_TAG_LEN 16

static const struct ltc_cipher_descriptor dummy = {.name = NULL};

static const struct dropbear_hash dropbear_chachapoly_mac =
	{NULL, POLY1305_KEY_LEN, POLY1305_TAG_LEN};

const struct dropbear_cipher dropbear_chachapoly =
	{&dummy, CHACHA20_KEY_LEN*2, CHACHA20_BLOCKSIZE};

static int dropbear_chachapoly_start(int UNUSED(cipher), const unsigned char* UNUSED(IV),
			const unsigned char *key, int keylen,
			int UNUSED(num_rounds), dropbear_chachapoly_state *state) {
	int err;

	TRACE2(("enter dropbear_chachapoly_start"))

	if (keylen != CHACHA20_KEY_LEN*2) {
		return CRYPT_ERROR;
	}

	if ((err = chacha_setup(&state->chacha, key,
				CHACHA20_KEY_LEN, 20)) != CRYPT_OK) {
		return err;
	}

	if ((err = chacha_setup(&state->header, key + CHACHA20_KEY_LEN,
				CHACHA20_KEY_LEN, 20) != CRYPT_OK)) {
		return err;
	}

	TRACE2(("leave dropbear_chachapoly_start"))
	return CRYPT_OK;
}

static int dropbear_chachapoly_crypt(unsigned int seq,
			const unsigned char *in, unsigned char *out,
			unsigned long len, unsigned long taglen,
			dropbear_chachapoly_state *state, int direction) {
	poly1305_state poly;
	unsigned char seqbuf[8], key[POLY1305_KEY_LEN], tag[POLY1305_TAG_LEN];
	int err;

	TRACE2(("enter dropbear_chachapoly_crypt"))

	if (len < 4 || taglen != POLY1305_TAG_LEN) {
		return CRYPT_ERROR;
	}

	STORE64H((uint64_t)seq, seqbuf);
	chacha_ivctr64(&state->chacha, seqbuf, sizeof(seqbuf), 0);
	if ((err = chacha_keystream(&state->chacha, key, sizeof(key))) != CRYPT_OK) {
		return err;
	}

	poly1305_init(&poly, key, sizeof(key));
	if (direction == LTC_DECRYPT) {
		poly1305_process(&poly, in, len);
		poly1305_done(&poly, tag, &taglen);
		if (constant_time_memcmp(in + len, tag, taglen) != 0) {
			return CRYPT_ERROR;
		}
	}

	chacha_ivctr64(&state->header, seqbuf, sizeof(seqbuf), 0);
	if ((err = chacha_crypt(&state->header, in, 4, out)) != CRYPT_OK) {
		return err;
	}

	chacha_ivctr64(&state->chacha, seqbuf, sizeof(seqbuf), 1);
	if ((err = chacha_crypt(&state->chacha, in + 4, len - 4, out + 4)) != CRYPT_OK) {
		return err;
	}

	if (direction == LTC_ENCRYPT) {
		poly1305_process(&poly, out, len);
		poly1305_done(&poly, out + len, &taglen);
	}

	TRACE2(("leave dropbear_chachapoly_crypt"))
	return CRYPT_OK;
}

static int dropbear_chachapoly_getlength(unsigned int seq,
			const unsigned char *in, unsigned int *outlen,
			unsigned long len, dropbear_chachapoly_state *state) {
	unsigned char seqbuf[8], buf[4];
	int err;

	TRACE2(("enter dropbear_chachapoly_getlength"))

	if (len < sizeof(buf)) {
		return CRYPT_ERROR;
	}

	STORE64H((uint64_t)seq, seqbuf);
	chacha_ivctr64(&state->header, seqbuf, sizeof(seqbuf), 0);
	if ((err = chacha_crypt(&state->header, in, sizeof(buf), buf)) != CRYPT_OK) {
		return err;
	}

	LOAD32H(*outlen, buf);

	TRACE2(("leave dropbear_chachapoly_getlength"))
	return CRYPT_OK;
}

const struct dropbear_cipher_mode dropbear_mode_chachapoly =
	{(void *)dropbear_chachapoly_start, NULL, NULL,
	 (void *)dropbear_chachapoly_crypt,
	 (void *)dropbear_chachapoly_getlength, &dropbear_chachapoly_mac};

#endif /* DROPBEAR_CHACHA20POLY1305 */