88 #define PRI64 "ll"
99#endif
1010
11- static prng_state yarrow_prng ;
11+ static prng_state timing_prng ;
12+ static const char * timing_prng_name ;
13+ static int timing_prng_id ;
1214
1315/* timing */
1416#define KTIMES 25
@@ -166,7 +168,7 @@ static void time_keysched(void)
166168 kl = cipher_descriptor [x ].min_key_length ;
167169 c1 = (ulong64 )- 1 ;
168170 for (y1 = 0 ; y1 < KTIMES ; y1 ++ ) {
169- yarrow_read (key , kl , & yarrow_prng );
171+ prng_descriptor [ timing_prng_id ]. read (key , kl , & timing_prng );
170172 t_start ();
171173 DO1 (key );
172174 t1 = t_read ();
@@ -692,11 +694,11 @@ static const struct {
692694 for (y = 0 ; y < 4 ; y ++ ) {
693695 t_start ();
694696 t1 = t_read ();
695- if ((err = dsa_generate_pqg (& yarrow_prng , find_prng ( "yarrow" ) , groups [x ].group , groups [x ].modulus , & key )) != CRYPT_OK ) {
697+ if ((err = dsa_generate_pqg (& timing_prng , timing_prng_id , groups [x ].group , groups [x ].modulus , & key )) != CRYPT_OK ) {
696698 fprintf (stderr , "\n\ndsa_generate_pqg says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
697699 exit (EXIT_FAILURE );
698700 }
699- if ((err = dsa_generate_key (& yarrow_prng , find_prng ( "yarrow" ) , & key )) != CRYPT_OK ) {
701+ if ((err = dsa_generate_key (& timing_prng , timing_prng_id , & key )) != CRYPT_OK ) {
700702 fprintf (stderr , "\n\ndsa_make_key says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
701703 exit (EXIT_FAILURE );
702704 }
@@ -734,8 +736,8 @@ static void time_rsa(void)
734736 ltc_rsa_op_parameters rsa_params = {
735737 .u .crypt .lparam = (const unsigned char * )"testprog" ,
736738 .u .crypt .lparamlen = 8 ,
737- .prng = & yarrow_prng ,
738- .wprng = find_prng ( "yarrow" ) ,
739+ .prng = & timing_prng ,
740+ .wprng = timing_prng_id ,
739741 .params .hash_idx = find_hash ("sha1" ),
740742 .params .mgf1_hash_idx = find_hash ("sha1" ),
741743 .params .saltlen = 8 ,
@@ -749,7 +751,7 @@ static void time_rsa(void)
749751 for (y = 0 ; y < 4 ; y ++ ) {
750752 t_start ();
751753 t1 = t_read ();
752- if ((err = rsa_make_key (& yarrow_prng , find_prng ( "yarrow" ) , x /8 , 65537 , & key )) != CRYPT_OK ) {
754+ if ((err = rsa_make_key (& timing_prng , timing_prng_id , x /8 , 65537 , & key )) != CRYPT_OK ) {
753755 fprintf (stderr , "\n\nrsa_make_key says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
754756 exit (EXIT_FAILURE );
755757 }
@@ -882,7 +884,7 @@ static void time_dh(void)
882884
883885 t_start ();
884886 t1 = t_read ();
885- if ((err = dh_generate_key (& yarrow_prng , find_prng ( "yarrow" ) , & key )) != CRYPT_OK ) {
887+ if ((err = dh_generate_key (& timing_prng , timing_prng_id , & key )) != CRYPT_OK ) {
886888 fprintf (stderr , "\n\ndh_make_key says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
887889 exit (EXIT_FAILURE );
888890 }
@@ -907,7 +909,7 @@ static void time_ecc(void)
907909 ulong64 t1 , t2 ;
908910 unsigned char buf [2 ][256 ] = { 0 };
909911 unsigned long i , w , x , y , z ;
910- int err , stat ;
912+ int err , stat , hashidx ;
911913 const unsigned long sizes [] = {
912914#ifdef LTC_ECC_SECP112R1
913915112 /8 ,
@@ -934,29 +936,48 @@ static void time_ecc(void)
934936521 /8 ,
935937#endif
936938100000 };
939+ prng_state ecc_prng ;
937940 ltc_ecc_sig_opts sig_opts = {
938941 .type = LTC_ECCSIG_RFC7518 ,
939- .prng = & yarrow_prng ,
940- .wprng = find_prng ( "yarrow" )
942+ .prng = & ecc_prng ,
943+ .wprng = timing_prng_id
941944 };
945+ const unsigned char prng_entropy [] = {
946+ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0a ,
947+ 0x0b , 0x0c , 0x0d , 0x0e , 0x0f , 0x10 , 0x11 , 0x12 , 0x13 , 0x14 ,
948+ 0x15 , 0x16 , 0x17 , 0x18 , 0x19 , 0x1a , 0x1b , 0x1c , 0x1d , 0x1e ,
949+ 0x1f , 0x20 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ,
950+ 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f , 0x10 , 0x11 , 0x12 ,
951+ 0x13 , 0x14 , 0x15 , 0x16 , 0x17 , 0x18 , 0x19 , 0x1a , 0x1b , 0x1c ,
952+ 0x1d , 0x1e , 0x1f , 0x20
953+ };
954+ if ((err = prng_descriptor [timing_prng_id ].pimport (prng_entropy , sizeof (prng_entropy ), & ecc_prng )) != CRYPT_OK ) {
955+ fprintf (stderr , "\n\nprng.import() says %s!\n" , error_to_string (err ));
956+ exit (EXIT_FAILURE );
957+ }
958+ if ((err = prng_descriptor [timing_prng_id ].ready (& ecc_prng )) != CRYPT_OK ) {
959+ fprintf (stderr , "\n\nprng.ready() says %s!\n" , error_to_string (err ));
960+ exit (EXIT_FAILURE );
961+ }
942962
943963 if (ltc_mp .name == NULL ) return ;
964+ hashidx = find_hash ("sha1" );
944965
945966 for (x = sizes [i = 0 ]; x < 100000 ; x = sizes [++ i ]) {
946967 t2 = 0 ;
947968 for (y = 0 ; y < 256 ; y ++ ) {
948969 t_start ();
949970 t1 = t_read ();
950- if ((err = ecc_make_key (& yarrow_prng , find_prng ( "yarrow" ) , x , & key )) != CRYPT_OK ) {
971+ if ((err = ecc_make_key (sig_opts . prng , sig_opts . wprng , x , & key )) != CRYPT_OK ) {
951972 fprintf (stderr , "\n\necc_make_key says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
952973 exit (EXIT_FAILURE );
953974 }
954975 t1 = t_read () - t1 ;
955976 t2 += t1 ;
956977
957978#ifdef LTC_PROFILE
958- t2 <<= 8 ;
959- break ;
979+ t2 <<= 8 ;
980+ break ;
960981#endif
961982
962983 if (y < 255 ) {
@@ -971,21 +992,22 @@ static void time_ecc(void)
971992 t_start ();
972993 t1 = t_read ();
973994 z = sizeof (buf [1 ]);
974- if ((err = ecc_encrypt_key (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng , find_prng ( "yarrow" ), find_hash ( "sha1" ) ,
995+ if ((err = ecc_encrypt_key (buf [0 ], 20 , buf [1 ], & z , sig_opts . prng , sig_opts . wprng , hashidx ,
975996 & key )) != CRYPT_OK ) {
976997 fprintf (stderr , "\n\necc_encrypt_key says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
977998 exit (EXIT_FAILURE );
978999 }
9791000 t1 = t_read () - t1 ;
9801001 t2 += t1 ;
9811002#ifdef LTC_PROFILE
982- t2 <<= 8 ;
983- break ;
1003+ t2 <<= 8 ;
1004+ break ;
9841005#endif
9851006 }
9861007 t2 >>= 8 ;
9871008 fprintf (stderr , "ECC-%lu encrypt_key took %15" PRI64 "u cycles\n" , x * 8 , t2 );
9881009
1010+
9891011 t2 = 0 ;
9901012 for (y = 0 ; y < 256 ; y ++ ) {
9911013 t_start ();
@@ -998,8 +1020,8 @@ static void time_ecc(void)
9981020 t1 = t_read () - t1 ;
9991021 t2 += t1 ;
10001022#ifdef LTC_PROFILE
1001- t2 <<= 8 ;
1002- break ;
1023+ t2 <<= 8 ;
1024+ break ;
10031025#endif
10041026 }
10051027 t2 >>= 8 ;
@@ -1017,8 +1039,8 @@ static void time_ecc(void)
10171039 t1 = t_read () - t1 ;
10181040 t2 += t1 ;
10191041#ifdef LTC_PROFILE
1020- t2 <<= 8 ;
1021- break ;
1042+ t2 <<= 8 ;
1043+ break ;
10221044#endif
10231045 }
10241046 t2 >>= 8 ;
@@ -1039,8 +1061,8 @@ static void time_ecc(void)
10391061 t1 = t_read () - t1 ;
10401062 t2 += t1 ;
10411063#ifdef LTC_PROFILE
1042- t2 <<= 8 ;
1043- break ;
1064+ t2 <<= 8 ;
1065+ break ;
10441066#endif
10451067 }
10461068 t2 >>= 8 ;
@@ -1245,8 +1267,8 @@ static void time_macs_(unsigned long MAC_SIZE)
12451267 exit (EXIT_FAILURE );
12461268 }
12471269
1248- yarrow_read (ctx .buf , ctx .size , & yarrow_prng );
1249- yarrow_read (ctx .key , 16 , & yarrow_prng );
1270+ prng_descriptor [ timing_prng_id ]. read (ctx .buf , ctx .size , & timing_prng );
1271+ prng_descriptor [ timing_prng_id ]. read (ctx .key , sizeof ( ctx . key ) , & timing_prng );
12501272
12511273 for (n = 0 ; n < LTC_ARRAY_SIZE (time_funs ); ++ n ) {
12521274 if (!should_skip (time_funs [n ].name ))
@@ -1498,9 +1520,9 @@ static void time_eacs_(unsigned long MAC_SIZE)
14981520
14991521 ctx .cipher_idx = find_cipher ("aes" );
15001522
1501- yarrow_read (ctx .buf , ctx .size , & yarrow_prng );
1502- yarrow_read (ctx .key , sizeof (ctx .key ), & yarrow_prng );
1503- yarrow_read (ctx .IV , sizeof (ctx .IV ), & yarrow_prng );
1523+ prng_descriptor [ timing_prng_id ]. read (ctx .buf , ctx .size , & timing_prng );
1524+ prng_descriptor [ timing_prng_id ]. read (ctx .key , sizeof (ctx .key ), & timing_prng );
1525+ prng_descriptor [ timing_prng_id ]. read (ctx .IV , sizeof (ctx .IV ), & timing_prng );
15041526
15051527 for (n = 0 ; n < LTC_ARRAY_SIZE (time_funs ); ++ n ) {
15061528 if (!should_skip (time_funs [n ].name ))
@@ -1605,8 +1627,12 @@ register_all_prngs();
16051627 } else if (argc > 3 ){
16061628 filter_arg = argv [3 ];
16071629 }
1608-
1609- if ((err = rng_make_prng (128 , find_prng ("yarrow" ), & yarrow_prng , NULL )) != CRYPT_OK ) {
1630+ if (find_prng ("sober128" ) != -1 )
1631+ timing_prng_name = "sober128" ;
1632+ else
1633+ timing_prng_name = "yarrow" ;
1634+ timing_prng_id = find_prng (timing_prng_name );
1635+ if ((err = rng_make_prng (128 , timing_prng_id , & timing_prng , NULL )) != CRYPT_OK ) {
16101636 fprintf (stderr , "rng_make_prng failed: %s\n" , error_to_string (err ));
16111637 exit (EXIT_FAILURE );
16121638}
0 commit comments