flipbip_string.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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* flipbip_strtok(char* s, const char* delim) {
  36. static char* last;
  37. return flipbip_strtok_r(s, delim, &last);
  38. }
  39. char* flipbip_strtok_r(char* s, const char* delim, char** last) {
  40. char* spanp;
  41. int c, sc;
  42. char* tok;
  43. if(s == NULL && (s = *last) == NULL) return (NULL);
  44. /*
  45. * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
  46. */
  47. cont:
  48. c = *s++;
  49. for(spanp = (char*)delim; (sc = *spanp++) != 0;) {
  50. if(c == sc) goto cont;
  51. }
  52. if(c == 0) { /* no non-delimiter characters */
  53. *last = NULL;
  54. return (NULL);
  55. }
  56. tok = s - 1;
  57. /*
  58. * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
  59. * Note that delim must have one NUL; we stop if we see that, too.
  60. */
  61. for(;;) {
  62. c = *s++;
  63. spanp = (char*)delim;
  64. do {
  65. if((sc = *spanp++) == c) {
  66. if(c == 0)
  67. s = NULL;
  68. else
  69. s[-1] = 0;
  70. *last = s;
  71. return (tok);
  72. }
  73. } while(sc != 0);
  74. }
  75. /* NOTREACHED */
  76. }
  77. void flipbip_btox(const unsigned char* in, int in_len, char* str) {
  78. for(int i = 0; i < in_len; i++) {
  79. unsigned char n;
  80. unsigned char x = in[i];
  81. str += 2;
  82. *(str + (i * 2)) = '\0';
  83. for(n = 2; n != 0; --n) {
  84. *(--str + (i * 2)) = "0123456789abcdef"[x & 0x0F];
  85. x >>= 4;
  86. }
  87. }
  88. }
  89. void flipbip_xtob(const char* str, unsigned char* out, int out_len) {
  90. int len = strlen(str) / 2;
  91. if(len > out_len) len = out_len;
  92. for(int i = 0; i < len; i++) {
  93. char c = 0;
  94. if(str[i * 2] >= '0' && str[i * 2] <= '9') c += (str[i * 2] - '0') << 4;
  95. if((str[i * 2] & ~0x20) >= 'A' && (str[i * 2] & ~0x20) <= 'F')
  96. c += (10 + (str[i * 2] & ~0x20) - 'A') << 4;
  97. if(str[i * 2 + 1] >= '0' && str[i * 2 + 1] <= '9') c += (str[i * 2 + 1] - '0');
  98. if((str[i * 2 + 1] & ~0x20) >= 'A' && (str[i * 2 + 1] & ~0x20) <= 'F')
  99. c += (10 + (str[i * 2 + 1] & ~0x20) - 'A');
  100. out[i] = c;
  101. }
  102. }
  103. void flipbip_cipher(
  104. const unsigned char* key_in,
  105. const unsigned int key_len,
  106. const char* in,
  107. char* out,
  108. const unsigned int io_len) {
  109. if(io_len > 512) return;
  110. RC4_CTX ctx;
  111. uint8_t buf[256];
  112. memzero(buf, 256);
  113. flipbip_xtob(in, buf, io_len / 2);
  114. rc4_init(&ctx, key_in, key_len);
  115. rc4_encrypt(&ctx, buf, 256);
  116. flipbip_btox(buf, io_len / 2, out);
  117. memzero(buf, 256);
  118. }