crypt-arc4.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* This code illustrates a sample implementation
  2. * of the Arcfour algorithm
  3. * Copyright (c) April 29, 1997 Kalle Kaukonen.
  4. * All Rights Reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or
  7. * without modification, are permitted provided that this copyright
  8. * notice and disclaimer are retained.
  9. *
  10. * THIS SOFTWARE IS PROVIDED BY KALLE KAUKONEN AND CONTRIBUTORS ``AS
  11. * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  12. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  13. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KALLE
  14. * KAUKONEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  15. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  17. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  18. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  19. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include "mupdf/fitz.h"
  24. #include <string.h>
  25. void
  26. fz_arc4_init(fz_arc4 *arc4, const unsigned char *key, size_t keylen)
  27. {
  28. unsigned int t, u;
  29. size_t keyindex;
  30. unsigned int stateindex;
  31. unsigned char *state;
  32. unsigned int counter;
  33. state = arc4->state;
  34. arc4->x = 0;
  35. arc4->y = 0;
  36. for (counter = 0; counter < 256; counter++)
  37. {
  38. state[counter] = counter;
  39. }
  40. keyindex = 0;
  41. stateindex = 0;
  42. for (counter = 0; counter < 256; counter++)
  43. {
  44. t = state[counter];
  45. stateindex = (stateindex + key[keyindex] + t) & 0xff;
  46. u = state[stateindex];
  47. state[stateindex] = t;
  48. state[counter] = u;
  49. if (++keyindex >= keylen)
  50. {
  51. keyindex = 0;
  52. }
  53. }
  54. }
  55. static unsigned char
  56. fz_arc4_next(fz_arc4 *arc4)
  57. {
  58. unsigned int x;
  59. unsigned int y;
  60. unsigned int sx, sy;
  61. unsigned char *state;
  62. state = arc4->state;
  63. x = (arc4->x + 1) & 0xff;
  64. sx = state[x];
  65. y = (sx + arc4->y) & 0xff;
  66. sy = state[y];
  67. arc4->x = x;
  68. arc4->y = y;
  69. state[y] = sx;
  70. state[x] = sy;
  71. return state[(sx + sy) & 0xff];
  72. }
  73. void
  74. fz_arc4_encrypt(fz_arc4 *arc4, unsigned char *dest, const unsigned char *src, size_t len)
  75. {
  76. size_t i;
  77. for (i = 0; i < len; i++)
  78. {
  79. unsigned char x;
  80. x = fz_arc4_next(arc4);
  81. dest[i] = src[i] ^ x;
  82. }
  83. }
  84. void fz_arc4_final(fz_arc4 *state)
  85. {
  86. memset(state, 0, sizeof(*state));
  87. }