Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file blowfish.h |
| 3 | * |
| 4 | * \brief Blowfish block cipher |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 9 | * |
| 10 | * This file is provided under the Apache License 2.0, or the |
| 11 | * GNU General Public License v2.0 or later. |
| 12 | * |
| 13 | * ********** |
| 14 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 15 | * |
| 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 17 | * not use this file except in compliance with the License. |
| 18 | * You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 27 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 28 | * ********** |
| 29 | * |
| 30 | * ********** |
| 31 | * GNU General Public License v2.0 or later: |
| 32 | * |
| 33 | * This program is free software; you can redistribute it and/or modify |
| 34 | * it under the terms of the GNU General Public License as published by |
| 35 | * the Free Software Foundation; either version 2 of the License, or |
| 36 | * (at your option) any later version. |
| 37 | * |
| 38 | * This program is distributed in the hope that it will be useful, |
| 39 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 40 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 41 | * GNU General Public License for more details. |
| 42 | * |
| 43 | * You should have received a copy of the GNU General Public License along |
| 44 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 45 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 46 | * |
| 47 | * ********** |
| 48 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 49 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 50 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #ifndef MBEDTLS_BLOWFISH_H |
| 52 | #define MBEDTLS_BLOWFISH_H |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 53 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 55 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 56 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 58 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 59 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 60 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 61 | #include <stdint.h> |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 62 | |
Hanno Becker | d2f3a00 | 2018-12-17 13:21:06 +0000 | [diff] [blame] | 63 | #include "platform_util.h" |
| 64 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | #define MBEDTLS_BLOWFISH_ENCRYPT 1 |
| 66 | #define MBEDTLS_BLOWFISH_DECRYPT 0 |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 67 | #define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448 |
| 68 | #define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32 |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 69 | #define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */ |
| 70 | #define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 71 | |
Hanno Becker | d2f3a00 | 2018-12-17 13:21:06 +0000 | [diff] [blame] | 72 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 73 | #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0016 ) |
Hanno Becker | d2f3a00 | 2018-12-17 13:21:06 +0000 | [diff] [blame] | 74 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 75 | #define MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA -0x0016 /**< Bad input data. */ |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 76 | |
Hanno Becker | 6640b0d | 2018-12-18 09:45:17 +0000 | [diff] [blame] | 77 | #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ |
| 78 | |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 79 | /* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used. |
| 80 | */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 81 | #define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 /**< Blowfish hardware accelerator failed. */ |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 82 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 83 | #ifdef __cplusplus |
| 84 | extern "C" { |
| 85 | #endif |
| 86 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 87 | #if !defined(MBEDTLS_BLOWFISH_ALT) |
| 88 | // Regular implementation |
| 89 | // |
| 90 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 91 | /** |
| 92 | * \brief Blowfish context structure |
| 93 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 94 | typedef struct mbedtls_blowfish_context |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 95 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 97 | uint32_t S[4][256]; /*!< key dependent S-boxes */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 98 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | mbedtls_blowfish_context; |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 100 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 101 | #else /* MBEDTLS_BLOWFISH_ALT */ |
| 102 | #include "blowfish_alt.h" |
| 103 | #endif /* MBEDTLS_BLOWFISH_ALT */ |
| 104 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 105 | /** |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 106 | * \brief Initialize a Blowfish context. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 107 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 108 | * \param ctx The Blowfish context to be initialized. |
Hanno Becker | 49acc64 | 2018-12-17 09:24:51 +0000 | [diff] [blame] | 109 | * This must not be \c NULL. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 110 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 112 | |
| 113 | /** |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 114 | * \brief Clear a Blowfish context. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 115 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 116 | * \param ctx The Blowfish context to be cleared. |
| 117 | * This may be \c NULL, in which case this function |
Hanno Becker | 3d9a349 | 2018-12-17 15:15:42 +0000 | [diff] [blame] | 118 | * returns immediately. If it is not \c NULL, it must |
| 119 | * point to an initialized Blowfish context. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 120 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 122 | |
| 123 | /** |
Hanno Becker | ed54128 | 2018-12-19 15:48:37 +0000 | [diff] [blame] | 124 | * \brief Perform a Blowfish key schedule operation. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 125 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 126 | * \param ctx The Blowfish context to perform the key schedule on. |
Hanno Becker | 49acc64 | 2018-12-17 09:24:51 +0000 | [diff] [blame] | 127 | * \param key The encryption key. This must be a readable buffer of |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 128 | * length \p keybits Bits. |
Hanno Becker | 49acc64 | 2018-12-17 09:24:51 +0000 | [diff] [blame] | 129 | * \param keybits The length of \p key in Bits. This must be between |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 130 | * \c 32 and \c 448 and a multiple of \c 8. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 131 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 132 | * \return \c 0 if successful. |
| 133 | * \return A negative error code on failure. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 134 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key, |
Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 136 | unsigned int keybits ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 137 | |
| 138 | /** |
Hanno Becker | ed54128 | 2018-12-19 15:48:37 +0000 | [diff] [blame] | 139 | * \brief Perform a Blowfish-ECB block encryption/decryption operation. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 140 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 141 | * \param ctx The Blowfish context to use. This must be initialized |
| 142 | * and bound to a key. |
| 143 | * \param mode The mode of operation. Possible values are |
| 144 | * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or |
| 145 | * #MBEDTLS_BLOWFISH_DECRYPT for decryption. |
Hanno Becker | 20376d6 | 2018-12-18 17:47:39 +0000 | [diff] [blame] | 146 | * \param input The input block. This must be a readable buffer |
| 147 | * of size \c 8 Bytes. |
| 148 | * \param output The output block. This must be a writable buffer |
| 149 | * of size \c 8 Bytes. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 150 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 151 | * \return \c 0 if successful. |
| 152 | * \return A negative error code on failure. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 153 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 155 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE], |
| 157 | unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 160 | /** |
Hanno Becker | ed54128 | 2018-12-19 15:48:37 +0000 | [diff] [blame] | 161 | * \brief Perform a Blowfish-CBC buffer encryption/decryption operation. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 162 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 163 | * \note Upon exit, the content of the IV is updated so that you can |
| 164 | * call the function same function again on the following |
| 165 | * block(s) of data and get the same result as if it was |
| 166 | * encrypted in one call. This allows a "streaming" usage. |
| 167 | * If on the other hand you need to retain the contents of the |
| 168 | * IV, you should either save it manually or use the cipher |
| 169 | * module instead. |
| 170 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 171 | * \param ctx The Blowfish context to use. This must be initialized |
| 172 | * and bound to a key. |
| 173 | * \param mode The mode of operation. Possible values are |
| 174 | * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or |
| 175 | * #MBEDTLS_BLOWFISH_DECRYPT for decryption. |
Hanno Becker | 49acc64 | 2018-12-17 09:24:51 +0000 | [diff] [blame] | 176 | * \param length The length of the input data in Bytes. This must be |
| 177 | * multiple of \c 8. |
Hanno Becker | 3d9a349 | 2018-12-17 15:15:42 +0000 | [diff] [blame] | 178 | * \param iv The initialization vector. This must be a read/write buffer |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 179 | * of length \c 8 Bytes. It is updated by this function. |
Hanno Becker | 49acc64 | 2018-12-17 09:24:51 +0000 | [diff] [blame] | 180 | * \param input The input data. This must be a readable buffer of length |
Hanno Becker | 20376d6 | 2018-12-18 17:47:39 +0000 | [diff] [blame] | 181 | * \p length Bytes. |
Hanno Becker | 49acc64 | 2018-12-17 09:24:51 +0000 | [diff] [blame] | 182 | * \param output The output data. This must be a writable buffer of length |
Hanno Becker | 20376d6 | 2018-12-18 17:47:39 +0000 | [diff] [blame] | 183 | * \p length Bytes. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 184 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 185 | * \return \c 0 if successful. |
| 186 | * \return A negative error code on failure. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 187 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 189 | int mode, |
| 190 | size_t length, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 192 | const unsigned char *input, |
| 193 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 195 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 197 | /** |
Hanno Becker | ed54128 | 2018-12-19 15:48:37 +0000 | [diff] [blame] | 198 | * \brief Perform a Blowfish CFB buffer encryption/decryption operation. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 199 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 200 | * \note Upon exit, the content of the IV is updated so that you can |
| 201 | * call the function same function again on the following |
| 202 | * block(s) of data and get the same result as if it was |
| 203 | * encrypted in one call. This allows a "streaming" usage. |
| 204 | * If on the other hand you need to retain the contents of the |
| 205 | * IV, you should either save it manually or use the cipher |
| 206 | * module instead. |
| 207 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 208 | * \param ctx The Blowfish context to use. This must be initialized |
| 209 | * and bound to a key. |
| 210 | * \param mode The mode of operation. Possible values are |
| 211 | * #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or |
| 212 | * #MBEDTLS_BLOWFISH_DECRYPT for decryption. |
| 213 | * \param length The length of the input data in Bytes. |
| 214 | * \param iv_off The offset in the initialiation vector. |
Hanno Becker | 3d9a349 | 2018-12-17 15:15:42 +0000 | [diff] [blame] | 215 | * The value pointed to must be smaller than \c 8 Bytes. |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 216 | * It is updated by this function to support the aforementioned |
| 217 | * streaming usage. |
Hanno Becker | 20376d6 | 2018-12-18 17:47:39 +0000 | [diff] [blame] | 218 | * \param iv The initialization vector. This must be a read/write buffer |
| 219 | * of size \c 8 Bytes. It is updated after use. |
Hanno Becker | 49acc64 | 2018-12-17 09:24:51 +0000 | [diff] [blame] | 220 | * \param input The input data. This must be a readable buffer of length |
Hanno Becker | 20376d6 | 2018-12-18 17:47:39 +0000 | [diff] [blame] | 221 | * \p length Bytes. |
Hanno Becker | 49acc64 | 2018-12-17 09:24:51 +0000 | [diff] [blame] | 222 | * \param output The output data. This must be a writable buffer of length |
Hanno Becker | 20376d6 | 2018-12-18 17:47:39 +0000 | [diff] [blame] | 223 | * \p length Bytes. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 224 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 225 | * \return \c 0 if successful. |
| 226 | * \return A negative error code on failure. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 227 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 229 | int mode, |
| 230 | size_t length, |
| 231 | size_t *iv_off, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 233 | const unsigned char *input, |
| 234 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 235 | #endif /*MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 236 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 238 | /** |
Hanno Becker | ed54128 | 2018-12-19 15:48:37 +0000 | [diff] [blame] | 239 | * \brief Perform a Blowfish-CTR buffer encryption/decryption operation. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 240 | * |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 241 | * \warning You must never reuse a nonce value with the same key. Doing so |
| 242 | * would void the encryption for the two messages encrypted with |
| 243 | * the same nonce and key. |
| 244 | * |
| 245 | * There are two common strategies for managing nonces with CTR: |
| 246 | * |
Manuel Pégourié-Gonnard | d0f143b | 2018-05-24 12:01:58 +0200 | [diff] [blame] | 247 | * 1. You can handle everything as a single message processed over |
| 248 | * successive calls to this function. In that case, you want to |
| 249 | * set \p nonce_counter and \p nc_off to 0 for the first call, and |
| 250 | * then preserve the values of \p nonce_counter, \p nc_off and \p |
| 251 | * stream_block across calls to this function as they will be |
| 252 | * updated by this function. |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 253 | * |
Manuel Pégourié-Gonnard | d0f143b | 2018-05-24 12:01:58 +0200 | [diff] [blame] | 254 | * With this strategy, you must not encrypt more than 2**64 |
| 255 | * blocks of data with the same key. |
| 256 | * |
| 257 | * 2. You can encrypt separate messages by dividing the \p |
| 258 | * nonce_counter buffer in two areas: the first one used for a |
| 259 | * per-message nonce, handled by yourself, and the second one |
| 260 | * updated by this function internally. |
| 261 | * |
| 262 | * For example, you might reserve the first 4 bytes for the |
| 263 | * per-message nonce, and the last 4 bytes for internal use. In that |
| 264 | * case, before calling this function on a new message you need to |
| 265 | * set the first 4 bytes of \p nonce_counter to your chosen nonce |
| 266 | * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p |
| 267 | * stream_block to be ignored). That way, you can encrypt at most |
| 268 | * 2**32 messages of up to 2**32 blocks each with the same key. |
| 269 | * |
| 270 | * The per-message nonce (or information sufficient to reconstruct |
| 271 | * it) needs to be communicated with the ciphertext and must be unique. |
| 272 | * The recommended way to ensure uniqueness is to use a message |
| 273 | * counter. |
| 274 | * |
| 275 | * Note that for both stategies, sizes are measured in blocks and |
| 276 | * that a Blowfish block is 8 bytes. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 277 | * |
Manuel Pégourié-Gonnard | fa0c47d | 2018-05-24 19:02:06 +0200 | [diff] [blame] | 278 | * \warning Upon return, \p stream_block contains sensitive data. Its |
| 279 | * content must not be written to insecure storage and should be |
| 280 | * securely discarded as soon as it's no longer needed. |
| 281 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 282 | * \param ctx The Blowfish context to use. This must be initialized |
| 283 | * and bound to a key. |
| 284 | * \param length The length of the input data in Bytes. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 285 | * \param nc_off The offset in the current stream_block (for resuming |
Hanno Becker | ed54128 | 2018-12-19 15:48:37 +0000 | [diff] [blame] | 286 | * within current cipher stream). The offset pointer |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 287 | * should be \c 0 at the start of a stream and must be |
| 288 | * smaller than \c 8. It is updated by this function. |
Hanno Becker | 3d9a349 | 2018-12-17 15:15:42 +0000 | [diff] [blame] | 289 | * \param nonce_counter The 64-bit nonce and counter. This must point to a |
| 290 | * read/write buffer of length \c 8 Bytes. |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 291 | * \param stream_block The saved stream-block for resuming. This must point to |
Hanno Becker | 3d9a349 | 2018-12-17 15:15:42 +0000 | [diff] [blame] | 292 | * a read/write buffer of length \c 8 Bytes. |
Hanno Becker | 20376d6 | 2018-12-18 17:47:39 +0000 | [diff] [blame] | 293 | * \param input The input data. This must be a readable buffer of |
| 294 | * length \p length Bytes. |
| 295 | * \param output The output data. This must be a writable buffer of |
| 296 | * length \p length Bytes. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 297 | * |
Hanno Becker | 3b4d6c6 | 2018-12-12 18:14:08 +0000 | [diff] [blame] | 298 | * \return \c 0 if successful. |
| 299 | * \return A negative error code on failure. |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 300 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx, |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 302 | size_t length, |
| 303 | size_t *nc_off, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE], |
| 305 | unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE], |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 306 | const unsigned char *input, |
| 307 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 309 | |
| 310 | #ifdef __cplusplus |
| 311 | } |
| 312 | #endif |
| 313 | |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 314 | #endif /* blowfish.h */ |