havege.c 9.1 KB

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