havege.c 10 KB

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