| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file arc4.h | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * | 
| Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief The ARCFOUR stream cipher | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 5 | * | 
|  | 6 | * \warning   ARC4 is considered a weak cipher and its use constitutes a | 
|  | 7 | *            security risk. We recommend considering stronger ciphers instead. | 
| Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 8 | */ | 
|  | 9 | /* | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 10 | *  Copyright The Mbed TLS Contributors | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 11 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 12 | * | 
|  | 13 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 14 | *  not use this file except in compliance with the License. | 
|  | 15 | *  You may obtain a copy of the License at | 
|  | 16 | * | 
|  | 17 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 18 | * | 
|  | 19 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 20 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 21 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 22 | *  See the License for the specific language governing permissions and | 
|  | 23 | *  limitations under the License. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 24 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #ifndef MBEDTLS_ARC4_H | 
|  | 27 | #define MBEDTLS_ARC4_H | 
| Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 28 | #include "mbedtls/private_access.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 31 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #endif | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 35 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 36 | #include <stddef.h> | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 37 |  | 
| Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 38 | #ifdef __cplusplus | 
|  | 39 | extern "C" { | 
|  | 40 | #endif | 
|  | 41 |  | 
| Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 42 | #if !defined(MBEDTLS_ARC4_ALT) | 
|  | 43 | // Regular implementation | 
|  | 44 | // | 
|  | 45 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 46 | /** | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 47 | * \brief     ARC4 context structure | 
|  | 48 | * | 
|  | 49 | * \warning   ARC4 is considered a weak cipher and its use constitutes a | 
|  | 50 | *            security risk. We recommend considering stronger ciphers instead. | 
|  | 51 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | */ | 
| Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 53 | typedef struct mbedtls_arc4_context | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 54 | { | 
| Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 55 | int MBEDTLS_PRIVATE(x);                      /*!< permutation index */ | 
|  | 56 | int MBEDTLS_PRIVATE(y);                      /*!< permutation index */ | 
|  | 57 | unsigned char MBEDTLS_PRIVATE(m)[256];       /*!< permutation table */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | mbedtls_arc4_context; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 |  | 
| Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 61 | #else  /* MBEDTLS_ARC4_ALT */ | 
|  | 62 | #include "arc4_alt.h" | 
|  | 63 | #endif /* MBEDTLS_ARC4_ALT */ | 
|  | 64 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 65 | /** | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 66 | * \brief          Initialize ARC4 context | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | * | 
|  | 68 | * \param ctx      ARC4 context to be initialized | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 69 | * | 
|  | 70 | * \warning        ARC4 is considered a weak cipher and its use constitutes a | 
|  | 71 | *                 security risk. We recommend considering stronger ciphers | 
|  | 72 | *                 instead. | 
|  | 73 | * | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 74 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | void mbedtls_arc4_init( mbedtls_arc4_context *ctx ); | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 76 |  | 
|  | 77 | /** | 
|  | 78 | * \brief          Clear ARC4 context | 
|  | 79 | * | 
|  | 80 | * \param ctx      ARC4 context to be cleared | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 81 | * | 
|  | 82 | * \warning        ARC4 is considered a weak cipher and its use constitutes a | 
|  | 83 | *                 security risk. We recommend considering stronger ciphers | 
|  | 84 | *                 instead. | 
|  | 85 | * | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 86 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | void mbedtls_arc4_free( mbedtls_arc4_context *ctx ); | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 88 |  | 
|  | 89 | /** | 
|  | 90 | * \brief          ARC4 key schedule | 
|  | 91 | * | 
|  | 92 | * \param ctx      ARC4 context to be setup | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 93 | * \param key      the secret key | 
| Manuel Pégourié-Gonnard | ce41125 | 2013-09-04 12:28:37 +0200 | [diff] [blame] | 94 | * \param keylen   length of the key, in bytes | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 95 | * | 
|  | 96 | * \warning        ARC4 is considered a weak cipher and its use constitutes a | 
|  | 97 | *                 security risk. We recommend considering stronger ciphers | 
|  | 98 | *                 instead. | 
|  | 99 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key, | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 102 | unsigned int keylen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 |  | 
|  | 104 | /** | 
|  | 105 | * \brief          ARC4 cipher function | 
|  | 106 | * | 
|  | 107 | * \param ctx      ARC4 context | 
| Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 108 | * \param length   length of the input data | 
|  | 109 | * \param input    buffer holding the input data | 
|  | 110 | * \param output   buffer for the output data | 
| Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 111 | * | 
| Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 112 | * \return         0 if successful | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 113 | * | 
|  | 114 | * \warning        ARC4 is considered a weak cipher and its use constitutes a | 
|  | 115 | *                 security risk. We recommend considering stronger ciphers | 
|  | 116 | *                 instead. | 
|  | 117 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input, | 
| Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 120 | unsigned char *output ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 121 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 122 | #if defined(MBEDTLS_SELF_TEST) | 
|  | 123 |  | 
| Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 124 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | * \brief          Checkup routine | 
|  | 126 | * | 
|  | 127 | * \return         0 if successful, or 1 if the test failed | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 128 | * | 
|  | 129 | * \warning        ARC4 is considered a weak cipher and its use constitutes a | 
|  | 130 | *                 security risk. We recommend considering stronger ciphers | 
|  | 131 | *                 instead. | 
|  | 132 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 133 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | int mbedtls_arc4_self_test( int verbose ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 136 | #endif /* MBEDTLS_SELF_TEST */ | 
|  | 137 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | #ifdef __cplusplus | 
|  | 139 | } | 
|  | 140 | #endif | 
|  | 141 |  | 
|  | 142 | #endif /* arc4.h */ |