Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbedtls by
blowfish.h
00001 /** 00002 * \file blowfish.h 00003 * 00004 * \brief Blowfish block cipher 00005 * 00006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00007 * SPDX-License-Identifier: Apache-2.0 00008 * 00009 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00010 * not use this file except in compliance with the License. 00011 * You may obtain a copy of the License at 00012 * 00013 * http://www.apache.org/licenses/LICENSE-2.0 00014 * 00015 * Unless required by applicable law or agreed to in writing, software 00016 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00017 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 * See the License for the specific language governing permissions and 00019 * limitations under the License. 00020 * 00021 * This file is part of mbed TLS (https://tls.mbed.org) 00022 */ 00023 #ifndef MBEDTLS_BLOWFISH_H 00024 #define MBEDTLS_BLOWFISH_H 00025 00026 #if !defined(MBEDTLS_CONFIG_FILE) 00027 #include "config.h" 00028 #else 00029 #include MBEDTLS_CONFIG_FILE 00030 #endif 00031 00032 #include <stddef.h> 00033 #include <stdint.h> 00034 00035 #define MBEDTLS_BLOWFISH_ENCRYPT 1 00036 #define MBEDTLS_BLOWFISH_DECRYPT 0 00037 #define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448 00038 #define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32 00039 #define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */ 00040 #define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */ 00041 00042 #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */ 00043 #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ 00044 00045 #if !defined(MBEDTLS_BLOWFISH_ALT) 00046 // Regular implementation 00047 // 00048 00049 #ifdef __cplusplus 00050 extern "C" { 00051 #endif 00052 00053 /** 00054 * \brief Blowfish context structure 00055 */ 00056 typedef struct 00057 { 00058 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ 00059 uint32_t S[4][256]; /*!< key dependent S-boxes */ 00060 } 00061 mbedtls_blowfish_context; 00062 00063 /** 00064 * \brief Initialize Blowfish context 00065 * 00066 * \param ctx Blowfish context to be initialized 00067 */ 00068 void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx ); 00069 00070 /** 00071 * \brief Clear Blowfish context 00072 * 00073 * \param ctx Blowfish context to be cleared 00074 */ 00075 void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx ); 00076 00077 /** 00078 * \brief Blowfish key schedule 00079 * 00080 * \param ctx Blowfish context to be initialized 00081 * \param key encryption key 00082 * \param keybits must be between 32 and 448 bits 00083 * 00084 * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH 00085 */ 00086 int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key, 00087 unsigned int keybits ); 00088 00089 /** 00090 * \brief Blowfish-ECB block encryption/decryption 00091 * 00092 * \param ctx Blowfish context 00093 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT 00094 * \param input 8-byte input block 00095 * \param output 8-byte output block 00096 * 00097 * \return 0 if successful 00098 */ 00099 int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx, 00100 int mode, 00101 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE], 00102 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] ); 00103 00104 #if defined(MBEDTLS_CIPHER_MODE_CBC) 00105 /** 00106 * \brief Blowfish-CBC buffer encryption/decryption 00107 * Length should be a multiple of the block 00108 * size (8 bytes) 00109 * 00110 * \note Upon exit, the content of the IV is updated so that you can 00111 * call the function same function again on the following 00112 * block(s) of data and get the same result as if it was 00113 * encrypted in one call. This allows a "streaming" usage. 00114 * If on the other hand you need to retain the contents of the 00115 * IV, you should either save it manually or use the cipher 00116 * module instead. 00117 * 00118 * \param ctx Blowfish context 00119 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT 00120 * \param length length of the input data 00121 * \param iv initialization vector (updated after use) 00122 * \param input buffer holding the input data 00123 * \param output buffer holding the output data 00124 * 00125 * \return 0 if successful, or 00126 * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH 00127 */ 00128 int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx, 00129 int mode, 00130 size_t length, 00131 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], 00132 const unsigned char *input, 00133 unsigned char *output ); 00134 #endif /* MBEDTLS_CIPHER_MODE_CBC */ 00135 00136 #if defined(MBEDTLS_CIPHER_MODE_CFB) 00137 /** 00138 * \brief Blowfish CFB buffer encryption/decryption. 00139 * 00140 * \note Upon exit, the content of the IV is updated so that you can 00141 * call the function same function again on the following 00142 * block(s) of data and get the same result as if it was 00143 * encrypted in one call. This allows a "streaming" usage. 00144 * If on the other hand you need to retain the contents of the 00145 * IV, you should either save it manually or use the cipher 00146 * module instead. 00147 * 00148 * \param ctx Blowfish context 00149 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT 00150 * \param length length of the input data 00151 * \param iv_off offset in IV (updated after use) 00152 * \param iv initialization vector (updated after use) 00153 * \param input buffer holding the input data 00154 * \param output buffer holding the output data 00155 * 00156 * \return 0 if successful 00157 */ 00158 int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx, 00159 int mode, 00160 size_t length, 00161 size_t *iv_off, 00162 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], 00163 const unsigned char *input, 00164 unsigned char *output ); 00165 #endif /*MBEDTLS_CIPHER_MODE_CFB */ 00166 00167 #if defined(MBEDTLS_CIPHER_MODE_CTR) 00168 /** 00169 * \brief Blowfish-CTR buffer encryption/decryption 00170 * 00171 * Warning: You have to keep the maximum use of your counter in mind! 00172 * 00173 * \param ctx Blowfish context 00174 * \param length The length of the data 00175 * \param nc_off The offset in the current stream_block (for resuming 00176 * within current cipher stream). The offset pointer to 00177 * should be 0 at the start of a stream. 00178 * \param nonce_counter The 64-bit nonce and counter. 00179 * \param stream_block The saved stream-block for resuming. Is overwritten 00180 * by the function. 00181 * \param input The input data stream 00182 * \param output The output data stream 00183 * 00184 * \return 0 if successful 00185 */ 00186 int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx, 00187 size_t length, 00188 size_t *nc_off, 00189 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE], 00190 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE], 00191 const unsigned char *input, 00192 unsigned char *output ); 00193 #endif /* MBEDTLS_CIPHER_MODE_CTR */ 00194 00195 #ifdef __cplusplus 00196 } 00197 #endif 00198 00199 #else /* MBEDTLS_BLOWFISH_ALT */ 00200 #include "blowfish_alt.h" 00201 #endif /* MBEDTLS_BLOWFISH_ALT */ 00202 00203 #endif /* blowfish.h */
Generated on Tue Jul 12 2022 17:25:41 by
