havege.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
  3. *
  4. * Copyright (C) 2006-2014, 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. #if !defined(POLARSSL_CONFIG_FILE)
  33. #include "polarssl/config.h"
  34. #else
  35. #include POLARSSL_CONFIG_FILE
  36. #endif
  37. #if defined(POLARSSL_HAVEGE_C)
  38. #include "polarssl/havege.h"
  39. #include "polarssl/timing.h"
  40. #include <string.h>
  41. /* Implementation that should never be optimized out by the compiler */
  42. static void polarssl_zeroize( void *v, size_t n ) {
  43. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  44. }
  45. /* ------------------------------------------------------------------------
  46. * On average, one iteration accesses two 8-word blocks in the havege WALK
  47. * table, and generates 16 words in the RES array.
  48. *
  49. * The data read in the WALK table is updated and permuted after each use.
  50. * The result of the hardware clock counter read is used for this update.
  51. *
  52. * 25 conditional tests are present. The conditional tests are grouped in
  53. * two nested groups of 12 conditional tests and 1 test that controls the
  54. * permutation; on average, there should be 6 tests executed and 3 of them
  55. * should be mispredicted.
  56. * ------------------------------------------------------------------------
  57. */
  58. #define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
  59. #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
  60. #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
  61. #define TST1_LEAVE U1++; }
  62. #define TST2_LEAVE U2++; }
  63. #define ONE_ITERATION \
  64. \
  65. PTEST = PT1 >> 20; \
  66. \
  67. TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
  68. TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
  69. TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
  70. \
  71. TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
  72. TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
  73. TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
  74. \
  75. PTX = (PT1 >> 18) & 7; \
  76. PT1 &= 0x1FFF; \
  77. PT2 &= 0x1FFF; \
  78. CLK = (int) hardclock(); \
  79. \
  80. i = 0; \
  81. A = &WALK[PT1 ]; RES[i++] ^= *A; \
  82. B = &WALK[PT2 ]; RES[i++] ^= *B; \
  83. C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
  84. D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
  85. \
  86. IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
  87. *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
  88. *B = IN ^ U1; \
  89. *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
  90. *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
  91. \
  92. A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
  93. B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
  94. C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
  95. D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
  96. \
  97. if( PTEST & 1 ) SWAP( A, C ); \
  98. \
  99. IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
  100. *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
  101. *B = IN; CLK = (int) hardclock(); \
  102. *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
  103. *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
  104. \
  105. A = &WALK[PT1 ^ 4]; \
  106. B = &WALK[PT2 ^ 1]; \
  107. \
  108. PTEST = PT2 >> 1; \
  109. \
  110. PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
  111. PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
  112. PTY = (PT2 >> 10) & 7; \
  113. \
  114. TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
  115. TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
  116. TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
  117. \
  118. TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
  119. TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
  120. TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
  121. \
  122. C = &WALK[PT1 ^ 5]; \
  123. D = &WALK[PT2 ^ 5]; \
  124. \
  125. RES[i++] ^= *A; \
  126. RES[i++] ^= *B; \
  127. RES[i++] ^= *C; \
  128. RES[i++] ^= *D; \
  129. \
  130. IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
  131. *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
  132. *B = IN ^ U2; \
  133. *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
  134. *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
  135. \
  136. A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
  137. B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
  138. C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
  139. D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
  140. \
  141. IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
  142. *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
  143. *B = IN; \
  144. *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
  145. *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
  146. \
  147. PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
  148. WALK[PT1 ^ PTX ^ 7] ) & (~1); \
  149. PT1 ^= (PT2 ^ 0x10) & 0x10; \
  150. \
  151. for( n++, i = 0; i < 16; i++ ) \
  152. hs->pool[n % COLLECT_SIZE] ^= RES[i];
  153. /*
  154. * Entropy gathering function
  155. */
  156. static void havege_fill( havege_state *hs )
  157. {
  158. int i, n = 0;
  159. int U1, U2, *A, *B, *C, *D;
  160. int PT1, PT2, *WALK, RES[16];
  161. int PTX, PTY, CLK, PTEST, IN;
  162. WALK = hs->WALK;
  163. PT1 = hs->PT1;
  164. PT2 = hs->PT2;
  165. PTX = U1 = 0;
  166. PTY = U2 = 0;
  167. memset( RES, 0, sizeof( RES ) );
  168. while( n < COLLECT_SIZE * 4 )
  169. {
  170. ONE_ITERATION
  171. ONE_ITERATION
  172. ONE_ITERATION
  173. ONE_ITERATION
  174. }
  175. hs->PT1 = PT1;
  176. hs->PT2 = PT2;
  177. hs->offset[0] = 0;
  178. hs->offset[1] = COLLECT_SIZE / 2;
  179. }
  180. /*
  181. * HAVEGE initialization
  182. */
  183. void havege_init( havege_state *hs )
  184. {
  185. memset( hs, 0, sizeof( havege_state ) );
  186. havege_fill( hs );
  187. }
  188. void havege_free( havege_state *hs )
  189. {
  190. if( hs == NULL )
  191. return;
  192. polarssl_zeroize( hs, sizeof( havege_state ) );
  193. }
  194. /*
  195. * HAVEGE rand function
  196. */
  197. int havege_random( void *p_rng, unsigned char *buf, size_t len )
  198. {
  199. int val;
  200. size_t use_len;
  201. havege_state *hs = (havege_state *) p_rng;
  202. unsigned char *p = buf;
  203. while( len > 0 )
  204. {
  205. use_len = len;
  206. if( use_len > sizeof(int) )
  207. use_len = sizeof(int);
  208. if( hs->offset[1] >= COLLECT_SIZE )
  209. havege_fill( hs );
  210. val = hs->pool[hs->offset[0]++];
  211. val ^= hs->pool[hs->offset[1]++];
  212. memcpy( p, &val, use_len );
  213. len -= use_len;
  214. p += use_len;
  215. }
  216. return( 0 );
  217. }
  218. #endif /* POLARSSL_HAVEGE_C */