ARC2.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * rc2.c : Source code for the RC2 block cipher
  3. *
  4. * Part of the Python Cryptography Toolkit
  5. *
  6. * ===================================================================
  7. * This file appears to contain code from the ARC2 implementation
  8. * "rc2.c" implementation (the "Original Code"), with modifications made
  9. * after it was incorporated into PyCrypto (the "Modifications").
  10. *
  11. * To the best of our knowledge, the Original Code was placed into the
  12. * public domain by its (anonymous) author:
  13. *
  14. * **********************************************************************
  15. * * To commemorate the 1996 RSA Data Security Conference, the following *
  16. * * code is released into the public domain by its author. Prost! *
  17. * * *
  18. * * This cipher uses 16-bit words and little-endian byte ordering. *
  19. * * I wonder which processor it was optimized for? *
  20. * * *
  21. * * Thanks to CodeView, SoftIce, and D86 for helping bring this code to *
  22. * * the public. *
  23. * **********************************************************************
  24. *
  25. * The Modifications to this file are dedicated to the public domain.
  26. * To the extent that dedication to the public domain is not available,
  27. * everyone is granted a worldwide, perpetual, royalty-free,
  28. * non-exclusive license to exercise all rights associated with the
  29. * contents of this file for any purpose whatsoever. No rights are
  30. * reserved.
  31. *
  32. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  33. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  34. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  35. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  36. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  37. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  38. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  39. * SOFTWARE.
  40. * ===================================================================
  41. *
  42. */
  43. #include <string.h>
  44. #include "Python.h"
  45. #define MODULE_NAME _ARC2
  46. #define BLOCK_SIZE 8
  47. #define KEY_SIZE 0
  48. #define PCT_ARC2_MODULE /* Defined to get ARC2's additional keyword arguments */
  49. typedef unsigned int U32;
  50. typedef unsigned short U16;
  51. typedef unsigned char U8;
  52. typedef struct
  53. {
  54. U16 xkey[64];
  55. int effective_keylen;
  56. } block_state;
  57. static void
  58. block_encrypt(block_state *self, U8 *in, U8 *out)
  59. {
  60. U16 x76, x54, x32, x10;
  61. int i;
  62. x76 = (in[7] << 8) + in[6];
  63. x54 = (in[5] << 8) + in[4];
  64. x32 = (in[3] << 8) + in[2];
  65. x10 = (in[1] << 8) + in[0];
  66. for (i = 0; i < 16; i++)
  67. {
  68. x10 += (x32 & ~x76) + (x54 & x76) + self->xkey[4*i+0];
  69. x10 = (x10 << 1) + (x10 >> 15 & 1);
  70. x32 += (x54 & ~x10) + (x76 & x10) + self->xkey[4*i+1];
  71. x32 = (x32 << 2) + (x32 >> 14 & 3);
  72. x54 += (x76 & ~x32) + (x10 & x32) + self->xkey[4*i+2];
  73. x54 = (x54 << 3) + (x54 >> 13 & 7);
  74. x76 += (x10 & ~x54) + (x32 & x54) + self->xkey[4*i+3];
  75. x76 = (x76 << 5) + (x76 >> 11 & 31);
  76. if (i == 4 || i == 10) {
  77. x10 += self->xkey[x76 & 63];
  78. x32 += self->xkey[x10 & 63];
  79. x54 += self->xkey[x32 & 63];
  80. x76 += self->xkey[x54 & 63];
  81. }
  82. }
  83. out[0] = (U8)x10;
  84. out[1] = (U8)(x10 >> 8);
  85. out[2] = (U8)x32;
  86. out[3] = (U8)(x32 >> 8);
  87. out[4] = (U8)x54;
  88. out[5] = (U8)(x54 >> 8);
  89. out[6] = (U8)x76;
  90. out[7] = (U8)(x76 >> 8);
  91. }
  92. static void
  93. block_decrypt(block_state *self, U8 *in, U8 *out)
  94. {
  95. U16 x76, x54, x32, x10;
  96. int i;
  97. x76 = (in[7] << 8) + in[6];
  98. x54 = (in[5] << 8) + in[4];
  99. x32 = (in[3] << 8) + in[2];
  100. x10 = (in[1] << 8) + in[0];
  101. i = 15;
  102. do {
  103. x76 &= 65535;
  104. x76 = (x76 << 11) + (x76 >> 5);
  105. x76 -= (x10 & ~x54) + (x32 & x54) + self->xkey[4*i+3];
  106. x54 &= 65535;
  107. x54 = (x54 << 13) + (x54 >> 3);
  108. x54 -= (x76 & ~x32) + (x10 & x32) + self->xkey[4*i+2];
  109. x32 &= 65535;
  110. x32 = (x32 << 14) + (x32 >> 2);
  111. x32 -= (x54 & ~x10) + (x76 & x10) + self->xkey[4*i+1];
  112. x10 &= 65535;
  113. x10 = (x10 << 15) + (x10 >> 1);
  114. x10 -= (x32 & ~x76) + (x54 & x76) + self->xkey[4*i+0];
  115. if (i == 5 || i == 11) {
  116. x76 -= self->xkey[x54 & 63];
  117. x54 -= self->xkey[x32 & 63];
  118. x32 -= self->xkey[x10 & 63];
  119. x10 -= self->xkey[x76 & 63];
  120. }
  121. } while (i--);
  122. out[0] = (U8)x10;
  123. out[1] = (U8)(x10 >> 8);
  124. out[2] = (U8)x32;
  125. out[3] = (U8)(x32 >> 8);
  126. out[4] = (U8)x54;
  127. out[5] = (U8)(x54 >> 8);
  128. out[6] = (U8)x76;
  129. out[7] = (U8)(x76 >> 8);
  130. }
  131. static void
  132. block_init(block_state *self, U8 *key, int keylength)
  133. {
  134. U8 x;
  135. U16 i;
  136. /* 256-entry permutation table, probably derived somehow from pi */
  137. static const U8 permute[256] = {
  138. 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
  139. 198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
  140. 23,154, 89,245,135,179, 79, 19, 97, 69,109,141, 9,129,125, 50,
  141. 189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,
  142. 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,
  143. 18,117,202, 31, 59,190,228,209, 66, 61,212, 48,163, 60,182, 38,
  144. 111,191, 14,218, 70,105, 7, 87, 39,242, 29,155,188,148, 67, 3,
  145. 248, 17,199,246,144,239, 62,231, 6,195,213, 47,200,102, 30,215,
  146. 8,232,234,222,128, 82,238,247,132,170,114,172, 53, 77,106, 42,
  147. 150, 26,210,113, 90, 21, 73,116, 75,159,208, 94, 4, 24,164,236,
  148. 194,224, 65,110, 15, 81,203,204, 36,145,175, 80,161,244,112, 57,
  149. 153,124, 58,133, 35,184,180,122,252, 2, 54, 91, 37, 85,151, 49,
  150. 45, 93,250,152,227,138,146,174, 5,223, 41, 16,103,108,186,201,
  151. 211, 0,230,207,225,158,168, 44, 99, 22, 1, 63, 88,226,137,169,
  152. 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,
  153. 197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173
  154. };
  155. if ((U32)keylength > sizeof(self->xkey)) {
  156. PyErr_SetString(PyExc_ValueError,
  157. "ARC2 key length must be less than 128 bytes");
  158. return;
  159. }
  160. memcpy(self->xkey, key, keylength);
  161. /* Phase 1: Expand input key to 128 bytes */
  162. if (keylength < 128) {
  163. i = 0;
  164. x = ((U8 *)self->xkey)[keylength-1];
  165. do {
  166. x = permute[(x + ((U8 *)self->xkey)[i++]) & 255];
  167. ((U8 *)self->xkey)[keylength++] = x;
  168. } while (keylength < 128);
  169. }
  170. /* Phase 2 - reduce effective key size to "effective_keylen" */
  171. keylength = (self->effective_keylen+7) >> 3;
  172. i = 128-keylength;
  173. x = permute[((U8 *)self->xkey)[i] & (255 >>
  174. (7 &
  175. ((self->effective_keylen %8 ) ? 8-(self->effective_keylen%8): 0))
  176. )];
  177. ((U8 *)self->xkey)[i] = x;
  178. while (i--) {
  179. x = permute[ x ^ ((U8 *)self->xkey)[i+keylength] ];
  180. ((U8 *)self->xkey)[i] = x;
  181. }
  182. /* Phase 3 - copy to self->xkey in little-endian order */
  183. i = 63;
  184. do {
  185. self->xkey[i] = ((U8 *)self->xkey)[2*i] +
  186. (((U8 *)self->xkey)[2*i+1] << 8);
  187. } while (i--);
  188. }
  189. #include "block_template.c"