blob: 34626eef4869a0b5ca399394b522aade2509d051 [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é-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkera9379c02012-07-04 11:02:11 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkera9379c02012-07-04 11:02:11 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_BLOWFISH_H
24#define MBEDTLS_BLOWFISH_H
Paul Bakkera9379c02012-07-04 11:02:11 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker90995b52013-06-24 19:20:35 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#define MBEDTLS_BLOWFISH_ENCRYPT 1
36#define MBEDTLS_BLOWFISH_DECRYPT 0
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +020037#define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448
38#define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
40#define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
Paul Bakkera9379c02012-07-04 11:02:11 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
43#define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
Paul Bakkera9379c02012-07-04 11:02:11 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_BLOWFISH_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020046// Regular implementation
47//
48
Paul Bakker407a0da2013-06-27 14:29:21 +020049#ifdef __cplusplus
50extern "C" {
51#endif
52
Paul Bakkera9379c02012-07-04 11:02:11 +000053/**
54 * \brief Blowfish context structure
55 */
56typedef struct
57{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
Paul Bakker5c2364c2012-10-01 14:41:15 +000059 uint32_t S[4][256]; /*!< key dependent S-boxes */
Paul Bakkera9379c02012-07-04 11:02:11 +000060}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061mbedtls_blowfish_context;
Paul Bakkera9379c02012-07-04 11:02:11 +000062
Paul Bakkera9379c02012-07-04 11:02:11 +000063/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020064 * \brief Initialize Blowfish context
65 *
66 * \param ctx Blowfish context to be initialized
67 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020069
70/**
71 * \brief Clear Blowfish context
72 *
73 * \param ctx Blowfish context to be cleared
74 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020076
77/**
Paul Bakker6132d0a2012-07-04 17:10:40 +000078 * \brief Blowfish key schedule
Paul Bakkera9379c02012-07-04 11:02:11 +000079 *
80 * \param ctx Blowfish context to be initialized
81 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020082 * \param keybits must be between 32 and 448 bits
Paul Bakkera9379c02012-07-04 11:02:11 +000083 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +000085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020087 unsigned int keybits );
Paul Bakkera9379c02012-07-04 11:02:11 +000088
89/**
90 * \brief Blowfish-ECB block encryption/decryption
91 *
92 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +000094 * \param input 8-byte input block
95 * \param output 8-byte output block
96 *
97 * \return 0 if successful
98 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000100 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
102 unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
Paul Bakkera9379c02012-07-04 11:02:11 +0000103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkera9379c02012-07-04 11:02:11 +0000105/**
106 * \brief Blowfish-CBC buffer encryption/decryption
107 * Length should be a multiple of the block
108 * size (8 bytes)
109 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000110 * \note Upon exit, the content of the IV is updated so that you can
111 * call the function same function again on the following
112 * block(s) of data and get the same result as if it was
113 * encrypted in one call. This allows a "streaming" usage.
114 * If on the other hand you need to retain the contents of the
115 * IV, you should either save it manually or use the cipher
116 * module instead.
117 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000118 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000120 * \param length length of the input data
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 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200125 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH
Paul Bakkera9379c02012-07-04 11:02:11 +0000127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000129 int mode,
130 size_t length,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000132 const unsigned char *input,
133 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkera9379c02012-07-04 11:02:11 +0000135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkera9379c02012-07-04 11:02:11 +0000137/**
138 * \brief Blowfish CFB buffer encryption/decryption.
139 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000140 * \note Upon exit, the content of the IV is updated so that you can
141 * call the function same function again on the following
142 * block(s) of data and get the same result as if it was
143 * encrypted in one call. This allows a "streaming" usage.
144 * If on the other hand you need to retain the contents of the
145 * IV, you should either save it manually or use the cipher
146 * module instead.
147 *
Paul Bakkera9379c02012-07-04 11:02:11 +0000148 * \param ctx Blowfish context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
Paul Bakkera9379c02012-07-04 11:02:11 +0000150 * \param length length of the input data
151 * \param iv_off offset in IV (updated after use)
152 * \param iv initialization vector (updated after use)
153 * \param input buffer holding the input data
154 * \param output buffer holding the output data
155 *
156 * \return 0 if successful
157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000159 int mode,
160 size_t length,
161 size_t *iv_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000163 const unsigned char *input,
164 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakkera9379c02012-07-04 11:02:11 +0000166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000168/**
Paul Bakkera9379c02012-07-04 11:02:11 +0000169 * \brief Blowfish-CTR buffer encryption/decryption
170 *
171 * Warning: You have to keep the maximum use of your counter in mind!
172 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200173 * \param ctx Blowfish context
Paul Bakkera9379c02012-07-04 11:02:11 +0000174 * \param length The length of the data
175 * \param nc_off The offset in the current stream_block (for resuming
176 * within current cipher stream). The offset pointer to
177 * should be 0 at the start of a stream.
178 * \param nonce_counter The 64-bit nonce and counter.
179 * \param stream_block The saved stream-block for resuming. Is overwritten
180 * by the function.
181 * \param input The input data stream
182 * \param output The output data stream
183 *
184 * \return 0 if successful
185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
Paul Bakkera9379c02012-07-04 11:02:11 +0000187 size_t length,
188 size_t *nc_off,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
190 unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
Paul Bakkera9379c02012-07-04 11:02:11 +0000191 const unsigned char *input,
192 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakkera9379c02012-07-04 11:02:11 +0000194
195#ifdef __cplusplus
196}
197#endif
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#else /* MBEDTLS_BLOWFISH_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200200#include "blowfish_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#endif /* MBEDTLS_BLOWFISH_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200202
Paul Bakkera9379c02012-07-04 11:02:11 +0000203#endif /* blowfish.h */