xxtea.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /***********************************************************************
  2. Copyright 2006-2009 Ma Bingyao
  3. Copyright 2013 Gao Chunhui, Liu Tao
  4. These sources is free software. Redistributions of source code must
  5. retain the above copyright notice. Redistributions in binary form
  6. must reproduce the above copyright notice. You can redistribute it
  7. freely. You can use it with any free or commercial software.
  8. These sources is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY. Without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. github: https://github.com/liut/pecl-xxtea
  12. *************************************************************************/
  13. #include "xxtea.h"
  14. #include <memory.h>
  15. #include <stdlib.h>
  16. static void xxtea_long_encrypt(xxtea_long *v, xxtea_long len, xxtea_long *k)
  17. {
  18. xxtea_long n = len - 1;
  19. xxtea_long z = v[n], y = v[0], p, q = 6 + 52 / (n + 1), sum = 0, e;
  20. if (n < 1) {
  21. return;
  22. }
  23. while (0 < q--) {
  24. sum += XXTEA_DELTA;
  25. e = sum >> 2 & 3;
  26. for (p = 0; p < n; p++) {
  27. y = v[p + 1];
  28. z = v[p] += XXTEA_MX;
  29. }
  30. y = v[0];
  31. z = v[n] += XXTEA_MX;
  32. }
  33. }
  34. static void xxtea_long_decrypt(xxtea_long *v, xxtea_long len, xxtea_long *k)
  35. {
  36. xxtea_long n = len - 1;
  37. xxtea_long z = v[n], y = v[0], p, q = 6 + 52 / (n + 1), sum = q * XXTEA_DELTA, e;
  38. if (n < 1) {
  39. return;
  40. }
  41. while (sum != 0) {
  42. e = sum >> 2 & 3;
  43. for (p = n; p > 0; p--) {
  44. z = v[p - 1];
  45. y = v[p] -= XXTEA_MX;
  46. }
  47. z = v[n];
  48. y = v[0] -= XXTEA_MX;
  49. sum -= XXTEA_DELTA;
  50. }
  51. }
  52. static unsigned char *fix_key_length(unsigned char *key, xxtea_long key_len)
  53. {
  54. unsigned char *tmp = (unsigned char *)malloc(16);
  55. memcpy(tmp, key, key_len);
  56. memset(tmp + key_len, '\0', 16 - key_len);
  57. return tmp;
  58. }
  59. static xxtea_long *xxtea_to_long_array(unsigned char *data, xxtea_long len, int include_length, xxtea_long *ret_len) {
  60. xxtea_long i, n, *result;
  61. n = len >> 2;
  62. n = (((len & 3) == 0) ? n : n + 1);
  63. if (include_length) {
  64. result = (xxtea_long *)malloc((n + 1) << 2);
  65. result[n] = len;
  66. *ret_len = n + 1;
  67. } else {
  68. result = (xxtea_long *)malloc(n << 2);
  69. *ret_len = n;
  70. }
  71. memset(result, 0, n << 2);
  72. for (i = 0; i < len; i++) {
  73. result[i >> 2] |= (xxtea_long)data[i] << ((i & 3) << 3);
  74. }
  75. return result;
  76. }
  77. static unsigned char *xxtea_to_byte_array(xxtea_long *data, xxtea_long len, int include_length, xxtea_long *ret_len) {
  78. xxtea_long i, n, m;
  79. unsigned char *result;
  80. n = len << 2;
  81. if (include_length) {
  82. m = data[len - 1];
  83. if ((m < n - 7) || (m > n - 4)) return NULL;
  84. n = m;
  85. }
  86. result = (unsigned char *)malloc(n + 1);
  87. for (i = 0; i < n; i++) {
  88. result[i] = (unsigned char)((data[i >> 2] >> ((i & 3) << 3)) & 0xff);
  89. }
  90. result[n] = '\0';
  91. *ret_len = n;
  92. return result;
  93. }
  94. static unsigned char *do_xxtea_encrypt(unsigned char *data, xxtea_long len, unsigned char *key, xxtea_long *ret_len) {
  95. unsigned char *result;
  96. xxtea_long *v, *k, v_len, k_len;
  97. v = xxtea_to_long_array(data, len, 1, &v_len);
  98. k = xxtea_to_long_array(key, 16, 0, &k_len);
  99. xxtea_long_encrypt(v, v_len, k);
  100. result = xxtea_to_byte_array(v, v_len, 0, ret_len);
  101. free(v);
  102. free(k);
  103. return result;
  104. }
  105. static unsigned char *do_xxtea_decrypt(unsigned char *data, xxtea_long len, unsigned char *key, xxtea_long *ret_len) {
  106. unsigned char *result;
  107. xxtea_long *v, *k, v_len, k_len;
  108. v = xxtea_to_long_array(data, len, 0, &v_len);
  109. k = xxtea_to_long_array(key, 16, 0, &k_len);
  110. xxtea_long_decrypt(v, v_len, k);
  111. result = xxtea_to_byte_array(v, v_len, 1, ret_len);
  112. free(v);
  113. free(k);
  114. return result;
  115. }
  116. unsigned char *xxtea_encrypt(unsigned char *data, xxtea_long data_len, unsigned char *key, xxtea_long key_len, xxtea_long *ret_length)
  117. {
  118. unsigned char *result;
  119. *ret_length = 0;
  120. if (key_len < 16) {
  121. unsigned char *key2 = fix_key_length(key, key_len);
  122. result = do_xxtea_encrypt(data, data_len, key2, ret_length);
  123. free(key2);
  124. }
  125. else
  126. {
  127. result = do_xxtea_encrypt(data, data_len, key, ret_length);
  128. }
  129. return result;
  130. }
  131. unsigned char *xxtea_decrypt(unsigned char *data, xxtea_long data_len, unsigned char *key, xxtea_long key_len, xxtea_long *ret_length)
  132. {
  133. unsigned char *result;
  134. *ret_length = 0;
  135. if (key_len < 16) {
  136. unsigned char *key2 = fix_key_length(key, key_len);
  137. result = do_xxtea_decrypt(data, data_len, key2, ret_length);
  138. free(key2);
  139. }
  140. else
  141. {
  142. result = do_xxtea_decrypt(data, data_len, key, ret_length);
  143. }
  144. return result;
  145. }
  146. /* }}} */