havege.c 8.8 KB

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