/src/libluksde/libfcrypto/libfcrypto_rc4_context.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * RC4 (de/en)crypt functions  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 2017-2024, Joachim Metz <joachim.metz@gmail.com>  | 
5  |  |  *  | 
6  |  |  * Refer to AUTHORS for acknowledgements.  | 
7  |  |  *  | 
8  |  |  * This program is free software: you can redistribute it and/or modify  | 
9  |  |  * it under the terms of the GNU Lesser General Public License as published by  | 
10  |  |  * the Free Software Foundation, either version 3 of the License, or  | 
11  |  |  * (at your option) any later version.  | 
12  |  |  *  | 
13  |  |  * This program is distributed in the hope that it will be useful,  | 
14  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
15  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  | 
16  |  |  * GNU General Public License for more details.  | 
17  |  |  *  | 
18  |  |  * You should have received a copy of the GNU Lesser General Public License  | 
19  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>.  | 
20  |  |  */  | 
21  |  |  | 
22  |  | #include <common.h>  | 
23  |  | #include <memory.h>  | 
24  |  | #include <types.h>  | 
25  |  |  | 
26  |  | #include "libfcrypto_definitions.h"  | 
27  |  | #include "libfcrypto_libcerror.h"  | 
28  |  | #include "libfcrypto_rc4_context.h"  | 
29  |  |  | 
30  |  | /* Creates a RC4 context  | 
31  |  |  * Make sure the value context is referencing, is set to NULL  | 
32  |  |  * Returns 1 if successful or -1 on error  | 
33  |  |  */  | 
34  |  | int libfcrypto_rc4_context_initialize(  | 
35  |  |      libfcrypto_rc4_context_t **context,  | 
36  |  |      libcerror_error_t **error )  | 
37  | 47  | { | 
38  | 47  |   libfcrypto_internal_rc4_context_t *internal_context = NULL;  | 
39  | 47  |   static char *function                               = "libfcrypto_rc4_context_initialize";  | 
40  |  |  | 
41  | 47  |   if( context == NULL )  | 
42  | 0  |   { | 
43  | 0  |     libcerror_error_set(  | 
44  | 0  |      error,  | 
45  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
46  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
47  | 0  |      "%s: invalid context.",  | 
48  | 0  |      function );  | 
49  |  | 
  | 
50  | 0  |     return( -1 );  | 
51  | 0  |   }  | 
52  | 47  |   if( *context != NULL )  | 
53  | 0  |   { | 
54  | 0  |     libcerror_error_set(  | 
55  | 0  |      error,  | 
56  | 0  |      LIBCERROR_ERROR_DOMAIN_RUNTIME,  | 
57  | 0  |      LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,  | 
58  | 0  |      "%s: invalid context value already set.",  | 
59  | 0  |      function );  | 
60  |  | 
  | 
61  | 0  |     return( -1 );  | 
62  | 0  |   }  | 
63  | 47  |   internal_context = memory_allocate_structure(  | 
64  | 47  |                       libfcrypto_internal_rc4_context_t );  | 
65  |  |  | 
66  | 47  |   if( internal_context == NULL )  | 
67  | 0  |   { | 
68  | 0  |     libcerror_error_set(  | 
69  | 0  |      error,  | 
70  | 0  |      LIBCERROR_ERROR_DOMAIN_MEMORY,  | 
71  | 0  |      LIBCERROR_MEMORY_ERROR_INSUFFICIENT,  | 
72  | 0  |      "%s: unable to create context.",  | 
73  | 0  |      function );  | 
74  |  | 
  | 
75  | 0  |     goto on_error;  | 
76  | 0  |   }  | 
77  | 47  |   if( memory_set(  | 
78  | 47  |        internal_context,  | 
79  | 47  |        0,  | 
80  | 47  |        sizeof( libfcrypto_internal_rc4_context_t ) ) == NULL )  | 
81  | 0  |   { | 
82  | 0  |     libcerror_error_set(  | 
83  | 0  |      error,  | 
84  | 0  |      LIBCERROR_ERROR_DOMAIN_MEMORY,  | 
85  | 0  |      LIBCERROR_MEMORY_ERROR_SET_FAILED,  | 
86  | 0  |      "%s: unable to clear context.",  | 
87  | 0  |      function );  | 
88  |  | 
  | 
89  | 0  |     goto on_error;  | 
90  | 0  |   }  | 
91  | 47  |   *context = (libfcrypto_rc4_context_t *) internal_context;  | 
92  |  |  | 
93  | 47  |   return( 1 );  | 
94  |  |  | 
95  | 0  | on_error:  | 
96  | 0  |   if( internal_context != NULL )  | 
97  | 0  |   { | 
98  | 0  |     memory_free(  | 
99  | 0  |      internal_context );  | 
100  | 0  |   }  | 
101  | 0  |   return( -1 );  | 
102  | 47  | }  | 
103  |  |  | 
104  |  | /* Frees a RC4 context  | 
105  |  |  * Returns 1 if successful or -1 on error  | 
106  |  |  */  | 
107  |  | int libfcrypto_rc4_context_free(  | 
108  |  |      libfcrypto_rc4_context_t **context,  | 
109  |  |      libcerror_error_t **error )  | 
110  | 47  | { | 
111  | 47  |   libfcrypto_internal_rc4_context_t *internal_context = NULL;  | 
112  | 47  |   static char *function                               = "libfcrypto_rc4_context_free";  | 
113  | 47  |   int result                                          = 1;  | 
114  |  |  | 
115  | 47  |   if( context == NULL )  | 
116  | 0  |   { | 
117  | 0  |     libcerror_error_set(  | 
118  | 0  |      error,  | 
119  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
120  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
121  | 0  |      "%s: invalid context.",  | 
122  | 0  |      function );  | 
123  |  | 
  | 
124  | 0  |     return( -1 );  | 
125  | 0  |   }  | 
126  | 47  |   if( *context != NULL )  | 
127  | 47  |   { | 
128  | 47  |     internal_context = (libfcrypto_internal_rc4_context_t *) *context;  | 
129  | 47  |     *context         = NULL;  | 
130  |  |  | 
131  | 47  |     if( memory_set(  | 
132  | 47  |          internal_context,  | 
133  | 47  |          0,  | 
134  | 47  |          sizeof( libfcrypto_internal_rc4_context_t ) ) == NULL )  | 
135  | 0  |     { | 
136  | 0  |       libcerror_error_set(  | 
137  | 0  |        error,  | 
138  | 0  |        LIBCERROR_ERROR_DOMAIN_MEMORY,  | 
139  | 0  |        LIBCERROR_MEMORY_ERROR_SET_FAILED,  | 
140  | 0  |        "%s: unable to clear context.",  | 
141  | 0  |        function );  | 
142  |  | 
  | 
143  | 0  |       result = -1;  | 
144  | 0  |     }  | 
145  | 47  |     memory_free(  | 
146  | 47  |      internal_context );  | 
147  | 47  |   }  | 
148  | 47  |   return( result );  | 
149  | 47  | }  | 
150  |  |  | 
151  |  | /* Sets the key  | 
152  |  |  * Returns 1 if successful or -1 on error  | 
153  |  |  */  | 
154  |  | int libfcrypto_rc4_context_set_key(  | 
155  |  |      libfcrypto_rc4_context_t *context,  | 
156  |  |      const uint8_t *key,  | 
157  |  |      size_t key_bit_size,  | 
158  |  |      libcerror_error_t **error )  | 
159  | 47  | { | 
160  | 47  |   libfcrypto_internal_rc4_context_t *internal_context = NULL;  | 
161  | 47  |   static char *function                               = "libfcrypto_rc4_context_set_key";  | 
162  | 47  |   size_t key_byte_index                               = 0;  | 
163  | 47  |   size_t key_byte_size                                = 0;  | 
164  | 47  |   uint16_t byte_value                                 = 0;  | 
165  | 47  |   uint8_t permutation_value                           = 0;  | 
166  | 47  |   uint8_t values_index                                = 0;  | 
167  |  |  | 
168  | 47  |   if( context == NULL )  | 
169  | 0  |   { | 
170  | 0  |     libcerror_error_set(  | 
171  | 0  |      error,  | 
172  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
173  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
174  | 0  |      "%s: invalid context.",  | 
175  | 0  |      function );  | 
176  |  | 
  | 
177  | 0  |     return( -1 );  | 
178  | 0  |   }  | 
179  | 47  |   internal_context = (libfcrypto_internal_rc4_context_t *) context;  | 
180  |  |  | 
181  | 47  |   if( key == NULL )  | 
182  | 0  |   { | 
183  | 0  |     libcerror_error_set(  | 
184  | 0  |      error,  | 
185  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
186  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
187  | 0  |      "%s: invalid key.",  | 
188  | 0  |      function );  | 
189  |  | 
  | 
190  | 0  |     return( -1 );  | 
191  | 0  |   }  | 
192  | 47  |   if( ( key_bit_size < 40 )  | 
193  | 47  |    || ( key_bit_size > 2048 )  | 
194  | 47  |    || ( ( key_bit_size % 8 ) != 0 ) )  | 
195  | 0  |   { | 
196  | 0  |     libcerror_error_set(  | 
197  | 0  |      error,  | 
198  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
199  | 0  |      LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,  | 
200  | 0  |      "%s: unsupported key bit size.",  | 
201  | 0  |      function );  | 
202  |  | 
  | 
203  | 0  |     return( -1 );  | 
204  | 0  |   }  | 
205  | 47  |   key_byte_size = key_bit_size / 8;  | 
206  |  |  | 
207  |  |   /* Also referred to as: Key Scheduling Algorithm (KSA)  | 
208  |  |    */  | 
209  | 47  |   for( byte_value = 0;  | 
210  | 12.0k  |        byte_value < 256;  | 
211  | 12.0k  |        byte_value++ )  | 
212  | 12.0k  |   { | 
213  | 12.0k  |     internal_context->permutations[ byte_value ] = (uint8_t) byte_value;  | 
214  | 12.0k  |   }  | 
215  | 47  |   for( byte_value = 0;  | 
216  | 12.0k  |        byte_value < 256;  | 
217  | 12.0k  |        byte_value++ )  | 
218  | 12.0k  |   { | 
219  | 12.0k  |     key_byte_index = byte_value % key_byte_size;  | 
220  |  |  | 
221  |  |     /* Note that the following operations are modulus 256  | 
222  |  |      */  | 
223  | 12.0k  |     values_index = ( values_index + internal_context->permutations[ byte_value ] + key[ key_byte_index ] ) & 0xff;  | 
224  |  |  | 
225  | 12.0k  |     permutation_value = internal_context->permutations[ byte_value ];  | 
226  | 12.0k  |     internal_context->permutations[ byte_value ] = internal_context->permutations[ values_index ];  | 
227  | 12.0k  |     internal_context->permutations[ values_index ] = permutation_value;  | 
228  | 12.0k  |   }  | 
229  | 47  |   internal_context->index[ 0 ] = 0;  | 
230  | 47  |   internal_context->index[ 1 ] = 0;  | 
231  |  |  | 
232  | 47  |   return( 1 );  | 
233  | 47  | }  | 
234  |  |  | 
235  |  | /* De- or encrypts a buffer of data using RC4  | 
236  |  |  * Returns 1 if successful or -1 on error  | 
237  |  |  */  | 
238  |  | int libfcrypto_rc4_crypt(  | 
239  |  |      libfcrypto_rc4_context_t *context,  | 
240  |  |      const uint8_t *input_data,  | 
241  |  |      size_t input_data_size,  | 
242  |  |      uint8_t *output_data,  | 
243  |  |      size_t output_data_size,  | 
244  |  |      libcerror_error_t **error )  | 
245  | 47  | { | 
246  | 47  |   libfcrypto_internal_rc4_context_t *internal_context = NULL;  | 
247  | 47  |   static char *function                               = "libfcrypto_rc4_crypt";  | 
248  | 47  |   size_t data_offset                                  = 0;  | 
249  | 47  |   uint8_t permutation_value                           = 0;  | 
250  | 47  |   uint8_t values_index1                               = 0;  | 
251  | 47  |   uint8_t values_index2                               = 0;  | 
252  |  |  | 
253  | 47  |   if( context == NULL )  | 
254  | 0  |   { | 
255  | 0  |     libcerror_error_set(  | 
256  | 0  |      error,  | 
257  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
258  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
259  | 0  |      "%s: invalid context.",  | 
260  | 0  |      function );  | 
261  |  | 
  | 
262  | 0  |     return( -1 );  | 
263  | 0  |   }  | 
264  | 47  |   internal_context = (libfcrypto_internal_rc4_context_t *) context;  | 
265  |  |  | 
266  | 47  |   if( input_data == NULL )  | 
267  | 0  |   { | 
268  | 0  |     libcerror_error_set(  | 
269  | 0  |      error,  | 
270  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
271  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
272  | 0  |      "%s: invalid input data.",  | 
273  | 0  |      function );  | 
274  |  | 
  | 
275  | 0  |     return( -1 );  | 
276  | 0  |   }  | 
277  | 47  |   if( input_data_size > (size_t) SSIZE_MAX )  | 
278  | 0  |   { | 
279  | 0  |     libcerror_error_set(  | 
280  | 0  |      error,  | 
281  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
282  | 0  |      LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,  | 
283  | 0  |      "%s: invalid input data size value exceeds maximum.",  | 
284  | 0  |      function );  | 
285  |  | 
  | 
286  | 0  |     return( -1 );  | 
287  | 0  |   }  | 
288  | 47  |   if( output_data == NULL )  | 
289  | 0  |   { | 
290  | 0  |     libcerror_error_set(  | 
291  | 0  |      error,  | 
292  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
293  | 0  |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,  | 
294  | 0  |      "%s: invalid output data.",  | 
295  | 0  |      function );  | 
296  |  | 
  | 
297  | 0  |     return( -1 );  | 
298  | 0  |   }  | 
299  | 47  |   if( output_data_size > (size_t) SSIZE_MAX )  | 
300  | 0  |   { | 
301  | 0  |     libcerror_error_set(  | 
302  | 0  |      error,  | 
303  | 0  |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS,  | 
304  | 0  |      LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,  | 
305  | 0  |      "%s: invalid output data size value exceeds maximum.",  | 
306  | 0  |      function );  | 
307  |  | 
  | 
308  | 0  |     return( -1 );  | 
309  | 0  |   }  | 
310  | 47  |   if( output_data_size < input_data_size )  | 
311  | 31  |   { | 
312  | 31  |     libcerror_error_set(  | 
313  | 31  |      error,  | 
314  | 31  |      LIBCERROR_ERROR_DOMAIN_RUNTIME,  | 
315  | 31  |      LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,  | 
316  | 31  |      "%s: invalid ouput data size smaller than input data size.",  | 
317  | 31  |      function );  | 
318  |  |  | 
319  | 31  |     return( -1 );  | 
320  | 31  |   }  | 
321  |  |   /* Also referred to as: Pseudo-Random Generator Algorithm (PRGA)  | 
322  |  |    */  | 
323  | 16  |   values_index1 = internal_context->index[ 0 ];  | 
324  | 16  |   values_index2 = internal_context->index[ 1 ];  | 
325  |  |  | 
326  | 437  |   while( data_offset < input_data_size )  | 
327  | 421  |   { | 
328  |  |     /* Note that the following operations are modulus 256  | 
329  |  |      */  | 
330  | 421  |     values_index1 = ( values_index1 + 1 ) & 0xff;  | 
331  | 421  |     values_index2 = ( values_index2 + internal_context->permutations[ values_index1 ] ) & 0xff;  | 
332  |  |  | 
333  | 421  |     permutation_value                               = internal_context->permutations[ values_index1 ];  | 
334  | 421  |     internal_context->permutations[ values_index1 ] = internal_context->permutations[ values_index2 ];  | 
335  | 421  |     internal_context->permutations[ values_index2 ] = permutation_value;  | 
336  |  |  | 
337  | 421  |     permutation_value = ( permutation_value + internal_context->permutations[ values_index1 ] ) & 0xff;  | 
338  |  |  | 
339  | 421  |     output_data[ data_offset ] = input_data[ data_offset ] ^ internal_context->permutations[ permutation_value ];  | 
340  |  |  | 
341  | 421  |     data_offset++;  | 
342  | 421  |   }  | 
343  | 16  |   internal_context->index[ 0 ] = values_index1;  | 
344  | 16  |   internal_context->index[ 1 ] = values_index2;  | 
345  |  |  | 
346  | 16  |   return( 1 );  | 
347  | 47  | }  | 
348  |  |  |