blob: 82b772ada5381e2cd6ce973d9db95d44f902ecc9 [file] [log] [blame]
Paul Bakkera9379c02012-07-04 11:02:11 +00001/**
2 * \file blowfish.h
3 *
4 * \brief Blowfish block cipher
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkera9379c02012-07-04 11:02:11 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkera9379c02012-07-04 11:02:11 +000023 */
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
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +020038#define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448
39#define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#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. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030044
45/* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used.
46 */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010047#define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 /**< Blowfish hardware accelerator failed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
Paul Bakkera9379c02012-07-04 11:02:11 +000050
Paul Bakker407a0da2013-06-27 14:29:21 +020051#ifdef __cplusplus
52extern "C" {
53#endif
54
Ron Eldorb2aacec2017-05-18 16:53:08 +030055#if !defined(MBEDTLS_BLOWFISH_ALT)
56// Regular implementation
57//
58
Paul Bakkera9379c02012-07-04 11:02:11 +000059/**
60 * \brief Blowfish context structure
61 */
Dawid Drozd428cc522018-07-24 10:02:47 +020062typedef struct mbedtls_blowfish_context
Paul Bakkera9379c02012-07-04 11:02:11 +000063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
Paul Bakker5c2364c2012-10-01 14:41:15 +000065 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067mbedtls_blowfish_context;
Paul Bakkera9379c02012-07-04 11:02:11 +000068
Ron Eldorb2aacec2017-05-18 16:53:08 +030069#else /* MBEDTLS_BLOWFISH_ALT */
70#include "blowfish_alt.h"
71#endif /* MBEDTLS_BLOWFISH_ALT */
72
Paul Bakkera9379c02012-07-04 11:02:11 +000073/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020074 * \brief Initialize Blowfish context
75 *
76 * \param ctx Blowfish context to be initialized
77 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020079
80/**
81 * \brief Clear Blowfish context
82 *
83 * \param ctx Blowfish context to be cleared
84 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020086
87/**
Paul Bakker6132d0a2012-07-04 17:10:40 +000088 * \brief Blowfish key schedule
Paul Bakkera9379c02012-07-04 11:02:11 +000089 *
90 * \param ctx Blowfish context to be initialized
91 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020092 * \param keybits must be between 32 and 448 bits
Paul Bakkera9379c02012-07-04 11:02:11 +000093 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +000095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020097 unsigned int keybits );
Paul Bakkera9379c02012-07-04 11:02:11 +000098
99/**
100 * \brief Blowfish-ECB block encryption/decryption
101 *
102 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000104 * \param input 8-byte input block
105 * \param output 8-byte output block
106 *
107 * \return 0 if successful
108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000110 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
112 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
Paul Bakkera9379c02012-07-04 11:02:11 +0000113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000115/**
116 * \brief Blowfish-CBC buffer encryption/decryption
117 * Length should be a multiple of the block
118 * size (8 bytes)
119 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000120 * \note Upon exit, the content of the IV is updated so that you can
121 * call the function same function again on the following
122 * block(s) of data and get the same result as if it was
123 * encrypted in one call. This allows a "streaming" usage.
124 * If on the other hand you need to retain the contents of the
125 * IV, you should either save it manually or use the cipher
126 * module instead.
127 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000128 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000130 * \param length length of the input data
131 * \param iv initialization vector (updated after use)
132 * \param input buffer holding the input data
133 * \param output buffer holding the output data
134 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200135 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +0000137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000139 int mode,
140 size_t length,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000142 const unsigned char *input,
143 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000147/**
148 * \brief Blowfish CFB buffer encryption/decryption.
149 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000150 * \note Upon exit, the content of the IV is updated so that you can
151 * call the function same function again on the following
152 * block(s) of data and get the same result as if it was
153 * encrypted in one call. This allows a "streaming" usage.
154 * If on the other hand you need to retain the contents of the
155 * IV, you should either save it manually or use the cipher
156 * module instead.
157 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000158 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000160 * \param length length of the input data
161 * \param iv_off offset in IV (updated after use)
162 * \param iv initialization vector (updated after use)
163 * \param input buffer holding the input data
164 * \param output buffer holding the output data
165 *
166 * \return 0 if successful
167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000169 int mode,
170 size_t length,
171 size_t *iv_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000173 const unsigned char *input,
174 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000178/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000179 * \brief Blowfish-CTR buffer encryption/decryption
180 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100181 * \warning You must never reuse a nonce value with the same key. Doing so
182 * would void the encryption for the two messages encrypted with
183 * the same nonce and key.
184 *
185 * There are two common strategies for managing nonces with CTR:
186 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200187 * 1. You can handle everything as a single message processed over
188 * successive calls to this function. In that case, you want to
189 * set \p nonce_counter and \p nc_off to 0 for the first call, and
190 * then preserve the values of \p nonce_counter, \p nc_off and \p
191 * stream_block across calls to this function as they will be
192 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100193 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200194 * With this strategy, you must not encrypt more than 2**64
195 * blocks of data with the same key.
196 *
197 * 2. You can encrypt separate messages by dividing the \p
198 * nonce_counter buffer in two areas: the first one used for a
199 * per-message nonce, handled by yourself, and the second one
200 * updated by this function internally.
201 *
202 * For example, you might reserve the first 4 bytes for the
203 * per-message nonce, and the last 4 bytes for internal use. In that
204 * case, before calling this function on a new message you need to
205 * set the first 4 bytes of \p nonce_counter to your chosen nonce
206 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
207 * stream_block to be ignored). That way, you can encrypt at most
208 * 2**32 messages of up to 2**32 blocks each with the same key.
209 *
210 * The per-message nonce (or information sufficient to reconstruct
211 * it) needs to be communicated with the ciphertext and must be unique.
212 * The recommended way to ensure uniqueness is to use a message
213 * counter.
214 *
215 * Note that for both stategies, sizes are measured in blocks and
216 * that a Blowfish block is 8 bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000217 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200218 * \warning Upon return, \p stream_block contains sensitive data. Its
219 * content must not be written to insecure storage and should be
220 * securely discarded as soon as it's no longer needed.
221 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200222 * \param ctx Blowfish context
Paul Bakkera9379c02012-07-04 11:02:11 +0000223 * \param length The length of the data
224 * \param nc_off The offset in the current stream_block (for resuming
225 * within current cipher stream). The offset pointer to
226 * should be 0 at the start of a stream.
227 * \param nonce_counter The 64-bit nonce and counter.
228 * \param stream_block The saved stream-block for resuming. Is overwritten
229 * by the function.
230 * \param input The input data stream
231 * \param output The output data stream
232 *
233 * \return 0 if successful
234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000236 size_t length,
237 size_t *nc_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
239 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000240 const unsigned char *input,
241 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000243
244#ifdef __cplusplus
245}
246#endif
247
Paul Bakkera9379c02012-07-04 11:02:11 +0000248#endif /* blowfish.h */