/src/mupdf/source/fitz/crypt-arc4.c
Line | Count | Source |
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 | | |
24 | | #include "mupdf/fitz.h" |
25 | | #include <string.h> |
26 | | |
27 | | void |
28 | | fz_arc4_init(fz_arc4 *arc4, const unsigned char *key, size_t keylen) |
29 | 0 | { |
30 | 0 | unsigned int t, u; |
31 | 0 | size_t keyindex; |
32 | 0 | unsigned int stateindex; |
33 | 0 | unsigned char *state; |
34 | 0 | unsigned int counter; |
35 | |
|
36 | 0 | state = arc4->state; |
37 | |
|
38 | 0 | arc4->x = 0; |
39 | 0 | arc4->y = 0; |
40 | |
|
41 | 0 | for (counter = 0; counter < 256; counter++) |
42 | 0 | { |
43 | 0 | state[counter] = counter; |
44 | 0 | } |
45 | |
|
46 | 0 | keyindex = 0; |
47 | 0 | stateindex = 0; |
48 | |
|
49 | 0 | for (counter = 0; counter < 256; counter++) |
50 | 0 | { |
51 | 0 | t = state[counter]; |
52 | 0 | stateindex = (stateindex + key[keyindex] + t) & 0xff; |
53 | 0 | u = state[stateindex]; |
54 | |
|
55 | 0 | state[stateindex] = t; |
56 | 0 | state[counter] = u; |
57 | |
|
58 | 0 | if (++keyindex >= keylen) |
59 | 0 | { |
60 | 0 | keyindex = 0; |
61 | 0 | } |
62 | 0 | } |
63 | 0 | } |
64 | | |
65 | | static unsigned char |
66 | | fz_arc4_next(fz_arc4 *arc4) |
67 | 0 | { |
68 | 0 | unsigned int x; |
69 | 0 | unsigned int y; |
70 | 0 | unsigned int sx, sy; |
71 | 0 | unsigned char *state; |
72 | |
|
73 | 0 | state = arc4->state; |
74 | |
|
75 | 0 | x = (arc4->x + 1) & 0xff; |
76 | 0 | sx = state[x]; |
77 | 0 | y = (sx + arc4->y) & 0xff; |
78 | 0 | sy = state[y]; |
79 | |
|
80 | 0 | arc4->x = x; |
81 | 0 | arc4->y = y; |
82 | |
|
83 | 0 | state[y] = sx; |
84 | 0 | state[x] = sy; |
85 | |
|
86 | 0 | return state[(sx + sy) & 0xff]; |
87 | 0 | } |
88 | | |
89 | | void |
90 | | fz_arc4_encrypt(fz_arc4 *arc4, unsigned char *dest, const unsigned char *src, size_t len) |
91 | 0 | { |
92 | 0 | size_t i; |
93 | 0 | for (i = 0; i < len; i++) |
94 | 0 | { |
95 | 0 | unsigned char x; |
96 | 0 | x = fz_arc4_next(arc4); |
97 | 0 | dest[i] = src[i] ^ x; |
98 | 0 | } |
99 | 0 | } |
100 | | |
101 | | void fz_arc4_final(fz_arc4 *state) |
102 | 0 | { |
103 | 0 | memset(state, 0, sizeof(*state)); |
104 | 0 | } |