blob: 4b4916e03a78810676dfecea282dac17565cbdcc [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. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010044#define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 /**< Blowfish hardware accelerator failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
Paul Bakkera9379c02012-07-04 11:02:11 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_BLOWFISH_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020048// Regular implementation
49//
50
Paul Bakker407a0da2013-06-27 14:29:21 +020051#ifdef __cplusplus
52extern "C" {
53#endif
54
Paul Bakkera9379c02012-07-04 11:02:11 +000055/**
56 * \brief Blowfish context structure
57 */
58typedef struct
59{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
Paul Bakker5c2364c2012-10-01 14:41:15 +000061 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063mbedtls_blowfish_context;
Paul Bakkera9379c02012-07-04 11:02:11 +000064
Paul Bakkera9379c02012-07-04 11:02:11 +000065/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020066 * \brief Initialize Blowfish context
67 *
68 * \param ctx Blowfish context to be initialized
69 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020071
72/**
73 * \brief Clear Blowfish context
74 *
75 * \param ctx Blowfish context to be cleared
76 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020078
79/**
Paul Bakker6132d0a2012-07-04 17:10:40 +000080 * \brief Blowfish key schedule
Paul Bakkera9379c02012-07-04 11:02:11 +000081 *
82 * \param ctx Blowfish context to be initialized
83 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020084 * \param keybits must be between 32 and 448 bits
Paul Bakkera9379c02012-07-04 11:02:11 +000085 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +000087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020089 unsigned int keybits );
Paul Bakkera9379c02012-07-04 11:02:11 +000090
91/**
92 * \brief Blowfish-ECB block encryption/decryption
93 *
94 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +000096 * \param input 8-byte input block
97 * \param output 8-byte output block
98 *
99 * \return 0 if successful
100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000102 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
104 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
Paul Bakkera9379c02012-07-04 11:02:11 +0000105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000107/**
108 * \brief Blowfish-CBC buffer encryption/decryption
109 * Length should be a multiple of the block
110 * size (8 bytes)
111 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000112 * \note Upon exit, the content of the IV is updated so that you can
113 * call the function same function again on the following
114 * block(s) of data and get the same result as if it was
115 * encrypted in one call. This allows a "streaming" usage.
116 * If on the other hand you need to retain the contents of the
117 * IV, you should either save it manually or use the cipher
118 * module instead.
119 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000120 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000122 * \param length length of the input data
123 * \param iv initialization vector (updated after use)
124 * \param input buffer holding the input data
125 * \param output buffer holding the output data
126 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200127 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +0000129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000131 int mode,
132 size_t length,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000134 const unsigned char *input,
135 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000139/**
140 * \brief Blowfish CFB buffer encryption/decryption.
141 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000142 * \note Upon exit, the content of the IV is updated so that you can
143 * call the function same function again on the following
144 * block(s) of data and get the same result as if it was
145 * encrypted in one call. This allows a "streaming" usage.
146 * If on the other hand you need to retain the contents of the
147 * IV, you should either save it manually or use the cipher
148 * module instead.
149 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000150 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000152 * \param length length of the input data
153 * \param iv_off offset in IV (updated after use)
154 * \param iv initialization vector (updated after use)
155 * \param input buffer holding the input data
156 * \param output buffer holding the output data
157 *
158 * \return 0 if successful
159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000161 int mode,
162 size_t length,
163 size_t *iv_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000165 const unsigned char *input,
166 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000170/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000171 * \brief Blowfish-CTR buffer encryption/decryption
172 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100173 * \warning You must never reuse a nonce value with the same key. Doing so
174 * would void the encryption for the two messages encrypted with
175 * the same nonce and key.
176 *
177 * There are two common strategies for managing nonces with CTR:
178 *
179 * 1. Use a counter starting at 0 or a random value. With this
180 * strategy, this function will increment the counter for you, so
181 * you only need to preserve the \p nonce_counter buffer between
182 * calls. With this strategy, you must not encrypt more than
183 * 2**64 blocks of data.
184 * 2. Use a randomly-generated \p nonce_counter for each call.
185 * With this strategy, you need to ensure the nonce is generated
186 * in an unbiased way and you must not encrypt more than 2**32
187 * block of data.
188 *
189 * Note that for both stategies, the limit is in number of blocks
190 * and that a Blowfish block is 8 bytes.
Paul Bakkera9379c02012-07-04 11:02:11 +0000191 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200192 * \param ctx Blowfish context
Paul Bakkera9379c02012-07-04 11:02:11 +0000193 * \param length The length of the data
194 * \param nc_off The offset in the current stream_block (for resuming
195 * within current cipher stream). The offset pointer to
196 * should be 0 at the start of a stream.
197 * \param nonce_counter The 64-bit nonce and counter.
198 * \param stream_block The saved stream-block for resuming. Is overwritten
199 * by the function.
200 * \param input The input data stream
201 * \param output The output data stream
202 *
203 * \return 0 if successful
204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000206 size_t length,
207 size_t *nc_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
209 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000210 const unsigned char *input,
211 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000213
214#ifdef __cplusplus
215}
216#endif
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#else /* MBEDTLS_BLOWFISH_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200219#include "blowfish_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#endif /* MBEDTLS_BLOWFISH_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200221
Paul Bakkera9379c02012-07-04 11:02:11 +0000222#endif /* blowfish.h */