havege.c 9.0 KB

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