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