Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / examples / nettle-openssl.c
1 /* nettle-openssl.c
2  *
3  * Glue that's used only by the benchmark, and subject to change.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2002 Niels Möller
9  *  
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.
14  * 
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.
19  * 
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,
23  * MA 02111-1301, USA.
24  */
25
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 /* Openssl glue, for comparative benchmarking only */
31
32 #if WITH_OPENSSL
33
34 /* No ancient ssleay compatibility */
35 #define NCOMPAT
36 #define OPENSSL_DISABLE_OLD_DES_SUPPORT
37
38 #include <assert.h>
39
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>
45
46 #include <openssl/md5.h>
47 #include <openssl/sha.h>
48
49 #include "nettle-internal.h"
50
51
52 /* AES */
53 static nettle_set_key_func openssl_aes_set_encrypt_key;
54 static void
55 openssl_aes_set_encrypt_key(void *ctx, unsigned length, const uint8_t *key)
56 {
57   AES_set_encrypt_key(key, length * 8, ctx);
58 }
59
60 static nettle_set_key_func openssl_aes_set_decrypt_key;
61 static void
62 openssl_aes_set_decrypt_key(void *ctx, unsigned length, const uint8_t *key)
63 {
64   AES_set_decrypt_key(key, length * 8, ctx);
65 }
66
67 static nettle_crypt_func openssl_aes_encrypt;
68 static void
69 openssl_aes_encrypt(void *ctx, unsigned length,
70                     uint8_t *dst, const uint8_t *src)
71 {
72   assert (!(length % AES_BLOCK_SIZE));
73   while (length)
74     {
75       AES_ecb_encrypt(src, dst, ctx, AES_ENCRYPT);
76       length -= AES_BLOCK_SIZE;
77       dst += AES_BLOCK_SIZE;
78       src += AES_BLOCK_SIZE;
79     }
80 }
81
82 static nettle_crypt_func openssl_aes_decrypt;
83 static void
84 openssl_aes_decrypt(void *ctx, unsigned length,
85                     uint8_t *dst, const uint8_t *src)
86 {
87   assert (!(length % AES_BLOCK_SIZE));
88   while (length)
89     {
90       AES_ecb_encrypt(src, dst, ctx, AES_DECRYPT);
91       length -= AES_BLOCK_SIZE;
92       dst += AES_BLOCK_SIZE;
93       src += AES_BLOCK_SIZE;
94     }
95 }
96
97 const struct nettle_cipher
98 nettle_openssl_aes128 = {
99   "openssl aes128", sizeof(AES_KEY),
100   16, 16,
101   openssl_aes_set_encrypt_key, openssl_aes_set_decrypt_key,
102   openssl_aes_encrypt, openssl_aes_decrypt
103 };
104
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
110    * benchmark). */
111   16, 24,
112   openssl_aes_set_encrypt_key, openssl_aes_set_decrypt_key,
113   openssl_aes_encrypt, openssl_aes_decrypt
114 };
115
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
121    * benchmark). */
122   16, 32,
123   openssl_aes_set_encrypt_key, openssl_aes_set_decrypt_key,
124   openssl_aes_encrypt, openssl_aes_decrypt
125 };
126
127 /* Arcfour */
128 static nettle_set_key_func openssl_arcfour_set_key;
129 static void
130 openssl_arcfour_set_key(void *ctx, unsigned length, const uint8_t *key)
131 {
132   RC4_set_key(ctx, length, key);
133 }
134
135 static nettle_crypt_func openssl_arcfour_crypt;
136 static void
137 openssl_arcfour_crypt(void *ctx, unsigned length,
138                       uint8_t *dst, const uint8_t *src)
139 {
140   RC4(ctx, length, src, dst);
141 }
142
143 const struct nettle_cipher
144 nettle_openssl_arcfour128 = {
145   "openssl arcfour128", sizeof(RC4_KEY),
146   0, 16,
147   openssl_arcfour_set_key, openssl_arcfour_set_key,
148   openssl_arcfour_crypt, openssl_arcfour_crypt
149 };
150
151 /* Blowfish */
152 static nettle_set_key_func openssl_bf_set_key;
153 static void
154 openssl_bf_set_key(void *ctx, unsigned length, const uint8_t *key)
155 {
156   BF_set_key(ctx, length, key);
157 }
158
159 static nettle_crypt_func openssl_bf_encrypt;
160 static void
161 openssl_bf_encrypt(void *ctx, unsigned length,
162                    uint8_t *dst, const uint8_t *src)
163 {
164   assert (!(length % BF_BLOCK));
165   while (length)
166     {
167       BF_ecb_encrypt(src, dst, ctx, BF_ENCRYPT);
168       length -= BF_BLOCK;
169       dst += BF_BLOCK;
170       src += BF_BLOCK;
171     }
172 }
173
174 static nettle_crypt_func openssl_bf_decrypt;
175 static void
176 openssl_bf_decrypt(void *ctx, unsigned length,
177                    uint8_t *dst, const uint8_t *src)
178 {
179   assert (!(length % BF_BLOCK));
180   while (length)
181     {
182       BF_ecb_encrypt(src, dst, ctx, BF_DECRYPT);
183       length -= BF_BLOCK;
184       dst += BF_BLOCK;
185       src += BF_BLOCK;
186     }
187 }
188
189 const struct nettle_cipher
190 nettle_openssl_blowfish128 = {
191   "openssl bf128", sizeof(BF_KEY),
192   8, 16,
193   openssl_bf_set_key, openssl_bf_set_key,
194   openssl_bf_encrypt, openssl_bf_decrypt
195 };
196
197
198 /* DES */
199 static nettle_set_key_func openssl_des_set_key;
200 static void
201 openssl_des_set_key(void *ctx, unsigned length, const uint8_t *key)
202 {
203   assert(length == 8);  
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);
209 }
210
211 #define DES_BLOCK_SIZE 8
212
213 static nettle_crypt_func openssl_des_encrypt;
214 static void
215 openssl_des_encrypt(void *ctx, unsigned length,
216                     uint8_t *dst, const uint8_t *src)
217 {
218   assert (!(length % DES_BLOCK_SIZE));
219   while (length)
220     {
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;
225     }
226 }
227
228 static nettle_crypt_func openssl_des_decrypt;
229 static void
230 openssl_des_decrypt(void *ctx, unsigned length,
231                     uint8_t *dst, const uint8_t *src)
232 {
233   assert (!(length % DES_BLOCK_SIZE));
234   while (length)
235     {
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;
240     }
241 }
242
243 const struct nettle_cipher
244 nettle_openssl_des = {
245   "openssl des", sizeof(DES_key_schedule),
246   8, 8,
247   openssl_des_set_key, openssl_des_set_key,
248   openssl_des_encrypt, openssl_des_decrypt
249 };
250
251
252 /* Cast128 */
253 static nettle_set_key_func openssl_cast_set_key;
254 static void
255 openssl_cast_set_key(void *ctx, unsigned length, const uint8_t *key)
256 {
257   CAST_set_key(ctx, length, key);
258 }
259
260 static nettle_crypt_func openssl_cast_encrypt;
261 static void
262 openssl_cast_encrypt(void *ctx, unsigned length,
263                      uint8_t *dst, const uint8_t *src)
264 {
265   assert (!(length % CAST_BLOCK));
266   while (length)
267     {
268       CAST_ecb_encrypt(src, dst, ctx, CAST_ENCRYPT);
269       length -= CAST_BLOCK;
270       dst += CAST_BLOCK;
271       src += CAST_BLOCK;
272     }
273 }
274
275 static nettle_crypt_func openssl_cast_decrypt;
276 static void
277 openssl_cast_decrypt(void *ctx, unsigned length,
278                      uint8_t *dst, const uint8_t *src)
279 {
280   assert (!(length % CAST_BLOCK));
281   while (length)
282     {
283       CAST_ecb_encrypt(src, dst, ctx, CAST_DECRYPT);
284       length -= CAST_BLOCK;
285       dst += CAST_BLOCK;
286       src += CAST_BLOCK;
287     }
288 }
289
290 const struct nettle_cipher
291 nettle_openssl_cast128 = {
292   "openssl cast128", sizeof(CAST_KEY),
293   8, CAST_KEY_LENGTH,
294   openssl_cast_set_key, openssl_cast_set_key,
295   openssl_cast_encrypt, openssl_cast_decrypt
296 };
297
298 /* Hash functions */
299
300 /* md5 */
301 static nettle_hash_init_func openssl_md5_init;
302 static void
303 openssl_md5_init(void *ctx)
304 {
305   MD5_Init(ctx);
306 }
307
308 static nettle_hash_update_func openssl_md5_update;
309 static void
310 openssl_md5_update(void *ctx,
311                     unsigned length,
312                     const uint8_t *src)
313 {
314   MD5_Update(ctx, src, length);
315 }
316
317 static nettle_hash_digest_func openssl_md5_digest;
318 static void
319 openssl_md5_digest(void *ctx,
320                     unsigned length, uint8_t *dst)
321 {
322   assert(length == SHA_DIGEST_LENGTH);
323   MD5_Final(dst, ctx);
324   MD5_Init(ctx);
325 }
326
327 const struct nettle_hash
328 nettle_openssl_md5 = {
329   "openssl md5", sizeof(SHA_CTX),
330   SHA_DIGEST_LENGTH, SHA_CBLOCK,
331   openssl_md5_init,
332   openssl_md5_update,
333   openssl_md5_digest
334 };
335
336 /* sha1 */
337 static nettle_hash_init_func openssl_sha1_init;
338 static void
339 openssl_sha1_init(void *ctx)
340 {
341   SHA1_Init(ctx);
342 }
343
344 static nettle_hash_update_func openssl_sha1_update;
345 static void
346 openssl_sha1_update(void *ctx,
347                     unsigned length,
348                     const uint8_t *src)
349 {
350   SHA1_Update(ctx, src, length);
351 }
352
353 static nettle_hash_digest_func openssl_sha1_digest;
354 static void
355 openssl_sha1_digest(void *ctx,
356                     unsigned length, uint8_t *dst)
357 {
358   assert(length == SHA_DIGEST_LENGTH);
359   SHA1_Final(dst, ctx);
360   SHA1_Init(ctx);
361 }
362
363 const struct nettle_hash
364 nettle_openssl_sha1 = {
365   "openssl sha1", sizeof(SHA_CTX),
366   SHA_DIGEST_LENGTH, SHA_CBLOCK,
367   openssl_sha1_init,
368   openssl_sha1_update,
369   openssl_sha1_digest
370 };
371   
372 #endif /* WITH_OPENSSL */