aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/plugins/serpent/serpent_crypter.c
blob: 613c337da08e97634380ccc9673e32118a1bed30 (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
175
176
177
178
179
180
181
182
/*
 * Copyright (C) JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
 * Copyright (C) 2005-2006 Martin Willi
 * Copyright (C) 2009 Andreas Steffen
 * 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 "serpent_crypter.h"
#include "serpent.h"

#define SERPENT_BLOCK_SIZE 16

typedef struct private_serpent_crypter_t private_serpent_crypter_t;

/**
 * Class implementing the AES symmetric encryption algorithm.
 * 
 * @ingroup crypters
 */
struct private_serpent_crypter_t {
	
	/**
	 * Public part of this class.
	 */
	serpent_crypter_t public;
	
	/**
	 * Serpent context
	 */
    serpent_context serpent_ctx;
	
	/**
	* Key size of this Serpent cipher object.
	*/
	u_int32_t key_size;
};

/**
 * Implementation of crypter_t.encrypt.
 */
static void encrypt(private_serpent_crypter_t *this, chunk_t data, chunk_t iv,
					chunk_t *encrypted)
{
	int pos = 0;
	u_int8_t *in, *out;
	
	if (encrypted)
	{
		*encrypted = chunk_alloc(data.len);
		out = encrypted->ptr;
	}
	else
	{
		out = data.ptr;
	}
	in = data.ptr;
	
	while ( pos < data.len)
	{
		const u_int32_t *iv_i;

		iv_i = (pos) ? (const u_int32_t*)(out - 16) :
					   (const u_int32_t*)(iv.ptr);
		*((u_int32_t *)(&out[ 0])) = iv_i[0] ^ *((const u_int32_t *)(&in[ 0]));
		*((u_int32_t *)(&out[ 4])) = iv_i[1] ^ *((const u_int32_t *)(&in[ 4]));
		*((u_int32_t *)(&out[ 8])) = iv_i[2] ^ *((const u_int32_t *)(&in[ 8]));
		*((u_int32_t *)(&out[12])) = iv_i[3] ^ *((const u_int32_t *)(&in[12]));

		serpent_encrypt(&this->serpent_ctx, out, out);

		in  += SERPENT_BLOCK_SIZE;
		out += SERPENT_BLOCK_SIZE;
		pos += SERPENT_BLOCK_SIZE;
	}
}

/**
 * Implementation of crypter_t.decrypt.
 */
static void decrypt(private_serpent_crypter_t *this, chunk_t data, chunk_t iv,
					chunk_t *decrypted)
{
	int pos = data.len - SERPENT_BLOCK_SIZE;
	u_int8_t *in, *out;
	
	if (decrypted)
	{
		*decrypted = chunk_alloc(data.len);
		out = decrypted->ptr;
	}
	else
	{
		out = data.ptr;
	}
	in = data.ptr;
	in  += pos;
	out += pos;

	while (pos >= 0)
	{
		const u_int32_t *iv_i;

		serpent_decrypt(&this->serpent_ctx, in, out);

		iv_i = (pos) ? (const u_int32_t*)(in - 16) :
					   (const u_int32_t*)(iv.ptr);
		*((u_int32_t *)(&out[ 0])) ^= iv_i[0];
		*((u_int32_t *)(&out[ 4])) ^= iv_i[1];
		*((u_int32_t *)(&out[ 8])) ^= iv_i[2];
		*((u_int32_t *)(&out[12])) ^= iv_i[3];

		in  -= SERPENT_BLOCK_SIZE;
		out -= SERPENT_BLOCK_SIZE;
		pos -= SERPENT_BLOCK_SIZE;
	}
}

/**
 * Implementation of crypter_t.get_block_size.
 */
static size_t get_block_size (private_serpent_crypter_t *this)
{
	return SERPENT_BLOCK_SIZE;
}

/**
 * Implementation of crypter_t.get_key_size.
 */
static size_t get_key_size (private_serpent_crypter_t *this)
{
	return this->key_size;
}

/**
 * Implementation of crypter_t.set_key.
 */
static void set_key (private_serpent_crypter_t *this, chunk_t key)
{
    serpent_set_key(&this->serpent_ctx, key.ptr, key.len);
}

/**
 * Implementation of crypter_t.destroy and serpent_crypter_t.destroy.
 */
static void destroy (private_serpent_crypter_t *this)
{
	free(this);
}

/*
 * Described in header
 */
serpent_crypter_t *serpent_crypter_create(encryption_algorithm_t algo, size_t key_size)
{
	private_serpent_crypter_t *this;
	
	if (algo != ENCR_SERPENT_CBC || !(key_size == 16 || key_size == 32))
	{
		return NULL;
	}
	
	this = malloc_thing(private_serpent_crypter_t);
	
	this->public.crypter_interface.encrypt = (void (*) (crypter_t *, chunk_t,chunk_t, chunk_t *)) encrypt;
	this->public.crypter_interface.decrypt = (void (*) (crypter_t *, chunk_t , chunk_t, chunk_t *)) decrypt;
	this->public.crypter_interface.get_block_size = (size_t (*) (crypter_t *)) get_block_size;
	this->public.crypter_interface.get_key_size = (size_t (*) (crypter_t *)) get_key_size;
	this->public.crypter_interface.set_key = (void (*) (crypter_t *,chunk_t)) set_key;
	this->public.crypter_interface.destroy = (void (*) (crypter_t *)) destroy;
	
	return &(this->public);
}