blob: b11c2e8f5fb83ed0eba1d8c5ebf96db938e26bc8 [file] [log] [blame]
Jaeden Ameroe54e6932018-08-06 16:19:58 +01001/**
2 * \file blowfish.h
3 *
4 * \brief Blowfish block cipher
5 */
6/*
7 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8 * 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.
21 *
22 * This file is part of Mbed Crypto (https://tls.mbed.org)
23 */
24#ifndef MBEDCRYPTO_BLOWFISH_H
25#define MBEDCRYPTO_BLOWFISH_H
26
27#if !defined(MBEDCRYPTO_CONFIG_FILE)
28#include "config.h"
29#else
30#include MBEDCRYPTO_CONFIG_FILE
31#endif
32
33#include <stddef.h>
34#include <stdint.h>
35
36#define MBEDCRYPTO_BLOWFISH_ENCRYPT 1
37#define MBEDCRYPTO_BLOWFISH_DECRYPT 0
38#define MBEDCRYPTO_BLOWFISH_MAX_KEY_BITS 448
39#define MBEDCRYPTO_BLOWFISH_MIN_KEY_BITS 32
40#define MBEDCRYPTO_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
41#define MBEDCRYPTO_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
42
43#define MBEDCRYPTO_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
44#define MBEDCRYPTO_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 /**< Blowfish hardware accelerator failed. */
45#define MBEDCRYPTO_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51#if !defined(MBEDCRYPTO_BLOWFISH_ALT)
52// Regular implementation
53//
54
55/**
56 * \brief Blowfish context structure
57 */
58typedef struct
59{
60 uint32_t P[MBEDCRYPTO_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
61 uint32_t S[4][256]; /*!< key dependent S-boxes */
62}
63mbedcrypto_blowfish_context;
64
65#else /* MBEDCRYPTO_BLOWFISH_ALT */
66#include "blowfish_alt.h"
67#endif /* MBEDCRYPTO_BLOWFISH_ALT */
68
69/**
70 * \brief Initialize Blowfish context
71 *
72 * \param ctx Blowfish context to be initialized
73 */
74void mbedcrypto_blowfish_init( mbedcrypto_blowfish_context *ctx );
75
76/**
77 * \brief Clear Blowfish context
78 *
79 * \param ctx Blowfish context to be cleared
80 */
81void mbedcrypto_blowfish_free( mbedcrypto_blowfish_context *ctx );
82
83/**
84 * \brief Blowfish key schedule
85 *
86 * \param ctx Blowfish context to be initialized
87 * \param key encryption key
88 * \param keybits must be between 32 and 448 bits
89 *
90 * \return 0 if successful, or MBEDCRYPTO_ERR_BLOWFISH_INVALID_KEY_LENGTH
91 */
92int mbedcrypto_blowfish_setkey( mbedcrypto_blowfish_context *ctx, const unsigned char *key,
93 unsigned int keybits );
94
95/**
96 * \brief Blowfish-ECB block encryption/decryption
97 *
98 * \param ctx Blowfish context
99 * \param mode MBEDCRYPTO_BLOWFISH_ENCRYPT or MBEDCRYPTO_BLOWFISH_DECRYPT
100 * \param input 8-byte input block
101 * \param output 8-byte output block
102 *
103 * \return 0 if successful
104 */
105int mbedcrypto_blowfish_crypt_ecb( mbedcrypto_blowfish_context *ctx,
106 int mode,
107 const unsigned char input[MBEDCRYPTO_BLOWFISH_BLOCKSIZE],
108 unsigned char output[MBEDCRYPTO_BLOWFISH_BLOCKSIZE] );
109
110#if defined(MBEDCRYPTO_CIPHER_MODE_CBC)
111/**
112 * \brief Blowfish-CBC buffer encryption/decryption
113 * Length should be a multiple of the block
114 * size (8 bytes)
115 *
116 * \note Upon exit, the content of the IV is updated so that you can
117 * call the function same function again on the following
118 * block(s) of data and get the same result as if it was
119 * encrypted in one call. This allows a "streaming" usage.
120 * If on the other hand you need to retain the contents of the
121 * IV, you should either save it manually or use the cipher
122 * module instead.
123 *
124 * \param ctx Blowfish context
125 * \param mode MBEDCRYPTO_BLOWFISH_ENCRYPT or MBEDCRYPTO_BLOWFISH_DECRYPT
126 * \param length length of the input data
127 * \param iv initialization vector (updated after use)
128 * \param input buffer holding the input data
129 * \param output buffer holding the output data
130 *
131 * \return 0 if successful, or
132 * MBEDCRYPTO_ERR_BLOWFISH_INVALID_INPUT_LENGTH
133 */
134int mbedcrypto_blowfish_crypt_cbc( mbedcrypto_blowfish_context *ctx,
135 int mode,
136 size_t length,
137 unsigned char iv[MBEDCRYPTO_BLOWFISH_BLOCKSIZE],
138 const unsigned char *input,
139 unsigned char *output );
140#endif /* MBEDCRYPTO_CIPHER_MODE_CBC */
141
142#if defined(MBEDCRYPTO_CIPHER_MODE_CFB)
143/**
144 * \brief Blowfish CFB buffer encryption/decryption.
145 *
146 * \note Upon exit, the content of the IV is updated so that you can
147 * call the function same function again on the following
148 * block(s) of data and get the same result as if it was
149 * encrypted in one call. This allows a "streaming" usage.
150 * If on the other hand you need to retain the contents of the
151 * IV, you should either save it manually or use the cipher
152 * module instead.
153 *
154 * \param ctx Blowfish context
155 * \param mode MBEDCRYPTO_BLOWFISH_ENCRYPT or MBEDCRYPTO_BLOWFISH_DECRYPT
156 * \param length length of the input data
157 * \param iv_off offset in IV (updated after use)
158 * \param iv initialization vector (updated after use)
159 * \param input buffer holding the input data
160 * \param output buffer holding the output data
161 *
162 * \return 0 if successful
163 */
164int mbedcrypto_blowfish_crypt_cfb64( mbedcrypto_blowfish_context *ctx,
165 int mode,
166 size_t length,
167 size_t *iv_off,
168 unsigned char iv[MBEDCRYPTO_BLOWFISH_BLOCKSIZE],
169 const unsigned char *input,
170 unsigned char *output );
171#endif /*MBEDCRYPTO_CIPHER_MODE_CFB */
172
173#if defined(MBEDCRYPTO_CIPHER_MODE_CTR)
174/**
175 * \brief Blowfish-CTR buffer encryption/decryption
176 *
177 * Warning: You have to keep the maximum use of your counter in mind!
178 *
179 * \param ctx Blowfish context
180 * \param length The length of the data
181 * \param nc_off The offset in the current stream_block (for resuming
182 * within current cipher stream). The offset pointer to
183 * should be 0 at the start of a stream.
184 * \param nonce_counter The 64-bit nonce and counter.
185 * \param stream_block The saved stream-block for resuming. Is overwritten
186 * by the function.
187 * \param input The input data stream
188 * \param output The output data stream
189 *
190 * \return 0 if successful
191 */
192int mbedcrypto_blowfish_crypt_ctr( mbedcrypto_blowfish_context *ctx,
193 size_t length,
194 size_t *nc_off,
195 unsigned char nonce_counter[MBEDCRYPTO_BLOWFISH_BLOCKSIZE],
196 unsigned char stream_block[MBEDCRYPTO_BLOWFISH_BLOCKSIZE],
197 const unsigned char *input,
198 unsigned char *output );
199#endif /* MBEDCRYPTO_CIPHER_MODE_CTR */
200
201#ifdef __cplusplus
202}
203#endif
204
205#endif /* blowfish.h */