havege.c 8.7 KB

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