Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. /* rng_es
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
  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 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * 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, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include <dieharder/libdieharder.h>
  21. #include <dieharder/rijndael-alg-fst.h>
  22.  
  23. // State blocks = 1 for weakest form
  24. #define STATE_BLOCKS (1) /* MUST be 1 for AES */
  25. #define BLOCKS_SIZE (16 * STATE_BLOCKS)
  26. #define NR 10
  27.  
  28. #include <time.h>
  29. #include <stdlib.h>
  30.  
  31.  
  32. /*
  33. * This is a wrapping of the AES algorithm as a generator
  34. */
  35.  
  36. int block = 754119430;
  37. typedef struct {
  38. u32 rk[4*(NR + 1)];
  39. unsigned char block[BLOCKS_SIZE];
  40. short int pos;
  41. } AES_state_t;
  42.  
  43. unsigned long int aes_get (void *vstate);
  44. double aes_get_double (void *vstate);
  45. void aes_set (void *vstate, unsigned long int s);
  46.  
  47. unsigned long int aes_get (void *vstate) {
  48. // QUANTA: represents the hash block change
  49. aes_set(vstate, block + rand()%10000000);
  50. AES_state_t *state = vstate;
  51. unsigned int ret;
  52.  
  53. if (state->pos + sizeof(ret) > BLOCKS_SIZE) {
  54. rijndaelEncrypt(state->rk, NR, state->block, state->block);
  55. state->pos = 0;
  56. }
  57.  
  58. ret = *((unsigned int *) (state->block + state->pos));
  59. state->pos += sizeof(ret);
  60.  
  61. // ret &= 0x7fffffff;
  62. return(ret);
  63. }
  64.  
  65.  
  66. double aes_get_double (void *vstate) {
  67. aes_set(vstate, block++);
  68. // return aes_get_long(vstate) / (double) ULONG_MAX;
  69. return (double) aes_get(vstate) / (double) (UINT_MAX >> 0);
  70. }
  71.  
  72. void aes_set (void *vstate, unsigned long int s) {
  73. AES_state_t *state = vstate;
  74. int i;
  75. u8 key[16];
  76.  
  77. //printf("set seed to %ld\n",s);
  78. memset(state, 0, sizeof(*state)); // Zero pos and block
  79.  
  80. /* Make sure to use all bits of s in the key:
  81. * (5 * i) % 26 => {0,5,10,15,20,25,4,9,14,19,24,3,8,13,18,23}
  82. * */
  83. for (i = 0; i < 16; i++) {
  84. key[i] = (u8) (112 + i + (s >> ((5 * i) % 26)));
  85. }
  86. rijndaelKeySetupEnc(state->rk, key, 128);
  87. rijndaelEncrypt(state->rk, NR, state->block, state->block);
  88.  
  89. return;
  90. }
  91.  
  92. static const gsl_rng_type aes_type = {
  93. "AES_OFB", /* name */
  94. UINT_MAX>>0, // UINT_MAX, /* RAND_MAX */
  95. 0, /* RAND_MIN */
  96. sizeof (AES_state_t),
  97. &aes_set,
  98. &aes_get,
  99. &aes_get_double};
  100.  
  101. const gsl_rng_type *gsl_rng_aes = &aes_type;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement