diff options
Diffstat (limited to 'lib/libssl/src/fips/rand/fips_randtest.c')
-rw-r--r-- | lib/libssl/src/fips/rand/fips_randtest.c | 301 |
1 files changed, 90 insertions, 211 deletions
diff --git a/lib/libssl/src/fips/rand/fips_randtest.c b/lib/libssl/src/fips/rand/fips_randtest.c index 6165944e56f..5582941c119 100644 --- a/lib/libssl/src/fips/rand/fips_randtest.c +++ b/lib/libssl/src/fips/rand/fips_randtest.c @@ -105,9 +105,12 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <ctype.h> #include <openssl/rand.h> #include <openssl/fips_rand.h> #include <openssl/err.h> +#include <openssl/bn.h> #include "e_os.h" @@ -120,42 +123,65 @@ int main(int argc, char *argv[]) #else -/* some FIPS 140-1 random number test */ -/* some simple tests */ - -static DES_cblock prng_key1={0x21,0x58,0x47,0xb7,0xc2,0x97,0x5a,0x8e}; -static DES_cblock prng_key2={0x61,0x23,0x05,0x96,0x18,0x91,0x86,0xac}; -static unsigned char prng_seed[8]={0x6b,0xa3,0x4f,0x07,0xe4,0x2a,0xb0,0xc}; +#include "fips_utl.h" typedef struct - { - DES_cblock keys[2]; - const unsigned char time[8]; - const unsigned char seed[8]; - const unsigned char block1[8]; - const unsigned char block100[8]; - } PRNGtest; - -/* FIXME: these test vectors are made up! */ -static PRNGtest t1= - { - { { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07 }, - { 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f }, - }, - { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, - { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, - { 0x33,0xc3,0xdf,0xfe,0x60,0x60,0x49,0x9e }, - { 0xcd,0x2b,0x41,0xaf,0x80,0x51,0x37,0xd8 } - }; -static PRNGtest t2= - { - { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff }, - { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } }, - { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff }, - { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff }, - { 0x65,0xf1,0xa4,0x07,0x42,0x38,0xd5,0x25 }, - { 0xbb,0x75,0x84,0x20,0x7a,0x44,0xf0,0xa0 } - }; + { + unsigned char DT[16]; + unsigned char V[16]; + unsigned char R[16]; + } AES_PRNG_MCT; + +static unsigned char aes_128_mct_key[16] = + {0x9f,0x5b,0x51,0x20,0x0b,0xf3,0x34,0xb5, + 0xd8,0x2b,0xe8,0xc3,0x72,0x55,0xc8,0x48}; + +static AES_PRNG_MCT aes_128_mct_tv = { + /* DT */ + {0x63,0x76,0xbb,0xe5,0x29,0x02,0xba,0x3b, + 0x67,0xc9,0x25,0xfa,0x70,0x1f,0x11,0xac}, + /* V */ + {0x57,0x2c,0x8e,0x76,0x87,0x26,0x47,0x97, + 0x7e,0x74,0xfb,0xdd,0xc4,0x95,0x01,0xd1}, + /* R */ + {0x48,0xe9,0xbd,0x0d,0x06,0xee,0x18,0xfb, + 0xe4,0x57,0x90,0xd5,0xc3,0xfc,0x9b,0x73} +}; + +static unsigned char aes_192_mct_key[24] = + {0xb7,0x6c,0x34,0xd1,0x09,0x67,0xab,0x73, + 0x4d,0x5a,0xd5,0x34,0x98,0x16,0x0b,0x91, + 0xbc,0x35,0x51,0x16,0x6b,0xae,0x93,0x8a}; + +static AES_PRNG_MCT aes_192_mct_tv = { + /* DT */ + {0x84,0xce,0x22,0x7d,0x91,0x5a,0xa3,0xc9, + 0x84,0x3c,0x0a,0xb3,0xa9,0x63,0x15,0x52}, + /* V */ + {0xb6,0xaf,0xe6,0x8f,0x99,0x9e,0x90,0x64, + 0xdd,0xc7,0x7a,0xc1,0xbb,0x90,0x3a,0x6d}, + /* R */ + {0xfc,0x85,0x60,0x9a,0x29,0x6f,0xef,0x21, + 0xdd,0x86,0x20,0x32,0x8a,0x29,0x6f,0x47} +}; + +static unsigned char aes_256_mct_key[32] = + {0x9b,0x05,0xc8,0x68,0xff,0x47,0xf8,0x3a, + 0xa6,0x3a,0xa8,0xcb,0x4e,0x71,0xb2,0xe0, + 0xb8,0x7e,0xf1,0x37,0xb6,0xb4,0xf6,0x6d, + 0x86,0x32,0xfc,0x1f,0x5e,0x1d,0x1e,0x50}; + +static AES_PRNG_MCT aes_256_mct_tv = { + /* DT */ + {0x31,0x6e,0x35,0x9a,0xb1,0x44,0xf0,0xee, + 0x62,0x6d,0x04,0x46,0xe0,0xa3,0x92,0x4c}, + /* V */ + {0x4f,0xcd,0xc1,0x87,0x82,0x1f,0x4d,0xa1, + 0x3e,0x0e,0x56,0x44,0x59,0xe8,0x83,0xca}, + /* R */ + {0xc8,0x87,0xc2,0x61,0x5b,0xd0,0xb9,0xe1, + 0xe7,0xf3,0x8b,0xd7,0x5b,0xd5,0xf1,0x8d} +}; static void dump(const unsigned char *b,int n) { @@ -174,196 +200,49 @@ static void compare(const unsigned char *result,const unsigned char *expected, if(result[i] != expected[i]) { puts("Random test failed, got:"); - dump(result,8); + dump(result,n); puts("\n expected:"); - dump(expected,8); + dump(expected,n); putchar('\n'); EXIT(1); } } -static void run_test(const PRNGtest *t) - { - unsigned char buf[8]; - int n; - - FIPS_set_prng_key(t->keys[0],t->keys[1]); - FIPS_test_mode(1,t->time); - RAND_seed(t->seed,sizeof t->seed); - if(RAND_bytes(buf,8) <= 0) +static void run_test(unsigned char *key, int keylen, AES_PRNG_MCT *tv) + { + unsigned char buf[16], dt[16]; + int i, j; + FIPS_rand_reset(); + FIPS_rand_test_mode(); + FIPS_rand_set_key(key, keylen); + FIPS_rand_seed(tv->V, 16); + memcpy(dt, tv->DT, 16); + for (i = 0; i < 10000; i++) { - ERR_print_errors_fp(stderr); - EXIT(2); + FIPS_rand_set_dt(dt); + FIPS_rand_bytes(buf, 16); + /* Increment DT */ + for (j = 15; j >= 0; j--) + { + dt[j]++; + if (dt[j]) + break; + } } - compare(buf,t->block1,8); - for(n=0 ; n < 99 ; ++n) - if(RAND_bytes(buf,8) <= 0) - { - ERR_print_errors_fp(stderr); - EXIT(2); - } - compare(buf,t->block100,8); - FIPS_test_mode(0,NULL); + + compare(buf,tv->R, 16); } int main() { - unsigned char buf[2500]; - int i,j,k,s,sign,nsign,err=0; - unsigned long n1; - unsigned long n2[16]; - unsigned long runs[2][34]; - /*double d; */ - long d; - - ERR_load_crypto_strings(); - RAND_set_rand_method(FIPS_rand_method()); - - run_test(&t1); - run_test(&t2); - - FIPS_set_prng_key(prng_key1,prng_key2); - RAND_seed(prng_seed,sizeof prng_seed); - - i = RAND_pseudo_bytes(buf,2500); - if (i <= 0) - { - printf ("init failed, the rand method is not properly installed\n"); - err++; - goto err; - } - - n1=0; - for (i=0; i<16; i++) n2[i]=0; - for (i=0; i<34; i++) runs[0][i]=runs[1][i]=0; - - /* test 1 and 2 */ - sign=0; - nsign=0; - for (i=0; i<2500; i++) - { - j=buf[i]; - - n2[j&0x0f]++; - n2[(j>>4)&0x0f]++; - - for (k=0; k<8; k++) - { - s=(j&0x01); - if (s == sign) - nsign++; - else - { - if (nsign > 34) nsign=34; - if (nsign != 0) - { - runs[sign][nsign-1]++; - if (nsign > 6) - runs[sign][5]++; - } - sign=s; - nsign=1; - } - - if (s) n1++; - j>>=1; - } - } - if (nsign > 34) nsign=34; - if (nsign != 0) runs[sign][nsign-1]++; - - /* test 1 */ - if (!((9654 < n1) && (n1 < 10346))) - { - printf("test 1 failed, X=%lu\n",n1); - err++; - } - printf("test 1 done\n"); - - /* test 2 */ -#ifdef undef - d=0; - for (i=0; i<16; i++) - d+=n2[i]*n2[i]; - d=d*16.0/5000.0-5000.0; - if (!((1.03 < d) && (d < 57.4))) - { - printf("test 2 failed, X=%.2f\n",d); - err++; - } -#endif - d=0; - for (i=0; i<16; i++) - d+=n2[i]*n2[i]; - d=(d*8)/25-500000; - if (!((103 < d) && (d < 5740))) - { - printf("test 2 failed, X=%ld.%02ld\n",d/100L,d%100L); - err++; - } - printf("test 2 done\n"); - - /* test 3 */ - for (i=0; i<2; i++) - { - if (!((2267 < runs[i][0]) && (runs[i][0] < 2733))) - { - printf("test 3 failed, bit=%d run=%d num=%lu\n", - i,1,runs[i][0]); - err++; - } - if (!((1079 < runs[i][1]) && (runs[i][1] < 1421))) - { - printf("test 3 failed, bit=%d run=%d num=%lu\n", - i,2,runs[i][1]); - err++; - } - if (!(( 502 < runs[i][2]) && (runs[i][2] < 748))) - { - printf("test 3 failed, bit=%d run=%d num=%lu\n", - i,3,runs[i][2]); - err++; - } - if (!(( 223 < runs[i][3]) && (runs[i][3] < 402))) - { - printf("test 3 failed, bit=%d run=%d num=%lu\n", - i,4,runs[i][3]); - err++; - } - if (!(( 90 < runs[i][4]) && (runs[i][4] < 223))) - { - printf("test 3 failed, bit=%d run=%d num=%lu\n", - i,5,runs[i][4]); - err++; - } - if (!(( 90 < runs[i][5]) && (runs[i][5] < 223))) - { - printf("test 3 failed, bit=%d run=%d num=%lu\n", - i,6,runs[i][5]); - err++; - } - } - printf("test 3 done\n"); - - /* test 4 */ - if (runs[0][33] != 0) - { - printf("test 4 failed, bit=%d run=%d num=%lu\n", - 0,34,runs[0][33]); - err++; - } - if (runs[1][33] != 0) - { - printf("test 4 failed, bit=%d run=%d num=%lu\n", - 1,34,runs[1][33]); - err++; - } - printf("test 4 done\n"); - err: - err=((err)?1:0); - EXIT(err); - return(err); + run_test(aes_128_mct_key, 16, &aes_128_mct_tv); + printf("FIPS PRNG test 1 done\n"); + run_test(aes_192_mct_key, 24, &aes_192_mct_tv); + printf("FIPS PRNG test 2 done\n"); + run_test(aes_256_mct_key, 32, &aes_256_mct_tv); + printf("FIPS PRNG test 3 done\n"); + return 0; } #endif |