blob: 0f192aa3a0b962c157c8b8cbeaea260b1a65bd45 [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
Hanno Beckerd2f3a002018-12-17 13:21:06 +000036#include "platform_util.h"
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#define MBEDTLS_BLOWFISH_ENCRYPT 1
39#define MBEDTLS_BLOWFISH_DECRYPT 0
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +020040#define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448
41#define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
43#define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
Paul Bakkera9379c02012-07-04 11:02:11 +000044
Hanno Beckerd2f3a002018-12-17 13:21:06 +000045#if !defined(MBEDTLS_DEPRECATED_REMOVED)
46#define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0016 )
47#define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0018 )
48#endif /* !MBEDTLS_DEPRECATED_REMOVED */
49#define MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA -0x0016 /**< Bad input data. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030050
51/* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used.
52 */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010053#define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 /**< Blowfish hardware accelerator failed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030054
Paul Bakker407a0da2013-06-27 14:29:21 +020055#ifdef __cplusplus
56extern "C" {
57#endif
58
Ron Eldorb2aacec2017-05-18 16:53:08 +030059#if !defined(MBEDTLS_BLOWFISH_ALT)
60// Regular implementation
61//
62
Paul Bakkera9379c02012-07-04 11:02:11 +000063/**
64 * \brief Blowfish context structure
65 */
Dawid Drozd428cc522018-07-24 10:02:47 +020066typedef struct mbedtls_blowfish_context
Paul Bakkera9379c02012-07-04 11:02:11 +000067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
Paul Bakker5c2364c2012-10-01 14:41:15 +000069 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071mbedtls_blowfish_context;
Paul Bakkera9379c02012-07-04 11:02:11 +000072
Ron Eldorb2aacec2017-05-18 16:53:08 +030073#else /* MBEDTLS_BLOWFISH_ALT */
74#include "blowfish_alt.h"
75#endif /* MBEDTLS_BLOWFISH_ALT */
76
Paul Bakkera9379c02012-07-04 11:02:11 +000077/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020078 * \brief Initialize Blowfish context
79 *
80 * \param ctx Blowfish context to be initialized
81 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020083
84/**
85 * \brief Clear Blowfish context
86 *
87 * \param ctx Blowfish context to be cleared
88 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020090
91/**
Paul Bakker6132d0a2012-07-04 17:10:40 +000092 * \brief Blowfish key schedule
Paul Bakkera9379c02012-07-04 11:02:11 +000093 *
94 * \param ctx Blowfish context to be initialized
95 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020096 * \param keybits must be between 32 and 448 bits
Paul Bakkera9379c02012-07-04 11:02:11 +000097 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +000099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200101 unsigned int keybits );
Paul Bakkera9379c02012-07-04 11:02:11 +0000102
103/**
104 * \brief Blowfish-ECB block encryption/decryption
105 *
106 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000108 * \param input 8-byte input block
109 * \param output 8-byte output block
110 *
111 * \return 0 if successful
112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000114 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
116 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
Paul Bakkera9379c02012-07-04 11:02:11 +0000117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000119/**
120 * \brief Blowfish-CBC buffer encryption/decryption
121 * Length should be a multiple of the block
122 * size (8 bytes)
123 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000124 * \note Upon exit, the content of the IV is updated so that you can
125 * call the function same function again on the following
126 * block(s) of data and get the same result as if it was
127 * encrypted in one call. This allows a "streaming" usage.
128 * If on the other hand you need to retain the contents of the
129 * IV, you should either save it manually or use the cipher
130 * module instead.
131 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000132 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000134 * \param length length of the input data
135 * \param iv initialization vector (updated after use)
136 * \param input buffer holding the input data
137 * \param output buffer holding the output data
138 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200139 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +0000141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000143 int mode,
144 size_t length,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000146 const unsigned char *input,
147 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000151/**
152 * \brief Blowfish CFB buffer encryption/decryption.
153 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000154 * \note Upon exit, the content of the IV is updated so that you can
155 * call the function same function again on the following
156 * block(s) of data and get the same result as if it was
157 * encrypted in one call. This allows a "streaming" usage.
158 * If on the other hand you need to retain the contents of the
159 * IV, you should either save it manually or use the cipher
160 * module instead.
161 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000162 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000164 * \param length length of the input data
165 * \param iv_off offset in IV (updated after use)
166 * \param iv initialization vector (updated after use)
167 * \param input buffer holding the input data
168 * \param output buffer holding the output data
169 *
170 * \return 0 if successful
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000173 int mode,
174 size_t length,
175 size_t *iv_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000177 const unsigned char *input,
178 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000182/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000183 * \brief Blowfish-CTR buffer encryption/decryption
184 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100185 * \warning You must never reuse a nonce value with the same key. Doing so
186 * would void the encryption for the two messages encrypted with
187 * the same nonce and key.
188 *
189 * There are two common strategies for managing nonces with CTR:
190 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200191 * 1. You can handle everything as a single message processed over
192 * successive calls to this function. In that case, you want to
193 * set \p nonce_counter and \p nc_off to 0 for the first call, and
194 * then preserve the values of \p nonce_counter, \p nc_off and \p
195 * stream_block across calls to this function as they will be
196 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100197 *
Manuel Pégourié-Gonnardd0f143b2018-05-24 12:01:58 +0200198 * With this strategy, you must not encrypt more than 2**64
199 * blocks of data with the same key.
200 *
201 * 2. You can encrypt separate messages by dividing the \p
202 * nonce_counter buffer in two areas: the first one used for a
203 * per-message nonce, handled by yourself, and the second one
204 * updated by this function internally.
205 *
206 * For example, you might reserve the first 4 bytes for the
207 * per-message nonce, and the last 4 bytes for internal use. In that
208 * case, before calling this function on a new message you need to
209 * set the first 4 bytes of \p nonce_counter to your chosen nonce
210 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
211 * stream_block to be ignored). That way, you can encrypt at most
212 * 2**32 messages of up to 2**32 blocks each with the same key.
213 *
214 * The per-message nonce (or information sufficient to reconstruct
215 * it) needs to be communicated with the ciphertext and must be unique.
216 * The recommended way to ensure uniqueness is to use a message
217 * counter.
218 *
219 * Note that for both stategies, sizes are measured in blocks and
220 * that a Blowfish block is 8 bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000221 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200222 * \warning Upon return, \p stream_block contains sensitive data. Its
223 * content must not be written to insecure storage and should be
224 * securely discarded as soon as it's no longer needed.
225 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200226 * \param ctx Blowfish context
Paul Bakkera9379c02012-07-04 11:02:11 +0000227 * \param length The length of the data
228 * \param nc_off The offset in the current stream_block (for resuming
229 * within current cipher stream). The offset pointer to
230 * should be 0 at the start of a stream.
231 * \param nonce_counter The 64-bit nonce and counter.
232 * \param stream_block The saved stream-block for resuming. Is overwritten
233 * by the function.
234 * \param input The input data stream
235 * \param output The output data stream
236 *
237 * \return 0 if successful
238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000240 size_t length,
241 size_t *nc_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
243 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000244 const unsigned char *input,
245 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000247
248#ifdef __cplusplus
249}
250#endif
251
Paul Bakkera9379c02012-07-04 11:02:11 +0000252#endif /* blowfish.h */