blob: d69205866a27e5861bd5d0d64e665ff12ec6249f [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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2012-2014, ARM Limited, All Rights Reserved
Paul Bakkera9379c02012-07-04 11:02:11 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkera9379c02012-07-04 11:02:11 +00009 *
Paul Bakkera9379c02012-07-04 11:02:11 +000010 * 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_BLOWFISH_H
25#define MBEDTLS_BLOWFISH_H
Paul Bakkera9379c02012-07-04 11:02:11 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_BLOWFISH_ENCRYPT 1
37#define MBEDTLS_BLOWFISH_DECRYPT 0
38#define MBEDTLS_BLOWFISH_MAX_KEY 448
39#define MBEDTLS_BLOWFISH_MIN_KEY 32
40#define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
41#define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
Paul Bakkera9379c02012-07-04 11:02:11 +000042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
44#define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
Paul Bakkera9379c02012-07-04 11:02:11 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if !defined(MBEDTLS_BLOWFISH_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020047// Regular implementation
48//
49
Paul Bakker407a0da2013-06-27 14:29:21 +020050#ifdef __cplusplus
51extern "C" {
52#endif
53
Paul Bakkera9379c02012-07-04 11:02:11 +000054/**
55 * \brief Blowfish context structure
56 */
57typedef struct
58{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
Paul Bakker5c2364c2012-10-01 14:41:15 +000060 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062mbedtls_blowfish_context;
Paul Bakkera9379c02012-07-04 11:02:11 +000063
Paul Bakkera9379c02012-07-04 11:02:11 +000064/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020065 * \brief Initialize Blowfish context
66 *
67 * \param ctx Blowfish context to be initialized
68 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020070
71/**
72 * \brief Clear Blowfish context
73 *
74 * \param ctx Blowfish context to be cleared
75 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020077
78/**
Paul Bakker6132d0a2012-07-04 17:10:40 +000079 * \brief Blowfish key schedule
Paul Bakkera9379c02012-07-04 11:02:11 +000080 *
81 * \param ctx Blowfish context to be initialized
82 * \param key encryption key
83 * \param keysize must be between 32 and 448 bits
84 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +000086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020088 unsigned int keysize );
Paul Bakkera9379c02012-07-04 11:02:11 +000089
90/**
91 * \brief Blowfish-ECB block encryption/decryption
92 *
93 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +000095 * \param input 8-byte input block
96 * \param output 8-byte output block
97 *
98 * \return 0 if successful
99 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000101 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
103 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
Paul Bakkera9379c02012-07-04 11:02:11 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000106/**
107 * \brief Blowfish-CBC buffer encryption/decryption
108 * Length should be a multiple of the block
109 * size (8 bytes)
110 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000111 * \note Upon exit, the content of the IV is updated so that you can
112 * call the function same function again on the following
113 * block(s) of data and get the same result as if it was
114 * encrypted in one call. This allows a "streaming" usage.
115 * If on the other hand you need to retain the contents of the
116 * IV, you should either save it manually or use the cipher
117 * module instead.
118 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000119 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000121 * \param length length of the input data
122 * \param iv initialization vector (updated after use)
123 * \param input buffer holding the input data
124 * \param output buffer holding the output data
125 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200126 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +0000128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000130 int mode,
131 size_t length,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000133 const unsigned char *input,
134 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000138/**
139 * \brief Blowfish CFB buffer encryption/decryption.
140 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000141 * \note Upon exit, the content of the IV is updated so that you can
142 * call the function same function again on the following
143 * block(s) of data and get the same result as if it was
144 * encrypted in one call. This allows a "streaming" usage.
145 * If on the other hand you need to retain the contents of the
146 * IV, you should either save it manually or use the cipher
147 * module instead.
148 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000149 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000151 * \param length length of the input data
152 * \param iv_off offset in IV (updated after use)
153 * \param iv initialization vector (updated after use)
154 * \param input buffer holding the input data
155 * \param output buffer holding the output data
156 *
157 * \return 0 if successful
158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000160 int mode,
161 size_t length,
162 size_t *iv_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000164 const unsigned char *input,
165 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000169/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000170 * \brief Blowfish-CTR buffer encryption/decryption
171 *
172 * Warning: You have to keep the maximum use of your counter in mind!
173 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200174 * \param ctx Blowfish context
Paul Bakkera9379c02012-07-04 11:02:11 +0000175 * \param length The length of the data
176 * \param nc_off The offset in the current stream_block (for resuming
177 * within current cipher stream). The offset pointer to
178 * should be 0 at the start of a stream.
179 * \param nonce_counter The 64-bit nonce and counter.
180 * \param stream_block The saved stream-block for resuming. Is overwritten
181 * by the function.
182 * \param input The input data stream
183 * \param output The output data stream
184 *
185 * \return 0 if successful
186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000188 size_t length,
189 size_t *nc_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
191 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000192 const unsigned char *input,
193 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000195
196#ifdef __cplusplus
197}
198#endif
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#else /* MBEDTLS_BLOWFISH_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200201#include "blowfish_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_BLOWFISH_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200203
Paul Bakkera9379c02012-07-04 11:02:11 +0000204#endif /* blowfish.h */