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