nettle-openssl.c 7.98 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* nettle-openssl.c
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

   Glue that's used only by the benchmark, and subject to change.

   Copyright (C) 2002 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * 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.

   or both in parallel, as here.

   GNU Nettle 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.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
Niels Möller's avatar
Niels Möller committed
33
34
35

#if HAVE_CONFIG_H
# include "config.h"
36
#endif
Niels Möller's avatar
Niels Möller committed
37
38
39

/* Openssl glue, for comparative benchmarking only */

40
#if WITH_OPENSSL
Niels Möller's avatar
Niels Möller committed
41

42
43
/* No ancient ssleay compatibility */
#define NCOMPAT
44
#define OPENSSL_DISABLE_OLD_DES_SUPPORT
45

46
#include <assert.h>
Niels Möller's avatar
Niels Möller committed
47

48
49
50
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
Niels Möller's avatar
Niels Möller committed
51

52
53
54
#include <openssl/md5.h>
#include <openssl/sha.h>

55
#include "nettle-internal.h"
Niels Möller's avatar
Niels Möller committed
56
57


58
/* AES */
59
60
61
62
63
64
static nettle_set_key_func openssl_aes128_set_encrypt_key;
static nettle_set_key_func openssl_aes128_set_decrypt_key;
static nettle_set_key_func openssl_aes192_set_encrypt_key;
static nettle_set_key_func openssl_aes192_set_decrypt_key;
static nettle_set_key_func openssl_aes256_set_encrypt_key;
static nettle_set_key_func openssl_aes256_set_decrypt_key;
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

struct AESCipher {
  EVP_CIPHER_CTX *ctx;
};

void
nettle_openssl_init(void)
{
  ERR_load_crypto_strings();
  OpenSSL_add_all_algorithms();
#if OPENSSL_VERSION_NUMBER >= 0x1010000
  CONF_modules_load_file(NULL, NULL, 0);
#else
  OPENSSL_config(NULL);
#endif
}

static void
openssl_evp_set_encrypt_key(void *ctx, const uint8_t *key, const EVP_CIPHER *cipher)
{
  EVP_CIPHER_CTX **ctxptr = ctx;
  *ctxptr = EVP_CIPHER_CTX_new();
  assert(EVP_EncryptInit_ex(*ctxptr, cipher, NULL, key, NULL) == 1);
  EVP_CIPHER_CTX_set_padding(*ctxptr, 0);
}
static void
openssl_evp_set_decrypt_key(void *ctx, const uint8_t *key, const EVP_CIPHER *cipher)
{
  EVP_CIPHER_CTX **ctxptr = ctx;
  *ctxptr = EVP_CIPHER_CTX_new();
  assert(EVP_DecryptInit_ex(*ctxptr, cipher, NULL, key, NULL) == 1);
  EVP_CIPHER_CTX_set_padding(*ctxptr, 0);
}
98
static void
99
openssl_aes128_set_encrypt_key(void *ctx, const uint8_t *key)
100
{
101
  openssl_evp_set_encrypt_key(ctx, key, EVP_aes_128_ecb());
102
103
104
105
}
static void
openssl_aes128_set_decrypt_key(void *ctx, const uint8_t *key)
{
106
  openssl_evp_set_decrypt_key(ctx, key, EVP_aes_128_ecb());
107
108
109
}

static void
110
111
openssl_aes192_set_encrypt_key(void *ctx, const uint8_t *key)
{
112
  openssl_evp_set_encrypt_key(ctx, key, EVP_aes_192_ecb());
113
114
115
116
}
static void
openssl_aes192_set_decrypt_key(void *ctx, const uint8_t *key)
{
117
  openssl_evp_set_decrypt_key(ctx, key, EVP_aes_192_ecb());
118
119
120
121
122
}

static void
openssl_aes256_set_encrypt_key(void *ctx, const uint8_t *key)
{
123
  openssl_evp_set_encrypt_key(ctx, key, EVP_aes_256_ecb());
124
125
126
}
static void
openssl_aes256_set_decrypt_key(void *ctx, const uint8_t *key)
127
{
128
  openssl_evp_set_decrypt_key(ctx, key, EVP_aes_256_ecb());
129
130
131
}

static void
132
openssl_evp_encrypt(const void *ctx, size_t length,
133
134
		    uint8_t *dst, const uint8_t *src)
{
135
136
137
  EVP_CIPHER_CTX * const*ctxptr = ctx;
  int len;
  assert(EVP_EncryptUpdate(*ctxptr, dst, &len, src, length) == 1);
138
139
140
}

static void
141
openssl_evp_decrypt(const void *ctx, size_t length,
142
143
		    uint8_t *dst, const uint8_t *src)
{
144
145
146
  EVP_CIPHER_CTX * const*ctxptr = ctx;
  int len;
  assert(EVP_DecryptUpdate(*ctxptr, dst, &len, src, length) == 1);
147
148
149
150
}

const struct nettle_cipher
nettle_openssl_aes128 = {
151
  "openssl aes128", sizeof(EVP_CIPHER_CTX **),
152
  16, 16,
153
  openssl_aes128_set_encrypt_key, openssl_aes128_set_decrypt_key,
154
  openssl_evp_encrypt, openssl_evp_decrypt
155
156
157
158
};

const struct nettle_cipher
nettle_openssl_aes192 = {
159
  "openssl aes192", sizeof(EVP_CIPHER_CTX **),
160
  16, 24,
161
  openssl_aes192_set_encrypt_key, openssl_aes192_set_decrypt_key,
162
  openssl_evp_encrypt, openssl_evp_decrypt
163
164
165
166
};

const struct nettle_cipher
nettle_openssl_aes256 = {
167
  "openssl aes256", sizeof(EVP_CIPHER_CTX **),
168
  16, 32,
169
  openssl_aes256_set_encrypt_key, openssl_aes256_set_decrypt_key,
170
  openssl_evp_encrypt, openssl_evp_decrypt
171
172
};

173
174
/* Arcfour */
static void
175
openssl_arcfour128_set_encrypt_key(void *ctx, const uint8_t *key)
176
{
177
  openssl_evp_set_encrypt_key(ctx, key, EVP_rc4());
178
179
180
}

static void
181
openssl_arcfour128_set_decrypt_key(void *ctx, const uint8_t *key)
182
{
183
  openssl_evp_set_decrypt_key(ctx, key, EVP_rc4());
184
185
186
187
}

const struct nettle_aead
nettle_openssl_arcfour128 = {
188
  "openssl arcfour128", sizeof(EVP_CIPHER_CTX **),
189
  1, 16, 0, 0,
190
191
  openssl_arcfour128_set_encrypt_key,
  openssl_arcfour128_set_decrypt_key,
192
  NULL, NULL,
193
194
  (nettle_crypt_func *)openssl_evp_encrypt,
  (nettle_crypt_func *)openssl_evp_decrypt,
195
196
197
  NULL,  
};

Niels Möller's avatar
Niels Möller committed
198
199
/* Blowfish */
static void
200
openssl_bf128_set_encrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
201
{
202
  openssl_evp_set_encrypt_key(ctx, key, EVP_bf_ecb());
Niels Möller's avatar
Niels Möller committed
203
204
205
}

static void
206
openssl_bf128_set_decrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
207
{
208
  openssl_evp_set_decrypt_key(ctx, key, EVP_bf_ecb());
Niels Möller's avatar
Niels Möller committed
209
210
211
212
}

const struct nettle_cipher
nettle_openssl_blowfish128 = {
213
  "openssl bf128", sizeof(EVP_CIPHER_CTX **),
214
  8, 16,
215
216
  openssl_bf128_set_encrypt_key, openssl_bf128_set_decrypt_key,
  openssl_evp_encrypt, openssl_evp_decrypt
Niels Möller's avatar
Niels Möller committed
217
218
219
220
221
};


/* DES */
static void
222
openssl_des_set_encrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
223
{
224
  openssl_evp_set_encrypt_key(ctx, key, EVP_des_ecb());
Niels Möller's avatar
Niels Möller committed
225
226
227
}

static void
228
openssl_des_set_decrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
229
{
230
  openssl_evp_set_decrypt_key(ctx, key, EVP_des_ecb());
Niels Möller's avatar
Niels Möller committed
231
232
233
234
}

const struct nettle_cipher
nettle_openssl_des = {
235
  "openssl des", sizeof(EVP_CIPHER_CTX **),
236
  8, 8,
237
238
  openssl_des_set_encrypt_key, openssl_des_set_decrypt_key,
  openssl_evp_encrypt, openssl_evp_decrypt
Niels Möller's avatar
Niels Möller committed
239
240
241
242
243
};


/* Cast128 */
static void
244
openssl_cast128_set_encrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
245
{
246
  openssl_evp_set_encrypt_key(ctx, key, EVP_cast5_ecb());
Niels Möller's avatar
Niels Möller committed
247
248
249
}

static void
250
openssl_cast128_set_decrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
251
{
252
  openssl_evp_set_decrypt_key(ctx, key, EVP_cast5_ecb());
Niels Möller's avatar
Niels Möller committed
253
254
255
256
}

const struct nettle_cipher
nettle_openssl_cast128 = {
257
258
259
260
  "openssl cast128", sizeof(EVP_CIPHER_CTX **),
  8, 16,
  openssl_cast128_set_encrypt_key, openssl_cast128_set_decrypt_key,
  openssl_evp_encrypt, openssl_evp_decrypt
Niels Möller's avatar
Niels Möller committed
261
262
};

263
264
265
/* Hash functions */

/* md5 */
266
static nettle_hash_init_func openssl_md5_init;
267
268
269
270
271
272
static void
openssl_md5_init(void *ctx)
{
  MD5_Init(ctx);
}

273
static nettle_hash_update_func openssl_md5_update;
274
275
static void
openssl_md5_update(void *ctx,
276
277
		   size_t length,
		   const uint8_t *src)
278
279
280
281
{
  MD5_Update(ctx, src, length);
}

282
static nettle_hash_digest_func openssl_md5_digest;
283
284
static void
openssl_md5_digest(void *ctx,
285
		   size_t length, uint8_t *dst)
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{
  assert(length == SHA_DIGEST_LENGTH);
  MD5_Final(dst, ctx);
  MD5_Init(ctx);
}

const struct nettle_hash
nettle_openssl_md5 = {
  "openssl md5", sizeof(SHA_CTX),
  SHA_DIGEST_LENGTH, SHA_CBLOCK,
  openssl_md5_init,
  openssl_md5_update,
  openssl_md5_digest
};

/* sha1 */
302
static nettle_hash_init_func openssl_sha1_init;
303
304
305
306
307
308
static void
openssl_sha1_init(void *ctx)
{
  SHA1_Init(ctx);
}

309
static nettle_hash_update_func openssl_sha1_update;
310
311
static void
openssl_sha1_update(void *ctx,
312
		    size_t length,
313
314
315
316
317
		    const uint8_t *src)
{
  SHA1_Update(ctx, src, length);
}

318
static nettle_hash_digest_func openssl_sha1_digest;
319
320
static void
openssl_sha1_digest(void *ctx,
321
		    size_t length, uint8_t *dst)
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
{
  assert(length == SHA_DIGEST_LENGTH);
  SHA1_Final(dst, ctx);
  SHA1_Init(ctx);
}

const struct nettle_hash
nettle_openssl_sha1 = {
  "openssl sha1", sizeof(SHA_CTX),
  SHA_DIGEST_LENGTH, SHA_CBLOCK,
  openssl_sha1_init,
  openssl_sha1_update,
  openssl_sha1_digest
};
  
337
#endif /* WITH_OPENSSL */