blob: 8c470ed3117b36c982280d6d8ea246caa15a30b7 [file] [log] [blame]
Paul Bakkera9379c02012-07-04 11:02:11 +00001/**
2 * \file blowfish.h
3 *
4 * \brief Blowfish block cipher
5 *
Paul Bakker90995b52013-06-24 19:20:35 +02006 * Copyright (C) 2012-2013, Brainspark B.V.
Paul Bakkera9379c02012-07-04 11:02:11 +00007 *
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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker90995b52013-06-24 19:20:35 +020035
Paul Bakkera9379c02012-07-04 11:02:11 +000036#include <string.h>
37
Paul Bakkerfa6a6202013-10-28 18:48:30 +010038#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000039#include <basetsd.h>
40typedef UINT32 uint32_t;
41#else
42#include <inttypes.h>
43#endif
44
Paul Bakkera9379c02012-07-04 11:02:11 +000045#define BLOWFISH_ENCRYPT 1
46#define BLOWFISH_DECRYPT 0
47#define BLOWFISH_MAX_KEY 448
48#define BLOWFISH_MIN_KEY 32
49#define BLOWFISH_ROUNDS 16 /* when increasing this value, make sure to extend the initialisation vectors */
50#define BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
51
52#define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
53#define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
54
Paul Bakker90995b52013-06-24 19:20:35 +020055#if !defined(POLARSSL_BLOWFISH_ALT)
56// Regular implementation
57//
58
Paul Bakker407a0da2013-06-27 14:29:21 +020059#ifdef __cplusplus
60extern "C" {
61#endif
62
Paul Bakkera9379c02012-07-04 11:02:11 +000063/**
64 * \brief Blowfish context structure
65 */
66typedef struct
67{
Paul Bakker5c2364c2012-10-01 14:41:15 +000068 uint32_t P[BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
69 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000070}
71blowfish_context;
72
Paul Bakkera9379c02012-07-04 11:02:11 +000073/**
Paul Bakker6132d0a2012-07-04 17:10:40 +000074 * \brief Blowfish key schedule
Paul Bakkera9379c02012-07-04 11:02:11 +000075 *
76 * \param ctx Blowfish context to be initialized
77 * \param key encryption key
78 * \param keysize must be between 32 and 448 bits
79 *
80 * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH
81 */
82int blowfish_setkey( blowfish_context *ctx, const unsigned char *key, unsigned int keysize );
83
84/**
85 * \brief Blowfish-ECB block encryption/decryption
86 *
87 * \param ctx Blowfish context
88 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
89 * \param input 8-byte input block
90 * \param output 8-byte output block
91 *
92 * \return 0 if successful
93 */
94int blowfish_crypt_ecb( blowfish_context *ctx,
95 int mode,
96 const unsigned char input[BLOWFISH_BLOCKSIZE],
97 unsigned char output[BLOWFISH_BLOCKSIZE] );
98
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020099#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000100/**
101 * \brief Blowfish-CBC buffer encryption/decryption
102 * Length should be a multiple of the block
103 * size (8 bytes)
104 *
105 * \param ctx Blowfish context
106 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
107 * \param length length of the input data
108 * \param iv initialization vector (updated after use)
109 * \param input buffer holding the input data
110 * \param output buffer holding the output data
111 *
112 * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH
113 */
114int blowfish_crypt_cbc( blowfish_context *ctx,
115 int mode,
116 size_t length,
117 unsigned char iv[BLOWFISH_BLOCKSIZE],
118 const unsigned char *input,
119 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200120#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000121
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200122#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000123/**
124 * \brief Blowfish CFB buffer encryption/decryption.
125 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000126 * \param ctx Blowfish context
127 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
128 * \param length length of the input data
129 * \param iv_off offset in IV (updated after use)
130 * \param iv initialization vector (updated after use)
131 * \param input buffer holding the input data
132 * \param output buffer holding the output data
133 *
134 * \return 0 if successful
135 */
136int blowfish_crypt_cfb64( blowfish_context *ctx,
137 int mode,
138 size_t length,
139 size_t *iv_off,
140 unsigned char iv[BLOWFISH_BLOCKSIZE],
141 const unsigned char *input,
142 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200143#endif /*POLARSSL_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000144
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200145#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000146/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000147 * \brief Blowfish-CTR buffer encryption/decryption
148 *
149 * Warning: You have to keep the maximum use of your counter in mind!
150 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200151 * \param ctx Blowfish context
Paul Bakkera9379c02012-07-04 11:02:11 +0000152 * \param length The length of the data
153 * \param nc_off The offset in the current stream_block (for resuming
154 * within current cipher stream). The offset pointer to
155 * should be 0 at the start of a stream.
156 * \param nonce_counter The 64-bit nonce and counter.
157 * \param stream_block The saved stream-block for resuming. Is overwritten
158 * by the function.
159 * \param input The input data stream
160 * \param output The output data stream
161 *
162 * \return 0 if successful
163 */
164int blowfish_crypt_ctr( blowfish_context *ctx,
165 size_t length,
166 size_t *nc_off,
167 unsigned char nonce_counter[BLOWFISH_BLOCKSIZE],
168 unsigned char stream_block[BLOWFISH_BLOCKSIZE],
169 const unsigned char *input,
170 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200171#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000172
173#ifdef __cplusplus
174}
175#endif
176
Paul Bakker90995b52013-06-24 19:20:35 +0200177#else /* POLARSSL_BLOWFISH_ALT */
178#include "blowfish_alt.h"
179#endif /* POLARSSL_BLOWFISH_ALT */
180
Paul Bakkera9379c02012-07-04 11:02:11 +0000181#endif /* blowfish.h */