openssl: Return the proper IV length for OpenSSL crypters
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_crypter.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "openssl_crypter.h"
17
18 #include <openssl/evp.h>
19
20 typedef struct private_openssl_crypter_t private_openssl_crypter_t;
21
22 /**
23 * Private data of openssl_crypter_t
24 */
25 struct private_openssl_crypter_t {
26
27 /**
28 * Public part of this class.
29 */
30 openssl_crypter_t public;
31
32 /*
33 * the key
34 */
35 chunk_t key;
36
37 /*
38 * the cipher to use
39 */
40 const EVP_CIPHER *cipher;
41 };
42
43 /**
44 * Look up an OpenSSL algorithm name and validate its key size
45 */
46 static char* lookup_algorithm(u_int16_t ikev2_algo, size_t *key_size)
47 {
48 struct {
49 /* identifier specified in IKEv2 */
50 int ikev2_id;
51 /* name of the algorithm, as used in OpenSSL */
52 char *name;
53 /* default key size in bytes */
54 size_t key_def;
55 /* minimum key size */
56 size_t key_min;
57 /* maximum key size */
58 size_t key_max;
59 } mappings[] = {
60 {ENCR_DES, "des", 8, 8, 8},
61 {ENCR_3DES, "des3", 24, 24, 24},
62 {ENCR_RC5, "rc5", 16, 5, 255},
63 {ENCR_IDEA, "idea", 16, 16, 16},
64 {ENCR_CAST, "cast", 16, 5, 16},
65 {ENCR_BLOWFISH, "blowfish", 16, 5, 56},
66 };
67 int i;
68
69 for (i = 0; i < countof(mappings); i++)
70 {
71 if (ikev2_algo == mappings[i].ikev2_id)
72 {
73 /* set the key size if it is not set */
74 if (*key_size == 0)
75 {
76 *key_size = mappings[i].key_def;
77 }
78 /* validate key size */
79 if (*key_size < mappings[i].key_min ||
80 *key_size > mappings[i].key_max)
81 {
82 return NULL;
83 }
84 return mappings[i].name;
85 }
86 }
87 return NULL;
88 }
89
90 /**
91 * Do the actual en/decryption in an EVP context
92 */
93 static bool crypt(private_openssl_crypter_t *this, chunk_t data, chunk_t iv,
94 chunk_t *dst, int enc)
95 {
96 int len;
97 u_char *out;
98
99 out = data.ptr;
100 if (dst)
101 {
102 *dst = chunk_alloc(data.len);
103 out = dst->ptr;
104 }
105 EVP_CIPHER_CTX ctx;
106 EVP_CIPHER_CTX_init(&ctx);
107 return EVP_CipherInit_ex(&ctx, this->cipher, NULL, NULL, NULL, enc) &&
108 EVP_CIPHER_CTX_set_padding(&ctx, 0) /* disable padding */ &&
109 EVP_CIPHER_CTX_set_key_length(&ctx, this->key.len) &&
110 EVP_CipherInit_ex(&ctx, NULL, NULL, this->key.ptr, iv.ptr, enc) &&
111 EVP_CipherUpdate(&ctx, out, &len, data.ptr, data.len) &&
112 /* since padding is disabled this does nothing */
113 EVP_CipherFinal_ex(&ctx, out + len, &len) &&
114 EVP_CIPHER_CTX_cleanup(&ctx);
115 }
116
117 METHOD(crypter_t, decrypt, bool,
118 private_openssl_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *dst)
119 {
120 return crypt(this, data, iv, dst, 0);
121 }
122
123 METHOD(crypter_t, encrypt, bool,
124 private_openssl_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *dst)
125 {
126 return crypt(this, data, iv, dst, 1);
127 }
128
129 METHOD(crypter_t, get_block_size, size_t,
130 private_openssl_crypter_t *this)
131 {
132 return this->cipher->block_size;
133 }
134
135 METHOD(crypter_t, get_iv_size, size_t,
136 private_openssl_crypter_t *this)
137 {
138 return this->cipher->iv_len;
139 }
140
141 METHOD(crypter_t, get_key_size, size_t,
142 private_openssl_crypter_t *this)
143 {
144 return this->key.len;
145 }
146
147 METHOD(crypter_t, set_key, bool,
148 private_openssl_crypter_t *this, chunk_t key)
149 {
150 memcpy(this->key.ptr, key.ptr, min(key.len, this->key.len));
151 return TRUE;
152 }
153
154 METHOD(crypter_t, destroy, void,
155 private_openssl_crypter_t *this)
156 {
157 chunk_clear(&this->key);
158 free(this);
159 }
160
161 /*
162 * Described in header
163 */
164 openssl_crypter_t *openssl_crypter_create(encryption_algorithm_t algo,
165 size_t key_size)
166 {
167 private_openssl_crypter_t *this;
168
169 INIT(this,
170 .public = {
171 .crypter = {
172 .encrypt = _encrypt,
173 .decrypt = _decrypt,
174 .get_block_size = _get_block_size,
175 .get_iv_size = _get_iv_size,
176 .get_key_size = _get_key_size,
177 .set_key = _set_key,
178 .destroy = _destroy,
179 },
180 },
181 );
182
183 switch (algo)
184 {
185 case ENCR_NULL:
186 this->cipher = EVP_enc_null();
187 key_size = 0;
188 break;
189 case ENCR_AES_CBC:
190 switch (key_size)
191 {
192 case 0:
193 key_size = 16;
194 /* FALL */
195 case 16: /* AES 128 */
196 this->cipher = EVP_get_cipherbyname("aes128");
197 break;
198 case 24: /* AES-192 */
199 this->cipher = EVP_get_cipherbyname("aes192");
200 break;
201 case 32: /* AES-256 */
202 this->cipher = EVP_get_cipherbyname("aes256");
203 break;
204 default:
205 free(this);
206 return NULL;
207 }
208 break;
209 case ENCR_CAMELLIA_CBC:
210 switch (key_size)
211 {
212 case 0:
213 key_size = 16;
214 /* FALL */
215 case 16: /* CAMELLIA 128 */
216 this->cipher = EVP_get_cipherbyname("camellia128");
217 break;
218 case 24: /* CAMELLIA 192 */
219 this->cipher = EVP_get_cipherbyname("camellia192");
220 break;
221 case 32: /* CAMELLIA 256 */
222 this->cipher = EVP_get_cipherbyname("camellia256");
223 break;
224 default:
225 free(this);
226 return NULL;
227 }
228 break;
229 case ENCR_DES_ECB:
230 key_size = 8;
231 this->cipher = EVP_des_ecb();
232 break;
233 default:
234 {
235 char* name;
236
237 name = lookup_algorithm(algo, &key_size);
238 if (!name)
239 {
240 /* algo unavailable or key_size invalid */
241 free(this);
242 return NULL;
243 }
244 this->cipher = EVP_get_cipherbyname(name);
245 break;
246 }
247 }
248
249 if (!this->cipher)
250 {
251 /* OpenSSL does not support the requested algo */
252 free(this);
253 return NULL;
254 }
255
256 this->key = chunk_alloc(key_size);
257
258 return &this->public;
259 }