summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/ctr/ctr_ipsec_crypter.c
blob: ddcae423b0470e7040af52400ece61f694bb849b (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) 2010 Martin Willi
 * Copyright (C) 2010 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 "ctr_ipsec_crypter.h"

typedef struct private_ctr_ipsec_crypter_t private_ctr_ipsec_crypter_t;

/**
 * Private data of an ctr_ipsec_crypter_t object.
 */
struct private_ctr_ipsec_crypter_t {

	/**
	 * Public ctr_ipsec_crypter_t interface.
	 */
	ctr_ipsec_crypter_t public;

	/**
	 * Underlying CBC crypter
	 */
	crypter_t *crypter;

	/**
	 * counter state
	 */
	struct {
		char nonce[4];
		char iv[8];
		u_int32_t counter;
	} __attribute__((packed)) state;
};

/**
 * Do the CTR crypto operation
 */
static void crypt_ctr(private_ctr_ipsec_crypter_t *this,
					  chunk_t in, chunk_t out)
{
	size_t is, bs;
	chunk_t state;

	is = this->crypter->get_iv_size(this->crypter);
	bs = sizeof(this->state);

	this->state.counter = htonl(1);
	state = chunk_create((char*)&this->state, bs);

	while (in.len > 0)
	{
		char iv[is], block[bs];

		memset(iv, 0, is);
		memcpy(block, state.ptr, bs);
		this->crypter->encrypt(this->crypter,
						chunk_create(block, bs), chunk_create(iv, is), NULL);
		chunk_increment(state);

		if (in.ptr != out.ptr)
		{
			memcpy(out.ptr, in.ptr, min(in.len, bs));
		}
		memxor(out.ptr, block, min(in.len, bs));
		in = chunk_skip(in, bs);
		out = chunk_skip(out, bs);
	}
}

METHOD(crypter_t, crypt, void,
	private_ctr_ipsec_crypter_t *this, chunk_t in, chunk_t iv, chunk_t *out)
{
	memcpy(this->state.iv, iv.ptr, sizeof(this->state.iv));

	if (out)
	{
		*out = chunk_alloc(in.len);
		crypt_ctr(this, in, *out);
	}
	else
	{
		crypt_ctr(this, in, in);
	}
}

METHOD(crypter_t, get_block_size, size_t,
	private_ctr_ipsec_crypter_t *this)
{
	return 1;
}

METHOD(crypter_t, get_iv_size, size_t,
	private_ctr_ipsec_crypter_t *this)
{
	return sizeof(this->state.iv);
}

METHOD(crypter_t, get_key_size, size_t,
	private_ctr_ipsec_crypter_t *this)
{
	return this->crypter->get_key_size(this->crypter)
			+ sizeof(this->state.nonce);
}

METHOD(crypter_t, set_key, void,
	private_ctr_ipsec_crypter_t *this, chunk_t key)
{
	memcpy(this->state.nonce, key.ptr + key.len - sizeof(this->state.nonce),
		   sizeof(this->state.nonce));
	key.len -= sizeof(this->state.nonce);
	this->crypter->set_key(this->crypter, key);
}

METHOD(crypter_t, destroy, void,
	private_ctr_ipsec_crypter_t *this)
{
	this->crypter->destroy(this->crypter);
	free(this);
}

/**
 * See header
 */
ctr_ipsec_crypter_t *ctr_ipsec_crypter_create(encryption_algorithm_t algo,
											  size_t key_size)
{
	private_ctr_ipsec_crypter_t *this;

	switch (algo)
	{
		case ENCR_AES_CTR:
			algo = ENCR_AES_CBC;
			break;
		case ENCR_CAMELLIA_CTR:
			algo = ENCR_CAMELLIA_CBC;
			break;
		default:
			return NULL;
	}

	INIT(this,
		.public = {
			.crypter = {
				.encrypt = _crypt,
				.decrypt = _crypt,
				.get_block_size = _get_block_size,
				.get_iv_size = _get_iv_size,
				.get_key_size = _get_key_size,
				.set_key = _set_key,
				.destroy = _destroy,
			},
		},
		.crypter = lib->crypto->create_crypter(lib->crypto, algo, key_size),
	);

	if (!this->crypter)
	{
		free(this);
		return NULL;
	}

	return &this->public;
}