import cyrus-sasl-2.1.23
[cyrus-sasl.git] / mac / libdes / src / speed.c
1 /* crypto/des/speed.c */\r/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)\r * All rights reserved.\r *\r * This package is an SSL implementation written\r * by Eric Young (eay@mincom.oz.au).\r * The implementation was written so as to conform with Netscapes SSL.\r * \r * This library is free for commercial and non-commercial use as long as\r * the following conditions are aheared to.  The following conditions\r * apply to all code found in this distribution, be it the RC4, RSA,\r * lhash, DES, etc., code; not just the SSL code.  The SSL documentation\r * included with this distribution is covered by the same copyright terms\r * except that the holder is Tim Hudson (tjh@mincom.oz.au).\r * \r * Copyright remains Eric Young's, and as such any Copyright notices in\r * the code are not to be removed.\r * If this package is used in a product, Eric Young should be given attribution\r * as the author of the parts of the library used.\r * This can be in the form of a textual message at program startup or\r * in documentation (online or textual) provided with the package.\r * \r * Redistribution and use in source and binary forms, with or without\r * modification, are permitted provided that the following conditions\r * are met:\r * 1. Redistributions of source code must retain the copyright\r *    notice, this list of conditions and the following disclaimer.\r * 2. Redistributions in binary form must reproduce the above copyright\r *    notice, this list of conditions and the following disclaimer in the\r *    documentation and/or other materials provided with the distribution.\r * 3. All advertising materials mentioning features or use of this software\r *    must display the following acknowledgement:\r *    "This product includes cryptographic software written by\r *     Eric Young (eay@mincom.oz.au)"\r *    The word 'cryptographic' can be left out if the rouines from the library\r *    being used are not cryptographic related :-).\r * 4. If you include any Windows specific code (or a derivative thereof) from \r *    the apps directory (application code) you must include an acknowledgement:\r *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"\r * \r * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND\r * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\r * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r * SUCH DAMAGE.\r * \r * The licence and distribution terms for any publically available version or\r * derivative of this code cannot be changed.  i.e. this code cannot simply be\r * copied and put under another distribution licence\r * [including the GNU Public Licence.]\r */\r\r/* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */\r/* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */\r\r#ifdef HAVE_CONFIG_H\r#include <config.h>\r#endif\r\r#if !defined(MSDOS) && !defined(WIN32)\r#define TIMES\r#endif\r\r#include <stdio.h>\r#ifdef HAVE_UNISTD_H\r#include <unistd.h>\r#endif\r#include <signal.h>\r#ifdef HAVE_TIME_H\r#include <time.h>\r#endif\r#ifdef HAVE_SYS_TYPES_H\r#include <sys/types.h>\r#endif\r#ifdef HAVE_SYS_TIMES_H\r#include <sys/times.h>\r#endif\r\r#ifdef VMS\r#include <types.h>\rstruct tms {\r     time_t tms_utime;\r      time_t tms_stime;\r      time_t tms_uchild;      /* I dunno...  */\r      time_t tms_uchildsys;   /* so these names are a guess :-) */\r   }\r#endif\r\r#ifdef HAVE_SYS_TIMEB_H\r#include <sys/timeb.h>\r#endif\r\r#include <limits.h>\r#ifdef HAVE_SYS_PARAM_H\r#include <sys/param.h>\r#endif\r\r#include "des.h"\r\r/* The following if from times(3) man page.  It may need to be changed */\r#ifndef HZ\r#ifndef CLK_TCK\r#ifndef VMS\r#define HZ       100.0\r#else /* VMS */\r#define HZ        100.0\r#endif\r#else /* CLK_TCK */\r#define HZ ((double)CLK_TCK)\r#endif\r#endif\r\r#define BUFSIZE    ((long)1024)\rlong run=0;\r\r#ifndef NOPROTO\rdouble Time_F(int s);\r#else\rdouble Time_F();\r#endif\r\r#ifdef SIGALRM\r#if defined(__STDC__) || defined(sgi)\r#define SIGRETTYPE void\r#else\r#define SIGRETTYPE int\r#endif\r\r#ifndef NOPROTO\rSIGRETTYPE sig_done(int sig);\r#else\rSIGRETTYPE sig_done();\r#endif\r\rSIGRETTYPE sig_done(sig)\rint sig;\r  {\r      signal(SIGALRM,sig_done);\r      run=0;\r#ifdef LINT\r     sig=sig;\r#endif\r        }\r#endif\r\r#define START 0\r#define STOP  1\r\rdouble Time_F(s)\rint s;\r     {\r      double ret;\r#ifdef TIMES\r       static struct tms tstart,tend;\r\r        if (s == START)\r                {\r              times(&tstart);\r                return(0);\r             }\r      else\r           {\r              times(&tend);\r          ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;\r            return((ret == 0.0)?1e-6:ret);\r         }\r#else /* !times() */\r static struct timeb tstart,tend;\r       long i;\r\r       if (s == START)\r                {\r              ftime(&tstart);\r                return(0);\r             }\r      else\r           {\r              ftime(&tend);\r          i=(long)tend.millitm-(long)tstart.millitm;\r             ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;\r              return((ret == 0.0)?1e-6:ret);\r         }\r#endif\r       }\r\rint main(argc,argv)\rint argc;\rchar **argv;\r  {\r      long count;\r    static unsigned char buf[BUFSIZE];\r     static des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};\r      static des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};\r      static des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};\r      des_key_schedule sch,sch2,sch3;\r        double a,b,c,d,e;\r#ifndef SIGALRM\r      long ca,cb,cc,cd,ce;\r#endif\r\r#ifndef TIMES\r     printf("To get the most acurate results, try to run this\n");\r  printf("program when this computer is idle.\n");\r#endif\r\r       des_set_key((C_Block *)key2,sch2);\r     des_set_key((C_Block *)key3,sch3);\r\r#ifndef SIGALRM\r    printf("First we calculate the approximate speed ...\n");\r      des_set_key((C_Block *)key,sch);\r       count=10;\r      do      {\r              long i;\r                DES_LONG data[2];\r\r             count*=2;\r              Time_F(START);\r         for (i=count; i; i--)\r                  des_encrypt(data,&(sch[0]),DES_ENCRYPT);\r               d=Time_F(STOP);\r                } while (d < 3.0);\r     ca=count;\r      cb=count*3;\r    cc=count*3*8/BUFSIZE+1;\r        cd=count*8/BUFSIZE+1;\r  ce=count/20+1;\r printf("Doing set_key %ld times\n",ca);\r#define COND(d) (count != (d))\r#define COUNT(d) (d)\r#else\r#define COND(c)       (run)\r#define COUNT(d) (count)\r signal(SIGALRM,sig_done);\r      printf("Doing set_key for 10 seconds\n");\r      alarm(10);\r#endif\r\r     Time_F(START);\r for (count=0,run=1; COND(ca); count++)\r         des_set_key((C_Block *)key,sch);\r       d=Time_F(STOP);\r        printf("%ld set_key's in %.2f seconds\n",count,d);\r     a=((double)COUNT(ca))/d;\r\r#ifdef SIGALRM\r       printf("Doing des_encrypt's for 10 seconds\n");\r        alarm(10);\r#else\r       printf("Doing des_encrypt %ld times\n",cb);\r#endif\r     Time_F(START);\r for (count=0,run=1; COND(cb); count++)\r         {\r              DES_LONG data[2];\r\r             des_encrypt(data,&(sch[0]),DES_ENCRYPT);\r               }\r      d=Time_F(STOP);\r        printf("%ld des_encrypt's in %.2f second\n",count,d);\r  b=((double)COUNT(cb)*8)/d;\r\r#ifdef SIGALRM\r     printf("Doing des_cbc_encrypt on %ld byte blocks for 10 seconds\n",\r            BUFSIZE);\r      alarm(10);\r#else\r       printf("Doing des_cbc_encrypt %ld times on %ld byte blocks\n",cc,\r              BUFSIZE);\r#endif\r       Time_F(START);\r for (count=0,run=1; COND(cc); count++)\r         des_ncbc_encrypt((C_Block *)buf,(C_Block *)buf,BUFSIZE,&(sch[0]),\r                      (C_Block *)&(key[0]),DES_ENCRYPT);\r     d=Time_F(STOP);\r        printf("%ld des_cbc_encrypt's of %ld byte blocks in %.2f second\n",\r            count,BUFSIZE,d);\r      c=((double)COUNT(cc)*BUFSIZE)/d;\r\r#ifdef SIGALRM\r       printf("Doing des_ede_cbc_encrypt on %ld byte blocks for 10 seconds\n",\r                BUFSIZE);\r      alarm(10);\r#else\r       printf("Doing des_ede_cbc_encrypt %ld times on %ld byte blocks\n",cd,\r          BUFSIZE);\r#endif\r       Time_F(START);\r for (count=0,run=1; COND(cd); count++)\r         des_ede3_cbc_encrypt((C_Block *)buf,(C_Block *)buf,BUFSIZE,\r                    &(sch[0]),\r                     &(sch2[0]),\r                    &(sch3[0]),\r                    (C_Block *)&(key[0]),\r                  DES_ENCRYPT);\r  d=Time_F(STOP);\r        printf("%ld des_ede_cbc_encrypt's of %ld byte blocks in %.2f second\n",\r                count,BUFSIZE,d);\r      d=((double)COUNT(cd)*BUFSIZE)/d;\r\r#ifdef SIGALRM\r       printf("Doing crypt for 10 seconds\n");\r        alarm(10);\r#else\r       printf("Doing crypt %ld times\n",ce);\r#endif\r   Time_F(START);\r for (count=0,run=1; COND(ce); count++)\r         crypt("testing1","ef");\r        e=Time_F(STOP);\r        printf("%ld crypts in %.2f second\n",count,e);\r e=((double)COUNT(ce))/e;\r\r      printf("set_key            per sec = %12.2f (%5.1fuS)\n",a,1.0e6/a);\r   printf("DES raw ecb bytes  per sec = %12.2f (%5.1fuS)\n",b,8.0e6/b);\r   printf("DES cbc bytes      per sec = %12.2f (%5.1fuS)\n",c,8.0e6/c);\r   printf("DES ede cbc bytes  per sec = %12.2f (%5.1fuS)\n",d,8.0e6/d);\r   printf("crypt              per sec = %12.2f (%5.1fuS)\n",e,1.0e6/e);\r   exit(0);\r#if defined(LINT) || defined(MSDOS)\r   return(0);\r#endif\r      }\r