Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2015
3,763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. int Juniper_rand_bytes(void *output, int size)
  2. {
  3.   unsigned int remaining; // esi@6
  4.   unsigned int index; // edi@6
  5.   signed int size_to_copy; // ebx@7
  6.   char *output_ptr; // ST00_4@9
  7.   char *str; // [sp+0h] [bp-18h]@2
  8.  
  9.   if ( size <= 0 || output == 0 )
  10.   {
  11.     str = "FIPS ERROR: bad parameter to PRNG\n";
  12. LABEL_3:
  13.     error_handler(str, 11);
  14.     return 0;
  15.   }
  16.   if ( !memcmp(zeros_, prng_seed, 8) || !memcmp(zeros_, prng_key, 8) )
  17.   {
  18.     str = "FIPS ERROR: PRNG not initialized\n";
  19.     goto LABEL_3;
  20.   }
  21.   remaining = size;
  22.   index = 0;
  23.   if ( size )
  24.   {
  25.     do
  26.     {
  27.       prng_generate();
  28.       size_to_copy = 32;
  29.       if ( remaining < 0x21 )
  30.         size_to_copy = remaining;
  31.       remaining -= size_to_copy;
  32.       output_ptr = (char *)output + index;
  33.       index += size_to_copy;
  34.       memcpy(output_ptr, prng_output_buffer, size_to_copy);
  35.     }
  36.     while ( size > index );
  37.   }
  38.   return 1;
  39. }
  40.  
  41. void prng_generate()
  42. {
  43.   int index; // eax@4
  44.   unsigned int bytes_generated; // ebx@4
  45.   int v2; // eax@9
  46.   int v3; // eax@12
  47.   char v4; // ST04_1@12
  48.   int time[2]; // [sp+10h] [bp-10h]@1
  49.  
  50.   time[0] = 0;
  51.   time[1] = get_cycles();
  52.   prng_output_index = 0;
  53.   ++blocks_generated_since_reseed;
  54.   if ( !do_not_need_to_reseed() )
  55.     prng_reseed();
  56.   for ( ; (unsigned int)prng_output_index <= 0x1F; prng_output_index += 8 )
  57.   {
  58.     memcpy(prev_prng_seed, prng_seed, 8);
  59.     memcpy(prev_prng_block, prng_block, 8);
  60.     ANSI_X9_31_generate_block(time, prng_seed, prng_key, prng_block);
  61.     if ( sub_561A30(0) && (!memcmp(prng_seed, prev_prng_seed, 8) || !memcmp(prng_block, prev_prng_block, 8)) )
  62.     {
  63.       v3 = sub_92D6F0();
  64.       sub_39E2C0(0, 6, v3, "FIPS ERROR: PRNG failure, duplicate random output\n", SLOBYTE(time[0]));
  65.       sub_65EDD0(67375118, v4);
  66.       error_handler("FIPS ERROR: PRNG failure, duplicate random output\n", 11);
  67.     }
  68.     index = prng_output_index;
  69.     bytes_generated = 0;
  70.     if ( prng_output_index )
  71.     {
  72.       do
  73.       {
  74.         if ( !memcmp(&prng_output_buffer[bytes_generated], prng_block, 8) )
  75.         {
  76.           v2 = sub_92D6F0();
  77.           sub_39E2C0(0, 6, v2, "FIPS ERROR: PRNG failure, duplicate random output\n", SLOBYTE(time[0]));
  78.           error_handler("FIPS ERROR: PRNG failure, duplicate random output\n", 11);
  79.         }
  80.         index = prng_output_index;
  81.         bytes_generated += 8;
  82.       }
  83.       while ( prng_output_index > bytes_generated );
  84.     }
  85.     memcpy(&prng_output_buffer[index], prng_block, 8);
  86.   }
  87. }
  88.  
  89. void prng_reseed(void)
  90. {
  91.   blocks_generated_since_reseed = 0;
  92.   if ( dualec_generate(prng_output_buffer, 32) != 32 )
  93.     error_handler("FIPS ERROR: PRNG failure, unable to reseed\n", 11);
  94.   memcpy(prng_seed, prng_output_buffer, 8);
  95.   prng_output_index = 8;
  96.   memcpy(prng_key, &prng_output_buffer[8], 24);
  97.   prng_output_index = 32;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement