mbed TLS Build
include/mbedtls/blowfish.h@0:cdf462088d13, 2017-01-05 (annotated)
- Committer:
- markrad
- Date:
- Thu Jan 05 00:18:44 2017 +0000
- Revision:
- 0:cdf462088d13
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /** |
markrad | 0:cdf462088d13 | 2 | * \file blowfish.h |
markrad | 0:cdf462088d13 | 3 | * |
markrad | 0:cdf462088d13 | 4 | * \brief Blowfish block cipher |
markrad | 0:cdf462088d13 | 5 | * |
markrad | 0:cdf462088d13 | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
markrad | 0:cdf462088d13 | 7 | * SPDX-License-Identifier: Apache-2.0 |
markrad | 0:cdf462088d13 | 8 | * |
markrad | 0:cdf462088d13 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
markrad | 0:cdf462088d13 | 10 | * not use this file except in compliance with the License. |
markrad | 0:cdf462088d13 | 11 | * You may obtain a copy of the License at |
markrad | 0:cdf462088d13 | 12 | * |
markrad | 0:cdf462088d13 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
markrad | 0:cdf462088d13 | 14 | * |
markrad | 0:cdf462088d13 | 15 | * Unless required by applicable law or agreed to in writing, software |
markrad | 0:cdf462088d13 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
markrad | 0:cdf462088d13 | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
markrad | 0:cdf462088d13 | 18 | * See the License for the specific language governing permissions and |
markrad | 0:cdf462088d13 | 19 | * limitations under the License. |
markrad | 0:cdf462088d13 | 20 | * |
markrad | 0:cdf462088d13 | 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
markrad | 0:cdf462088d13 | 22 | */ |
markrad | 0:cdf462088d13 | 23 | #ifndef MBEDTLS_BLOWFISH_H |
markrad | 0:cdf462088d13 | 24 | #define MBEDTLS_BLOWFISH_H |
markrad | 0:cdf462088d13 | 25 | |
markrad | 0:cdf462088d13 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) |
markrad | 0:cdf462088d13 | 27 | #include "config.h" |
markrad | 0:cdf462088d13 | 28 | #else |
markrad | 0:cdf462088d13 | 29 | #include MBEDTLS_CONFIG_FILE |
markrad | 0:cdf462088d13 | 30 | #endif |
markrad | 0:cdf462088d13 | 31 | |
markrad | 0:cdf462088d13 | 32 | #include <stddef.h> |
markrad | 0:cdf462088d13 | 33 | #include <stdint.h> |
markrad | 0:cdf462088d13 | 34 | |
markrad | 0:cdf462088d13 | 35 | #define MBEDTLS_BLOWFISH_ENCRYPT 1 |
markrad | 0:cdf462088d13 | 36 | #define MBEDTLS_BLOWFISH_DECRYPT 0 |
markrad | 0:cdf462088d13 | 37 | #define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448 |
markrad | 0:cdf462088d13 | 38 | #define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32 |
markrad | 0:cdf462088d13 | 39 | #define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */ |
markrad | 0:cdf462088d13 | 40 | #define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */ |
markrad | 0:cdf462088d13 | 41 | |
markrad | 0:cdf462088d13 | 42 | #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */ |
markrad | 0:cdf462088d13 | 43 | #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ |
markrad | 0:cdf462088d13 | 44 | |
markrad | 0:cdf462088d13 | 45 | #if !defined(MBEDTLS_BLOWFISH_ALT) |
markrad | 0:cdf462088d13 | 46 | // Regular implementation |
markrad | 0:cdf462088d13 | 47 | // |
markrad | 0:cdf462088d13 | 48 | |
markrad | 0:cdf462088d13 | 49 | #ifdef __cplusplus |
markrad | 0:cdf462088d13 | 50 | extern "C" { |
markrad | 0:cdf462088d13 | 51 | #endif |
markrad | 0:cdf462088d13 | 52 | |
markrad | 0:cdf462088d13 | 53 | /** |
markrad | 0:cdf462088d13 | 54 | * \brief Blowfish context structure |
markrad | 0:cdf462088d13 | 55 | */ |
markrad | 0:cdf462088d13 | 56 | typedef struct |
markrad | 0:cdf462088d13 | 57 | { |
markrad | 0:cdf462088d13 | 58 | uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ |
markrad | 0:cdf462088d13 | 59 | uint32_t S[4][256]; /*!< key dependent S-boxes */ |
markrad | 0:cdf462088d13 | 60 | } |
markrad | 0:cdf462088d13 | 61 | mbedtls_blowfish_context; |
markrad | 0:cdf462088d13 | 62 | |
markrad | 0:cdf462088d13 | 63 | /** |
markrad | 0:cdf462088d13 | 64 | * \brief Initialize Blowfish context |
markrad | 0:cdf462088d13 | 65 | * |
markrad | 0:cdf462088d13 | 66 | * \param ctx Blowfish context to be initialized |
markrad | 0:cdf462088d13 | 67 | */ |
markrad | 0:cdf462088d13 | 68 | void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx ); |
markrad | 0:cdf462088d13 | 69 | |
markrad | 0:cdf462088d13 | 70 | /** |
markrad | 0:cdf462088d13 | 71 | * \brief Clear Blowfish context |
markrad | 0:cdf462088d13 | 72 | * |
markrad | 0:cdf462088d13 | 73 | * \param ctx Blowfish context to be cleared |
markrad | 0:cdf462088d13 | 74 | */ |
markrad | 0:cdf462088d13 | 75 | void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx ); |
markrad | 0:cdf462088d13 | 76 | |
markrad | 0:cdf462088d13 | 77 | /** |
markrad | 0:cdf462088d13 | 78 | * \brief Blowfish key schedule |
markrad | 0:cdf462088d13 | 79 | * |
markrad | 0:cdf462088d13 | 80 | * \param ctx Blowfish context to be initialized |
markrad | 0:cdf462088d13 | 81 | * \param key encryption key |
markrad | 0:cdf462088d13 | 82 | * \param keybits must be between 32 and 448 bits |
markrad | 0:cdf462088d13 | 83 | * |
markrad | 0:cdf462088d13 | 84 | * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH |
markrad | 0:cdf462088d13 | 85 | */ |
markrad | 0:cdf462088d13 | 86 | int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key, |
markrad | 0:cdf462088d13 | 87 | unsigned int keybits ); |
markrad | 0:cdf462088d13 | 88 | |
markrad | 0:cdf462088d13 | 89 | /** |
markrad | 0:cdf462088d13 | 90 | * \brief Blowfish-ECB block encryption/decryption |
markrad | 0:cdf462088d13 | 91 | * |
markrad | 0:cdf462088d13 | 92 | * \param ctx Blowfish context |
markrad | 0:cdf462088d13 | 93 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
markrad | 0:cdf462088d13 | 94 | * \param input 8-byte input block |
markrad | 0:cdf462088d13 | 95 | * \param output 8-byte output block |
markrad | 0:cdf462088d13 | 96 | * |
markrad | 0:cdf462088d13 | 97 | * \return 0 if successful |
markrad | 0:cdf462088d13 | 98 | */ |
markrad | 0:cdf462088d13 | 99 | int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx, |
markrad | 0:cdf462088d13 | 100 | int mode, |
markrad | 0:cdf462088d13 | 101 | const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE], |
markrad | 0:cdf462088d13 | 102 | unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] ); |
markrad | 0:cdf462088d13 | 103 | |
markrad | 0:cdf462088d13 | 104 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
markrad | 0:cdf462088d13 | 105 | /** |
markrad | 0:cdf462088d13 | 106 | * \brief Blowfish-CBC buffer encryption/decryption |
markrad | 0:cdf462088d13 | 107 | * Length should be a multiple of the block |
markrad | 0:cdf462088d13 | 108 | * size (8 bytes) |
markrad | 0:cdf462088d13 | 109 | * |
markrad | 0:cdf462088d13 | 110 | * \note Upon exit, the content of the IV is updated so that you can |
markrad | 0:cdf462088d13 | 111 | * call the function same function again on the following |
markrad | 0:cdf462088d13 | 112 | * block(s) of data and get the same result as if it was |
markrad | 0:cdf462088d13 | 113 | * encrypted in one call. This allows a "streaming" usage. |
markrad | 0:cdf462088d13 | 114 | * If on the other hand you need to retain the contents of the |
markrad | 0:cdf462088d13 | 115 | * IV, you should either save it manually or use the cipher |
markrad | 0:cdf462088d13 | 116 | * module instead. |
markrad | 0:cdf462088d13 | 117 | * |
markrad | 0:cdf462088d13 | 118 | * \param ctx Blowfish context |
markrad | 0:cdf462088d13 | 119 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
markrad | 0:cdf462088d13 | 120 | * \param length length of the input data |
markrad | 0:cdf462088d13 | 121 | * \param iv initialization vector (updated after use) |
markrad | 0:cdf462088d13 | 122 | * \param input buffer holding the input data |
markrad | 0:cdf462088d13 | 123 | * \param output buffer holding the output data |
markrad | 0:cdf462088d13 | 124 | * |
markrad | 0:cdf462088d13 | 125 | * \return 0 if successful, or |
markrad | 0:cdf462088d13 | 126 | * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH |
markrad | 0:cdf462088d13 | 127 | */ |
markrad | 0:cdf462088d13 | 128 | int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx, |
markrad | 0:cdf462088d13 | 129 | int mode, |
markrad | 0:cdf462088d13 | 130 | size_t length, |
markrad | 0:cdf462088d13 | 131 | unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], |
markrad | 0:cdf462088d13 | 132 | const unsigned char *input, |
markrad | 0:cdf462088d13 | 133 | unsigned char *output ); |
markrad | 0:cdf462088d13 | 134 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
markrad | 0:cdf462088d13 | 135 | |
markrad | 0:cdf462088d13 | 136 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
markrad | 0:cdf462088d13 | 137 | /** |
markrad | 0:cdf462088d13 | 138 | * \brief Blowfish CFB buffer encryption/decryption. |
markrad | 0:cdf462088d13 | 139 | * |
markrad | 0:cdf462088d13 | 140 | * \note Upon exit, the content of the IV is updated so that you can |
markrad | 0:cdf462088d13 | 141 | * call the function same function again on the following |
markrad | 0:cdf462088d13 | 142 | * block(s) of data and get the same result as if it was |
markrad | 0:cdf462088d13 | 143 | * encrypted in one call. This allows a "streaming" usage. |
markrad | 0:cdf462088d13 | 144 | * If on the other hand you need to retain the contents of the |
markrad | 0:cdf462088d13 | 145 | * IV, you should either save it manually or use the cipher |
markrad | 0:cdf462088d13 | 146 | * module instead. |
markrad | 0:cdf462088d13 | 147 | * |
markrad | 0:cdf462088d13 | 148 | * \param ctx Blowfish context |
markrad | 0:cdf462088d13 | 149 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT |
markrad | 0:cdf462088d13 | 150 | * \param length length of the input data |
markrad | 0:cdf462088d13 | 151 | * \param iv_off offset in IV (updated after use) |
markrad | 0:cdf462088d13 | 152 | * \param iv initialization vector (updated after use) |
markrad | 0:cdf462088d13 | 153 | * \param input buffer holding the input data |
markrad | 0:cdf462088d13 | 154 | * \param output buffer holding the output data |
markrad | 0:cdf462088d13 | 155 | * |
markrad | 0:cdf462088d13 | 156 | * \return 0 if successful |
markrad | 0:cdf462088d13 | 157 | */ |
markrad | 0:cdf462088d13 | 158 | int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx, |
markrad | 0:cdf462088d13 | 159 | int mode, |
markrad | 0:cdf462088d13 | 160 | size_t length, |
markrad | 0:cdf462088d13 | 161 | size_t *iv_off, |
markrad | 0:cdf462088d13 | 162 | unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], |
markrad | 0:cdf462088d13 | 163 | const unsigned char *input, |
markrad | 0:cdf462088d13 | 164 | unsigned char *output ); |
markrad | 0:cdf462088d13 | 165 | #endif /*MBEDTLS_CIPHER_MODE_CFB */ |
markrad | 0:cdf462088d13 | 166 | |
markrad | 0:cdf462088d13 | 167 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
markrad | 0:cdf462088d13 | 168 | /** |
markrad | 0:cdf462088d13 | 169 | * \brief Blowfish-CTR buffer encryption/decryption |
markrad | 0:cdf462088d13 | 170 | * |
markrad | 0:cdf462088d13 | 171 | * Warning: You have to keep the maximum use of your counter in mind! |
markrad | 0:cdf462088d13 | 172 | * |
markrad | 0:cdf462088d13 | 173 | * \param ctx Blowfish context |
markrad | 0:cdf462088d13 | 174 | * \param length The length of the data |
markrad | 0:cdf462088d13 | 175 | * \param nc_off The offset in the current stream_block (for resuming |
markrad | 0:cdf462088d13 | 176 | * within current cipher stream). The offset pointer to |
markrad | 0:cdf462088d13 | 177 | * should be 0 at the start of a stream. |
markrad | 0:cdf462088d13 | 178 | * \param nonce_counter The 64-bit nonce and counter. |
markrad | 0:cdf462088d13 | 179 | * \param stream_block The saved stream-block for resuming. Is overwritten |
markrad | 0:cdf462088d13 | 180 | * by the function. |
markrad | 0:cdf462088d13 | 181 | * \param input The input data stream |
markrad | 0:cdf462088d13 | 182 | * \param output The output data stream |
markrad | 0:cdf462088d13 | 183 | * |
markrad | 0:cdf462088d13 | 184 | * \return 0 if successful |
markrad | 0:cdf462088d13 | 185 | */ |
markrad | 0:cdf462088d13 | 186 | int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx, |
markrad | 0:cdf462088d13 | 187 | size_t length, |
markrad | 0:cdf462088d13 | 188 | size_t *nc_off, |
markrad | 0:cdf462088d13 | 189 | unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE], |
markrad | 0:cdf462088d13 | 190 | unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE], |
markrad | 0:cdf462088d13 | 191 | const unsigned char *input, |
markrad | 0:cdf462088d13 | 192 | unsigned char *output ); |
markrad | 0:cdf462088d13 | 193 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
markrad | 0:cdf462088d13 | 194 | |
markrad | 0:cdf462088d13 | 195 | #ifdef __cplusplus |
markrad | 0:cdf462088d13 | 196 | } |
markrad | 0:cdf462088d13 | 197 | #endif |
markrad | 0:cdf462088d13 | 198 | |
markrad | 0:cdf462088d13 | 199 | #else /* MBEDTLS_BLOWFISH_ALT */ |
markrad | 0:cdf462088d13 | 200 | #include "blowfish_alt.h" |
markrad | 0:cdf462088d13 | 201 | #endif /* MBEDTLS_BLOWFISH_ALT */ |
markrad | 0:cdf462088d13 | 202 | |
markrad | 0:cdf462088d13 | 203 | #endif /* blowfish.h */ |