Download | Plain Text | No Line Numbers


  1. #include <string.h>
  2. #include <assert.h>
  3. #include "cipher.h"
  4.  
  5. static unsigned char key[] = {
  6. 0x0b, 0x59, 0xed, 0x3d, 0x31, 0x1b, 0x3b, 0xce,
  7. 0x69, 0x14, 0xa9, 0x8f, 0xe8, 0xbe, 0x49, 0x42,
  8. 0x13, 0xa4, 0x98, 0x0a, 0x89, 0x9d, 0xb0, 0x4f,
  9. 0xa9, 0x0b, 0x25, 0x4e, 0xc4, 0x74, 0x2b, 0x63,
  10. 0x44, 0x91, 0xe0, 0xc6, 0x40, 0x87, 0xeb, 0x16,
  11. 0x91, 0xff, 0xcd, 0x9c, 0xf8, 0x56, 0x5a, 0xd7,
  12. 0x86, 0x6b, 0x6e, 0xad, 0xc6, 0x91, 0xc7, 0xaa,
  13. 0x6b, 0xf0, 0x30, 0xac, 0xe8, 0x4f, 0xb0, 0xe2,
  14. 0x5b, 0xf7, 0xa8, 0xb1, 0xec, 0xf9, 0x69, 0x5a,
  15. 0x3d, 0xce, 0xfe, 0x0e, 0x94, 0x1b, 0x19, 0x99,
  16. 0x32, 0xd7, 0x8a, 0x8f, 0xa4, 0xe7, 0x54, 0xce,
  17. 0xb7, 0x6d, 0xd6, 0x9f, 0xc2, 0xd0, 0xd9, 0x62,
  18. 0xcb, 0xd1, 0xf0, 0x2a, 0x36, 0xcf, 0x6d, 0xdd,
  19. 0x23, 0xd3, 0xc5, 0x45, 0x3e, 0x15, 0x64, 0x2d,
  20. 0x05, 0x59, 0x85, 0x20, 0x2c, 0x56, 0x64, 0xce,
  21. 0xff, 0xb5, 0x42, 0x6e, 0x64, 0x9a, 0x46, 0x14,
  22. 0x6e, 0x58, 0x82, 0xff, 0x44, 0x29, 0xaf, 0xbc,
  23. 0xac, 0x4b, 0xd3, 0x53, 0x44, 0xb9, 0x70, 0x17,
  24. 0x8c, 0x0c, 0x66, 0xae, 0xfd, 0x8d, 0x95, 0x8d,
  25. 0x36, 0x07, 0xe4, 0x72, 0x6e, 0xdd, 0x07, 0xda,
  26. 0x17, 0x66, 0xb2, 0xff, 0xae, 0x26, 0xaa, 0x7f,
  27. 0x88, 0xe1, 0xd3, 0xf2, 0xcc, 0x9b, 0xc5, 0x9b,
  28. 0xf5, 0x31, 0x19, 0x3b, 0xd8, 0xb3, 0xbc, 0xaa,
  29. 0x67, 0x31, 0x05, 0x0e, 0x75, 0x25, 0x04, 0x0f,
  30. 0x7f, 0xb9, 0x33, 0xad, 0x72, 0xa0, 0xf2, 0x51,
  31. 0xde, 0xd0, 0xf9, 0xb3, 0xe2, 0x36, 0xbc, 0xaf,
  32. 0xe0, 0xcb, 0x0c, 0x1e, 0x4a, 0xf7, 0x04, 0xda,
  33. 0x9c, 0xf0, 0xa3, 0x7e, 0x84, 0xef, 0x98, 0x47,
  34. 0x8d, 0x59, 0x0c, 0xf2, 0x79, 0x3e, 0x37, 0xf4,
  35. 0x4d, 0xfe, 0x29, 0x9d, 0xec, 0xe8, 0x84, 0x7b,
  36. 0xe6, 0x1f, 0x93, 0xbf, 0x27, 0x2b, 0xa7, 0x8e,
  37. 0x45, 0xfc, 0x68, 0x92, 0xb6, 0xb7, 0x33, 0x14,
  38. };
  39.  
  40. static unsigned int key_size = sizeof(key);
  41.  
  42. int CipherInit(ctx_t *ctx)
  43. {
  44. unsigned char val, oldval, tmp;
  45. unsigned int i;
  46.  
  47. assert(key_size == sizeof(ctx->cipher));
  48.  
  49. /* initalize values */
  50. ctx->out = NULL;
  51. ctx->off1 = 0;
  52. ctx->off2 = 0;
  53. ctx->prepend_hashkey = 1;
  54.  
  55. for(i = 0; i < key_size; ++i)
  56. ctx->cipher[i] = (unsigned char)i;
  57.  
  58. oldval = 0;
  59. for(i = 0; i < key_size; ++i)
  60. {
  61. val = ctx->cipher[i] + oldval + key[i];
  62.  
  63. /* swap values */
  64. tmp = ctx->cipher[i];
  65. ctx->cipher[i] = ctx->cipher[val];
  66. ctx->cipher[val] = tmp;
  67.  
  68. oldval = val;
  69. }
  70.  
  71. return i;
  72. }
  73.  
  74. uint32_t CipherUpdate(ctx_t *ctx, const char *in_data, uint32_t in_len)
  75. {
  76. uint32_t in_ix = 0, out_ix = 0;
  77. unsigned char off1, off2;
  78. unsigned char val1, val2;
  79.  
  80. assert(key_size == sizeof(ctx->cipher));
  81.  
  82. if (ctx->prepend_hashkey == 1)
  83. {
  84. ctx->out[out_ix++] = '#';
  85. ctx->prepend_hashkey = 0;
  86. /* store in_len inside ctx buffer. this is completely
  87.   * insane but confixx does it this way too
  88.   */
  89. ctx->ret = in_len + 1;
  90. }
  91. else
  92. ctx->ret = in_len;
  93.  
  94. /* retrieve offsets */
  95. off1 = ctx->off1;
  96. off2 = ctx->off2;
  97.  
  98. while(in_ix < in_len)
  99. {
  100. ++off1;
  101.  
  102. val1 = ctx->cipher[off1];
  103. val2 = ctx->cipher[((unsigned char)(val1 + off2))];
  104.  
  105. /* swap values */
  106. ctx->cipher[((unsigned char)(val1 + off2))] = val1;
  107. ctx->cipher[off1] = val2;
  108.  
  109. off2 = val1 + off2;
  110.  
  111. /* "decrypt" */
  112. ctx->out[out_ix++] = in_data[in_ix++] ^ ctx->cipher[((unsigned char)(val1 + val2))];
  113. }
  114.  
  115. /* store offsets */
  116. ctx->off1 = off1;
  117. ctx->off2 = off2;
  118.  
  119. return ctx->ret;
  120. }
  121.