3 * Glue that's used only by the benchmark, and subject to change.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2002 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
30 /* Openssl glue, for comparative benchmarking only */
34 /* No ancient ssleay compatibility */
36 #define OPENSSL_DISABLE_OLD_DES_SUPPORT
40 #include <openssl/aes.h>
41 #include <openssl/blowfish.h>
42 #include <openssl/des.h>
43 #include <openssl/cast.h>
44 #include <openssl/rc4.h>
46 #include <openssl/md5.h>
47 #include <openssl/sha.h>
49 #include "nettle-internal.h"
53 static nettle_set_key_func openssl_aes_set_encrypt_key;
55 openssl_aes_set_encrypt_key(void *ctx, unsigned length, const uint8_t *key)
57 AES_set_encrypt_key(key, length * 8, ctx);
60 static nettle_set_key_func openssl_aes_set_decrypt_key;
62 openssl_aes_set_decrypt_key(void *ctx, unsigned length, const uint8_t *key)
64 AES_set_decrypt_key(key, length * 8, ctx);
67 static nettle_crypt_func openssl_aes_encrypt;
69 openssl_aes_encrypt(void *ctx, unsigned length,
70 uint8_t *dst, const uint8_t *src)
72 assert (!(length % AES_BLOCK_SIZE));
75 AES_ecb_encrypt(src, dst, ctx, AES_ENCRYPT);
76 length -= AES_BLOCK_SIZE;
77 dst += AES_BLOCK_SIZE;
78 src += AES_BLOCK_SIZE;
82 static nettle_crypt_func openssl_aes_decrypt;
84 openssl_aes_decrypt(void *ctx, unsigned length,
85 uint8_t *dst, const uint8_t *src)
87 assert (!(length % AES_BLOCK_SIZE));
90 AES_ecb_encrypt(src, dst, ctx, AES_DECRYPT);
91 length -= AES_BLOCK_SIZE;
92 dst += AES_BLOCK_SIZE;
93 src += AES_BLOCK_SIZE;
97 const struct nettle_cipher
98 nettle_openssl_aes128 = {
99 "openssl aes128", sizeof(AES_KEY),
101 openssl_aes_set_encrypt_key, openssl_aes_set_decrypt_key,
102 openssl_aes_encrypt, openssl_aes_decrypt
105 const struct nettle_cipher
106 nettle_openssl_aes192 = {
107 "openssl aes192", sizeof(AES_KEY),
108 /* Claim no block size, so that the benchmark doesn't try CBC mode
109 * (as openssl cipher + nettle cbc is somewhat pointless to
112 openssl_aes_set_encrypt_key, openssl_aes_set_decrypt_key,
113 openssl_aes_encrypt, openssl_aes_decrypt
116 const struct nettle_cipher
117 nettle_openssl_aes256 = {
118 "openssl aes256", sizeof(AES_KEY),
119 /* Claim no block size, so that the benchmark doesn't try CBC mode
120 * (as openssl cipher + nettle cbc is somewhat pointless to
123 openssl_aes_set_encrypt_key, openssl_aes_set_decrypt_key,
124 openssl_aes_encrypt, openssl_aes_decrypt
128 static nettle_set_key_func openssl_arcfour_set_key;
130 openssl_arcfour_set_key(void *ctx, unsigned length, const uint8_t *key)
132 RC4_set_key(ctx, length, key);
135 static nettle_crypt_func openssl_arcfour_crypt;
137 openssl_arcfour_crypt(void *ctx, unsigned length,
138 uint8_t *dst, const uint8_t *src)
140 RC4(ctx, length, src, dst);
143 const struct nettle_cipher
144 nettle_openssl_arcfour128 = {
145 "openssl arcfour128", sizeof(RC4_KEY),
147 openssl_arcfour_set_key, openssl_arcfour_set_key,
148 openssl_arcfour_crypt, openssl_arcfour_crypt
152 static nettle_set_key_func openssl_bf_set_key;
154 openssl_bf_set_key(void *ctx, unsigned length, const uint8_t *key)
156 BF_set_key(ctx, length, key);
159 static nettle_crypt_func openssl_bf_encrypt;
161 openssl_bf_encrypt(void *ctx, unsigned length,
162 uint8_t *dst, const uint8_t *src)
164 assert (!(length % BF_BLOCK));
167 BF_ecb_encrypt(src, dst, ctx, BF_ENCRYPT);
174 static nettle_crypt_func openssl_bf_decrypt;
176 openssl_bf_decrypt(void *ctx, unsigned length,
177 uint8_t *dst, const uint8_t *src)
179 assert (!(length % BF_BLOCK));
182 BF_ecb_encrypt(src, dst, ctx, BF_DECRYPT);
189 const struct nettle_cipher
190 nettle_openssl_blowfish128 = {
191 "openssl bf128", sizeof(BF_KEY),
193 openssl_bf_set_key, openssl_bf_set_key,
194 openssl_bf_encrypt, openssl_bf_decrypt
199 static nettle_set_key_func openssl_des_set_key;
201 openssl_des_set_key(void *ctx, unsigned length, const uint8_t *key)
204 /* Not sure what "unchecked" means. We want to ignore parity bits,
205 but it would still make sense to check for weak keys. */
206 /* Explicit cast used as I don't want to care about openssl's broken
207 array typedefs DES_cblock and const_DES_cblock. */
208 DES_set_key_unchecked( (void *) key, ctx);
211 #define DES_BLOCK_SIZE 8
213 static nettle_crypt_func openssl_des_encrypt;
215 openssl_des_encrypt(void *ctx, unsigned length,
216 uint8_t *dst, const uint8_t *src)
218 assert (!(length % DES_BLOCK_SIZE));
221 DES_ecb_encrypt((void *) src, (void *) dst, ctx, DES_ENCRYPT);
222 length -= DES_BLOCK_SIZE;
223 dst += DES_BLOCK_SIZE;
224 src += DES_BLOCK_SIZE;
228 static nettle_crypt_func openssl_des_decrypt;
230 openssl_des_decrypt(void *ctx, unsigned length,
231 uint8_t *dst, const uint8_t *src)
233 assert (!(length % DES_BLOCK_SIZE));
236 DES_ecb_encrypt((void *) src, (void *) dst, ctx, DES_DECRYPT);
237 length -= DES_BLOCK_SIZE;
238 dst += DES_BLOCK_SIZE;
239 src += DES_BLOCK_SIZE;
243 const struct nettle_cipher
244 nettle_openssl_des = {
245 "openssl des", sizeof(DES_key_schedule),
247 openssl_des_set_key, openssl_des_set_key,
248 openssl_des_encrypt, openssl_des_decrypt
253 static nettle_set_key_func openssl_cast_set_key;
255 openssl_cast_set_key(void *ctx, unsigned length, const uint8_t *key)
257 CAST_set_key(ctx, length, key);
260 static nettle_crypt_func openssl_cast_encrypt;
262 openssl_cast_encrypt(void *ctx, unsigned length,
263 uint8_t *dst, const uint8_t *src)
265 assert (!(length % CAST_BLOCK));
268 CAST_ecb_encrypt(src, dst, ctx, CAST_ENCRYPT);
269 length -= CAST_BLOCK;
275 static nettle_crypt_func openssl_cast_decrypt;
277 openssl_cast_decrypt(void *ctx, unsigned length,
278 uint8_t *dst, const uint8_t *src)
280 assert (!(length % CAST_BLOCK));
283 CAST_ecb_encrypt(src, dst, ctx, CAST_DECRYPT);
284 length -= CAST_BLOCK;
290 const struct nettle_cipher
291 nettle_openssl_cast128 = {
292 "openssl cast128", sizeof(CAST_KEY),
294 openssl_cast_set_key, openssl_cast_set_key,
295 openssl_cast_encrypt, openssl_cast_decrypt
301 static nettle_hash_init_func openssl_md5_init;
303 openssl_md5_init(void *ctx)
308 static nettle_hash_update_func openssl_md5_update;
310 openssl_md5_update(void *ctx,
314 MD5_Update(ctx, src, length);
317 static nettle_hash_digest_func openssl_md5_digest;
319 openssl_md5_digest(void *ctx,
320 unsigned length, uint8_t *dst)
322 assert(length == SHA_DIGEST_LENGTH);
327 const struct nettle_hash
328 nettle_openssl_md5 = {
329 "openssl md5", sizeof(SHA_CTX),
330 SHA_DIGEST_LENGTH, SHA_CBLOCK,
337 static nettle_hash_init_func openssl_sha1_init;
339 openssl_sha1_init(void *ctx)
344 static nettle_hash_update_func openssl_sha1_update;
346 openssl_sha1_update(void *ctx,
350 SHA1_Update(ctx, src, length);
353 static nettle_hash_digest_func openssl_sha1_digest;
355 openssl_sha1_digest(void *ctx,
356 unsigned length, uint8_t *dst)
358 assert(length == SHA_DIGEST_LENGTH);
359 SHA1_Final(dst, ctx);
363 const struct nettle_hash
364 nettle_openssl_sha1 = {
365 "openssl sha1", sizeof(SHA_CTX),
366 SHA_DIGEST_LENGTH, SHA_CBLOCK,
372 #endif /* WITH_OPENSSL */