source: trunk/programs/test/benchmark.c @ 1215

Revision 1215, 12.2 KB checked in by paul, 2 months ago (diff)
  • Use standard IV of 12
Line 
1/*
2 *  Benchmark demonstration program
3 *
4 *  Copyright (C) 2006-2011, Brainspark B.V.
5 *
6 *  This file is part of PolarSSL (http://www.polarssl.org)
7 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 *  All rights reserved.
10 *
11 *  This program is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  This program is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License along
22 *  with this program; if not, write to the Free Software Foundation, Inc.,
23 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
34#include "polarssl/config.h"
35
36#include "polarssl/md4.h"
37#include "polarssl/md5.h"
38#include "polarssl/sha1.h"
39#include "polarssl/sha2.h"
40#include "polarssl/sha4.h"
41#include "polarssl/arc4.h"
42#include "polarssl/des.h"
43#include "polarssl/aes.h"
44#include "polarssl/camellia.h"
45#include "polarssl/gcm.h"
46#include "polarssl/rsa.h"
47#include "polarssl/timing.h"
48#include "polarssl/havege.h"
49#include "polarssl/ctr_drbg.h"
50
51#define BUFSIZE         1024
52#define HEADER_FORMAT   "  %-15s :  "
53
54static int myrand( void *rng_state, unsigned char *output, size_t len )
55{
56    size_t use_len;
57    int rnd;
58
59    if( rng_state != NULL )
60        rng_state  = NULL;
61
62    while( len > 0 )
63    {
64        use_len = len;
65        if( use_len > sizeof(int) )
66            use_len = sizeof(int);
67
68        rnd = rand();
69        memcpy( output, &rnd, use_len );
70        output += use_len;
71        len -= use_len;
72    }
73
74    return( 0 );
75}
76
77unsigned char buf[BUFSIZE];
78
79#if !defined(POLARSSL_TIMING_C)
80int main( int argc, char *argv[] )
81{
82    ((void) argc);
83    ((void) argv);
84
85    printf("POLARSSL_TIMING_C not defined.\n");
86    return( 0 );
87}
88#else
89int main( int argc, char *argv[] )
90{
91    int keysize;
92    unsigned long i, j, tsc;
93    unsigned char tmp[64];
94#if defined(POLARSSL_ARC4_C)
95    arc4_context arc4;
96#endif
97#if defined(POLARSSL_DES_C)
98    des3_context des3;
99    des_context des;
100#endif
101#if defined(POLARSSL_AES_C)
102    aes_context aes;
103#if defined(POLARSSL_GCM_C)
104    gcm_context gcm;
105#endif
106#endif
107#if defined(POLARSSL_CAMELLIA_C)
108    camellia_context camellia;
109#endif
110#if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) &&    \
111    defined(POLARSSL_GENPRIME)
112    rsa_context rsa;
113#endif
114#if defined(POLARSSL_HAVEGE_C)
115    havege_state hs;
116#endif
117#if defined(POLARSSL_CTR_DRBG_C)
118    ctr_drbg_context    ctr_drbg;
119#endif
120    ((void) argc);
121    ((void) argv);
122
123    memset( buf, 0xAA, sizeof( buf ) );
124
125    printf( "\n" );
126
127#if defined(POLARSSL_MD4_C)
128    printf( HEADER_FORMAT, "MD4" );
129    fflush( stdout );
130
131    set_alarm( 1 );
132    for( i = 1; ! alarmed; i++ )
133        md4( buf, BUFSIZE, tmp );
134
135    tsc = hardclock();
136    for( j = 0; j < 1024; j++ )
137        md4( buf, BUFSIZE, tmp );
138
139    printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
140                    ( hardclock() - tsc ) / ( j * BUFSIZE ) );
141#endif
142
143#if defined(POLARSSL_MD5_C)
144    printf( HEADER_FORMAT, "MD5" );
145    fflush( stdout );
146
147    set_alarm( 1 );
148    for( i = 1; ! alarmed; i++ )
149        md5( buf, BUFSIZE, tmp );
150
151    tsc = hardclock();
152    for( j = 0; j < 1024; j++ )
153        md5( buf, BUFSIZE, tmp );
154
155    printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
156                    ( hardclock() - tsc ) / ( j * BUFSIZE ) );
157#endif
158
159#if defined(POLARSSL_SHA1_C)
160    printf( HEADER_FORMAT, "SHA-1" );
161    fflush( stdout );
162
163    set_alarm( 1 );
164    for( i = 1; ! alarmed; i++ )
165        sha1( buf, BUFSIZE, tmp );
166
167    tsc = hardclock();
168    for( j = 0; j < 1024; j++ )
169        sha1( buf, BUFSIZE, tmp );
170
171    printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
172                    ( hardclock() - tsc ) / ( j * BUFSIZE ) );
173#endif
174
175#if defined(POLARSSL_SHA2_C)
176    printf( HEADER_FORMAT, "SHA-256" );
177    fflush( stdout );
178
179    set_alarm( 1 );
180    for( i = 1; ! alarmed; i++ )
181        sha2( buf, BUFSIZE, tmp, 0 );
182
183    tsc = hardclock();
184    for( j = 0; j < 1024; j++ )
185        sha2( buf, BUFSIZE, tmp, 0 );
186
187    printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
188                    ( hardclock() - tsc ) / ( j * BUFSIZE ) );
189#endif
190
191#if defined(POLARSSL_SHA4_C)
192    printf( HEADER_FORMAT, "SHA-512" );
193    fflush( stdout );
194
195    set_alarm( 1 );
196    for( i = 1; ! alarmed; i++ )
197        sha4( buf, BUFSIZE, tmp, 0 );
198
199    tsc = hardclock();
200    for( j = 0; j < 1024; j++ )
201        sha4( buf, BUFSIZE, tmp, 0 );
202
203    printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
204                    ( hardclock() - tsc ) / ( j * BUFSIZE ) );
205#endif
206
207#if defined(POLARSSL_ARC4_C)
208    printf( HEADER_FORMAT, "ARC4" );
209    fflush( stdout );
210
211    arc4_setup( &arc4, tmp, 32 );
212
213    set_alarm( 1 );
214    for( i = 1; ! alarmed; i++ )
215        arc4_crypt( &arc4, BUFSIZE, buf, buf );
216
217    tsc = hardclock();
218    for( j = 0; j < 1024; j++ )
219        arc4_crypt( &arc4, BUFSIZE, buf, buf );
220
221    printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
222                    ( hardclock() - tsc ) / ( j * BUFSIZE ) );
223#endif
224
225#if defined(POLARSSL_DES_C)
226    printf( HEADER_FORMAT, "3DES" );
227    fflush( stdout );
228
229    des3_set3key_enc( &des3, tmp );
230
231    set_alarm( 1 );
232    for( i = 1; ! alarmed; i++ )
233        des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
234
235    tsc = hardclock();
236    for( j = 0; j < 1024; j++ )
237        des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
238
239    printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
240                    ( hardclock() - tsc ) / ( j * BUFSIZE ) );
241
242    printf( HEADER_FORMAT, "DES" );
243    fflush( stdout );
244
245    des_setkey_enc( &des, tmp );
246
247    set_alarm( 1 );
248    for( i = 1; ! alarmed; i++ )
249        des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
250
251    tsc = hardclock();
252    for( j = 0; j < 1024; j++ )
253        des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
254
255    printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
256                    ( hardclock() - tsc ) / ( j * BUFSIZE ) );
257#endif
258
259#if defined(POLARSSL_AES_C)
260    for( keysize = 128; keysize <= 256; keysize += 64 )
261    {
262        printf( "  AES-CBC-%d     :  ", keysize );
263        fflush( stdout );
264
265        memset( buf, 0, sizeof( buf ) );
266        memset( tmp, 0, sizeof( tmp ) );
267        aes_setkey_enc( &aes, tmp, keysize );
268
269        set_alarm( 1 );
270
271        for( i = 1; ! alarmed; i++ )
272            aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
273
274        tsc = hardclock();
275        for( j = 0; j < 4096; j++ )
276            aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
277
278        printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
279                        ( hardclock() - tsc ) / ( j * BUFSIZE ) );
280    }
281#if defined(POLARSSL_GCM_C)
282    for( keysize = 128; keysize <= 256; keysize += 64 )
283    {
284        printf( "  AES-GCM-%d     :  ", keysize );
285        fflush( stdout );
286
287        memset( buf, 0, sizeof( buf ) );
288        memset( tmp, 0, sizeof( tmp ) );
289        gcm_init( &gcm, tmp, keysize );
290
291        set_alarm( 1 );
292
293        for( i = 1; ! alarmed; i++ )
294            gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp, 12, NULL, 0, buf, buf, 16, tmp );
295
296        tsc = hardclock();
297        for( j = 0; j < 4096; j++ )
298            gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp, 12, NULL, 0, buf, buf, 16, tmp );
299
300        printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
301                        ( hardclock() - tsc ) / ( j * BUFSIZE ) );
302    }
303#endif
304#endif
305
306#if defined(POLARSSL_CAMELLIA_C)
307    for( keysize = 128; keysize <= 256; keysize += 64 )
308    {
309        printf( "  CAMELLIA-CBC-%d:  ", keysize );
310        fflush( stdout );
311
312        memset( buf, 0, sizeof( buf ) );
313        memset( tmp, 0, sizeof( tmp ) );
314        camellia_setkey_enc( &camellia, tmp, keysize );
315
316        set_alarm( 1 );
317
318        for( i = 1; ! alarmed; i++ )
319            camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
320
321        tsc = hardclock();
322        for( j = 0; j < 4096; j++ )
323            camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
324
325        printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
326                        ( hardclock() - tsc ) / ( j * BUFSIZE ) );
327    }
328#endif
329
330#if defined(POLARSSL_HAVEGE_C)
331    printf( HEADER_FORMAT, "HAVEGE" );
332    fflush( stdout );
333
334    havege_init( &hs );
335
336    set_alarm( 1 );
337    for( i = 1; ! alarmed; i++ )
338        havege_random( &hs, buf, BUFSIZE );
339
340    tsc = hardclock();
341    for( j = 1; j < 1024; j++ )
342        havege_random( &hs, buf, BUFSIZE );
343
344    printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
345                    ( hardclock() - tsc ) / ( j * BUFSIZE ) );
346#endif
347
348#if defined(POLARSSL_CTR_DRBG_C)
349    printf( HEADER_FORMAT, "CTR_DRBG (NOPR)" );
350    fflush( stdout );
351
352    if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
353        exit(1);
354
355    set_alarm( 1 );
356    for( i = 1; ! alarmed; i++ )
357        if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
358            exit(1);
359
360    tsc = hardclock();
361    for( j = 1; j < 1024; j++ )
362        if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
363            exit(1);
364
365    printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
366                    ( hardclock() - tsc ) / ( j * BUFSIZE ) );
367
368    printf( HEADER_FORMAT, "CTR_DRBG (PR)" );
369    fflush( stdout );
370
371    if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
372        exit(1);
373
374    ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
375
376    set_alarm( 1 );
377    for( i = 1; ! alarmed; i++ )
378        if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
379            exit(1);
380
381    tsc = hardclock();
382    for( j = 1; j < 1024; j++ )
383        if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
384            exit(1);
385
386    printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
387                    ( hardclock() - tsc ) / ( j * BUFSIZE ) );
388#endif
389
390#if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) &&    \
391    defined(POLARSSL_GENPRIME)
392    rsa_init( &rsa, RSA_PKCS_V15, 0 );
393    rsa_gen_key( &rsa, myrand, NULL, 1024, 65537 );
394
395    printf( HEADER_FORMAT, "RSA-1024" );
396    fflush( stdout );
397    set_alarm( 3 );
398
399    for( i = 1; ! alarmed; i++ )
400    {
401        buf[0] = 0;
402        rsa_public( &rsa, buf, buf );
403    }
404
405    printf( "%9lu  public/s\n", i / 3 );
406
407    printf( HEADER_FORMAT, "RSA-1024" );
408    fflush( stdout );
409    set_alarm( 3 );
410
411    for( i = 1; ! alarmed; i++ )
412    {
413        buf[0] = 0;
414        rsa_private( &rsa, buf, buf );
415    }
416
417    printf( "%9lu private/s\n", i / 3 );
418
419    rsa_free( &rsa );
420
421    rsa_init( &rsa, RSA_PKCS_V15, 0 );
422    rsa_gen_key( &rsa, myrand, NULL, 2048, 65537 );
423
424    printf( HEADER_FORMAT, "RSA-2048" );
425    fflush( stdout );
426    set_alarm( 3 );
427
428    for( i = 1; ! alarmed; i++ )
429    {
430        buf[0] = 0;
431        rsa_public( &rsa, buf, buf );
432    }
433
434    printf( "%9lu  public/s\n", i / 3 );
435
436    printf( HEADER_FORMAT, "RSA-2048" );
437    fflush( stdout );
438    set_alarm( 3 );
439
440    for( i = 1; ! alarmed; i++ )
441    {
442        buf[0] = 0;
443        rsa_private( &rsa, buf, buf );
444    }
445
446    printf( "%9lu private/s\n", i / 3 );
447
448    rsa_free( &rsa );
449
450    rsa_init( &rsa, RSA_PKCS_V15, 0 );
451    rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 );
452
453    printf( HEADER_FORMAT, "RSA-4096" );
454    fflush( stdout );
455    set_alarm( 3 );
456
457    for( i = 1; ! alarmed; i++ )
458    {
459        buf[0] = 0;
460        rsa_public( &rsa, buf, buf );
461    }
462
463    printf( "%9lu  public/s\n", i / 3 );
464
465    printf( HEADER_FORMAT, "RSA-4096" );
466    fflush( stdout );
467    set_alarm( 3 );
468
469    for( i = 1; ! alarmed; i++ )
470    {
471        buf[0] = 0;
472        rsa_private( &rsa, buf, buf );
473    }
474
475    printf( "%9lu private/s\n", i / 3 );
476
477    rsa_free( &rsa );
478#endif
479
480    printf( "\n" );
481
482#if defined(_WIN32)
483    printf( "  Press Enter to exit this program.\n" );
484    fflush( stdout ); getchar();
485#endif
486
487    return( 0 );
488}
489#endif /* POLARSSL_TIMING_C */
Note: See TracBrowser for help on using the repository browser.

What are you looking for?