ARC2.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* ===================================================================
  2. *
  3. * Copyright (c) 2015, Legrandin <helderijs@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. * ===================================================================
  30. */
  31. #include "pycrypto_common.h"
  32. #include "block_base.h"
  33. FAKE_INIT(raw_arc2)
  34. #define MODULE_NAME ARC2
  35. #define BLOCK_SIZE 8
  36. #define KEY_SIZE 0
  37. typedef struct
  38. {
  39. uint16_t exp_key[64];
  40. } block_state;
  41. static int
  42. block_init(block_state *self, const uint8_t *key, size_t t /* key_bytes */,
  43. size_t effective_key_bits)
  44. {
  45. uint8_t t8, tm;
  46. int i;
  47. uint8_t bkey[128];
  48. static const uint8_t permute[256] = {
  49. 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
  50. 198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
  51. 23,154, 89,245,135,179, 79, 19, 97, 69,109,141, 9,129,125, 50,
  52. 189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,
  53. 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,
  54. 18,117,202, 31, 59,190,228,209, 66, 61,212, 48,163, 60,182, 38,
  55. 111,191, 14,218, 70,105, 7, 87, 39,242, 29,155,188,148, 67, 3,
  56. 248, 17,199,246,144,239, 62,231, 6,195,213, 47,200,102, 30,215,
  57. 8,232,234,222,128, 82,238,247,132,170,114,172, 53, 77,106, 42,
  58. 150, 26,210,113, 90, 21, 73,116, 75,159,208, 94, 4, 24,164,236,
  59. 194,224, 65,110, 15, 81,203,204, 36,145,175, 80,161,244,112, 57,
  60. 153,124, 58,133, 35,184,180,122,252, 2, 54, 91, 37, 85,151, 49,
  61. 45, 93,250,152,227,138,146,174, 5,223, 41, 16,103,108,186,201,
  62. 211, 0,230,207,225,158,168, 44, 99, 22, 1, 63, 88,226,137,169,
  63. 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,
  64. 197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173
  65. };
  66. if (NULL == self)
  67. return ERR_NULL;
  68. if ((t < 5) || (t > 128))
  69. return ERR_KEY_SIZE;
  70. if ((effective_key_bits < 40) || (effective_key_bits > 1024))
  71. return ERR_KEY_SIZE;
  72. memcpy(bkey, key, t);
  73. t8 = (effective_key_bits + 7) / 8;
  74. tm = (1 << (8 - (t8*8 - effective_key_bits))) - 1;
  75. for (i=t; i<128; i++)
  76. bkey[i] = permute[(bkey[i-1] + bkey[i-t]) % 256];
  77. bkey[128-t8] = permute[bkey[128-t8] & tm];
  78. for (i=127-t8; i>=0; i--)
  79. bkey[i] = permute[bkey[i+1] ^ bkey[i+t8]];
  80. for (i=0; i<64; i++)
  81. self->exp_key[i] = bkey[2*i] + 256*bkey[2*i+1];
  82. return 0;
  83. }
  84. #define ROL16(x, p) ((((x) << (p)) | ((x) >> (16-(p)))) & 0xFFFF)
  85. #define ROR16(x, p) ((((x) >> (p)) | ((x) << (16-(p)))) & 0xFFFF)
  86. static inline void mix_round(uint16_t *r, const uint16_t *k, size_t *j)
  87. {
  88. r[0] += k[(*j)++] + (r[3] & r[2]) + (~r[3] & r[1]);
  89. r[0] = ROL16(r[0], 1);
  90. r[1] += k[(*j)++] + (r[0] & r[3]) + (~r[0] & r[2]);
  91. r[1] = ROL16(r[1], 2);
  92. r[2] += k[(*j)++] + (r[1] & r[0]) + (~r[1] & r[3]);
  93. r[2] = ROL16(r[2], 3);
  94. r[3] += k[(*j)++] + (r[2] & r[1]) + (~r[2] & r[0]);
  95. r[3] = ROL16(r[3], 5);
  96. }
  97. static inline void inv_mix_round(uint16_t *r, const uint16_t *k, size_t *j)
  98. {
  99. r[3] = ROR16(r[3], 5);
  100. r[3] -= k[(*j)--] + (r[2] & r[1]) + (~r[2] & r[0]);
  101. r[2] = ROR16(r[2], 3);
  102. r[2] -= k[(*j)--] + (r[1] & r[0]) + (~r[1] & r[3]);
  103. r[1] = ROR16(r[1], 2);
  104. r[1] -= k[(*j)--] + (r[0] & r[3]) + (~r[0] & r[2]);
  105. r[0] = ROR16(r[0], 1);
  106. r[0] -= k[(*j)--] + (r[3] & r[2]) + (~r[3] & r[1]);
  107. }
  108. static inline void mash_round(uint16_t *r, const uint16_t *k)
  109. {
  110. r[0] += k[r[3] & 63];
  111. r[1] += k[r[0] & 63];
  112. r[2] += k[r[1] & 63];
  113. r[3] += k[r[2] & 63];
  114. }
  115. static inline void inv_mash_round(uint16_t *r, const uint16_t *k)
  116. {
  117. r[3] -= k[r[2] & 63];
  118. r[2] -= k[r[1] & 63];
  119. r[1] -= k[r[0] & 63];
  120. r[0] -= k[r[3] & 63];
  121. }
  122. static void block_encrypt(const block_state *self, const uint8_t *in, uint8_t *out)
  123. {
  124. uint16_t r[4];
  125. const uint16_t *k;
  126. size_t i, j;
  127. k = self->exp_key;
  128. j = 0;
  129. for (i=0; i<4; i++) {
  130. r[i] = in[2*i] + 256*in[2*i+1];
  131. }
  132. for (i=0; i<5; i++) mix_round(r, k, &j);
  133. mash_round(r, k);
  134. for (i=0; i<6; i++) mix_round(r, k, &j);
  135. mash_round(r, k);
  136. for (i=0; i<5; i++) mix_round(r, k, &j);
  137. for (i=0; i<4; i++) {
  138. out[2*i] = r[i] & 255;
  139. out[2*i+1] = r[i] >> 8;
  140. }
  141. }
  142. static void block_decrypt(const block_state *self, const uint8_t *in, uint8_t *out)
  143. {
  144. uint16_t r[4];
  145. const uint16_t *k;
  146. size_t i, j;
  147. k = self->exp_key;
  148. for (i=0; i<4; i++) {
  149. r[i] = in[2*i] + 256*in[2*i+1];
  150. }
  151. j = 63;
  152. for (i=0; i<5; i++) inv_mix_round(r, k, &j);
  153. inv_mash_round(r, k);
  154. for (i=0; i<6; i++) inv_mix_round(r, k, &j);
  155. inv_mash_round(r, k);
  156. for (i=0; i<5; i++) inv_mix_round(r, k, &j);
  157. for (i=0; i<4; i++) {
  158. out[2*i] = r[i] & 255;
  159. out[2*i+1] = r[i] >> 8;
  160. }
  161. }
  162. static void
  163. block_finalize(block_state* self)
  164. {
  165. }
  166. #define NON_STANDARD_START_OPERATION
  167. #include "block_common.c"
  168. EXPORT_SYM int ARC2_start_operation(const uint8_t key[], size_t key_len, size_t effective_key_len, ARC2_State **pResult)
  169. {
  170. BlockBase *block_base;
  171. if ((key == NULL) || (pResult == NULL))
  172. return ERR_NULL;
  173. *pResult = calloc(1, sizeof(ARC2_State));
  174. if (NULL == *pResult)
  175. return ERR_MEMORY;
  176. block_base = &((*pResult)->base_state);
  177. block_base->encrypt = &ARC2_encrypt;
  178. block_base->decrypt = &ARC2_decrypt;
  179. block_base->destructor = &ARC2_stop_operation;
  180. block_base->block_len = BLOCK_SIZE;
  181. return block_init(&(*pResult)->algo_state, (unsigned char*)key,
  182. key_len, effective_key_len);
  183. }