ARC4.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * arc4.c : Implementation for the Alleged-RC4 stream cipher
  3. *
  4. * Part of the Python Cryptography Toolkit
  5. *
  6. * Originally written by: A.M. Kuchling
  7. *
  8. * ===================================================================
  9. * The contents of this file are dedicated to the public domain. To
  10. * the extent that dedication to the public domain is not available,
  11. * everyone is granted a worldwide, perpetual, royalty-free,
  12. * non-exclusive license to exercise all rights associated with the
  13. * contents of this file for any purpose whatsoever.
  14. * No rights are reserved.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. * ===================================================================
  25. *
  26. */
  27. #include "common.h"
  28. FAKE_INIT(ARC4)
  29. typedef struct
  30. {
  31. uint8_t state[256];
  32. uint8_t x,y;
  33. } stream_state;
  34. EXPORT_SYM int ARC4_stream_encrypt(stream_state *rc4State, const uint8_t in[], uint8_t out[], size_t len)
  35. {
  36. unsigned i;
  37. unsigned x=rc4State->x, y=rc4State->y;
  38. for (i=0; i<len; i++)
  39. {
  40. x = (x + 1) % 256;
  41. y = (y + rc4State->state[x]) % 256;
  42. {
  43. unsigned t; /* Exchange state[x] and state[y] */
  44. t = rc4State->state[x];
  45. rc4State->state[x] = rc4State->state[y];
  46. rc4State->state[y] = (uint8_t)t;
  47. }
  48. {
  49. unsigned xorIndex; /* XOR the data with the stream data */
  50. xorIndex=(rc4State->state[x]+rc4State->state[y]) % 256;
  51. out[i] = in[i] ^ rc4State->state[xorIndex];
  52. }
  53. }
  54. rc4State->x=(uint8_t)x;
  55. rc4State->y=(uint8_t)y;
  56. return 0;
  57. }
  58. EXPORT_SYM int ARC4_stream_init(uint8_t *key, size_t keylen, stream_state **pRc4State)
  59. {
  60. unsigned i;
  61. unsigned index1, index2;
  62. stream_state *rc4State;
  63. if (NULL == pRc4State || NULL == key)
  64. return ERR_NULL;
  65. *pRc4State = rc4State = calloc(1, sizeof(stream_state));
  66. if (NULL == rc4State)
  67. return ERR_MEMORY;
  68. for(i=0; i<256; i++)
  69. rc4State->state[i]=(uint8_t)i;
  70. rc4State->x=0;
  71. rc4State->y=0;
  72. index1=0;
  73. index2=0;
  74. for(i=0; i<256; i++)
  75. {
  76. unsigned t;
  77. index2 = ( (unsigned)key[index1] + rc4State->state[i] + index2) % 256;
  78. t = rc4State->state[i];
  79. rc4State->state[i] = rc4State->state[index2];
  80. rc4State->state[index2] = (uint8_t)t;
  81. index1 = (index1 + 1) % (unsigned)keylen;
  82. }
  83. return 0;
  84. }
  85. EXPORT_SYM int ARC4_stream_destroy(stream_state *rc4State)
  86. {
  87. free(rc4State);
  88. return 0;
  89. }