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