rc4.c
Go to the documentation of this file.
1 /**
2  * @file rc4.c
3  * @brief RC4 encryption algorithm
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCrypto Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.6
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "cipher/rc4.h"
37 
38 //Check crypto library configuration
39 #if (RC4_SUPPORT == ENABLED)
40 
41 //Common interface for encryption algorithms
43 {
44  "RC4",
45  sizeof(Rc4Context),
47  0,
51  NULL,
52  NULL
53 };
54 
55 
56 /**
57  * @brief Initialize an RC4 context using the supplied key
58  * @param[in] context Pointer to the RC4 context to initialize
59  * @param[in] key Pointer to the key
60  * @param[in] length Length of the key
61  * @return Error code
62  **/
63 
64 error_t rc4Init(Rc4Context *context, const uint8_t *key, size_t length)
65 {
66  uint_t i;
67  uint_t j;
68  uint8_t temp;
69 
70  //Check parameters
71  if(context == NULL || key == NULL)
73 
74  //Clear context
75  context->i = 0;
76  context->j = 0;
77 
78  //Initialize the S array with identity permutation
79  for(i = 0; i < 256; i++)
80  {
81  context->s[i] = i;
82  }
83 
84  //S is then processed for 256 iterations
85  for(i = 0, j = 0; i < 256; i++)
86  {
87  //Randomize the permutations using the supplied key
88  j = (j + context->s[i] + key[i % length]) % 256;
89 
90  //Swap the values of S[i] and S[j]
91  temp = context->s[i];
92  context->s[i] = context->s[j];
93  context->s[j] = temp;
94  }
95 
96  //RC4 context successfully initialized
97  return NO_ERROR;
98 }
99 
100 
101 /**
102  * @brief Encrypt/decrypt data with the RC4 algorithm
103  * @param[in] context Pointer to the RC4 context
104  * @param[in] input Pointer to the data to encrypt/decrypt
105  * @param[in] output Pointer to the resulting data
106  * @param[in] length Length of the input data
107  **/
108 
109 void rc4Cipher(Rc4Context *context, const uint8_t *input, uint8_t *output, size_t length)
110 {
111  uint8_t temp;
112 
113  //Restore context
114  uint_t i = context->i;
115  uint_t j = context->j;
116  uint8_t *s = context->s;
117 
118  //Encryption loop
119  while(length > 0)
120  {
121  //Adjust indices
122  i = (i + 1) % 256;
123  j = (j + s[i]) % 256;
124 
125  //Swap the values of S[i] and S[j]
126  temp = s[i];
127  s[i] = s[j];
128  s[j] = temp;
129 
130  //Valid input and output?
131  if(input != NULL && output != NULL)
132  {
133  //XOR the input data with the RC4 stream
134  *output = *input ^ s[(s[i] + s[j]) % 256];
135 
136  //Increment data pointers
137  input++;
138  output++;
139  }
140 
141  //Remaining bytes to process
142  length--;
143  }
144 
145  //Save context
146  context->i = i;
147  context->j = j;
148 }
149 
150 #endif
uint8_t length
Definition: dtls_misc.h:149
void(* CipherAlgoDecryptStream)(void *context, const uint8_t *input, uint8_t *output, size_t length)
Definition: crypto.h:1102
uint_t j
Definition: rc4.h:53
error_t rc4Init(Rc4Context *context, const uint8_t *key, size_t length)
Initialize an RC4 context using the supplied key.
Definition: rc4.c:64
RC4 algorithm context.
Definition: rc4.h:50
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:42
void(* CipherAlgoEncryptStream)(void *context, const uint8_t *input, uint8_t *output, size_t length)
Definition: crypto.h:1101
General definitions for cryptographic algorithms.
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:1100
void rc4Cipher(Rc4Context *context, const uint8_t *input, uint8_t *output, size_t length)
Encrypt/decrypt data with the RC4 algorithm.
Definition: rc4.c:109
uint8_t s
Common interface for encryption algorithms.
Definition: crypto.h:1150
uint8_t s[256]
Definition: rc4.h:54
@ CIPHER_ALGO_TYPE_STREAM
Definition: crypto.h:1068
uint_t i
Definition: rc4.h:52
const CipherAlgo rc4CipherAlgo
Definition: rc4.c:42
unsigned int uint_t
Definition: compiler_port.h:45
RC4 encryption algorithm.
@ NO_ERROR
Success.
Definition: error.h:44