mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /**
ansond 0:137634ff4186 2 * \file blowfish.h
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * \brief Blowfish block cipher
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * Copyright (C) 2012-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 9 *
ansond 0:137634ff4186 10 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 11 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 12 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 13 * (at your option) any later version.
ansond 0:137634ff4186 14 *
ansond 0:137634ff4186 15 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 18 * GNU General Public License for more details.
ansond 0:137634ff4186 19 *
ansond 0:137634ff4186 20 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 21 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 23 */
ansond 0:137634ff4186 24 #ifndef POLARSSL_BLOWFISH_H
ansond 0:137634ff4186 25 #define POLARSSL_BLOWFISH_H
ansond 0:137634ff4186 26
ansond 0:137634ff4186 27 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 28 #include "config.h"
ansond 0:137634ff4186 29 #else
ansond 0:137634ff4186 30 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 31 #endif
ansond 0:137634ff4186 32
ansond 0:137634ff4186 33 #include <stddef.h>
ansond 0:137634ff4186 34
ansond 0:137634ff4186 35 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
ansond 0:137634ff4186 36 #include <basetsd.h>
ansond 0:137634ff4186 37 typedef UINT32 uint32_t;
ansond 0:137634ff4186 38 #else
ansond 0:137634ff4186 39 #include <inttypes.h>
ansond 0:137634ff4186 40 #endif
ansond 0:137634ff4186 41
ansond 0:137634ff4186 42 #define BLOWFISH_ENCRYPT 1
ansond 0:137634ff4186 43 #define BLOWFISH_DECRYPT 0
ansond 0:137634ff4186 44 #define BLOWFISH_MAX_KEY 448
ansond 0:137634ff4186 45 #define BLOWFISH_MIN_KEY 32
ansond 0:137634ff4186 46 #define BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
ansond 0:137634ff4186 47 #define BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
ansond 0:137634ff4186 48
ansond 0:137634ff4186 49 #define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
ansond 0:137634ff4186 50 #define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
ansond 0:137634ff4186 51
ansond 0:137634ff4186 52 #if !defined(POLARSSL_BLOWFISH_ALT)
ansond 0:137634ff4186 53 // Regular implementation
ansond 0:137634ff4186 54 //
ansond 0:137634ff4186 55
ansond 0:137634ff4186 56 #ifdef __cplusplus
ansond 0:137634ff4186 57 extern "C" {
ansond 0:137634ff4186 58 #endif
ansond 0:137634ff4186 59
ansond 0:137634ff4186 60 /**
ansond 0:137634ff4186 61 * \brief Blowfish context structure
ansond 0:137634ff4186 62 */
ansond 0:137634ff4186 63 typedef struct
ansond 0:137634ff4186 64 {
ansond 0:137634ff4186 65 uint32_t P[BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
ansond 0:137634ff4186 66 uint32_t S[4][256]; /*!< key dependent S-boxes */
ansond 0:137634ff4186 67 }
ansond 0:137634ff4186 68 blowfish_context;
ansond 0:137634ff4186 69
ansond 0:137634ff4186 70 /**
ansond 0:137634ff4186 71 * \brief Initialize Blowfish context
ansond 0:137634ff4186 72 *
ansond 0:137634ff4186 73 * \param ctx Blowfish context to be initialized
ansond 0:137634ff4186 74 */
ansond 0:137634ff4186 75 void blowfish_init( blowfish_context *ctx );
ansond 0:137634ff4186 76
ansond 0:137634ff4186 77 /**
ansond 0:137634ff4186 78 * \brief Clear Blowfish context
ansond 0:137634ff4186 79 *
ansond 0:137634ff4186 80 * \param ctx Blowfish context to be cleared
ansond 0:137634ff4186 81 */
ansond 0:137634ff4186 82 void blowfish_free( blowfish_context *ctx );
ansond 0:137634ff4186 83
ansond 0:137634ff4186 84 /**
ansond 0:137634ff4186 85 * \brief Blowfish key schedule
ansond 0:137634ff4186 86 *
ansond 0:137634ff4186 87 * \param ctx Blowfish context to be initialized
ansond 0:137634ff4186 88 * \param key encryption key
ansond 0:137634ff4186 89 * \param keysize must be between 32 and 448 bits
ansond 0:137634ff4186 90 *
ansond 0:137634ff4186 91 * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH
ansond 0:137634ff4186 92 */
ansond 0:137634ff4186 93 int blowfish_setkey( blowfish_context *ctx, const unsigned char *key,
ansond 0:137634ff4186 94 unsigned int keysize );
ansond 0:137634ff4186 95
ansond 0:137634ff4186 96 /**
ansond 0:137634ff4186 97 * \brief Blowfish-ECB block encryption/decryption
ansond 0:137634ff4186 98 *
ansond 0:137634ff4186 99 * \param ctx Blowfish context
ansond 0:137634ff4186 100 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
ansond 0:137634ff4186 101 * \param input 8-byte input block
ansond 0:137634ff4186 102 * \param output 8-byte output block
ansond 0:137634ff4186 103 *
ansond 0:137634ff4186 104 * \return 0 if successful
ansond 0:137634ff4186 105 */
ansond 0:137634ff4186 106 int blowfish_crypt_ecb( blowfish_context *ctx,
ansond 0:137634ff4186 107 int mode,
ansond 0:137634ff4186 108 const unsigned char input[BLOWFISH_BLOCKSIZE],
ansond 0:137634ff4186 109 unsigned char output[BLOWFISH_BLOCKSIZE] );
ansond 0:137634ff4186 110
ansond 0:137634ff4186 111 #if defined(POLARSSL_CIPHER_MODE_CBC)
ansond 0:137634ff4186 112 /**
ansond 0:137634ff4186 113 * \brief Blowfish-CBC buffer encryption/decryption
ansond 0:137634ff4186 114 * Length should be a multiple of the block
ansond 0:137634ff4186 115 * size (8 bytes)
ansond 0:137634ff4186 116 *
ansond 0:137634ff4186 117 * \note Upon exit, the content of the IV is updated so that you can
ansond 0:137634ff4186 118 * call the function same function again on the following
ansond 0:137634ff4186 119 * block(s) of data and get the same result as if it was
ansond 0:137634ff4186 120 * encrypted in one call. This allows a "streaming" usage.
ansond 0:137634ff4186 121 * If on the other hand you need to retain the contents of the
ansond 0:137634ff4186 122 * IV, you should either save it manually or use the cipher
ansond 0:137634ff4186 123 * module instead.
ansond 0:137634ff4186 124 *
ansond 0:137634ff4186 125 * \param ctx Blowfish context
ansond 0:137634ff4186 126 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
ansond 0:137634ff4186 127 * \param length length of the input data
ansond 0:137634ff4186 128 * \param iv initialization vector (updated after use)
ansond 0:137634ff4186 129 * \param input buffer holding the input data
ansond 0:137634ff4186 130 * \param output buffer holding the output data
ansond 0:137634ff4186 131 *
ansond 0:137634ff4186 132 * \return 0 if successful, or
ansond 0:137634ff4186 133 * POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH
ansond 0:137634ff4186 134 */
ansond 0:137634ff4186 135 int blowfish_crypt_cbc( blowfish_context *ctx,
ansond 0:137634ff4186 136 int mode,
ansond 0:137634ff4186 137 size_t length,
ansond 0:137634ff4186 138 unsigned char iv[BLOWFISH_BLOCKSIZE],
ansond 0:137634ff4186 139 const unsigned char *input,
ansond 0:137634ff4186 140 unsigned char *output );
ansond 0:137634ff4186 141 #endif /* POLARSSL_CIPHER_MODE_CBC */
ansond 0:137634ff4186 142
ansond 0:137634ff4186 143 #if defined(POLARSSL_CIPHER_MODE_CFB)
ansond 0:137634ff4186 144 /**
ansond 0:137634ff4186 145 * \brief Blowfish CFB buffer encryption/decryption.
ansond 0:137634ff4186 146 *
ansond 0:137634ff4186 147 * \note Upon exit, the content of the IV is updated so that you can
ansond 0:137634ff4186 148 * call the function same function again on the following
ansond 0:137634ff4186 149 * block(s) of data and get the same result as if it was
ansond 0:137634ff4186 150 * encrypted in one call. This allows a "streaming" usage.
ansond 0:137634ff4186 151 * If on the other hand you need to retain the contents of the
ansond 0:137634ff4186 152 * IV, you should either save it manually or use the cipher
ansond 0:137634ff4186 153 * module instead.
ansond 0:137634ff4186 154 *
ansond 0:137634ff4186 155 * \param ctx Blowfish context
ansond 0:137634ff4186 156 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
ansond 0:137634ff4186 157 * \param length length of the input data
ansond 0:137634ff4186 158 * \param iv_off offset in IV (updated after use)
ansond 0:137634ff4186 159 * \param iv initialization vector (updated after use)
ansond 0:137634ff4186 160 * \param input buffer holding the input data
ansond 0:137634ff4186 161 * \param output buffer holding the output data
ansond 0:137634ff4186 162 *
ansond 0:137634ff4186 163 * \return 0 if successful
ansond 0:137634ff4186 164 */
ansond 0:137634ff4186 165 int blowfish_crypt_cfb64( blowfish_context *ctx,
ansond 0:137634ff4186 166 int mode,
ansond 0:137634ff4186 167 size_t length,
ansond 0:137634ff4186 168 size_t *iv_off,
ansond 0:137634ff4186 169 unsigned char iv[BLOWFISH_BLOCKSIZE],
ansond 0:137634ff4186 170 const unsigned char *input,
ansond 0:137634ff4186 171 unsigned char *output );
ansond 0:137634ff4186 172 #endif /*POLARSSL_CIPHER_MODE_CFB */
ansond 0:137634ff4186 173
ansond 0:137634ff4186 174 #if defined(POLARSSL_CIPHER_MODE_CTR)
ansond 0:137634ff4186 175 /**
ansond 0:137634ff4186 176 * \brief Blowfish-CTR buffer encryption/decryption
ansond 0:137634ff4186 177 *
ansond 0:137634ff4186 178 * Warning: You have to keep the maximum use of your counter in mind!
ansond 0:137634ff4186 179 *
ansond 0:137634ff4186 180 * \param ctx Blowfish context
ansond 0:137634ff4186 181 * \param length The length of the data
ansond 0:137634ff4186 182 * \param nc_off The offset in the current stream_block (for resuming
ansond 0:137634ff4186 183 * within current cipher stream). The offset pointer to
ansond 0:137634ff4186 184 * should be 0 at the start of a stream.
ansond 0:137634ff4186 185 * \param nonce_counter The 64-bit nonce and counter.
ansond 0:137634ff4186 186 * \param stream_block The saved stream-block for resuming. Is overwritten
ansond 0:137634ff4186 187 * by the function.
ansond 0:137634ff4186 188 * \param input The input data stream
ansond 0:137634ff4186 189 * \param output The output data stream
ansond 0:137634ff4186 190 *
ansond 0:137634ff4186 191 * \return 0 if successful
ansond 0:137634ff4186 192 */
ansond 0:137634ff4186 193 int blowfish_crypt_ctr( blowfish_context *ctx,
ansond 0:137634ff4186 194 size_t length,
ansond 0:137634ff4186 195 size_t *nc_off,
ansond 0:137634ff4186 196 unsigned char nonce_counter[BLOWFISH_BLOCKSIZE],
ansond 0:137634ff4186 197 unsigned char stream_block[BLOWFISH_BLOCKSIZE],
ansond 0:137634ff4186 198 const unsigned char *input,
ansond 0:137634ff4186 199 unsigned char *output );
ansond 0:137634ff4186 200 #endif /* POLARSSL_CIPHER_MODE_CTR */
ansond 0:137634ff4186 201
ansond 0:137634ff4186 202 #ifdef __cplusplus
ansond 0:137634ff4186 203 }
ansond 0:137634ff4186 204 #endif
ansond 0:137634ff4186 205
ansond 0:137634ff4186 206 #else /* POLARSSL_BLOWFISH_ALT */
ansond 0:137634ff4186 207 #include "blowfish_alt.h"
ansond 0:137634ff4186 208 #endif /* POLARSSL_BLOWFISH_ALT */
ansond 0:137634ff4186 209
ansond 0:137634ff4186 210 #endif /* blowfish.h */
ansond 0:137634ff4186 211