]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rc4/rc4_enc.c
Copyright consolidation 04/10
[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 *
62867571
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
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>
58964a49 11#include "rc4_locl.h"
d02b48c6 12
c80fd6b2
MC
13/*-
14 * RC4 as implemented from a posting from
d02b48c6
RE
15 * Newsgroups: sci.crypt
16 * From: sterndark@netcom.com (David Sterndark)
17 * Subject: RC4 Algorithm revealed.
18 * Message-ID: <sternCvKL4B.Hyy@netcom.com>
19 * Date: Wed, 14 Sep 1994 06:35:31 GMT
20 */
21
f768be81 22void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
0f113f3e
MC
23 unsigned char *outdata)
24{
25 register RC4_INT *d;
26 register RC4_INT x, y, tx, ty;
27 size_t i;
28
29 x = key->x;
30 y = key->y;
31 d = key->data;
d02b48c6
RE
32
33#define LOOP(in,out) \
0f113f3e
MC
34 x=((x+1)&0xff); \
35 tx=d[x]; \
36 y=(tx+y)&0xff; \
37 d[x]=ty=d[y]; \
38 d[y]=tx; \
39 (out) = d[(tx+ty)&0xff]^ (in);
d02b48c6 40
0f113f3e
MC
41 i = len >> 3;
42 if (i) {
43 for (;;) {
3e9e810f
RS
44 LOOP(indata[0], outdata[0]);
45 LOOP(indata[1], outdata[1]);
46 LOOP(indata[2], outdata[2]);
47 LOOP(indata[3], outdata[3]);
48 LOOP(indata[4], outdata[4]);
49 LOOP(indata[5], outdata[5]);
50 LOOP(indata[6], outdata[6]);
51 LOOP(indata[7], outdata[7]);
0f113f3e
MC
52 indata += 8;
53 outdata += 8;
0f113f3e
MC
54 if (--i == 0)
55 break;
56 }
57 }
58 i = len & 0x07;
59 if (i) {
60 for (;;) {
3e9e810f 61 LOOP(indata[0], outdata[0]);
0f113f3e
MC
62 if (--i == 0)
63 break;
3e9e810f 64 LOOP(indata[1], outdata[1]);
0f113f3e
MC
65 if (--i == 0)
66 break;
3e9e810f 67 LOOP(indata[2], outdata[2]);
0f113f3e
MC
68 if (--i == 0)
69 break;
3e9e810f 70 LOOP(indata[3], outdata[3]);
0f113f3e
MC
71 if (--i == 0)
72 break;
3e9e810f 73 LOOP(indata[4], outdata[4]);
0f113f3e
MC
74 if (--i == 0)
75 break;
3e9e810f 76 LOOP(indata[5], outdata[5]);
0f113f3e
MC
77 if (--i == 0)
78 break;
3e9e810f 79 LOOP(indata[6], outdata[6]);
0f113f3e
MC
80 if (--i == 0)
81 break;
82 }
83 }
84 key->x = x;
85 key->y = y;
86}