helpers.function 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <polarssl/config.h>
  2. #ifdef _MSC_VER
  3. #include <basetsd.h>
  4. typedef UINT32 uint32_t;
  5. #else
  6. #include <inttypes.h>
  7. #endif
  8. /*
  9. * 32-bit integer manipulation macros (big endian)
  10. */
  11. #ifndef GET_ULONG_BE
  12. #define GET_ULONG_BE(n,b,i) \
  13. { \
  14. (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
  15. | ( (unsigned long) (b)[(i) + 1] << 16 ) \
  16. | ( (unsigned long) (b)[(i) + 2] << 8 ) \
  17. | ( (unsigned long) (b)[(i) + 3] ); \
  18. }
  19. #endif
  20. #ifndef PUT_ULONG_BE
  21. #define PUT_ULONG_BE(n,b,i) \
  22. { \
  23. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  24. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  25. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  26. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  27. }
  28. #endif
  29. int unhexify(unsigned char *obuf, const char *ibuf)
  30. {
  31. unsigned char c, c2;
  32. int len = strlen(ibuf) / 2;
  33. assert(!(strlen(ibuf) %1)); // must be even number of bytes
  34. while (*ibuf != 0)
  35. {
  36. c = *ibuf++;
  37. if( c >= '0' && c <= '9' )
  38. c -= '0';
  39. else if( c >= 'a' && c <= 'f' )
  40. c -= 'a' - 10;
  41. else if( c >= 'A' && c <= 'F' )
  42. c -= 'A' - 10;
  43. else
  44. assert( 0 );
  45. c2 = *ibuf++;
  46. if( c2 >= '0' && c2 <= '9' )
  47. c2 -= '0';
  48. else if( c2 >= 'a' && c2 <= 'f' )
  49. c2 -= 'a' - 10;
  50. else if( c2 >= 'A' && c2 <= 'F' )
  51. c2 -= 'A' - 10;
  52. else
  53. assert( 0 );
  54. *obuf++ = ( c << 4 ) | c2;
  55. }
  56. return len;
  57. }
  58. void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
  59. {
  60. unsigned char l, h;
  61. while (len != 0)
  62. {
  63. h = (*ibuf) / 16;
  64. l = (*ibuf) % 16;
  65. if( h < 10 )
  66. *obuf++ = '0' + h;
  67. else
  68. *obuf++ = 'a' + h - 10;
  69. if( l < 10 )
  70. *obuf++ = '0' + l;
  71. else
  72. *obuf++ = 'a' + l - 10;
  73. ++ibuf;
  74. len--;
  75. }
  76. }
  77. /**
  78. * This function just returns data from rand().
  79. * Although predictable and often similar on multiple
  80. * runs, this does not result in identical random on
  81. * each run. So do not use this if the results of a
  82. * test depend on the random data that is generated.
  83. *
  84. * rng_state shall be NULL.
  85. */
  86. static int rnd_std_rand( void *rng_state )
  87. {
  88. if( rng_state != NULL )
  89. rng_state = NULL;
  90. return( rand() );
  91. }
  92. /**
  93. * This function only returns zeros
  94. *
  95. * rng_state shall be NULL.
  96. */
  97. static int rnd_zero_rand( void *rng_state )
  98. {
  99. if( rng_state != NULL )
  100. rng_state = NULL;
  101. return( 0 );
  102. }
  103. typedef struct
  104. {
  105. unsigned char *buf;
  106. int length;
  107. int per_call;
  108. } rnd_buf_info;
  109. /**
  110. * This function returns random based on a buffer it receives.
  111. *
  112. * rng_state shall be a pointer to a rnd_buf_info structure.
  113. *
  114. * The number of bytes released from the buffer on each call to
  115. * the random function is specified by per_call. (Can be between
  116. * 1 and 4)
  117. *
  118. * After the buffer is empty it will return rand();
  119. */
  120. static int rnd_buffer_rand( void *rng_state )
  121. {
  122. rnd_buf_info *info = (rnd_buf_info *) rng_state;
  123. int res;
  124. if( rng_state == NULL )
  125. return( rand() );
  126. if( info->per_call > 4 )
  127. info->per_call = 4;
  128. else if( info->per_call < 1 )
  129. info->per_call = 1;
  130. res = rand();
  131. if( info->length >= info->per_call )
  132. {
  133. memcpy( &res, info->buf, info->per_call );
  134. info->buf += info->per_call;
  135. info->length -= info->per_call;
  136. }
  137. else if( info->length > 0 )
  138. {
  139. memcpy( &res, info->buf, info->length );
  140. info->length = 0;
  141. }
  142. return( res );
  143. }
  144. /**
  145. * Info structure for the pseudo random function
  146. *
  147. * Key should be set at the start to a test-unique value.
  148. * Do not forget endianness!
  149. * State( v0, v1 ) should be set to zero.
  150. */
  151. typedef struct
  152. {
  153. uint32_t key[16];
  154. uint32_t v0, v1;
  155. } rnd_pseudo_info;
  156. /**
  157. * This function returns random based on a pseudo random function.
  158. * This means the results should be identical on all systems.
  159. * Pseudo random is based on the XTEA encryption algorithm to
  160. * generate pseudorandom.
  161. *
  162. * rng_state shall be a pointer to a rnd_pseudo_info structure.
  163. */
  164. static int rnd_pseudo_rand( void *rng_state )
  165. {
  166. rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
  167. uint32_t i, *k, sum = 0, delta=0x9E3779B9;
  168. if( rng_state == NULL )
  169. return( rand() );
  170. k = info->key;
  171. for( i = 0; i < 32; i++ )
  172. {
  173. info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
  174. sum += delta;
  175. info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
  176. }
  177. return( info->v0 );
  178. }