havege.c 9.3 KB

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