Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/include/polarssl/blowfish.h@0:796d0f61a05b, 2018-09-27 (annotated)
- Committer:
- HannesTschofenig
- Date:
- Thu Sep 27 06:34:22 2018 +0000
- Revision:
- 0:796d0f61a05b
Example AES-GCM test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /** |
HannesTschofenig | 0:796d0f61a05b | 2 | * \file blowfish.h |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * \brief Blowfish block cipher |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * Copyright (C) 2012-2014, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 7 | * |
HannesTschofenig | 0:796d0f61a05b | 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 12 | * |
HannesTschofenig | 0:796d0f61a05b | 13 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 14 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 15 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 16 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 17 | * |
HannesTschofenig | 0:796d0f61a05b | 18 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 21 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 22 | * |
HannesTschofenig | 0:796d0f61a05b | 23 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 26 | */ |
HannesTschofenig | 0:796d0f61a05b | 27 | #ifndef POLARSSL_BLOWFISH_H |
HannesTschofenig | 0:796d0f61a05b | 28 | #define POLARSSL_BLOWFISH_H |
HannesTschofenig | 0:796d0f61a05b | 29 | |
HannesTschofenig | 0:796d0f61a05b | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 31 | #include "config.h" |
HannesTschofenig | 0:796d0f61a05b | 32 | #else |
HannesTschofenig | 0:796d0f61a05b | 33 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 34 | #endif |
HannesTschofenig | 0:796d0f61a05b | 35 | |
HannesTschofenig | 0:796d0f61a05b | 36 | #include <string.h> |
HannesTschofenig | 0:796d0f61a05b | 37 | |
HannesTschofenig | 0:796d0f61a05b | 38 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
HannesTschofenig | 0:796d0f61a05b | 39 | #include <basetsd.h> |
HannesTschofenig | 0:796d0f61a05b | 40 | typedef UINT32 uint32_t; |
HannesTschofenig | 0:796d0f61a05b | 41 | #else |
HannesTschofenig | 0:796d0f61a05b | 42 | #include <inttypes.h> |
HannesTschofenig | 0:796d0f61a05b | 43 | #endif |
HannesTschofenig | 0:796d0f61a05b | 44 | |
HannesTschofenig | 0:796d0f61a05b | 45 | #define BLOWFISH_ENCRYPT 1 |
HannesTschofenig | 0:796d0f61a05b | 46 | #define BLOWFISH_DECRYPT 0 |
HannesTschofenig | 0:796d0f61a05b | 47 | #define BLOWFISH_MAX_KEY 448 |
HannesTschofenig | 0:796d0f61a05b | 48 | #define BLOWFISH_MIN_KEY 32 |
HannesTschofenig | 0:796d0f61a05b | 49 | #define BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */ |
HannesTschofenig | 0:796d0f61a05b | 50 | #define BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */ |
HannesTschofenig | 0:796d0f61a05b | 51 | |
HannesTschofenig | 0:796d0f61a05b | 52 | #define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */ |
HannesTschofenig | 0:796d0f61a05b | 53 | #define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ |
HannesTschofenig | 0:796d0f61a05b | 54 | |
HannesTschofenig | 0:796d0f61a05b | 55 | #if !defined(POLARSSL_BLOWFISH_ALT) |
HannesTschofenig | 0:796d0f61a05b | 56 | // Regular implementation |
HannesTschofenig | 0:796d0f61a05b | 57 | // |
HannesTschofenig | 0:796d0f61a05b | 58 | |
HannesTschofenig | 0:796d0f61a05b | 59 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 60 | extern "C" { |
HannesTschofenig | 0:796d0f61a05b | 61 | #endif |
HannesTschofenig | 0:796d0f61a05b | 62 | |
HannesTschofenig | 0:796d0f61a05b | 63 | /** |
HannesTschofenig | 0:796d0f61a05b | 64 | * \brief Blowfish context structure |
HannesTschofenig | 0:796d0f61a05b | 65 | */ |
HannesTschofenig | 0:796d0f61a05b | 66 | typedef struct |
HannesTschofenig | 0:796d0f61a05b | 67 | { |
HannesTschofenig | 0:796d0f61a05b | 68 | uint32_t P[BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ |
HannesTschofenig | 0:796d0f61a05b | 69 | uint32_t S[4][256]; /*!< key dependent S-boxes */ |
HannesTschofenig | 0:796d0f61a05b | 70 | } |
HannesTschofenig | 0:796d0f61a05b | 71 | blowfish_context; |
HannesTschofenig | 0:796d0f61a05b | 72 | |
HannesTschofenig | 0:796d0f61a05b | 73 | /** |
HannesTschofenig | 0:796d0f61a05b | 74 | * \brief Blowfish key schedule |
HannesTschofenig | 0:796d0f61a05b | 75 | * |
HannesTschofenig | 0:796d0f61a05b | 76 | * \param ctx Blowfish context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 77 | * \param key encryption key |
HannesTschofenig | 0:796d0f61a05b | 78 | * \param keysize must be between 32 and 448 bits |
HannesTschofenig | 0:796d0f61a05b | 79 | * |
HannesTschofenig | 0:796d0f61a05b | 80 | * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH |
HannesTschofenig | 0:796d0f61a05b | 81 | */ |
HannesTschofenig | 0:796d0f61a05b | 82 | int blowfish_setkey( blowfish_context *ctx, const unsigned char *key, |
HannesTschofenig | 0:796d0f61a05b | 83 | unsigned int keysize ); |
HannesTschofenig | 0:796d0f61a05b | 84 | |
HannesTschofenig | 0:796d0f61a05b | 85 | /** |
HannesTschofenig | 0:796d0f61a05b | 86 | * \brief Blowfish-ECB block encryption/decryption |
HannesTschofenig | 0:796d0f61a05b | 87 | * |
HannesTschofenig | 0:796d0f61a05b | 88 | * \param ctx Blowfish context |
HannesTschofenig | 0:796d0f61a05b | 89 | * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT |
HannesTschofenig | 0:796d0f61a05b | 90 | * \param input 8-byte input block |
HannesTschofenig | 0:796d0f61a05b | 91 | * \param output 8-byte output block |
HannesTschofenig | 0:796d0f61a05b | 92 | * |
HannesTschofenig | 0:796d0f61a05b | 93 | * \return 0 if successful |
HannesTschofenig | 0:796d0f61a05b | 94 | */ |
HannesTschofenig | 0:796d0f61a05b | 95 | int blowfish_crypt_ecb( blowfish_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 96 | int mode, |
HannesTschofenig | 0:796d0f61a05b | 97 | const unsigned char input[BLOWFISH_BLOCKSIZE], |
HannesTschofenig | 0:796d0f61a05b | 98 | unsigned char output[BLOWFISH_BLOCKSIZE] ); |
HannesTschofenig | 0:796d0f61a05b | 99 | |
HannesTschofenig | 0:796d0f61a05b | 100 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
HannesTschofenig | 0:796d0f61a05b | 101 | /** |
HannesTschofenig | 0:796d0f61a05b | 102 | * \brief Blowfish-CBC buffer encryption/decryption |
HannesTschofenig | 0:796d0f61a05b | 103 | * Length should be a multiple of the block |
HannesTschofenig | 0:796d0f61a05b | 104 | * size (8 bytes) |
HannesTschofenig | 0:796d0f61a05b | 105 | * |
HannesTschofenig | 0:796d0f61a05b | 106 | * \param ctx Blowfish context |
HannesTschofenig | 0:796d0f61a05b | 107 | * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT |
HannesTschofenig | 0:796d0f61a05b | 108 | * \param length length of the input data |
HannesTschofenig | 0:796d0f61a05b | 109 | * \param iv initialization vector (updated after use) |
HannesTschofenig | 0:796d0f61a05b | 110 | * \param input buffer holding the input data |
HannesTschofenig | 0:796d0f61a05b | 111 | * \param output buffer holding the output data |
HannesTschofenig | 0:796d0f61a05b | 112 | * |
HannesTschofenig | 0:796d0f61a05b | 113 | * \return 0 if successful, or |
HannesTschofenig | 0:796d0f61a05b | 114 | * POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH |
HannesTschofenig | 0:796d0f61a05b | 115 | */ |
HannesTschofenig | 0:796d0f61a05b | 116 | int blowfish_crypt_cbc( blowfish_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 117 | int mode, |
HannesTschofenig | 0:796d0f61a05b | 118 | size_t length, |
HannesTschofenig | 0:796d0f61a05b | 119 | unsigned char iv[BLOWFISH_BLOCKSIZE], |
HannesTschofenig | 0:796d0f61a05b | 120 | const unsigned char *input, |
HannesTschofenig | 0:796d0f61a05b | 121 | unsigned char *output ); |
HannesTschofenig | 0:796d0f61a05b | 122 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
HannesTschofenig | 0:796d0f61a05b | 123 | |
HannesTschofenig | 0:796d0f61a05b | 124 | #if defined(POLARSSL_CIPHER_MODE_CFB) |
HannesTschofenig | 0:796d0f61a05b | 125 | /** |
HannesTschofenig | 0:796d0f61a05b | 126 | * \brief Blowfish CFB buffer encryption/decryption. |
HannesTschofenig | 0:796d0f61a05b | 127 | * |
HannesTschofenig | 0:796d0f61a05b | 128 | * \param ctx Blowfish context |
HannesTschofenig | 0:796d0f61a05b | 129 | * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT |
HannesTschofenig | 0:796d0f61a05b | 130 | * \param length length of the input data |
HannesTschofenig | 0:796d0f61a05b | 131 | * \param iv_off offset in IV (updated after use) |
HannesTschofenig | 0:796d0f61a05b | 132 | * \param iv initialization vector (updated after use) |
HannesTschofenig | 0:796d0f61a05b | 133 | * \param input buffer holding the input data |
HannesTschofenig | 0:796d0f61a05b | 134 | * \param output buffer holding the output data |
HannesTschofenig | 0:796d0f61a05b | 135 | * |
HannesTschofenig | 0:796d0f61a05b | 136 | * \return 0 if successful |
HannesTschofenig | 0:796d0f61a05b | 137 | */ |
HannesTschofenig | 0:796d0f61a05b | 138 | int blowfish_crypt_cfb64( blowfish_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 139 | int mode, |
HannesTschofenig | 0:796d0f61a05b | 140 | size_t length, |
HannesTschofenig | 0:796d0f61a05b | 141 | size_t *iv_off, |
HannesTschofenig | 0:796d0f61a05b | 142 | unsigned char iv[BLOWFISH_BLOCKSIZE], |
HannesTschofenig | 0:796d0f61a05b | 143 | const unsigned char *input, |
HannesTschofenig | 0:796d0f61a05b | 144 | unsigned char *output ); |
HannesTschofenig | 0:796d0f61a05b | 145 | #endif /*POLARSSL_CIPHER_MODE_CFB */ |
HannesTschofenig | 0:796d0f61a05b | 146 | |
HannesTschofenig | 0:796d0f61a05b | 147 | #if defined(POLARSSL_CIPHER_MODE_CTR) |
HannesTschofenig | 0:796d0f61a05b | 148 | /** |
HannesTschofenig | 0:796d0f61a05b | 149 | * \brief Blowfish-CTR buffer encryption/decryption |
HannesTschofenig | 0:796d0f61a05b | 150 | * |
HannesTschofenig | 0:796d0f61a05b | 151 | * Warning: You have to keep the maximum use of your counter in mind! |
HannesTschofenig | 0:796d0f61a05b | 152 | * |
HannesTschofenig | 0:796d0f61a05b | 153 | * \param ctx Blowfish context |
HannesTschofenig | 0:796d0f61a05b | 154 | * \param length The length of the data |
HannesTschofenig | 0:796d0f61a05b | 155 | * \param nc_off The offset in the current stream_block (for resuming |
HannesTschofenig | 0:796d0f61a05b | 156 | * within current cipher stream). The offset pointer to |
HannesTschofenig | 0:796d0f61a05b | 157 | * should be 0 at the start of a stream. |
HannesTschofenig | 0:796d0f61a05b | 158 | * \param nonce_counter The 64-bit nonce and counter. |
HannesTschofenig | 0:796d0f61a05b | 159 | * \param stream_block The saved stream-block for resuming. Is overwritten |
HannesTschofenig | 0:796d0f61a05b | 160 | * by the function. |
HannesTschofenig | 0:796d0f61a05b | 161 | * \param input The input data stream |
HannesTschofenig | 0:796d0f61a05b | 162 | * \param output The output data stream |
HannesTschofenig | 0:796d0f61a05b | 163 | * |
HannesTschofenig | 0:796d0f61a05b | 164 | * \return 0 if successful |
HannesTschofenig | 0:796d0f61a05b | 165 | */ |
HannesTschofenig | 0:796d0f61a05b | 166 | int blowfish_crypt_ctr( blowfish_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 167 | size_t length, |
HannesTschofenig | 0:796d0f61a05b | 168 | size_t *nc_off, |
HannesTschofenig | 0:796d0f61a05b | 169 | unsigned char nonce_counter[BLOWFISH_BLOCKSIZE], |
HannesTschofenig | 0:796d0f61a05b | 170 | unsigned char stream_block[BLOWFISH_BLOCKSIZE], |
HannesTschofenig | 0:796d0f61a05b | 171 | const unsigned char *input, |
HannesTschofenig | 0:796d0f61a05b | 172 | unsigned char *output ); |
HannesTschofenig | 0:796d0f61a05b | 173 | #endif /* POLARSSL_CIPHER_MODE_CTR */ |
HannesTschofenig | 0:796d0f61a05b | 174 | |
HannesTschofenig | 0:796d0f61a05b | 175 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 176 | } |
HannesTschofenig | 0:796d0f61a05b | 177 | #endif |
HannesTschofenig | 0:796d0f61a05b | 178 | |
HannesTschofenig | 0:796d0f61a05b | 179 | #else /* POLARSSL_BLOWFISH_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 180 | #include "blowfish_alt.h" |
HannesTschofenig | 0:796d0f61a05b | 181 | #endif /* POLARSSL_BLOWFISH_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 182 | |
HannesTschofenig | 0:796d0f61a05b | 183 | #endif /* blowfish.h */ |
HannesTschofenig | 0:796d0f61a05b | 184 | |
HannesTschofenig | 0:796d0f61a05b | 185 |