Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers blowfish.h Source File

blowfish.h

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