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 | /* |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 10 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 30 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 52 | * This file is part of mbed TLS (https://tls.mbed.org) |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 53 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 54 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #ifndef MBEDTLS_ARC4_H |
| 56 | #define MBEDTLS_ARC4_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 59 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 60 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 62 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 63 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 64 | #include <stddef.h> |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 65 | |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 66 | /* MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED is deprecated and should not be used. */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 67 | #define MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED -0x0019 /**< ARC4 hardware accelerator failed. */ |
| 68 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 69 | #ifdef __cplusplus |
| 70 | extern "C" { |
| 71 | #endif |
| 72 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 73 | #if !defined(MBEDTLS_ARC4_ALT) |
| 74 | // Regular implementation |
| 75 | // |
| 76 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | /** |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 78 | * \brief ARC4 context structure |
| 79 | * |
| 80 | * \warning ARC4 is considered a weak cipher and its use constitutes a |
| 81 | * security risk. We recommend considering stronger ciphers instead. |
| 82 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 83 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 84 | typedef struct mbedtls_arc4_context |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | { |
| 86 | int x; /*!< permutation index */ |
| 87 | int y; /*!< permutation index */ |
| 88 | unsigned char m[256]; /*!< permutation table */ |
| 89 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | mbedtls_arc4_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 92 | #else /* MBEDTLS_ARC4_ALT */ |
| 93 | #include "arc4_alt.h" |
| 94 | #endif /* MBEDTLS_ARC4_ALT */ |
| 95 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | /** |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 97 | * \brief Initialize ARC4 context |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | * |
| 99 | * \param ctx ARC4 context to be initialized |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 100 | * |
| 101 | * \warning ARC4 is considered a weak cipher and its use constitutes a |
| 102 | * security risk. We recommend considering stronger ciphers |
| 103 | * instead. |
| 104 | * |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 105 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | void mbedtls_arc4_init( mbedtls_arc4_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * \brief Clear ARC4 context |
| 110 | * |
| 111 | * \param ctx ARC4 context to be cleared |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 112 | * |
| 113 | * \warning ARC4 is considered a weak cipher and its use constitutes a |
| 114 | * security risk. We recommend considering stronger ciphers |
| 115 | * instead. |
| 116 | * |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 117 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | void mbedtls_arc4_free( mbedtls_arc4_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * \brief ARC4 key schedule |
| 122 | * |
| 123 | * \param ctx ARC4 context to be setup |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 | * \param key the secret key |
Manuel Pégourié-Gonnard | ce41125 | 2013-09-04 12:28:37 +0200 | [diff] [blame] | 125 | * \param keylen length of the key, in bytes |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 126 | * |
| 127 | * \warning ARC4 is considered a weak cipher and its use constitutes a |
| 128 | * security risk. We recommend considering stronger ciphers |
| 129 | * instead. |
| 130 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 133 | unsigned int keylen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * \brief ARC4 cipher function |
| 137 | * |
| 138 | * \param ctx ARC4 context |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 139 | * \param length length of the input data |
| 140 | * \param input buffer holding the input data |
| 141 | * \param output buffer for the output data |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 142 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 143 | * \return 0 if successful |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 144 | * |
| 145 | * \warning ARC4 is considered a weak cipher and its use constitutes a |
| 146 | * security risk. We recommend considering stronger ciphers |
| 147 | * instead. |
| 148 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | 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] | 151 | unsigned char *output ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | |
Ron Eldor | fa8f635 | 2017-06-20 15:48:46 +0300 | [diff] [blame] | 153 | #if defined(MBEDTLS_SELF_TEST) |
| 154 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 155 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | * \brief Checkup routine |
| 157 | * |
| 158 | * \return 0 if successful, or 1 if the test failed |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 159 | * |
| 160 | * \warning ARC4 is considered a weak cipher and its use constitutes a |
| 161 | * security risk. We recommend considering stronger ciphers |
| 162 | * instead. |
| 163 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | int mbedtls_arc4_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | |
Ron Eldor | fa8f635 | 2017-06-20 15:48:46 +0300 | [diff] [blame] | 167 | #endif /* MBEDTLS_SELF_TEST */ |
| 168 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 169 | #ifdef __cplusplus |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | #endif /* arc4.h */ |