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-2024 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 2.4.4
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,
54 };
55 
56 
57 /**
58  * @brief Initialize an RC4 context using the supplied key
59  * @param[in] context Pointer to the RC4 context to initialize
60  * @param[in] key Pointer to the key
61  * @param[in] length Length of the key
62  * @return Error code
63  **/
64 
65 error_t rc4Init(Rc4Context *context, const uint8_t *key, size_t length)
66 {
67  uint_t i;
68  uint_t j;
69  uint8_t temp;
70 
71  //Check parameters
72  if(context == NULL || key == NULL)
74 
75  //Clear context
76  context->i = 0;
77  context->j = 0;
78 
79  //Initialize the S array with identity permutation
80  for(i = 0; i < 256; i++)
81  {
82  context->s[i] = i;
83  }
84 
85  //S is then processed for 256 iterations
86  for(i = 0, j = 0; i < 256; i++)
87  {
88  //Randomize the permutations using the supplied key
89  j = (j + context->s[i] + key[i % length]) % 256;
90 
91  //Swap the values of S[i] and S[j]
92  temp = context->s[i];
93  context->s[i] = context->s[j];
94  context->s[j] = temp;
95  }
96 
97  //RC4 context successfully initialized
98  return NO_ERROR;
99 }
100 
101 
102 /**
103  * @brief Encrypt/decrypt data with the RC4 algorithm
104  * @param[in] context Pointer to the RC4 context
105  * @param[in] input Pointer to the data to encrypt/decrypt (optional)
106  * @param[in] output Pointer to the resulting data (optional)
107  * @param[in] length Length of the input data
108  **/
109 
110 void rc4Cipher(Rc4Context *context, const uint8_t *input, uint8_t *output,
111  size_t length)
112 {
113  uint8_t temp;
114 
115  //Restore context
116  uint_t i = context->i;
117  uint_t j = context->j;
118  uint8_t *s = context->s;
119 
120  //Encryption loop
121  while(length > 0)
122  {
123  //Adjust indices
124  i = (i + 1) % 256;
125  j = (j + s[i]) % 256;
126 
127  //Swap the values of S[i] and S[j]
128  temp = s[i];
129  s[i] = s[j];
130  s[j] = temp;
131 
132  //Valid input and output?
133  if(input != NULL && output != NULL)
134  {
135  //XOR the input data with the RC4 stream
136  *output = *input ^ s[(s[i] + s[j]) % 256];
137 
138  //Increment data pointers
139  input++;
140  output++;
141  }
142 
143  //Remaining bytes to process
144  length--;
145  }
146 
147  //Save context
148  context->i = i;
149  context->j = j;
150 }
151 
152 
153 /**
154  * @brief Release RC4 context
155  * @param[in] context Pointer to the RC4 context
156  **/
157 
158 void rc4Deinit(Rc4Context *context)
159 {
160  //Clear RC4 context
161  osMemset(context, 0, sizeof(Rc4Context));
162 }
163 
164 #endif
void(* CipherAlgoDecryptStream)(void *context, const uint8_t *input, uint8_t *output, size_t length)
Definition: crypto.h:1006
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:65
void rc4Deinit(Rc4Context *context)
Release RC4 context.
Definition: rc4.c:158
RC4 algorithm context.
Definition: rc4.h:51
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
void(* CipherAlgoEncryptStream)(void *context, const uint8_t *input, uint8_t *output, size_t length)
Definition: crypto.h:1003
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:368
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:1000
void rc4Cipher(Rc4Context *context, const uint8_t *input, uint8_t *output, size_t length)
Encrypt/decrypt data with the RC4 algorithm.
Definition: rc4.c:110
Common interface for encryption algorithms.
Definition: crypto.h:1068
uint8_t s[256]
Definition: rc4.h:54
uint8_t s
Definition: igmp_common.h:234
@ CIPHER_ALGO_TYPE_STREAM
Definition: crypto.h:952
uint_t i
Definition: rc4.h:52
const CipherAlgo rc4CipherAlgo
Definition: rc4.c:42
unsigned int uint_t
Definition: compiler_port.h:50
#define osMemset(p, value, length)
Definition: os_port.h:135
void(* CipherAlgoDeinit)(void *context)
Definition: crypto.h:1015
RC4 encryption algorithm.
@ NO_ERROR
Success.
Definition: error.h:44