1 /* crypto/des/des_opts.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/* define PART1, PART2, PART3 or PART4 to build only with a few of the options.
\r * This is for machines with 64k code segment size restrictions. */
\r\r#ifndef MSDOS
\r#define TIMES
\r#endif
\r\r#include <stdio.h>
\r#ifndef MSDOS
\r#include <unistd.h>
\r#else
\r#include <io.h>
\rextern void exit();
\r#endif
\r#include <signal.h>
\r#ifndef VMS
\r#ifndef _IRIX
\r#include <time.h>
\r#endif
\r#ifdef TIMES
\r#include <sys/types.h>
\r#include <sys/times.h>
\r#endif
\r#else /* 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#ifndef TIMES
\r#include <sys/timeb.h>
\r#endif
\r\r#ifdef sun
\r#include <limits.h>
\r#include <sys/param.h>
\r#endif
\r\r#include "des.h"
\r#include "spr.h"
\r\r#define DES_DEFAULT_OPTIONS
\r\r#if !defined(PART1) && !defined(PART2) && !defined(PART3) && !defined(PART4)
\r#define PART1
\r#define PART2
\r#define PART3
\r#define PART4
\r#endif
\r\r#ifdef PART1
\r\r#undef DES_UNROLL
\r#undef DES_RISC1
\r#undef DES_RISC2
\r#undef DES_PTR
\r#undef D_ENCRYPT
\r#define des_encrypt des_encrypt_u4_cisc_idx
\r#define des_encrypt2 des_encrypt2_u4_cisc_idx
\r#define des_encrypt3 des_encrypt3_u4_cisc_idx
\r#define des_decrypt3 des_decrypt3_u4_cisc_idx
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#define DES_UNROLL
\r#undef DES_RISC1
\r#undef DES_RISC2
\r#undef DES_PTR
\r#undef D_ENCRYPT
\r#undef des_encrypt
\r#undef des_encrypt2
\r#undef des_encrypt3
\r#undef des_decrypt3
\r#define des_encrypt des_encrypt_u16_cisc_idx
\r#define des_encrypt2 des_encrypt2_u16_cisc_idx
\r#define des_encrypt3 des_encrypt3_u16_cisc_idx
\r#define des_decrypt3 des_decrypt3_u16_cisc_idx
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#undef DES_UNROLL
\r#define DES_RISC1
\r#undef DES_RISC2
\r#undef DES_PTR
\r#undef D_ENCRYPT
\r#undef des_encrypt
\r#undef des_encrypt2
\r#undef des_encrypt3
\r#undef des_decrypt3
\r#define des_encrypt des_encrypt_u4_risc1_idx
\r#define des_encrypt2 des_encrypt2_u4_risc1_idx
\r#define des_encrypt3 des_encrypt3_u4_risc1_idx
\r#define des_decrypt3 des_decrypt3_u4_risc1_idx
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#endif
\r\r#ifdef PART2
\r\r#undef DES_UNROLL
\r#undef DES_RISC1
\r#define DES_RISC2
\r#undef DES_PTR
\r#undef D_ENCRYPT
\r#undef des_encrypt
\r#undef des_encrypt2
\r#undef des_encrypt3
\r#undef des_decrypt3
\r#define des_encrypt des_encrypt_u4_risc2_idx
\r#define des_encrypt2 des_encrypt2_u4_risc2_idx
\r#define des_encrypt3 des_encrypt3_u4_risc2_idx
\r#define des_decrypt3 des_decrypt3_u4_risc2_idx
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#define DES_UNROLL
\r#define DES_RISC1
\r#undef DES_RISC2
\r#undef DES_PTR
\r#undef D_ENCRYPT
\r#undef des_encrypt
\r#undef des_encrypt2
\r#undef des_encrypt3
\r#undef des_decrypt3
\r#define des_encrypt des_encrypt_u16_risc1_idx
\r#define des_encrypt2 des_encrypt2_u16_risc1_idx
\r#define des_encrypt3 des_encrypt3_u16_risc1_idx
\r#define des_decrypt3 des_decrypt3_u16_risc1_idx
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#define DES_UNROLL
\r#undef DES_RISC1
\r#define DES_RISC2
\r#undef DES_PTR
\r#undef D_ENCRYPT
\r#undef des_encrypt
\r#undef des_encrypt2
\r#undef des_encrypt3
\r#undef des_decrypt3
\r#define des_encrypt des_encrypt_u16_risc2_idx
\r#define des_encrypt2 des_encrypt2_u16_risc2_idx
\r#define des_encrypt3 des_encrypt3_u16_risc2_idx
\r#define des_decrypt3 des_decrypt3_u16_risc2_idx
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#endif
\r\r#ifdef PART3
\r\r#undef DES_UNROLL
\r#undef DES_RISC1
\r#undef DES_RISC2
\r#define DES_PTR
\r#undef D_ENCRYPT
\r#undef des_encrypt
\r#undef des_encrypt2
\r#undef des_encrypt3
\r#undef des_decrypt3
\r#define des_encrypt des_encrypt_u4_cisc_ptr
\r#define des_encrypt2 des_encrypt2_u4_cisc_ptr
\r#define des_encrypt3 des_encrypt3_u4_cisc_ptr
\r#define des_decrypt3 des_decrypt3_u4_cisc_ptr
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#define DES_UNROLL
\r#undef DES_RISC1
\r#undef DES_RISC2
\r#define DES_PTR
\r#undef D_ENCRYPT
\r#undef des_encrypt
\r#undef des_encrypt2
\r#undef des_encrypt3
\r#undef des_decrypt3
\r#define des_encrypt des_encrypt_u16_cisc_ptr
\r#define des_encrypt2 des_encrypt2_u16_cisc_ptr
\r#define des_encrypt3 des_encrypt3_u16_cisc_ptr
\r#define des_decrypt3 des_decrypt3_u16_cisc_ptr
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#undef DES_UNROLL
\r#define DES_RISC1
\r#undef DES_RISC2
\r#define DES_PTR
\r#undef D_ENCRYPT
\r#undef des_encrypt
\r#undef des_encrypt2
\r#undef des_encrypt3
\r#undef des_decrypt3
\r#define des_encrypt des_encrypt_u4_risc1_ptr
\r#define des_encrypt2 des_encrypt2_u4_risc1_ptr
\r#define des_encrypt3 des_encrypt3_u4_risc1_ptr
\r#define des_decrypt3 des_decrypt3_u4_risc1_ptr
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#endif
\r\r#ifdef PART4
\r\r#undef DES_UNROLL
\r#undef DES_RISC1
\r#define DES_RISC2
\r#define DES_PTR
\r#undef D_ENCRYPT
\r#undef des_encrypt
\r#undef des_encrypt2
\r#undef des_encrypt3
\r#undef des_decrypt3
\r#define des_encrypt des_encrypt_u4_risc2_ptr
\r#define des_encrypt2 des_encrypt2_u4_risc2_ptr
\r#define des_encrypt3 des_encrypt3_u4_risc2_ptr
\r#define des_decrypt3 des_decrypt3_u4_risc2_ptr
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#define DES_UNROLL
\r#define DES_RISC1
\r#undef DES_RISC2
\r#define DES_PTR
\r#undef D_ENCRYPT
\r#undef des_encrypt
\r#undef des_encrypt2
\r#undef des_encrypt3
\r#undef des_decrypt3
\r#define des_encrypt des_encrypt_u16_risc1_ptr
\r#define des_encrypt2 des_encrypt2_u16_risc1_ptr
\r#define des_encrypt3 des_encrypt3_u16_risc1_ptr
\r#define des_decrypt3 des_decrypt3_u16_risc1_ptr
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#define DES_UNROLL
\r#undef DES_RISC1
\r#define DES_RISC2
\r#define DES_PTR
\r#undef D_ENCRYPT
\r#undef des_encrypt
\r#undef des_encrypt2
\r#undef des_encrypt3
\r#undef des_decrypt3
\r#define des_encrypt des_encrypt_u16_risc2_ptr
\r#define des_encrypt2 des_encrypt2_u16_risc2_ptr
\r#define des_encrypt3 des_encrypt3_u16_risc2_ptr
\r#define des_decrypt3 des_decrypt3_u16_risc2_ptr
\r#undef HEADER_DES_LOCL_H
\r#include "des_enc.c"
\r\r#endif
\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\r#ifdef SIGALRM
\r#define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
\r#else
\r#define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
\r#endif
\r \r#define time_it(func,name,index) \
\r print_name(name); \
\r Time_F(START); \
\r for (count=0,run=1; COND(cb); count++) \
\r { \
\r unsigned long d[2]; \
\r func(d,&(sch[0]),DES_ENCRYPT); \
\r } \
\r tm[index]=Time_F(STOP); \
\r fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
\r tm[index]=((double)COUNT(cb))/tm[index];
\r\r#define print_it(name,index) \
\r fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
\r tm[index]*8,1.0e6/tm[index]);
\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 d,tm[16],max=0;
\r int rank[16];
\r char *str[16];
\r int max_idx=0,i,num=0,j;
\r#ifndef SIGALARM
\r long ca,cb,cc,cd,ce;
\r#endif
\r\r for (i=0; i<12; i++)
\r {
\r tm[i]=0.0;
\r rank[i]=0;
\r }
\r\r#ifndef TIMES
\r fprintf(stderr,"To get the most acurate results, try to run this\n");
\r fprintf(stderr,"program when this computer is idle.\n");
\r#endif
\r\r des_set_key((C_Block *)key,sch);
\r des_set_key((C_Block *)key2,sch2);
\r des_set_key((C_Block *)key3,sch3);
\r\r#ifndef SIGALRM
\r fprintf(stderr,"First we calculate the approximate speed ...\n");
\r des_set_key((C_Block *)key,sch);
\r count=10;
\r do {
\r long i;
\r unsigned 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\r ce=count/20+1;
\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 alarm(10);
\r#endif
\r\r#ifdef PART1
\r time_it(des_encrypt_u4_cisc_idx, "des_encrypt_u4_cisc_idx ", 0);
\r time_it(des_encrypt_u16_cisc_idx, "des_encrypt_u16_cisc_idx ", 1);
\r time_it(des_encrypt_u4_risc1_idx, "des_encrypt_u4_risc1_idx ", 2);
\r num+=3;
\r#endif
\r#ifdef PART2
\r time_it(des_encrypt_u16_risc1_idx,"des_encrypt_u16_risc1_idx", 3);
\r time_it(des_encrypt_u4_risc2_idx, "des_encrypt_u4_risc2_idx ", 4);
\r time_it(des_encrypt_u16_risc2_idx,"des_encrypt_u16_risc2_idx", 5);
\r num+=3;
\r#endif
\r#ifdef PART3
\r time_it(des_encrypt_u4_cisc_ptr, "des_encrypt_u4_cisc_ptr ", 6);
\r time_it(des_encrypt_u16_cisc_ptr, "des_encrypt_u16_cisc_ptr ", 7);
\r time_it(des_encrypt_u4_risc1_ptr, "des_encrypt_u4_risc1_ptr ", 8);
\r num+=3;
\r#endif
\r#ifdef PART4
\r time_it(des_encrypt_u16_risc1_ptr,"des_encrypt_u16_risc1_ptr", 9);
\r time_it(des_encrypt_u4_risc2_ptr, "des_encrypt_u4_risc2_ptr ",10);
\r time_it(des_encrypt_u16_risc2_ptr,"des_encrypt_u16_risc2_ptr",11);
\r num+=3;
\r#endif
\r\r#ifdef PART1
\r str[0]=" 4 c i";
\r print_it("des_encrypt_u4_cisc_idx ",0);
\r max=tm[0];
\r max_idx=0;
\r str[1]="16 c i";
\r print_it("des_encrypt_u16_cisc_idx ",1);
\r if (max < tm[1]) { max=tm[1]; max_idx=1; }
\r str[2]=" 4 r1 i";
\r print_it("des_encrypt_u4_risc1_idx ",2);
\r if (max < tm[2]) { max=tm[2]; max_idx=2; }
\r#endif
\r#ifdef PART2
\r str[3]="16 r1 i";
\r print_it("des_encrypt_u16_risc1_idx",3);
\r if (max < tm[3]) { max=tm[3]; max_idx=3; }
\r str[4]=" 4 r2 i";
\r print_it("des_encrypt_u4_risc2_idx ",4);
\r if (max < tm[4]) { max=tm[4]; max_idx=4; }
\r str[5]="16 r2 i";
\r print_it("des_encrypt_u16_risc2_idx",5);
\r if (max < tm[5]) { max=tm[5]; max_idx=5; }
\r#endif
\r#ifdef PART3
\r str[6]=" 4 c p";
\r print_it("des_encrypt_u4_cisc_ptr ",6);
\r if (max < tm[6]) { max=tm[6]; max_idx=6; }
\r str[7]="16 c p";
\r print_it("des_encrypt_u16_cisc_ptr ",7);
\r if (max < tm[7]) { max=tm[7]; max_idx=7; }
\r str[8]=" 4 r1 p";
\r print_it("des_encrypt_u4_risc1_ptr ",8);
\r if (max < tm[8]) { max=tm[8]; max_idx=8; }
\r#endif
\r#ifdef PART4
\r str[9]="16 r1 p";
\r print_it("des_encrypt_u16_risc1_ptr",9);
\r if (max < tm[9]) { max=tm[9]; max_idx=9; }
\r str[10]=" 4 r2 p";
\r print_it("des_encrypt_u4_risc2_ptr ",10);
\r if (max < tm[10]) { max=tm[10]; max_idx=10; }
\r str[11]="16 r2 p";
\r print_it("des_encrypt_u16_risc2_ptr",11);
\r if (max < tm[11]) { max=tm[11]; max_idx=11; }
\r#endif
\r printf("options des ecb/s\n");
\r printf("%s %12.2f 100.0%%\n",str[max_idx],tm[max_idx]);
\r d=tm[max_idx];
\r tm[max_idx]= -2.0;
\r max= -1.0;
\r for (;;)
\r {
\r for (i=0; i<12; i++)
\r {
\r if (max < tm[i]) { max=tm[i]; j=i; }
\r }
\r if (max < 0.0) break;
\r printf("%s %12.2f %4.1f%%\n",str[j],tm[j],tm[j]/d*100.0);
\r tm[j]= -2.0;
\r max= -1.0;
\r }
\r\r switch (max_idx)
\r {
\r case 0:
\r printf("-DDES_DEFAULT_OPTIONS\n");
\r break;
\r case 1:
\r printf("-DDES_UNROLL\n");
\r break;
\r case 2:
\r printf("-DDES_RISC1\n");
\r break;
\r case 3:
\r printf("-DDES_UNROLL -DDES_RISC1\n");
\r break;
\r case 4:
\r printf("-DDES_RISC2\n");
\r break;
\r case 5:
\r printf("-DDES_UNROLL -DDES_RISC2\n");
\r break;
\r case 6:
\r printf("-DDES_PTR\n");
\r break;
\r case 7:
\r printf("-DDES_UNROLL -DDES_PTR\n");
\r break;
\r case 8:
\r printf("-DDES_RISC1 -DDES_PTR\n");
\r break;
\r case 9:
\r printf("-DDES_UNROLL -DDES_RISC1 -DDES_PTR\n");
\r break;
\r case 10:
\r printf("-DDES_RISC2 -DDES_PTR\n");
\r break;
\r case 11:
\r printf("-DDES_UNROLL -DDES_RISC2 -DDES_PTR\n");
\r break;
\r }
\r exit(0);
\r#if defined(LINT) || defined(MSDOS)
\r return(0);
\r#endif
\r }
\r