havege.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * The HAVEGE RNG was designed by Andre Seznec in 2002.
  27. *
  28. * http://www.irisa.fr/caps/projects/hipsor/publi.php
  29. *
  30. * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
  31. */
  32. #include "config.h"
  33. #if defined(POLARSSL_HAVEGE_C)
  34. #include "polarssl/havege.h"
  35. #include "polarssl/timing.h"
  36. #include <string.h>
  37. #include <time.h>
  38. /* ------------------------------------------------------------------------
  39. * On average, one iteration accesses two 8-word blocks in the havege WALK
  40. * table, and generates 16 words in the RES array.
  41. *
  42. * The data read in the WALK table is updated and permuted after each use.
  43. * The result of the hardware clock counter read is used for this update.
  44. *
  45. * 25 conditional tests are present. The conditional tests are grouped in
  46. * two nested groups of 12 conditional tests and 1 test that controls the
  47. * permutation; on average, there should be 6 tests executed and 3 of them
  48. * should be mispredicted.
  49. * ------------------------------------------------------------------------
  50. */
  51. #define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
  52. #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
  53. #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
  54. #define TST1_LEAVE U1++; }
  55. #define TST2_LEAVE U2++; }
  56. #define ONE_ITERATION \
  57. \
  58. PTEST = PT1 >> 20; \
  59. \
  60. TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
  61. TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
  62. TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
  63. \
  64. TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
  65. TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
  66. TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
  67. \
  68. PTX = (PT1 >> 18) & 7; \
  69. PT1 &= 0x1FFF; \
  70. PT2 &= 0x1FFF; \
  71. CLK = (int) hardclock(); \
  72. \
  73. i = 0; \
  74. A = &WALK[PT1 ]; RES[i++] ^= *A; \
  75. B = &WALK[PT2 ]; RES[i++] ^= *B; \
  76. C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
  77. D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
  78. \
  79. IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
  80. *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
  81. *B = IN ^ U1; \
  82. *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
  83. *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
  84. \
  85. A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
  86. B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
  87. C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
  88. D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
  89. \
  90. if( PTEST & 1 ) SWAP( A, C ); \
  91. \
  92. IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
  93. *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
  94. *B = IN; CLK = (int) hardclock(); \
  95. *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
  96. *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
  97. \
  98. A = &WALK[PT1 ^ 4]; \
  99. B = &WALK[PT2 ^ 1]; \
  100. \
  101. PTEST = PT2 >> 1; \
  102. \
  103. PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
  104. PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
  105. PTY = (PT2 >> 10) & 7; \
  106. \
  107. TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
  108. TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
  109. TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
  110. \
  111. TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
  112. TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
  113. TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
  114. \
  115. C = &WALK[PT1 ^ 5]; \
  116. D = &WALK[PT2 ^ 5]; \
  117. \
  118. RES[i++] ^= *A; \
  119. RES[i++] ^= *B; \
  120. RES[i++] ^= *C; \
  121. RES[i++] ^= *D; \
  122. \
  123. IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
  124. *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
  125. *B = IN ^ U2; \
  126. *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
  127. *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
  128. \
  129. A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
  130. B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
  131. C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
  132. D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
  133. \
  134. IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
  135. *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
  136. *B = IN; \
  137. *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
  138. *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
  139. \
  140. PT1 = ( RES[(i - 8) ^ PTX] ^ \
  141. WALK[PT1 ^ PTX ^ 7] ) & (~1); \
  142. PT1 ^= (PT2 ^ 0x10) & 0x10; \
  143. \
  144. for( n++, i = 0; i < 16; i++ ) \
  145. hs->pool[n % COLLECT_SIZE] ^= RES[i];
  146. /*
  147. * Entropy gathering function
  148. */
  149. static void havege_fill( havege_state *hs )
  150. {
  151. int i, n = 0;
  152. int U1, U2, *A, *B, *C, *D;
  153. int PT1, PT2, *WALK, RES[16];
  154. int PTX, PTY, CLK, PTEST, IN;
  155. WALK = hs->WALK;
  156. PT1 = hs->PT1;
  157. PT2 = hs->PT2;
  158. PTX = U1 = 0;
  159. PTY = U2 = 0;
  160. memset( RES, 0, sizeof( RES ) );
  161. while( n < COLLECT_SIZE * 4 )
  162. {
  163. ONE_ITERATION
  164. ONE_ITERATION
  165. ONE_ITERATION
  166. ONE_ITERATION
  167. }
  168. hs->PT1 = PT1;
  169. hs->PT2 = PT2;
  170. hs->offset[0] = 0;
  171. hs->offset[1] = COLLECT_SIZE / 2;
  172. }
  173. /*
  174. * HAVEGE initialization
  175. */
  176. void havege_init( havege_state *hs )
  177. {
  178. memset( hs, 0, sizeof( havege_state ) );
  179. havege_fill( hs );
  180. }
  181. /*
  182. * HAVEGE rand function
  183. */
  184. int havege_rand( void *p_rng )
  185. {
  186. int ret;
  187. havege_state *hs = (havege_state *) p_rng;
  188. if( hs->offset[1] >= COLLECT_SIZE )
  189. havege_fill( hs );
  190. ret = hs->pool[hs->offset[0]++];
  191. ret ^= hs->pool[hs->offset[1]++];
  192. return( ret );
  193. }
  194. #endif