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