]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rc4/rc4_enc.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / rc4 / rc4_enc.c
CommitLineData
62867571
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
5e4435a7 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
d02b48c6
RE
8 */
9
ec577822 10#include <openssl/rc4.h>
706457b7 11#include "rc4_local.h"
d02b48c6 12
c80fd6b2
MC
13/*-
14 * RC4 as implemented from a posting from
d02b48c6 15 * Newsgroups: sci.crypt
d02b48c6
RE
16 * Subject: RC4 Algorithm revealed.
17 * Message-ID: <sternCvKL4B.Hyy@netcom.com>
18 * Date: Wed, 14 Sep 1994 06:35:31 GMT
19 */
20
f768be81 21void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
0f113f3e
MC
22 unsigned char *outdata)
23{
24 register RC4_INT *d;
25 register RC4_INT x, y, tx, ty;
26 size_t i;
27
28 x = key->x;
29 y = key->y;
30 d = key->data;
d02b48c6
RE
31
32#define LOOP(in,out) \
0f113f3e
MC
33 x=((x+1)&0xff); \
34 tx=d[x]; \
35 y=(tx+y)&0xff; \
36 d[x]=ty=d[y]; \
37 d[y]=tx; \
38 (out) = d[(tx+ty)&0xff]^ (in);
d02b48c6 39
0f113f3e
MC
40 i = len >> 3;
41 if (i) {
42 for (;;) {
3e9e810f
RS
43 LOOP(indata[0], outdata[0]);
44 LOOP(indata[1], outdata[1]);
45 LOOP(indata[2], outdata[2]);
46 LOOP(indata[3], outdata[3]);
47 LOOP(indata[4], outdata[4]);
48 LOOP(indata[5], outdata[5]);
49 LOOP(indata[6], outdata[6]);
50 LOOP(indata[7], outdata[7]);
0f113f3e
MC
51 indata += 8;
52 outdata += 8;
0f113f3e
MC
53 if (--i == 0)
54 break;
55 }
56 }
57 i = len & 0x07;
58 if (i) {
59 for (;;) {
3e9e810f 60 LOOP(indata[0], outdata[0]);
0f113f3e
MC
61 if (--i == 0)
62 break;
3e9e810f 63 LOOP(indata[1], outdata[1]);
0f113f3e
MC
64 if (--i == 0)
65 break;
3e9e810f 66 LOOP(indata[2], outdata[2]);
0f113f3e
MC
67 if (--i == 0)
68 break;
3e9e810f 69 LOOP(indata[3], outdata[3]);
0f113f3e
MC
70 if (--i == 0)
71 break;
3e9e810f 72 LOOP(indata[4], outdata[4]);
0f113f3e
MC
73 if (--i == 0)
74 break;
3e9e810f 75 LOOP(indata[5], outdata[5]);
0f113f3e
MC
76 if (--i == 0)
77 break;
3e9e810f 78 LOOP(indata[6], outdata[6]);
0f113f3e
MC
79 if (--i == 0)
80 break;
81 }
82 }
83 key->x = x;
84 key->y = y;
85}