1 /**
2  * \file arc4.h
3  *
4  * \brief The ARCFOUR stream cipher
5  *
6  * \warning   ARC4 is considered a weak cipher and its use constitutes a
7  *            security risk. We recommend considering stronger ciphers instead.
8  */
9 /*
10  *  Copyright The Mbed TLS Contributors
11  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
12  *
13  *  This file is provided under the Apache License 2.0, or the
14  *  GNU General Public License v2.0 or later.
15  *
16  *  **********
17  *  Apache License 2.0:
18  *
19  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
20  *  not use this file except in compliance with the License.
21  *  You may obtain a copy of the License at
22  *
23  *  http://www.apache.org/licenses/LICENSE-2.0
24  *
25  *  Unless required by applicable law or agreed to in writing, software
26  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28  *  See the License for the specific language governing permissions and
29  *  limitations under the License.
30  *
31  *  **********
32  *
33  *  **********
34  *  GNU General Public License v2.0 or later:
35  *
36  *  This program is free software; you can redistribute it and/or modify
37  *  it under the terms of the GNU General Public License as published by
38  *  the Free Software Foundation; either version 2 of the License, or
39  *  (at your option) any later version.
40  *
41  *  This program is distributed in the hope that it will be useful,
42  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
43  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44  *  GNU General Public License for more details.
45  *
46  *  You should have received a copy of the GNU General Public License along
47  *  with this program; if not, write to the Free Software Foundation, Inc.,
48  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
49  *
50  *  **********
51  *
52  */
53 #ifndef MBEDTLS_ARC4_H
54 #define MBEDTLS_ARC4_H
55 
56 #if !defined(MBEDTLS_CONFIG_FILE)
57 #include "config.h"
58 #else
59 #include MBEDTLS_CONFIG_FILE
60 #endif
61 
62 #include <stddef.h>
63 
64 #define MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED                  -0x0019  /**< ARC4 hardware accelerator failed. */
65 
66 #if !defined(MBEDTLS_ARC4_ALT)
67 // Regular implementation
68 //
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
74 /**
75  * \brief     ARC4 context structure
76  *
77  * \warning   ARC4 is considered a weak cipher and its use constitutes a
78  *            security risk. We recommend considering stronger ciphers instead.
79  *
80  */
81 typedef struct
82 {
83     int x;                      /*!< permutation index */
84     int y;                      /*!< permutation index */
85     unsigned char m[256];       /*!< permutation table */
86 }
87 mbedtls_arc4_context;
88 
89 /**
90  * \brief          Initialize ARC4 context
91  *
92  * \param ctx      ARC4 context to be initialized
93  *
94  * \warning        ARC4 is considered a weak cipher and its use constitutes a
95  *                 security risk. We recommend considering stronger ciphers
96  *                 instead.
97  *
98  */
99 void mbedtls_arc4_init( mbedtls_arc4_context *ctx );
100 
101 /**
102  * \brief          Clear ARC4 context
103  *
104  * \param ctx      ARC4 context to be cleared
105  *
106  * \warning        ARC4 is considered a weak cipher and its use constitutes a
107  *                 security risk. We recommend considering stronger ciphers
108  *                 instead.
109  *
110  */
111 void mbedtls_arc4_free( mbedtls_arc4_context *ctx );
112 
113 /**
114  * \brief          ARC4 key schedule
115  *
116  * \param ctx      ARC4 context to be setup
117  * \param key      the secret key
118  * \param keylen   length of the key, in bytes
119  *
120  * \warning        ARC4 is considered a weak cipher and its use constitutes a
121  *                 security risk. We recommend considering stronger ciphers
122  *                 instead.
123  *
124  */
125 void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
126                  unsigned int keylen );
127 
128 /**
129  * \brief          ARC4 cipher function
130  *
131  * \param ctx      ARC4 context
132  * \param length   length of the input data
133  * \param input    buffer holding the input data
134  * \param output   buffer for the output data
135  *
136  * \return         0 if successful
137  *
138  * \warning        ARC4 is considered a weak cipher and its use constitutes a
139  *                 security risk. We recommend considering stronger ciphers
140  *                 instead.
141  *
142  */
143 int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
144                 unsigned char *output );
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #else  /* MBEDTLS_ARC4_ALT */
151 #include "arc4_alt.h"
152 #endif /* MBEDTLS_ARC4_ALT */
153 
154 #ifdef __cplusplus
155 extern "C" {
156 #endif
157 
158 /**
159  * \brief          Checkup routine
160  *
161  * \return         0 if successful, or 1 if the test failed
162  *
163  * \warning        ARC4 is considered a weak cipher and its use constitutes a
164  *                 security risk. We recommend considering stronger ciphers
165  *                 instead.
166  *
167  */
168 int mbedtls_arc4_self_test( int verbose );
169 
170 #ifdef __cplusplus
171 }
172 #endif
173 
174 #endif /* arc4.h */
175