blob: f91b38da66e80f5918a2473bcaf8c29c99e0cd4d [file] [log] [blame]
Paul Bakkera9379c02012-07-04 11:02:11 +00001/**
2 * \file blowfish.h
3 *
4 * \brief Blowfish block cipher
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2012-2014, ARM Limited, All Rights Reserved
Paul Bakkera9379c02012-07-04 11:02:11 +00007 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00008 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkera9379c02012-07-04 11:02:11 +00009 *
Paul Bakkera9379c02012-07-04 11:02:11 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#ifndef POLARSSL_BLOWFISH_H
25#define POLARSSL_BLOWFISH_H
26
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Paul Bakkera9379c02012-07-04 11:02:11 +000033#include <string.h>
34
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000036#include <basetsd.h>
37typedef UINT32 uint32_t;
38#else
39#include <inttypes.h>
40#endif
41
Paul Bakkera9379c02012-07-04 11:02:11 +000042#define BLOWFISH_ENCRYPT 1
43#define BLOWFISH_DECRYPT 0
44#define BLOWFISH_MAX_KEY 448
45#define BLOWFISH_MIN_KEY 32
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020046#define BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
Paul Bakkera9379c02012-07-04 11:02:11 +000047#define BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
48
49#define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
50#define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
51
Paul Bakker90995b52013-06-24 19:20:35 +020052#if !defined(POLARSSL_BLOWFISH_ALT)
53// Regular implementation
54//
55
Paul Bakker407a0da2013-06-27 14:29:21 +020056#ifdef __cplusplus
57extern "C" {
58#endif
59
Paul Bakkera9379c02012-07-04 11:02:11 +000060/**
61 * \brief Blowfish context structure
62 */
63typedef struct
64{
Paul Bakker5c2364c2012-10-01 14:41:15 +000065 uint32_t P[BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
66 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000067}
68blowfish_context;
69
Paul Bakkera9379c02012-07-04 11:02:11 +000070/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020071 * \brief Initialize Blowfish context
72 *
73 * \param ctx Blowfish context to be initialized
74 */
75void blowfish_init( blowfish_context *ctx );
76
77/**
78 * \brief Clear Blowfish context
79 *
80 * \param ctx Blowfish context to be cleared
81 */
82void blowfish_free( blowfish_context *ctx );
83
84/**
Paul Bakker6132d0a2012-07-04 17:10:40 +000085 * \brief Blowfish key schedule
Paul Bakkera9379c02012-07-04 11:02:11 +000086 *
87 * \param ctx Blowfish context to be initialized
88 * \param key encryption key
89 * \param keysize must be between 32 and 448 bits
90 *
91 * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH
92 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020093int blowfish_setkey( blowfish_context *ctx, const unsigned char *key,
94 unsigned int keysize );
Paul Bakkera9379c02012-07-04 11:02:11 +000095
96/**
97 * \brief Blowfish-ECB block encryption/decryption
98 *
99 * \param ctx Blowfish context
100 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
101 * \param input 8-byte input block
102 * \param output 8-byte output block
103 *
104 * \return 0 if successful
105 */
106int blowfish_crypt_ecb( blowfish_context *ctx,
107 int mode,
108 const unsigned char input[BLOWFISH_BLOCKSIZE],
109 unsigned char output[BLOWFISH_BLOCKSIZE] );
110
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200111#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000112/**
113 * \brief Blowfish-CBC buffer encryption/decryption
114 * Length should be a multiple of the block
115 * size (8 bytes)
116 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000117 * \note Upon exit, the content of the IV is updated so that you can
118 * call the function same function again on the following
119 * block(s) of data and get the same result as if it was
120 * encrypted in one call. This allows a "streaming" usage.
121 * If on the other hand you need to retain the contents of the
122 * IV, you should either save it manually or use the cipher
123 * module instead.
124 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000125 * \param ctx Blowfish context
126 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
127 * \param length length of the input data
128 * \param iv initialization vector (updated after use)
129 * \param input buffer holding the input data
130 * \param output buffer holding the output data
131 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200132 * \return 0 if successful, or
133 * POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +0000134 */
135int blowfish_crypt_cbc( blowfish_context *ctx,
136 int mode,
137 size_t length,
138 unsigned char iv[BLOWFISH_BLOCKSIZE],
139 const unsigned char *input,
140 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200141#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000142
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200143#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000144/**
145 * \brief Blowfish CFB buffer encryption/decryption.
146 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000147 * \note Upon exit, the content of the IV is updated so that you can
148 * call the function same function again on the following
149 * block(s) of data and get the same result as if it was
150 * encrypted in one call. This allows a "streaming" usage.
151 * If on the other hand you need to retain the contents of the
152 * IV, you should either save it manually or use the cipher
153 * module instead.
154 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000155 * \param ctx Blowfish context
156 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
157 * \param length length of the input data
158 * \param iv_off offset in IV (updated after use)
159 * \param iv initialization vector (updated after use)
160 * \param input buffer holding the input data
161 * \param output buffer holding the output data
162 *
163 * \return 0 if successful
164 */
165int blowfish_crypt_cfb64( blowfish_context *ctx,
166 int mode,
167 size_t length,
168 size_t *iv_off,
169 unsigned char iv[BLOWFISH_BLOCKSIZE],
170 const unsigned char *input,
171 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200172#endif /*POLARSSL_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000173
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200174#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000175/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000176 * \brief Blowfish-CTR buffer encryption/decryption
177 *
178 * Warning: You have to keep the maximum use of your counter in mind!
179 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200180 * \param ctx Blowfish context
Paul Bakkera9379c02012-07-04 11:02:11 +0000181 * \param length The length of the data
182 * \param nc_off The offset in the current stream_block (for resuming
183 * within current cipher stream). The offset pointer to
184 * should be 0 at the start of a stream.
185 * \param nonce_counter The 64-bit nonce and counter.
186 * \param stream_block The saved stream-block for resuming. Is overwritten
187 * by the function.
188 * \param input The input data stream
189 * \param output The output data stream
190 *
191 * \return 0 if successful
192 */
193int blowfish_crypt_ctr( blowfish_context *ctx,
194 size_t length,
195 size_t *nc_off,
196 unsigned char nonce_counter[BLOWFISH_BLOCKSIZE],
197 unsigned char stream_block[BLOWFISH_BLOCKSIZE],
198 const unsigned char *input,
199 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200200#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000201
202#ifdef __cplusplus
203}
204#endif
205
Paul Bakker90995b52013-06-24 19:20:35 +0200206#else /* POLARSSL_BLOWFISH_ALT */
207#include "blowfish_alt.h"
208#endif /* POLARSSL_BLOWFISH_ALT */
209
Paul Bakkera9379c02012-07-04 11:02:11 +0000210#endif /* blowfish.h */