blob: 89f06bd5a0e356a7bcf3a288b4f14c96faf01271 [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
32#define BLOWFISH_ENCRYPT 1
33#define BLOWFISH_DECRYPT 0
34#define BLOWFISH_MAX_KEY 448
35#define BLOWFISH_MIN_KEY 32
36#define BLOWFISH_ROUNDS 16 /* when increasing this value, make sure to extend the initialisation vectors */
37#define BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
38
39#define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
40#define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
41
42/**
43 * \brief Blowfish context structure
44 */
45typedef struct
46{
47 unsigned long P[BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
48 unsigned long S[4][256]; /*!< key dependent S-boxes */
49}
50blowfish_context;
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56/**
57 * \brief Blowfish key schedule (encryption)
58 *
59 * \param ctx Blowfish context to be initialized
60 * \param key encryption key
61 * \param keysize must be between 32 and 448 bits
62 *
63 * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH
64 */
65int blowfish_setkey( blowfish_context *ctx, const unsigned char *key, unsigned int keysize );
66
67/**
68 * \brief Blowfish-ECB block encryption/decryption
69 *
70 * \param ctx Blowfish context
71 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
72 * \param input 8-byte input block
73 * \param output 8-byte output block
74 *
75 * \return 0 if successful
76 */
77int blowfish_crypt_ecb( blowfish_context *ctx,
78 int mode,
79 const unsigned char input[BLOWFISH_BLOCKSIZE],
80 unsigned char output[BLOWFISH_BLOCKSIZE] );
81
82/**
83 * \brief Blowfish-CBC buffer encryption/decryption
84 * Length should be a multiple of the block
85 * size (8 bytes)
86 *
87 * \param ctx Blowfish context
88 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
89 * \param length length of the input data
90 * \param iv initialization vector (updated after use)
91 * \param input buffer holding the input data
92 * \param output buffer holding the output data
93 *
94 * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH
95 */
96int blowfish_crypt_cbc( blowfish_context *ctx,
97 int mode,
98 size_t length,
99 unsigned char iv[BLOWFISH_BLOCKSIZE],
100 const unsigned char *input,
101 unsigned char *output );
102
103/**
104 * \brief Blowfish CFB buffer encryption/decryption.
105 *
106 * both
107 * \param ctx Blowfish context
108 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
109 * \param length length of the input data
110 * \param iv_off offset in IV (updated after use)
111 * \param iv initialization vector (updated after use)
112 * \param input buffer holding the input data
113 * \param output buffer holding the output data
114 *
115 * \return 0 if successful
116 */
117int blowfish_crypt_cfb64( blowfish_context *ctx,
118 int mode,
119 size_t length,
120 size_t *iv_off,
121 unsigned char iv[BLOWFISH_BLOCKSIZE],
122 const unsigned char *input,
123 unsigned char *output );
124
125/*
126 * \brief Blowfish-CTR buffer encryption/decryption
127 *
128 * Warning: You have to keep the maximum use of your counter in mind!
129 *
130 * \param length The length of the data
131 * \param nc_off The offset in the current stream_block (for resuming
132 * within current cipher stream). The offset pointer to
133 * should be 0 at the start of a stream.
134 * \param nonce_counter The 64-bit nonce and counter.
135 * \param stream_block The saved stream-block for resuming. Is overwritten
136 * by the function.
137 * \param input The input data stream
138 * \param output The output data stream
139 *
140 * \return 0 if successful
141 */
142int blowfish_crypt_ctr( blowfish_context *ctx,
143 size_t length,
144 size_t *nc_off,
145 unsigned char nonce_counter[BLOWFISH_BLOCKSIZE],
146 unsigned char stream_block[BLOWFISH_BLOCKSIZE],
147 const unsigned char *input,
148 unsigned char *output );
149
150#ifdef __cplusplus
151}
152#endif
153
154#endif /* blowfish.h */