nettle-openssl.c 8.27 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* nettle-openssl.c
2
3
4

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

5
   Copyright (C) 2002, 2017 Niels Möller
Niels Möller's avatar
Niels Möller committed
6
   Copyright (C) 2017 Red Hat, Inc.
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

   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
34
35
36

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

/* Openssl glue, for comparative benchmarking only */

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

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

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

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

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

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

58
59
60
61
62
/* We use Openssl's EVP api for all openssl ciphers. This API selects
   platform-specific implementations if appropriate, e.g., using x86
   AES-NI instructions. */
struct openssl_cipher_ctx {
  EVP_CIPHER_CTX *evp;
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
};

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
78
79
openssl_evp_set_encrypt_key(void *p, const uint8_t *key,
			    const EVP_CIPHER *cipher)
80
{
81
82
83
84
  struct openssl_cipher_ctx *ctx = p;
  ctx->evp = EVP_CIPHER_CTX_new();
  assert(EVP_EncryptInit_ex(ctx->evp, cipher, NULL, key, NULL) == 1);
  EVP_CIPHER_CTX_set_padding(ctx->evp, 0);
85
86
}
static void
87
88
openssl_evp_set_decrypt_key(void *p, const uint8_t *key,
			    const EVP_CIPHER *cipher)
89
{
90
91
92
93
  struct openssl_cipher_ctx *ctx = p;
  ctx->evp = EVP_CIPHER_CTX_new();
  assert(EVP_DecryptInit_ex(ctx->evp, cipher, NULL, key, NULL) == 1);
  EVP_CIPHER_CTX_set_padding(ctx->evp, 0);
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

static void
openssl_evp_encrypt(const void *p, size_t length,
		    uint8_t *dst, const uint8_t *src)
{
  const struct openssl_cipher_ctx *ctx = p;
  int len;
  assert(EVP_EncryptUpdate(ctx->evp, dst, &len, src, length) == 1);
}
static void
openssl_evp_decrypt(const void *p, size_t length,
		    uint8_t *dst, const uint8_t *src)
{
  const struct openssl_cipher_ctx *ctx = p;
  int len;
  assert(EVP_DecryptUpdate(ctx->evp, dst, &len, src, length) == 1);
}

/* AES */
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;

121
static void
122
openssl_aes128_set_encrypt_key(void *ctx, const uint8_t *key)
123
{
124
  openssl_evp_set_encrypt_key(ctx, key, EVP_aes_128_ecb());
125
126
127
128
}
static void
openssl_aes128_set_decrypt_key(void *ctx, const uint8_t *key)
{
129
  openssl_evp_set_decrypt_key(ctx, key, EVP_aes_128_ecb());
130
131
132
}

static void
133
134
openssl_aes192_set_encrypt_key(void *ctx, const uint8_t *key)
{
135
  openssl_evp_set_encrypt_key(ctx, key, EVP_aes_192_ecb());
136
137
138
139
}
static void
openssl_aes192_set_decrypt_key(void *ctx, const uint8_t *key)
{
140
  openssl_evp_set_decrypt_key(ctx, key, EVP_aes_192_ecb());
141
142
143
144
145
}

static void
openssl_aes256_set_encrypt_key(void *ctx, const uint8_t *key)
{
146
  openssl_evp_set_encrypt_key(ctx, key, EVP_aes_256_ecb());
147
148
149
}
static void
openssl_aes256_set_decrypt_key(void *ctx, const uint8_t *key)
150
{
151
  openssl_evp_set_decrypt_key(ctx, key, EVP_aes_256_ecb());
152
153
154
155
}

const struct nettle_cipher
nettle_openssl_aes128 = {
156
  "openssl aes128", sizeof(struct openssl_cipher_ctx),
157
  16, 16,
158
  openssl_aes128_set_encrypt_key, openssl_aes128_set_decrypt_key,
159
  openssl_evp_encrypt, openssl_evp_decrypt
160
161
162
163
};

const struct nettle_cipher
nettle_openssl_aes192 = {
164
  "openssl aes192", sizeof(struct openssl_cipher_ctx),
165
  16, 24,
166
  openssl_aes192_set_encrypt_key, openssl_aes192_set_decrypt_key,
167
  openssl_evp_encrypt, openssl_evp_decrypt
168
169
170
171
};

const struct nettle_cipher
nettle_openssl_aes256 = {
172
  "openssl aes256", sizeof(struct openssl_cipher_ctx),
173
  16, 32,
174
  openssl_aes256_set_encrypt_key, openssl_aes256_set_decrypt_key,
175
  openssl_evp_encrypt, openssl_evp_decrypt
176
177
};

178
179
/* Arcfour */
static void
180
openssl_arcfour128_set_encrypt_key(void *ctx, const uint8_t *key)
181
{
182
  openssl_evp_set_encrypt_key(ctx, key, EVP_rc4());
183
184
185
}

static void
186
openssl_arcfour128_set_decrypt_key(void *ctx, const uint8_t *key)
187
{
188
  openssl_evp_set_decrypt_key(ctx, key, EVP_rc4());
189
190
191
192
}

const struct nettle_aead
nettle_openssl_arcfour128 = {
193
  "openssl arcfour128", sizeof(struct openssl_cipher_ctx),
194
  1, 16, 0, 0,
195
196
  openssl_arcfour128_set_encrypt_key,
  openssl_arcfour128_set_decrypt_key,
197
  NULL, NULL,
198
199
  (nettle_crypt_func *)openssl_evp_encrypt,
  (nettle_crypt_func *)openssl_evp_decrypt,
200
201
202
  NULL,  
};

Niels Möller's avatar
Niels Möller committed
203
204
/* Blowfish */
static void
205
openssl_bf128_set_encrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
206
{
207
  openssl_evp_set_encrypt_key(ctx, key, EVP_bf_ecb());
Niels Möller's avatar
Niels Möller committed
208
209
210
}

static void
211
openssl_bf128_set_decrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
212
{
213
  openssl_evp_set_decrypt_key(ctx, key, EVP_bf_ecb());
Niels Möller's avatar
Niels Möller committed
214
215
216
217
}

const struct nettle_cipher
nettle_openssl_blowfish128 = {
218
  "openssl bf128", sizeof(struct openssl_cipher_ctx),
219
  8, 16,
220
221
  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
222
223
224
225
226
};


/* DES */
static void
227
openssl_des_set_encrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
228
{
229
  openssl_evp_set_encrypt_key(ctx, key, EVP_des_ecb());
Niels Möller's avatar
Niels Möller committed
230
231
232
}

static void
233
openssl_des_set_decrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
234
{
235
  openssl_evp_set_decrypt_key(ctx, key, EVP_des_ecb());
Niels Möller's avatar
Niels Möller committed
236
237
238
239
}

const struct nettle_cipher
nettle_openssl_des = {
240
  "openssl des", sizeof(struct openssl_cipher_ctx),
241
  8, 8,
242
243
  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
244
245
246
247
248
};


/* Cast128 */
static void
249
openssl_cast128_set_encrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
250
{
251
  openssl_evp_set_encrypt_key(ctx, key, EVP_cast5_ecb());
Niels Möller's avatar
Niels Möller committed
252
253
254
}

static void
255
openssl_cast128_set_decrypt_key(void *ctx, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
256
{
257
  openssl_evp_set_decrypt_key(ctx, key, EVP_cast5_ecb());
Niels Möller's avatar
Niels Möller committed
258
259
260
261
}

const struct nettle_cipher
nettle_openssl_cast128 = {
262
  "openssl cast128", sizeof(struct openssl_cipher_ctx),
263
264
265
  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
266
267
};

268
269
270
/* Hash functions */

/* md5 */
271
static nettle_hash_init_func openssl_md5_init;
272
273
274
275
276
277
static void
openssl_md5_init(void *ctx)
{
  MD5_Init(ctx);
}

278
static nettle_hash_update_func openssl_md5_update;
279
280
static void
openssl_md5_update(void *ctx,
281
282
		   size_t length,
		   const uint8_t *src)
283
284
285
286
{
  MD5_Update(ctx, src, length);
}

287
static nettle_hash_digest_func openssl_md5_digest;
288
289
static void
openssl_md5_digest(void *ctx,
290
		   size_t length, uint8_t *dst)
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
{
  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 */
307
static nettle_hash_init_func openssl_sha1_init;
308
309
310
311
312
313
static void
openssl_sha1_init(void *ctx)
{
  SHA1_Init(ctx);
}

314
static nettle_hash_update_func openssl_sha1_update;
315
316
static void
openssl_sha1_update(void *ctx,
317
		    size_t length,
318
319
320
321
322
		    const uint8_t *src)
{
  SHA1_Update(ctx, src, length);
}

323
static nettle_hash_digest_func openssl_sha1_digest;
324
325
static void
openssl_sha1_digest(void *ctx,
326
		    size_t length, uint8_t *dst)
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
{
  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
};
  
342
#endif /* WITH_OPENSSL */