blob: 313d898bb17983abbbad5fd9f2a6f8925bd74160 [file] [log] [blame]
Paul Bakkera9379c02012-07-04 11:02:11 +00001/**
2 * \file blowfish.h
3 *
4 * \brief Blowfish block cipher
5 *
6 * Copyright (C) 2012-2012, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_BLOWFISH_H
28#define POLARSSL_BLOWFISH_H
29
30#include <string.h>
31
Paul Bakker5c2364c2012-10-01 14:41:15 +000032#ifdef _MSC_VER
33#include <basetsd.h>
34typedef UINT32 uint32_t;
35#else
36#include <inttypes.h>
37#endif
38
Paul Bakkera9379c02012-07-04 11:02:11 +000039#define BLOWFISH_ENCRYPT 1
40#define BLOWFISH_DECRYPT 0
41#define BLOWFISH_MAX_KEY 448
42#define BLOWFISH_MIN_KEY 32
43#define BLOWFISH_ROUNDS 16 /* when increasing this value, make sure to extend the initialisation vectors */
44#define BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
45
46#define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
47#define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
48
49/**
50 * \brief Blowfish context structure
51 */
52typedef struct
53{
Paul Bakker5c2364c2012-10-01 14:41:15 +000054 uint32_t P[BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
55 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000056}
57blowfish_context;
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62
63/**
Paul Bakker6132d0a2012-07-04 17:10:40 +000064 * \brief Blowfish key schedule
Paul Bakkera9379c02012-07-04 11:02:11 +000065 *
66 * \param ctx Blowfish context to be initialized
67 * \param key encryption key
68 * \param keysize must be between 32 and 448 bits
69 *
70 * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH
71 */
72int blowfish_setkey( blowfish_context *ctx, const unsigned char *key, unsigned int keysize );
73
74/**
75 * \brief Blowfish-ECB block encryption/decryption
76 *
77 * \param ctx Blowfish context
78 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
79 * \param input 8-byte input block
80 * \param output 8-byte output block
81 *
82 * \return 0 if successful
83 */
84int blowfish_crypt_ecb( blowfish_context *ctx,
85 int mode,
86 const unsigned char input[BLOWFISH_BLOCKSIZE],
87 unsigned char output[BLOWFISH_BLOCKSIZE] );
88
89/**
90 * \brief Blowfish-CBC buffer encryption/decryption
91 * Length should be a multiple of the block
92 * size (8 bytes)
93 *
94 * \param ctx Blowfish context
95 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
96 * \param length length of the input data
97 * \param iv initialization vector (updated after use)
98 * \param input buffer holding the input data
99 * \param output buffer holding the output data
100 *
101 * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH
102 */
103int blowfish_crypt_cbc( blowfish_context *ctx,
104 int mode,
105 size_t length,
106 unsigned char iv[BLOWFISH_BLOCKSIZE],
107 const unsigned char *input,
108 unsigned char *output );
109
110/**
111 * \brief Blowfish CFB buffer encryption/decryption.
112 *
113 * both
114 * \param ctx Blowfish context
115 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
116 * \param length length of the input data
117 * \param iv_off offset in IV (updated after use)
118 * \param iv initialization vector (updated after use)
119 * \param input buffer holding the input data
120 * \param output buffer holding the output data
121 *
122 * \return 0 if successful
123 */
124int blowfish_crypt_cfb64( blowfish_context *ctx,
125 int mode,
126 size_t length,
127 size_t *iv_off,
128 unsigned char iv[BLOWFISH_BLOCKSIZE],
129 const unsigned char *input,
130 unsigned char *output );
131
Paul Bakker9a736322012-11-14 12:39:52 +0000132/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000133 * \brief Blowfish-CTR buffer encryption/decryption
134 *
135 * Warning: You have to keep the maximum use of your counter in mind!
136 *
137 * \param length The length of the data
138 * \param nc_off The offset in the current stream_block (for resuming
139 * within current cipher stream). The offset pointer to
140 * should be 0 at the start of a stream.
141 * \param nonce_counter The 64-bit nonce and counter.
142 * \param stream_block The saved stream-block for resuming. Is overwritten
143 * by the function.
144 * \param input The input data stream
145 * \param output The output data stream
146 *
147 * \return 0 if successful
148 */
149int blowfish_crypt_ctr( blowfish_context *ctx,
150 size_t length,
151 size_t *nc_off,
152 unsigned char nonce_counter[BLOWFISH_BLOCKSIZE],
153 unsigned char stream_block[BLOWFISH_BLOCKSIZE],
154 const unsigned char *input,
155 unsigned char *output );
156
157#ifdef __cplusplus
158}
159#endif
160
161#endif /* blowfish.h */