* Initialize an RC4 state buffer using the supplied key, * which can have arbitrary length. */
| 58 | * which can have arbitrary length. |
| 59 | */ |
| 60 | void |
| 61 | rc4_init(struct rc4_state *const state, const u_char *key, int keylen) |
| 62 | { |
| 63 | u_char j; |
| 64 | int i, k; |
| 65 | |
| 66 | /* Initialize state with identity permutation */ |
| 67 | for (i = 0; i < 256; i++) |
| 68 | state->perm[i] = (u_char)i; |
| 69 | state->index1 = 0; |
| 70 | state->index2 = 0; |
| 71 | |
| 72 | /* Randomize the permutation using key data */ |
| 73 | for (j = i = k = 0; i < 256; i++) { |
| 74 | j += state->perm[i] + key[k]; |
| 75 | swap_bytes(&state->perm[i], &state->perm[j]); |
| 76 | if (++k >= keylen) |
| 77 | k = 0; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Encrypt some data using the supplied RC4 state buffer. |
no test coverage detected