Home | History | Annotate | Download | only in sha
      1 /* crypto/sha/sha256t.c */
      2 /* ====================================================================
      3  * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
      4  * ====================================================================
      5  */
      6 #include <stdio.h>
      7 #include <string.h>
      8 #include <stdlib.h>
      9 
     10 #include <openssl/sha.h>
     11 #include <openssl/evp.h>
     12 
     13 #if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA256)
     14 int main(int argc, char *argv[])
     15 {
     16     printf("No SHA256 support\n");
     17     return(0);
     18 }
     19 #else
     20 
     21 unsigned char app_b1[SHA256_DIGEST_LENGTH] = {
     22 	0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,
     23 	0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23,
     24 	0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,
     25 	0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad	};
     26 
     27 unsigned char app_b2[SHA256_DIGEST_LENGTH] = {
     28 	0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8,
     29 	0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39,
     30 	0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67,
     31 	0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1	};
     32 
     33 unsigned char app_b3[SHA256_DIGEST_LENGTH] = {
     34 	0xcd,0xc7,0x6e,0x5c,0x99,0x14,0xfb,0x92,
     35 	0x81,0xa1,0xc7,0xe2,0x84,0xd7,0x3e,0x67,
     36 	0xf1,0x80,0x9a,0x48,0xa4,0x97,0x20,0x0e,
     37 	0x04,0x6d,0x39,0xcc,0xc7,0x11,0x2c,0xd0	};
     38 
     39 unsigned char addenum_1[SHA224_DIGEST_LENGTH] = {
     40 	0x23,0x09,0x7d,0x22,0x34,0x05,0xd8,0x22,
     41 	0x86,0x42,0xa4,0x77,0xbd,0xa2,0x55,0xb3,
     42 	0x2a,0xad,0xbc,0xe4,0xbd,0xa0,0xb3,0xf7,
     43 	0xe3,0x6c,0x9d,0xa7 };
     44 
     45 unsigned char addenum_2[SHA224_DIGEST_LENGTH] = {
     46 	0x75,0x38,0x8b,0x16,0x51,0x27,0x76,0xcc,
     47 	0x5d,0xba,0x5d,0xa1,0xfd,0x89,0x01,0x50,
     48 	0xb0,0xc6,0x45,0x5c,0xb4,0xf5,0x8b,0x19,
     49 	0x52,0x52,0x25,0x25 };
     50 
     51 unsigned char addenum_3[SHA224_DIGEST_LENGTH] = {
     52 	0x20,0x79,0x46,0x55,0x98,0x0c,0x91,0xd8,
     53 	0xbb,0xb4,0xc1,0xea,0x97,0x61,0x8a,0x4b,
     54 	0xf0,0x3f,0x42,0x58,0x19,0x48,0xb2,0xee,
     55 	0x4e,0xe7,0xad,0x67 };
     56 
     57 int main (int argc,char **argv)
     58 { unsigned char md[SHA256_DIGEST_LENGTH];
     59   int		i;
     60   EVP_MD_CTX	evp;
     61 
     62     fprintf(stdout,"Testing SHA-256 ");
     63 
     64     EVP_Digest ("abc",3,md,NULL,EVP_sha256(),NULL);
     65     if (memcmp(md,app_b1,sizeof(app_b1)))
     66     {	fflush(stdout);
     67 	fprintf(stderr,"\nTEST 1 of 3 failed.\n");
     68 	return 1;
     69     }
     70     else
     71 	fprintf(stdout,"."); fflush(stdout);
     72 
     73     EVP_Digest ("abcdbcde""cdefdefg""efghfghi""ghijhijk"
     74 		"ijkljklm""klmnlmno""mnopnopq",56,md,NULL,EVP_sha256(),NULL);
     75     if (memcmp(md,app_b2,sizeof(app_b2)))
     76     {	fflush(stdout);
     77 	fprintf(stderr,"\nTEST 2 of 3 failed.\n");
     78 	return 1;
     79     }
     80     else
     81 	fprintf(stdout,"."); fflush(stdout);
     82 
     83     EVP_MD_CTX_init (&evp);
     84     EVP_DigestInit_ex (&evp,EVP_sha256(),NULL);
     85     for (i=0;i<1000000;i+=160)
     86 	EVP_DigestUpdate (&evp,	"aaaaaaaa""aaaaaaaa""aaaaaaaa""aaaaaaaa"
     87 				"aaaaaaaa""aaaaaaaa""aaaaaaaa""aaaaaaaa"
     88 				"aaaaaaaa""aaaaaaaa""aaaaaaaa""aaaaaaaa"
     89 				"aaaaaaaa""aaaaaaaa""aaaaaaaa""aaaaaaaa"
     90 				"aaaaaaaa""aaaaaaaa""aaaaaaaa""aaaaaaaa",
     91 				(1000000-i)<160?1000000-i:160);
     92     EVP_DigestFinal_ex (&evp,md,NULL);
     93     EVP_MD_CTX_cleanup (&evp);
     94 
     95     if (memcmp(md,app_b3,sizeof(app_b3)))
     96     {	fflush(stdout);
     97 	fprintf(stderr,"\nTEST 3 of 3 failed.\n");
     98 	return 1;
     99     }
    100     else
    101 	fprintf(stdout,"."); fflush(stdout);
    102 
    103     fprintf(stdout," passed.\n"); fflush(stdout);
    104 
    105     fprintf(stdout,"Testing SHA-224 ");
    106 
    107     EVP_Digest ("abc",3,md,NULL,EVP_sha224(),NULL);
    108     if (memcmp(md,addenum_1,sizeof(addenum_1)))
    109     {	fflush(stdout);
    110 	fprintf(stderr,"\nTEST 1 of 3 failed.\n");
    111 	return 1;
    112     }
    113     else
    114 	fprintf(stdout,"."); fflush(stdout);
    115 
    116     EVP_Digest ("abcdbcde""cdefdefg""efghfghi""ghijhijk"
    117 		"ijkljklm""klmnlmno""mnopnopq",56,md,NULL,EVP_sha224(),NULL);
    118     if (memcmp(md,addenum_2,sizeof(addenum_2)))
    119     {	fflush(stdout);
    120 	fprintf(stderr,"\nTEST 2 of 3 failed.\n");
    121 	return 1;
    122     }
    123     else
    124 	fprintf(stdout,"."); fflush(stdout);
    125 
    126     EVP_MD_CTX_init (&evp);
    127     EVP_DigestInit_ex (&evp,EVP_sha224(),NULL);
    128     for (i=0;i<1000000;i+=64)
    129 	EVP_DigestUpdate (&evp,	"aaaaaaaa""aaaaaaaa""aaaaaaaa""aaaaaaaa"
    130 				"aaaaaaaa""aaaaaaaa""aaaaaaaa""aaaaaaaa",
    131 				(1000000-i)<64?1000000-i:64);
    132     EVP_DigestFinal_ex (&evp,md,NULL);
    133     EVP_MD_CTX_cleanup (&evp);
    134 
    135     if (memcmp(md,addenum_3,sizeof(addenum_3)))
    136     {	fflush(stdout);
    137 	fprintf(stderr,"\nTEST 3 of 3 failed.\n");
    138 	return 1;
    139     }
    140     else
    141 	fprintf(stdout,"."); fflush(stdout);
    142 
    143     fprintf(stdout," passed.\n"); fflush(stdout);
    144 
    145   return 0;
    146 }
    147 #endif
    148