Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

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 #ifdef __cplusplus
00048 extern "C" {
00049 #endif
00050 
00051 #if !defined(MBEDTLS_BLOWFISH_ALT)
00052 // Regular implementation
00053 //
00054 
00055 /**
00056  * \brief          Blowfish context structure
00057  */
00058 typedef struct mbedtls_blowfish_context
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 #else  /* MBEDTLS_BLOWFISH_ALT */
00066 #include "blowfish_alt.h"
00067 #endif /* MBEDTLS_BLOWFISH_ALT */
00068 
00069 /**
00070  * \brief          Initialize Blowfish context
00071  *
00072  * \param ctx      Blowfish context to be initialized
00073  */
00074 void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
00075 
00076 /**
00077  * \brief          Clear Blowfish context
00078  *
00079  * \param ctx      Blowfish context to be cleared
00080  */
00081 void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
00082 
00083 /**
00084  * \brief          Blowfish key schedule
00085  *
00086  * \param ctx      Blowfish context to be initialized
00087  * \param key      encryption key
00088  * \param keybits  must be between 32 and 448 bits
00089  *
00090  * \return         0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH
00091  */
00092 int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
00093                      unsigned int keybits );
00094 
00095 /**
00096  * \brief          Blowfish-ECB block encryption/decryption
00097  *
00098  * \param ctx      Blowfish context
00099  * \param mode     MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
00100  * \param input    8-byte input block
00101  * \param output   8-byte output block
00102  *
00103  * \return         0 if successful
00104  */
00105 int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
00106                         int mode,
00107                         const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
00108                         unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
00109 
00110 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00111 /**
00112  * \brief          Blowfish-CBC buffer encryption/decryption
00113  *                 Length should be a multiple of the block
00114  *                 size (8 bytes)
00115  *
00116  * \note           Upon exit, the content of the IV is updated so that you can
00117  *                 call the function same function again on the following
00118  *                 block(s) of data and get the same result as if it was
00119  *                 encrypted in one call. This allows a "streaming" usage.
00120  *                 If on the other hand you need to retain the contents of the
00121  *                 IV, you should either save it manually or use the cipher
00122  *                 module instead.
00123  *
00124  * \param ctx      Blowfish context
00125  * \param mode     MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
00126  * \param length   length of the input data
00127  * \param iv       initialization vector (updated after use)
00128  * \param input    buffer holding the input data
00129  * \param output   buffer holding the output data
00130  *
00131  * \return         0 if successful, or
00132  *                 MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH
00133  */
00134 int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
00135                         int mode,
00136                         size_t length,
00137                         unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
00138                         const unsigned char *input,
00139                         unsigned char *output );
00140 #endif /* MBEDTLS_CIPHER_MODE_CBC */
00141 
00142 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00143 /**
00144  * \brief          Blowfish CFB buffer encryption/decryption.
00145  *
00146  * \note           Upon exit, the content of the IV is updated so that you can
00147  *                 call the function same function again on the following
00148  *                 block(s) of data and get the same result as if it was
00149  *                 encrypted in one call. This allows a "streaming" usage.
00150  *                 If on the other hand you need to retain the contents of the
00151  *                 IV, you should either save it manually or use the cipher
00152  *                 module instead.
00153  *
00154  * \param ctx      Blowfish context
00155  * \param mode     MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
00156  * \param length   length of the input data
00157  * \param iv_off   offset in IV (updated after use)
00158  * \param iv       initialization vector (updated after use)
00159  * \param input    buffer holding the input data
00160  * \param output   buffer holding the output data
00161  *
00162  * \return         0 if successful
00163  */
00164 int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
00165                           int mode,
00166                           size_t length,
00167                           size_t *iv_off,
00168                           unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
00169                           const unsigned char *input,
00170                           unsigned char *output );
00171 #endif /*MBEDTLS_CIPHER_MODE_CFB */
00172 
00173 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00174 /**
00175  * \brief               Blowfish-CTR buffer encryption/decryption
00176  *
00177  * \warning    You must never reuse a nonce value with the same key. Doing so
00178  *             would void the encryption for the two messages encrypted with
00179  *             the same nonce and key.
00180  *
00181  *             There are two common strategies for managing nonces with CTR:
00182  *
00183  *             1. You can handle everything as a single message processed over
00184  *             successive calls to this function. In that case, you want to
00185  *             set \p nonce_counter and \p nc_off to 0 for the first call, and
00186  *             then preserve the values of \p nonce_counter, \p nc_off and \p
00187  *             stream_block across calls to this function as they will be
00188  *             updated by this function.
00189  *
00190  *             With this strategy, you must not encrypt more than 2**64
00191  *             blocks of data with the same key.
00192  *
00193  *             2. You can encrypt separate messages by dividing the \p
00194  *             nonce_counter buffer in two areas: the first one used for a
00195  *             per-message nonce, handled by yourself, and the second one
00196  *             updated by this function internally.
00197  *
00198  *             For example, you might reserve the first 4 bytes for the
00199  *             per-message nonce, and the last 4 bytes for internal use. In that
00200  *             case, before calling this function on a new message you need to
00201  *             set the first 4 bytes of \p nonce_counter to your chosen nonce
00202  *             value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
00203  *             stream_block to be ignored). That way, you can encrypt at most
00204  *             2**32 messages of up to 2**32 blocks each with the same key.
00205  *
00206  *             The per-message nonce (or information sufficient to reconstruct
00207  *             it) needs to be communicated with the ciphertext and must be unique.
00208  *             The recommended way to ensure uniqueness is to use a message
00209  *             counter.
00210  *
00211  *             Note that for both stategies, sizes are measured in blocks and
00212  *             that a Blowfish block is 8 bytes.
00213  *
00214  * \warning    Upon return, \p stream_block contains sensitive data. Its
00215  *             content must not be written to insecure storage and should be
00216  *             securely discarded as soon as it's no longer needed.
00217  *
00218  * \param ctx           Blowfish context
00219  * \param length        The length of the data
00220  * \param nc_off        The offset in the current stream_block (for resuming
00221  *                      within current cipher stream). The offset pointer to
00222  *                      should be 0 at the start of a stream.
00223  * \param nonce_counter The 64-bit nonce and counter.
00224  * \param stream_block  The saved stream-block for resuming. Is overwritten
00225  *                      by the function.
00226  * \param input         The input data stream
00227  * \param output        The output data stream
00228  *
00229  * \return         0 if successful
00230  */
00231 int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
00232                         size_t length,
00233                         size_t *nc_off,
00234                         unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
00235                         unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
00236                         const unsigned char *input,
00237                         unsigned char *output );
00238 #endif /* MBEDTLS_CIPHER_MODE_CTR */
00239 
00240 #ifdef __cplusplus
00241 }
00242 #endif
00243 
00244 #endif /* blowfish.h */