1 /* nettle-openssl.c
2
3 Glue that's used only by the benchmark, and subject to change.
4
5 Copyright (C) 2002, 2017 Niels Möller
6 Copyright (C) 2017 Red Hat, Inc.
7
8 This file is part of GNU Nettle.
9
10 GNU Nettle is free software: you can redistribute it and/or
11 modify it under the terms of either:
12
13 * the GNU Lesser General Public License as published by the Free
14 Software Foundation; either version 3 of the License, or (at your
15 option) any later version.
16
17 or
18
19 * the GNU General Public License as published by the Free
20 Software Foundation; either version 2 of the License, or (at your
21 option) any later version.
22
23 or both in parallel, as here.
24
25 GNU Nettle is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 General Public License for more details.
29
30 You should have received copies of the GNU General Public License and
31 the GNU Lesser General Public License along with this program. If
32 not, see http://www.gnu.org/licenses/.
33 */
34
35 #if HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 /* Openssl glue, for comparative benchmarking only */
40
41 #if WITH_OPENSSL
42
43 /* No ancient ssleay compatibility */
44 #define NCOMPAT
45 #define OPENSSL_DISABLE_OLD_DES_SUPPORT
46
47 #include <assert.h>
48
49 #include <openssl/conf.h>
50 #include <openssl/evp.h>
51 #include <openssl/err.h>
52
53 #include <openssl/md5.h>
54 #include <openssl/sha.h>
55
56 #include "nettle-internal.h"
57
58 /* We use Openssl's EVP api for all openssl ciphers. This API selects
59 platform-specific implementations if appropriate, e.g., using x86
60 AES-NI instructions. */
61 struct openssl_cipher_ctx {
62 EVP_CIPHER_CTX *evp;
63 };
64
65 void
nettle_openssl_init(void)66 nettle_openssl_init(void)
67 {
68 ERR_load_crypto_strings();
69 OpenSSL_add_all_algorithms();
70 #if OPENSSL_VERSION_NUMBER >= 0x1010000
71 CONF_modules_load_file(NULL, NULL, 0);
72 #else
73 OPENSSL_config(NULL);
74 #endif
75 }
76
77 static void
openssl_evp_set_encrypt_key(void * p,const uint8_t * key,const EVP_CIPHER * cipher)78 openssl_evp_set_encrypt_key(void *p, const uint8_t *key,
79 const EVP_CIPHER *cipher)
80 {
81 struct openssl_cipher_ctx *ctx = p;
82 int ret;
83 ctx->evp = EVP_CIPHER_CTX_new();
84 ret = EVP_CipherInit_ex(ctx->evp, cipher, NULL, key, NULL, 1);
85 assert(ret == 1);
86 EVP_CIPHER_CTX_set_padding(ctx->evp, 0);
87 }
88 static void
openssl_evp_set_decrypt_key(void * p,const uint8_t * key,const EVP_CIPHER * cipher)89 openssl_evp_set_decrypt_key(void *p, const uint8_t *key,
90 const EVP_CIPHER *cipher)
91 {
92 struct openssl_cipher_ctx *ctx = p;
93 int ret;
94 ctx->evp = EVP_CIPHER_CTX_new();
95 ret = EVP_CipherInit_ex(ctx->evp, cipher, NULL, key, NULL, 0);
96 assert(ret == 1);
97 EVP_CIPHER_CTX_set_padding(ctx->evp, 0);
98 }
99
100 static void
openssl_evp_encrypt(const void * p,size_t length,uint8_t * dst,const uint8_t * src)101 openssl_evp_encrypt(const void *p, size_t length,
102 uint8_t *dst, const uint8_t *src)
103 {
104 const struct openssl_cipher_ctx *ctx = p;
105 int len;
106 int ret = EVP_EncryptUpdate(ctx->evp, dst, &len, src, length);
107 assert(ret == 1);
108 }
109 static void
openssl_evp_decrypt(const void * p,size_t length,uint8_t * dst,const uint8_t * src)110 openssl_evp_decrypt(const void *p, size_t length,
111 uint8_t *dst, const uint8_t *src)
112 {
113 const struct openssl_cipher_ctx *ctx = p;
114 int len;
115 int ret = EVP_DecryptUpdate(ctx->evp, dst, &len, src, length);
116 assert(ret == 1);
117 }
118
119 static void
openssl_evp_set_nonce(void * p,const uint8_t * nonce)120 openssl_evp_set_nonce(void *p, const uint8_t *nonce)
121 {
122 const struct openssl_cipher_ctx *ctx = p;
123 int ret = EVP_CipherInit_ex(ctx->evp, NULL, NULL, NULL, nonce, -1);
124 assert(ret == 1);
125 }
126
127 static void
openssl_evp_update(void * p,size_t length,const uint8_t * src)128 openssl_evp_update(void *p, size_t length, const uint8_t *src)
129 {
130 const struct openssl_cipher_ctx *ctx = p;
131 int len;
132 int ret = EVP_EncryptUpdate(ctx->evp, NULL, &len, src, length);
133 assert(ret == 1);
134 }
135
136 /* This will work for encryption only! */
137 static void
openssl_evp_gcm_digest(void * p,size_t length,uint8_t * dst)138 openssl_evp_gcm_digest(void *p, size_t length, uint8_t *dst)
139 {
140 const struct openssl_cipher_ctx *ctx = p;
141 int ret = EVP_CIPHER_CTX_ctrl(ctx->evp, EVP_CTRL_GCM_GET_TAG, length, dst);
142 assert(ret == 1);
143 }
144
145 static void
openssl_evp_aead_encrypt(void * p,size_t length,uint8_t * dst,const uint8_t * src)146 openssl_evp_aead_encrypt(void *p, size_t length,
147 uint8_t *dst, const uint8_t *src)
148 {
149 const struct openssl_cipher_ctx *ctx = p;
150 int len;
151 int ret = EVP_EncryptUpdate(ctx->evp, dst, &len, src, length);
152 assert(ret == 1);
153 }
154
155 static void
openssl_evp_aead_decrypt(void * p,size_t length,uint8_t * dst,const uint8_t * src)156 openssl_evp_aead_decrypt(void *p, size_t length,
157 uint8_t *dst, const uint8_t *src)
158 {
159 const struct openssl_cipher_ctx *ctx = p;
160 int len;
161 int ret = EVP_DecryptUpdate(ctx->evp, dst, &len, src, length);
162 assert(ret == 1);
163 }
164
165 /* AES */
166 static nettle_set_key_func openssl_aes128_set_encrypt_key;
167 static nettle_set_key_func openssl_aes128_set_decrypt_key;
168 static nettle_set_key_func openssl_aes192_set_encrypt_key;
169 static nettle_set_key_func openssl_aes192_set_decrypt_key;
170 static nettle_set_key_func openssl_aes256_set_encrypt_key;
171 static nettle_set_key_func openssl_aes256_set_decrypt_key;
172
173 static void
openssl_aes128_set_encrypt_key(void * ctx,const uint8_t * key)174 openssl_aes128_set_encrypt_key(void *ctx, const uint8_t *key)
175 {
176 openssl_evp_set_encrypt_key(ctx, key, EVP_aes_128_ecb());
177 }
178 static void
openssl_aes128_set_decrypt_key(void * ctx,const uint8_t * key)179 openssl_aes128_set_decrypt_key(void *ctx, const uint8_t *key)
180 {
181 openssl_evp_set_decrypt_key(ctx, key, EVP_aes_128_ecb());
182 }
183
184 static void
openssl_aes192_set_encrypt_key(void * ctx,const uint8_t * key)185 openssl_aes192_set_encrypt_key(void *ctx, const uint8_t *key)
186 {
187 openssl_evp_set_encrypt_key(ctx, key, EVP_aes_192_ecb());
188 }
189 static void
openssl_aes192_set_decrypt_key(void * ctx,const uint8_t * key)190 openssl_aes192_set_decrypt_key(void *ctx, const uint8_t *key)
191 {
192 openssl_evp_set_decrypt_key(ctx, key, EVP_aes_192_ecb());
193 }
194
195 static void
openssl_aes256_set_encrypt_key(void * ctx,const uint8_t * key)196 openssl_aes256_set_encrypt_key(void *ctx, const uint8_t *key)
197 {
198 openssl_evp_set_encrypt_key(ctx, key, EVP_aes_256_ecb());
199 }
200 static void
openssl_aes256_set_decrypt_key(void * ctx,const uint8_t * key)201 openssl_aes256_set_decrypt_key(void *ctx, const uint8_t *key)
202 {
203 openssl_evp_set_decrypt_key(ctx, key, EVP_aes_256_ecb());
204 }
205
206 const struct nettle_cipher
207 nettle_openssl_aes128 = {
208 "openssl aes128", sizeof(struct openssl_cipher_ctx),
209 16, 16,
210 openssl_aes128_set_encrypt_key, openssl_aes128_set_decrypt_key,
211 openssl_evp_encrypt, openssl_evp_decrypt
212 };
213
214 const struct nettle_cipher
215 nettle_openssl_aes192 = {
216 "openssl aes192", sizeof(struct openssl_cipher_ctx),
217 16, 24,
218 openssl_aes192_set_encrypt_key, openssl_aes192_set_decrypt_key,
219 openssl_evp_encrypt, openssl_evp_decrypt
220 };
221
222 const struct nettle_cipher
223 nettle_openssl_aes256 = {
224 "openssl aes256", sizeof(struct openssl_cipher_ctx),
225 16, 32,
226 openssl_aes256_set_encrypt_key, openssl_aes256_set_decrypt_key,
227 openssl_evp_encrypt, openssl_evp_decrypt
228 };
229
230 /* AES-GCM */
231 static void
openssl_gcm_aes128_set_encrypt_key(void * ctx,const uint8_t * key)232 openssl_gcm_aes128_set_encrypt_key(void *ctx, const uint8_t *key)
233 {
234 openssl_evp_set_encrypt_key(ctx, key, EVP_aes_128_gcm());
235 }
236 static void
openssl_gcm_aes128_set_decrypt_key(void * ctx,const uint8_t * key)237 openssl_gcm_aes128_set_decrypt_key(void *ctx, const uint8_t *key)
238 {
239 openssl_evp_set_decrypt_key(ctx, key, EVP_aes_128_gcm());
240 }
241
242 static void
openssl_gcm_aes192_set_encrypt_key(void * ctx,const uint8_t * key)243 openssl_gcm_aes192_set_encrypt_key(void *ctx, const uint8_t *key)
244 {
245 openssl_evp_set_encrypt_key(ctx, key, EVP_aes_192_gcm());
246 }
247 static void
openssl_gcm_aes192_set_decrypt_key(void * ctx,const uint8_t * key)248 openssl_gcm_aes192_set_decrypt_key(void *ctx, const uint8_t *key)
249 {
250 openssl_evp_set_decrypt_key(ctx, key, EVP_aes_192_gcm());
251 }
252
253 static void
openssl_gcm_aes256_set_encrypt_key(void * ctx,const uint8_t * key)254 openssl_gcm_aes256_set_encrypt_key(void *ctx, const uint8_t *key)
255 {
256 openssl_evp_set_encrypt_key(ctx, key, EVP_aes_256_gcm());
257 }
258 static void
openssl_gcm_aes256_set_decrypt_key(void * ctx,const uint8_t * key)259 openssl_gcm_aes256_set_decrypt_key(void *ctx, const uint8_t *key)
260 {
261 openssl_evp_set_decrypt_key(ctx, key, EVP_aes_256_gcm());
262 }
263
264 const struct nettle_aead
265 nettle_openssl_gcm_aes128 = {
266 "openssl gcm_aes128", sizeof(struct openssl_cipher_ctx),
267 16, 16, 12, 16,
268 openssl_gcm_aes128_set_encrypt_key, openssl_gcm_aes128_set_decrypt_key,
269 openssl_evp_set_nonce, openssl_evp_update,
270 openssl_evp_aead_encrypt, openssl_evp_aead_decrypt,
271 openssl_evp_gcm_digest
272 };
273
274 const struct nettle_aead
275 nettle_openssl_gcm_aes192 = {
276 "openssl gcm_aes192", sizeof(struct openssl_cipher_ctx),
277 16, 24, 12, 16,
278 openssl_gcm_aes192_set_encrypt_key, openssl_gcm_aes192_set_decrypt_key,
279 openssl_evp_set_nonce, openssl_evp_update,
280 openssl_evp_aead_encrypt, openssl_evp_aead_decrypt,
281 openssl_evp_gcm_digest
282 };
283
284 const struct nettle_aead
285 nettle_openssl_gcm_aes256 = {
286 "openssl gcm_aes256", sizeof(struct openssl_cipher_ctx),
287 16, 32, 12, 16,
288 openssl_gcm_aes256_set_encrypt_key, openssl_gcm_aes256_set_decrypt_key,
289 openssl_evp_set_nonce, openssl_evp_update,
290 openssl_evp_aead_encrypt, openssl_evp_aead_decrypt,
291 openssl_evp_gcm_digest
292 };
293
294 /* Arcfour */
295 static void
openssl_arcfour128_set_encrypt_key(void * ctx,const uint8_t * key)296 openssl_arcfour128_set_encrypt_key(void *ctx, const uint8_t *key)
297 {
298 openssl_evp_set_encrypt_key(ctx, key, EVP_rc4());
299 }
300
301 static void
openssl_arcfour128_set_decrypt_key(void * ctx,const uint8_t * key)302 openssl_arcfour128_set_decrypt_key(void *ctx, const uint8_t *key)
303 {
304 openssl_evp_set_decrypt_key(ctx, key, EVP_rc4());
305 }
306
307 const struct nettle_aead
308 nettle_openssl_arcfour128 = {
309 "openssl arcfour128", sizeof(struct openssl_cipher_ctx),
310 1, 16, 0, 0,
311 openssl_arcfour128_set_encrypt_key,
312 openssl_arcfour128_set_decrypt_key,
313 NULL, NULL,
314 (nettle_crypt_func *)openssl_evp_encrypt,
315 (nettle_crypt_func *)openssl_evp_decrypt,
316 NULL,
317 };
318
319 /* Blowfish */
320 static void
openssl_bf128_set_encrypt_key(void * ctx,const uint8_t * key)321 openssl_bf128_set_encrypt_key(void *ctx, const uint8_t *key)
322 {
323 openssl_evp_set_encrypt_key(ctx, key, EVP_bf_ecb());
324 }
325
326 static void
openssl_bf128_set_decrypt_key(void * ctx,const uint8_t * key)327 openssl_bf128_set_decrypt_key(void *ctx, const uint8_t *key)
328 {
329 openssl_evp_set_decrypt_key(ctx, key, EVP_bf_ecb());
330 }
331
332 const struct nettle_cipher
333 nettle_openssl_blowfish128 = {
334 "openssl bf128", sizeof(struct openssl_cipher_ctx),
335 8, 16,
336 openssl_bf128_set_encrypt_key, openssl_bf128_set_decrypt_key,
337 openssl_evp_encrypt, openssl_evp_decrypt
338 };
339
340
341 /* DES */
342 static void
openssl_des_set_encrypt_key(void * ctx,const uint8_t * key)343 openssl_des_set_encrypt_key(void *ctx, const uint8_t *key)
344 {
345 openssl_evp_set_encrypt_key(ctx, key, EVP_des_ecb());
346 }
347
348 static void
openssl_des_set_decrypt_key(void * ctx,const uint8_t * key)349 openssl_des_set_decrypt_key(void *ctx, const uint8_t *key)
350 {
351 openssl_evp_set_decrypt_key(ctx, key, EVP_des_ecb());
352 }
353
354 const struct nettle_cipher
355 nettle_openssl_des = {
356 "openssl des", sizeof(struct openssl_cipher_ctx),
357 8, 8,
358 openssl_des_set_encrypt_key, openssl_des_set_decrypt_key,
359 openssl_evp_encrypt, openssl_evp_decrypt
360 };
361
362
363 /* Cast128 */
364 static void
openssl_cast128_set_encrypt_key(void * ctx,const uint8_t * key)365 openssl_cast128_set_encrypt_key(void *ctx, const uint8_t *key)
366 {
367 openssl_evp_set_encrypt_key(ctx, key, EVP_cast5_ecb());
368 }
369
370 static void
openssl_cast128_set_decrypt_key(void * ctx,const uint8_t * key)371 openssl_cast128_set_decrypt_key(void *ctx, const uint8_t *key)
372 {
373 openssl_evp_set_decrypt_key(ctx, key, EVP_cast5_ecb());
374 }
375
376 const struct nettle_cipher
377 nettle_openssl_cast128 = {
378 "openssl cast128", sizeof(struct openssl_cipher_ctx),
379 8, 16,
380 openssl_cast128_set_encrypt_key, openssl_cast128_set_decrypt_key,
381 openssl_evp_encrypt, openssl_evp_decrypt
382 };
383
384 /* Hash functions */
385
386 /* md5 */
387 static nettle_hash_init_func openssl_md5_init;
388 static void
openssl_md5_init(void * ctx)389 openssl_md5_init(void *ctx)
390 {
391 MD5_Init(ctx);
392 }
393
394 static nettle_hash_update_func openssl_md5_update;
395 static void
openssl_md5_update(void * ctx,size_t length,const uint8_t * src)396 openssl_md5_update(void *ctx,
397 size_t length,
398 const uint8_t *src)
399 {
400 MD5_Update(ctx, src, length);
401 }
402
403 static nettle_hash_digest_func openssl_md5_digest;
404 static void
openssl_md5_digest(void * ctx,size_t length,uint8_t * dst)405 openssl_md5_digest(void *ctx,
406 size_t length, uint8_t *dst)
407 {
408 assert(length == SHA_DIGEST_LENGTH);
409 MD5_Final(dst, ctx);
410 MD5_Init(ctx);
411 }
412
413 const struct nettle_hash
414 nettle_openssl_md5 = {
415 "openssl md5", sizeof(SHA_CTX),
416 SHA_DIGEST_LENGTH, SHA_CBLOCK,
417 openssl_md5_init,
418 openssl_md5_update,
419 openssl_md5_digest
420 };
421
422 /* sha1 */
423 static nettle_hash_init_func openssl_sha1_init;
424 static void
openssl_sha1_init(void * ctx)425 openssl_sha1_init(void *ctx)
426 {
427 SHA1_Init(ctx);
428 }
429
430 static nettle_hash_update_func openssl_sha1_update;
431 static void
openssl_sha1_update(void * ctx,size_t length,const uint8_t * src)432 openssl_sha1_update(void *ctx,
433 size_t length,
434 const uint8_t *src)
435 {
436 SHA1_Update(ctx, src, length);
437 }
438
439 static nettle_hash_digest_func openssl_sha1_digest;
440 static void
openssl_sha1_digest(void * ctx,size_t length,uint8_t * dst)441 openssl_sha1_digest(void *ctx,
442 size_t length, uint8_t *dst)
443 {
444 assert(length == SHA_DIGEST_LENGTH);
445 SHA1_Final(dst, ctx);
446 SHA1_Init(ctx);
447 }
448
449 const struct nettle_hash
450 nettle_openssl_sha1 = {
451 "openssl sha1", sizeof(SHA_CTX),
452 SHA_DIGEST_LENGTH, SHA_CBLOCK,
453 openssl_sha1_init,
454 openssl_sha1_update,
455 openssl_sha1_digest
456 };
457
458 #endif /* WITH_OPENSSL */
459