flipbip_string.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 1988 Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "flipbip_string.h"
  30. #include <ctype.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include "../crypto/memzero.h"
  34. #include "../crypto/rc4.h"
  35. char *
  36. flipbip_strtok(char *s, const char *delim)
  37. {
  38. static char *last;
  39. return flipbip_strtok_r(s, delim, &last);
  40. }
  41. char *
  42. flipbip_strtok_r(char *s, const char *delim, char **last)
  43. {
  44. char *spanp;
  45. int c, sc;
  46. char *tok;
  47. if (s == NULL && (s = *last) == NULL)
  48. return (NULL);
  49. /*
  50. * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
  51. */
  52. cont:
  53. c = *s++;
  54. for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
  55. if (c == sc)
  56. goto cont;
  57. }
  58. if (c == 0) { /* no non-delimiter characters */
  59. *last = NULL;
  60. return (NULL);
  61. }
  62. tok = s - 1;
  63. /*
  64. * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
  65. * Note that delim must have one NUL; we stop if we see that, too.
  66. */
  67. for (;;) {
  68. c = *s++;
  69. spanp = (char *)delim;
  70. do {
  71. if ((sc = *spanp++) == c) {
  72. if (c == 0)
  73. s = NULL;
  74. else
  75. s[-1] = 0;
  76. *last = s;
  77. return (tok);
  78. }
  79. } while (sc != 0);
  80. }
  81. /* NOTREACHED */
  82. }
  83. void
  84. flipbip_btox(const unsigned char in, char *str)
  85. {
  86. unsigned char n;
  87. unsigned char i = in;
  88. str += 2;
  89. *str = '\0';
  90. for (n = 2; n != 0; --n) {
  91. *--str = "0123456789abcdef"[i & 0x0F];
  92. i >>= 4;
  93. }
  94. }
  95. void
  96. flipbip_xtob(const char *str, unsigned char *out, int out_len)
  97. {
  98. int len = strlen(str) / 2;
  99. if (len > out_len) len = out_len;
  100. for (int i = 0; i < len; i++) {
  101. char c = 0;
  102. if (str[i * 2] >= '0' && str[i * 2] <= '9')
  103. c += (str[i * 2] - '0') << 4;
  104. if ((str[i * 2] & ~0x20) >= 'A' && (str[i * 2] & ~0x20) <= 'F')
  105. c += (10 + (str[i * 2] & ~0x20) - 'A') << 4;
  106. if (str[i * 2 + 1] >= '0' && str[i * 2 + 1] <= '9')
  107. c += (str[i * 2 + 1] - '0');
  108. if ((str[i * 2 + 1] & ~0x20) >= 'A' && (str[i * 2 + 1] & ~0x20) <= 'F')
  109. c += (10 + (str[i * 2 + 1] & ~0x20) - 'A');
  110. out[i] = c;
  111. }
  112. }
  113. void
  114. flipbip_cipher(const unsigned char* key_in, const unsigned int key_len, const char* in, char* out, const unsigned int io_len)
  115. {
  116. if (io_len > 512) return;
  117. RC4_CTX ctx;
  118. uint8_t buf[256];
  119. memzero(buf, 256);
  120. flipbip_xtob(in, buf, io_len / 2);
  121. rc4_init(&ctx, key_in, key_len);
  122. rc4_encrypt(&ctx, buf, 256);
  123. for (size_t i = 0; i < (io_len / 2); i++) {
  124. flipbip_btox(buf[i], out + i * 2);
  125. }
  126. memzero(buf, 256);
  127. }