core.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. int
  2. hydro_init(void)
  3. {
  4. hydro_random_ensure_initialized();
  5. return 0;
  6. }
  7. void
  8. hydro_memzero(void *pnt, size_t len)
  9. {
  10. #ifdef HAVE_EXPLICIT_BZERO
  11. explicit_bzero(pnt, len);
  12. #else
  13. volatile unsigned char *volatile pnt_ = (volatile unsigned char *volatile) pnt;
  14. size_t i = (size_t) 0U;
  15. while (i < len) {
  16. pnt_[i++] = 0U;
  17. }
  18. #endif
  19. }
  20. void
  21. hydro_increment(uint8_t *n, size_t len)
  22. {
  23. size_t i;
  24. uint_fast16_t c = 1U;
  25. for (i = 0; i < len; i++) {
  26. c += (uint_fast16_t) n[i];
  27. n[i] = (uint8_t) c;
  28. c >>= 8;
  29. }
  30. }
  31. char *
  32. hydro_bin2hex(char *hex, size_t hex_maxlen, const uint8_t *bin, size_t bin_len)
  33. {
  34. size_t i = (size_t) 0U;
  35. unsigned int x;
  36. int b;
  37. int c;
  38. if (bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U) {
  39. abort();
  40. }
  41. while (i < bin_len) {
  42. c = bin[i] & 0xf;
  43. b = bin[i] >> 4;
  44. x = (unsigned char) (87U + c + (((c - 10U) >> 8) & ~38U)) << 8 |
  45. (unsigned char) (87U + b + (((b - 10U) >> 8) & ~38U));
  46. hex[i * 2U] = (char) x;
  47. x >>= 8;
  48. hex[i * 2U + 1U] = (char) x;
  49. i++;
  50. }
  51. hex[i * 2U] = 0U;
  52. return hex;
  53. }
  54. int
  55. hydro_hex2bin(uint8_t *bin, size_t bin_maxlen, const char *hex, size_t hex_len, const char *ignore,
  56. const char **hex_end_p)
  57. {
  58. size_t bin_pos = (size_t) 0U;
  59. size_t hex_pos = (size_t) 0U;
  60. int ret = 0;
  61. unsigned char c;
  62. unsigned char c_alpha0, c_alpha;
  63. unsigned char c_num0, c_num;
  64. uint8_t c_acc = 0U;
  65. uint8_t c_val;
  66. unsigned char state = 0U;
  67. while (hex_pos < hex_len) {
  68. c = (unsigned char) hex[hex_pos];
  69. c_num = c ^ 48U;
  70. c_num0 = (c_num - 10U) >> 8;
  71. c_alpha = (c & ~32U) - 55U;
  72. c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8;
  73. if ((c_num0 | c_alpha0) == 0U) {
  74. if (ignore != NULL && state == 0U && strchr(ignore, c) != NULL) {
  75. hex_pos++;
  76. continue;
  77. }
  78. break;
  79. }
  80. c_val = (uint8_t) ((c_num0 & c_num) | (c_alpha0 & c_alpha));
  81. if (bin_pos >= bin_maxlen) {
  82. ret = -1;
  83. errno = ERANGE;
  84. break;
  85. }
  86. if (state == 0U) {
  87. c_acc = c_val * 16U;
  88. } else {
  89. bin[bin_pos++] = c_acc | c_val;
  90. }
  91. state = ~state;
  92. hex_pos++;
  93. }
  94. if (state != 0U) {
  95. hex_pos--;
  96. errno = EINVAL;
  97. ret = -1;
  98. }
  99. if (ret != 0) {
  100. bin_pos = (size_t) 0U;
  101. }
  102. if (hex_end_p != NULL) {
  103. *hex_end_p = &hex[hex_pos];
  104. } else if (hex_pos != hex_len) {
  105. errno = EINVAL;
  106. ret = -1;
  107. }
  108. if (ret != 0) {
  109. return ret;
  110. }
  111. return (int) bin_pos;
  112. }
  113. bool
  114. hydro_equal(const void *b1_, const void *b2_, size_t len)
  115. {
  116. const volatile uint8_t *volatile b1 = (const volatile uint8_t *volatile) b1_;
  117. const uint8_t *b2 = (const uint8_t *) b2_;
  118. size_t i;
  119. uint8_t d = (uint8_t) 0U;
  120. if (b1 == b2) {
  121. d = ~d;
  122. }
  123. for (i = 0U; i < len; i++) {
  124. d |= b1[i] ^ b2[i];
  125. }
  126. return (bool) (1 & ((d - 1) >> 8));
  127. }
  128. int
  129. hydro_compare(const uint8_t *b1_, const uint8_t *b2_, size_t len)
  130. {
  131. const volatile uint8_t *volatile b1 = (const volatile uint8_t *volatile) b1_;
  132. const uint8_t *b2 = (const uint8_t *) b2_;
  133. uint8_t gt = 0U;
  134. uint8_t eq = 1U;
  135. size_t i;
  136. i = len;
  137. while (i != 0U) {
  138. i--;
  139. gt |= ((b2[i] - b1[i]) >> 8) & eq;
  140. eq &= ((b2[i] ^ b1[i]) - 1) >> 8;
  141. }
  142. return (int) (gt + gt + eq) - 1;
  143. }
  144. int
  145. hydro_pad(unsigned char *buf, size_t unpadded_buflen, size_t blocksize, size_t max_buflen)
  146. {
  147. unsigned char * tail;
  148. size_t i;
  149. size_t xpadlen;
  150. size_t xpadded_len;
  151. volatile unsigned char mask;
  152. unsigned char barrier_mask;
  153. if (blocksize <= 0U || max_buflen > INT_MAX) {
  154. return -1;
  155. }
  156. xpadlen = blocksize - 1U;
  157. if ((blocksize & (blocksize - 1U)) == 0U) {
  158. xpadlen -= unpadded_buflen & (blocksize - 1U);
  159. } else {
  160. xpadlen -= unpadded_buflen % blocksize;
  161. }
  162. if (SIZE_MAX - unpadded_buflen <= xpadlen) {
  163. return -1;
  164. }
  165. xpadded_len = unpadded_buflen + xpadlen;
  166. if (xpadded_len >= max_buflen) {
  167. return -1;
  168. }
  169. tail = &buf[xpadded_len];
  170. mask = 0U;
  171. for (i = 0; i < blocksize; i++) {
  172. barrier_mask = (unsigned char) (((i ^ xpadlen) - 1U) >> ((sizeof(size_t) - 1U) * CHAR_BIT));
  173. tail[-i] = (tail[-i] & mask) | (0x80 & barrier_mask);
  174. mask |= barrier_mask;
  175. }
  176. return (int) (xpadded_len + 1);
  177. }
  178. int
  179. hydro_unpad(const unsigned char *buf, size_t padded_buflen, size_t blocksize)
  180. {
  181. const unsigned char *tail;
  182. unsigned char acc = 0U;
  183. unsigned char c;
  184. unsigned char valid = 0U;
  185. volatile size_t pad_len = 0U;
  186. size_t i;
  187. size_t is_barrier;
  188. if (padded_buflen < blocksize || blocksize <= 0U) {
  189. return -1;
  190. }
  191. tail = &buf[padded_buflen - 1U];
  192. for (i = 0U; i < blocksize; i++) {
  193. c = tail[-i];
  194. is_barrier = (((acc - 1U) & (pad_len - 1U) & ((c ^ 0x80) - 1U)) >> 8) & 1U;
  195. acc |= c;
  196. pad_len |= (i & -is_barrier);
  197. valid |= (unsigned char) is_barrier;
  198. }
  199. if (valid == 0) {
  200. return -1;
  201. }
  202. return (int) (padded_buflen - 1 - pad_len);
  203. }