blob: c9c8672898cb6cc71dabf3512e55d997d6ce2c50 [file] [log] [blame]
Paul Bakkera9379c02012-07-04 11:02:11 +00001/**
2 * \file blowfish.h
3 *
4 * \brief Blowfish block cipher
5 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006 * Copyright (C) 2012-2014, 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
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020049#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 +000050#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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020082int blowfish_setkey( blowfish_context *ctx, const unsigned char *key,
83 unsigned int keysize );
Paul Bakkera9379c02012-07-04 11:02:11 +000084
85/**
86 * \brief Blowfish-ECB block encryption/decryption
87 *
88 * \param ctx Blowfish context
89 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
90 * \param input 8-byte input block
91 * \param output 8-byte output block
92 *
93 * \return 0 if successful
94 */
95int blowfish_crypt_ecb( blowfish_context *ctx,
96 int mode,
97 const unsigned char input[BLOWFISH_BLOCKSIZE],
98 unsigned char output[BLOWFISH_BLOCKSIZE] );
99
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200100#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000101/**
102 * \brief Blowfish-CBC buffer encryption/decryption
103 * Length should be a multiple of the block
104 * size (8 bytes)
105 *
106 * \param ctx Blowfish context
107 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
108 * \param length length of the input data
109 * \param iv initialization vector (updated after use)
110 * \param input buffer holding the input data
111 * \param output buffer holding the output data
112 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200113 * \return 0 if successful, or
114 * POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +0000115 */
116int blowfish_crypt_cbc( blowfish_context *ctx,
117 int mode,
118 size_t length,
119 unsigned char iv[BLOWFISH_BLOCKSIZE],
120 const unsigned char *input,
121 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200122#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000123
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200124#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000125/**
126 * \brief Blowfish CFB buffer encryption/decryption.
127 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000128 * \param ctx Blowfish context
129 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
130 * \param length length of the input data
131 * \param iv_off offset in IV (updated after use)
132 * \param iv initialization vector (updated after use)
133 * \param input buffer holding the input data
134 * \param output buffer holding the output data
135 *
136 * \return 0 if successful
137 */
138int blowfish_crypt_cfb64( blowfish_context *ctx,
139 int mode,
140 size_t length,
141 size_t *iv_off,
142 unsigned char iv[BLOWFISH_BLOCKSIZE],
143 const unsigned char *input,
144 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200145#endif /*POLARSSL_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000146
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200147#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000148/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000149 * \brief Blowfish-CTR buffer encryption/decryption
150 *
151 * Warning: You have to keep the maximum use of your counter in mind!
152 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200153 * \param ctx Blowfish context
Paul Bakkera9379c02012-07-04 11:02:11 +0000154 * \param length The length of the data
155 * \param nc_off The offset in the current stream_block (for resuming
156 * within current cipher stream). The offset pointer to
157 * should be 0 at the start of a stream.
158 * \param nonce_counter The 64-bit nonce and counter.
159 * \param stream_block The saved stream-block for resuming. Is overwritten
160 * by the function.
161 * \param input The input data stream
162 * \param output The output data stream
163 *
164 * \return 0 if successful
165 */
166int blowfish_crypt_ctr( blowfish_context *ctx,
167 size_t length,
168 size_t *nc_off,
169 unsigned char nonce_counter[BLOWFISH_BLOCKSIZE],
170 unsigned char stream_block[BLOWFISH_BLOCKSIZE],
171 const unsigned char *input,
172 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200173#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000174
175#ifdef __cplusplus
176}
177#endif
178
Paul Bakker90995b52013-06-24 19:20:35 +0200179#else /* POLARSSL_BLOWFISH_ALT */
180#include "blowfish_alt.h"
181#endif /* POLARSSL_BLOWFISH_ALT */
182
Paul Bakkera9379c02012-07-04 11:02:11 +0000183#endif /* blowfish.h */