qvinhprolol

Untitled

Jan 10th, 2023
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | Cybersecurity | 0 0
  1. /**************************************************************************\
  2. |
  3. |    Copyright (C) 2009 Marc Stevens
  4. |
  5. |    This program is free software: you can redistribute it and/or modify
  6. |    it under the terms of the GNU General Public License as published by
  7. |    the Free Software Foundation, either version 3 of the License, or
  8. |    (at your option) any later version.
  9. |
  10. |    This program is distributed in the hope that it will be useful,
  11. |    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. |    GNU General Public License for more details.
  14. |
  15. |    You should have received a copy of the GNU General Public License
  16. |    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. |
  18. \**************************************************************************/
  19.  
  20. #include <time.h>
  21. #include <iostream>
  22. #include "rng.hpp"
  23.  
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>    // open
  27. #include <unistd.h>   // read, close
  28. namespace hashclash {
  29.     void getosrnd(uint32 buf[256])
  30.     {
  31.         int fd;
  32.         if ((fd = open("/dev/urandom", O_RDONLY)) < 0) return;
  33.         read(fd, reinterpret_cast<char*>(buf), 256*4);
  34.         close(fd);
  35.     }
  36. }
  37.  
  38.  
  39. namespace hashclash {
  40.  
  41.     void getosrnd(uint32 buf[256]);
  42.  
  43.     uint32 seedd;
  44.     uint32 seed32_1;
  45.     uint32 seed32_2;
  46.     uint32 seed32_3;
  47.     uint32 seed32_4;
  48.  
  49.     void seed(uint32 s)
  50.     {
  51.         seedd = 0;
  52.         seed32_1 = s;
  53.         seed32_2 = 2;
  54.         seed32_3 = 3;
  55.         seed32_4 = 4;
  56.         for (unsigned i = 0; i < 0x1000; ++i)
  57.             xrng128();
  58.     }
  59.  
  60.     void seed(uint32* sbuf, unsigned len)
  61.     {
  62.         seedd = 0;
  63.         seed32_1 = 1;
  64.         seed32_2 = 2;
  65.         seed32_3 = 3;
  66.         seed32_4 = 4;
  67.         for (unsigned i = 0; i < len; ++i)
  68.         {
  69.             seed32_1 ^= sbuf[i];
  70.             xrng128();
  71.         }
  72.         for (unsigned i = 0; i < 0x1000; ++i)
  73.             xrng128();
  74.     }
  75.  
  76.     void addseed(uint32 s)
  77.     {
  78.         xrng128();
  79.         seed32_1 ^= s;
  80.         xrng128();
  81.     }
  82.  
  83.     void addseed(const uint32* sbuf, unsigned len)
  84.     {
  85.         xrng128();
  86.         for (unsigned i = 0; i < len; ++i)
  87.         {
  88.             seed32_1 ^= sbuf[i];
  89.             xrng128();
  90.         }
  91.     }
  92.  
  93.     struct hashclash_rng__init {
  94.         hashclash_rng__init()
  95.         {
  96.             seed(uint32(time(NULL)));
  97.             uint32 rndbuf[256]; // uninitialized on purpose
  98.             addseed(rndbuf, 256);
  99.             getosrnd(rndbuf);
  100.             addseed(rndbuf, 256);
  101.         }
  102.     };
  103.     hashclash_rng__init hashclash_rng__init__now;
  104.  
  105.     void hashclash_rng_hpp_init()
  106.     {
  107.         hashclash_rng__init here;
  108.     }
  109.  
  110. } // namespace hashclash
  111.  
Advertisement
Add Comment
Please, Sign In to add comment