adcb0927ae1986c17c723aae06fb9420fd751515
[krb5.git] / src / lib / crypto / crypto_tests / t_shs.c
1 /****************************************************************************
2 *                                                                           *
3 *                               SHS Test Code                               *
4 *                                                                           *
5 ****************************************************************************/
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <time.h>
10 #include "shs.h"
11
12 /* Test the SHS implementation */
13
14 #ifdef NEW_SHS
15
16 static SHS_LONG shsTestResults[][ 5 ] = {
17     { 0xA9993E36L, 0x4706816AL, 0xBA3E2571L, 0x7850C26CL, 0x9CD0D89DL, },
18     { 0x84983E44L, 0x1C3BD26EL, 0xBAAE4AA1L, 0xF95129E5L, 0xE54670F1L, },
19     { 0x34AA973CL, 0xD4C4DAA4L, 0xF61EEB2BL, 0xDBAD2731L, 0x6534016FL, }
20     };
21
22 #else
23
24 static SHS_LONG shsTestResults[][ 5 ] = {
25     { 0x0164B8A9L, 0x14CD2A5EL, 0x74C4F7FFL, 0x082C4D97L, 0xF1EDF880L },
26     { 0xD2516EE1L, 0xACFA5BAFL, 0x33DFC1C4L, 0x71E43844L, 0x9EF134C8L },
27     { 0x3232AFFAL, 0x48628A26L, 0x653B5AAAL, 0x44541FD9L, 0x0D690603L }
28     };
29 #endif /* NEW_SHS */
30
31 static int compareSHSresults(shsInfo, shsTestLevel)
32 SHS_INFO *shsInfo;
33 int shsTestLevel;
34 {
35     int i, fail = 0;
36
37     /* Compare the returned digest and required values */
38     for( i = 0; i < 5; i++ )
39         if( shsInfo->digest[ i ] != shsTestResults[ shsTestLevel ][ i ] )
40             fail = 1;
41     if (fail) {
42         printf("\nExpected: ");
43         for (i = 0; i < 5; i++) {
44             printf("%8.8lx ", (unsigned long) shsTestResults[shsTestLevel][i]);
45         }
46         printf("\nGot:      ");
47         for (i = 0; i < 5; i++) {
48             printf("%8.8lx ", (unsigned long) shsInfo->digest[i]);
49         }
50         printf("\n");
51         return( -1 );
52     }
53     return( 0 );
54 }
55
56 int
57 main()
58 {
59     SHS_INFO shsInfo;
60     unsigned int i;
61 #if 0
62     time_t secondCount;
63     SHS_BYTE data[ 200 ];
64 #endif
65
66     /* Make sure we've got the endianness set right.  If the machine is
67        big-endian (up to 64 bits) the following value will be signed,
68        otherwise it will be unsigned.  Unfortunately we can't test for odd
69        things like middle-endianness without knowing the size of the data
70        types */
71
72     /* Test SHS against values given in SHS standards document */
73     printf( "Running SHS test 1 ... " );
74     shsInit( &shsInfo );
75     shsUpdate( &shsInfo, ( SHS_BYTE * ) "abc", 3 );
76     shsFinal( &shsInfo );
77     if( compareSHSresults( &shsInfo, 0 ) == -1 )
78         {
79         putchar( '\n' );
80         puts( "SHS test 1 failed" );
81         exit( -1 );
82         }
83 #ifdef NEW_SHS
84     puts( "passed, result= A9993E364706816ABA3E25717850C26C9CD0D89D" );
85 #else
86     puts( "passed, result= 0164B8A914CD2A5E74C4F7FF082C4D97F1EDF880" );
87 #endif /* NEW_SHS */
88
89     printf( "Running SHS test 2 ... " );
90     shsInit( &shsInfo );
91     shsUpdate( &shsInfo, ( SHS_BYTE * ) "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56 );
92     shsFinal( &shsInfo );
93     if( compareSHSresults( &shsInfo, 1 ) == -1 )
94         {
95         putchar( '\n' );
96         puts( "SHS test 2 failed" );
97         exit( -1 );
98         }
99 #ifdef NEW_SHS
100     puts( "passed, result= 84983E441C3BD26EBAAE4AA1F95129E5E54670F1" );
101 #else
102     puts( "passed, result= D2516EE1ACFA5BAF33DFC1C471E438449EF134C8" );
103 #endif /* NEW_SHS */
104
105     printf( "Running SHS test 3 ... " );
106     shsInit( &shsInfo );
107     for( i = 0; i < 15625; i++ )
108         shsUpdate( &shsInfo, ( SHS_BYTE * ) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 64 );
109     shsFinal( &shsInfo );
110     if( compareSHSresults( &shsInfo, 2 ) == -1 )
111         {
112         putchar( '\n' );
113         puts( "SHS test 3 failed" );
114         exit( -1 );
115         }
116 #ifdef NEW_SHS
117     puts( "passed, result= 34AA973CD4C4DAA4F61EEB2BDBAD27316534016F" );
118 #else
119     puts( "passed, result= 3232AFFA48628A26653B5AAA44541FD90D690603" );
120 #endif /* NEW_SHS */
121
122 #if 0
123     printf( "\nTesting speed for 100MB data... " );
124     shsInit( &shsInfo );
125     secondCount = time( NULL );
126     for( i = 0; i < 500000U; i++ )
127         shsUpdate( &shsInfo, data, 200 );
128     secondCount = time( NULL ) - secondCount;
129     printf( "done.  Time = %ld seconds, %ld kbytes/second.\n", \
130             secondCount, 100500L / secondCount );
131 #endif
132
133     puts( "\nAll SHS tests passed" );
134     exit( 0 );
135 }