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