core.h 5.6 KB

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