blob: c4168f6dd4d7e9c4b6fb8a434ddbff620ee6ae1c [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é-Gonnard0edee5e2015-01-26 15:29:40 +00006 * Copyright (C) 2012-2013, ARM Limited, All Rights Reserved
Paul Bakkera9379c02012-07-04 11:02:11 +00007 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkera9379c02012-07-04 11:02:11 +00009 *
10 * 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
Paul Bakker4087c472013-06-12 16:49:10 +020027#include "config.h"
28
Paul Bakkera9379c02012-07-04 11:02:11 +000029#include <string.h>
30
Paul Bakker5c2364c2012-10-01 14:41:15 +000031#ifdef _MSC_VER
32#include <basetsd.h>
33typedef UINT32 uint32_t;
34#else
35#include <inttypes.h>
36#endif
37
Paul Bakkera9379c02012-07-04 11:02:11 +000038#define BLOWFISH_ENCRYPT 1
39#define BLOWFISH_DECRYPT 0
40#define BLOWFISH_MAX_KEY 448
41#define BLOWFISH_MIN_KEY 32
42#define BLOWFISH_ROUNDS 16 /* when increasing this value, make sure to extend the initialisation vectors */
43#define BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
44
45#define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
46#define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
47
Paul Bakker4087c472013-06-12 16:49:10 +020048#if !defined(POLARSSL_BLOWFISH_ALT)
49// Regular implementation
50//
51
Paul Bakkera9379c02012-07-04 11:02:11 +000052/**
53 * \brief Blowfish context structure
54 */
55typedef struct
56{
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 uint32_t P[BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
58 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000059}
60blowfish_context;
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
66/**
Paul Bakker6132d0a2012-07-04 17:10:40 +000067 * \brief Blowfish key schedule
Paul Bakkera9379c02012-07-04 11:02:11 +000068 *
69 * \param ctx Blowfish context to be initialized
70 * \param key encryption key
71 * \param keysize must be between 32 and 448 bits
72 *
73 * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH
74 */
75int blowfish_setkey( blowfish_context *ctx, const unsigned char *key, unsigned int keysize );
76
77/**
78 * \brief Blowfish-ECB block encryption/decryption
79 *
80 * \param ctx Blowfish context
81 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
82 * \param input 8-byte input block
83 * \param output 8-byte output block
84 *
85 * \return 0 if successful
86 */
87int blowfish_crypt_ecb( blowfish_context *ctx,
88 int mode,
89 const unsigned char input[BLOWFISH_BLOCKSIZE],
90 unsigned char output[BLOWFISH_BLOCKSIZE] );
91
92/**
93 * \brief Blowfish-CBC buffer encryption/decryption
94 * Length should be a multiple of the block
95 * size (8 bytes)
96 *
97 * \param ctx Blowfish context
98 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
99 * \param length length of the input data
100 * \param iv initialization vector (updated after use)
101 * \param input buffer holding the input data
102 * \param output buffer holding the output data
103 *
104 * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH
105 */
106int blowfish_crypt_cbc( blowfish_context *ctx,
107 int mode,
108 size_t length,
109 unsigned char iv[BLOWFISH_BLOCKSIZE],
110 const unsigned char *input,
111 unsigned char *output );
112
113/**
114 * \brief Blowfish CFB buffer encryption/decryption.
115 *
116 * both
117 * \param ctx Blowfish context
118 * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT
119 * \param length length of the input data
120 * \param iv_off offset in IV (updated after use)
121 * \param iv initialization vector (updated after use)
122 * \param input buffer holding the input data
123 * \param output buffer holding the output data
124 *
125 * \return 0 if successful
126 */
127int blowfish_crypt_cfb64( blowfish_context *ctx,
128 int mode,
129 size_t length,
130 size_t *iv_off,
131 unsigned char iv[BLOWFISH_BLOCKSIZE],
132 const unsigned char *input,
133 unsigned char *output );
134
Paul Bakker9a736322012-11-14 12:39:52 +0000135/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000136 * \brief Blowfish-CTR buffer encryption/decryption
137 *
138 * Warning: You have to keep the maximum use of your counter in mind!
139 *
140 * \param length The length of the data
141 * \param nc_off The offset in the current stream_block (for resuming
142 * within current cipher stream). The offset pointer to
143 * should be 0 at the start of a stream.
144 * \param nonce_counter The 64-bit nonce and counter.
145 * \param stream_block The saved stream-block for resuming. Is overwritten
146 * by the function.
147 * \param input The input data stream
148 * \param output The output data stream
149 *
150 * \return 0 if successful
151 */
152int blowfish_crypt_ctr( blowfish_context *ctx,
153 size_t length,
154 size_t *nc_off,
155 unsigned char nonce_counter[BLOWFISH_BLOCKSIZE],
156 unsigned char stream_block[BLOWFISH_BLOCKSIZE],
157 const unsigned char *input,
158 unsigned char *output );
159
160#ifdef __cplusplus
161}
162#endif
163
Paul Bakker4087c472013-06-12 16:49:10 +0200164#else /* POLARSSL_BLOWFISH_ALT */
165#include "blowfish_alt.h"
166#endif /* POLARSSL_BLOWFISH_ALT */
167
Paul Bakkera9379c02012-07-04 11:02:11 +0000168#endif /* blowfish.h */