blowfish.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  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 TLS (https://tls.mbed.org)
  23. */
  24. #ifndef MBEDTLS_BLOWFISH_H
  25. #define MBEDTLS_BLOWFISH_H
  26. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #define MBEDTLS_BLOWFISH_ENCRYPT 1
  34. #define MBEDTLS_BLOWFISH_DECRYPT 0
  35. #define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448
  36. #define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32
  37. #define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
  38. #define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */
  39. #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */
  40. #define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED -0x0017 /**< Blowfish hardware accelerator failed. */
  41. #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #if !defined(MBEDTLS_BLOWFISH_ALT)
  46. // Regular implementation
  47. //
  48. /**
  49. * \brief Blowfish context structure
  50. */
  51. typedef struct
  52. {
  53. uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */
  54. uint32_t S[4][256]; /*!< key dependent S-boxes */
  55. }
  56. mbedtls_blowfish_context;
  57. #else /* MBEDTLS_BLOWFISH_ALT */
  58. #include "blowfish_alt.h"
  59. #endif /* MBEDTLS_BLOWFISH_ALT */
  60. /**
  61. * \brief Initialize Blowfish context
  62. *
  63. * \param ctx Blowfish context to be initialized
  64. */
  65. void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
  66. /**
  67. * \brief Clear Blowfish context
  68. *
  69. * \param ctx Blowfish context to be cleared
  70. */
  71. void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
  72. /**
  73. * \brief Blowfish key schedule
  74. *
  75. * \param ctx Blowfish context to be initialized
  76. * \param key encryption key
  77. * \param keybits must be between 32 and 448 bits
  78. *
  79. * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH
  80. */
  81. int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
  82. unsigned int keybits );
  83. /**
  84. * \brief Blowfish-ECB block encryption/decryption
  85. *
  86. * \param ctx Blowfish context
  87. * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
  88. * \param input 8-byte input block
  89. * \param output 8-byte output block
  90. *
  91. * \return 0 if successful
  92. */
  93. int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
  94. int mode,
  95. const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
  96. unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
  97. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  98. /**
  99. * \brief Blowfish-CBC buffer encryption/decryption
  100. * Length should be a multiple of the block
  101. * size (8 bytes)
  102. *
  103. * \note Upon exit, the content of the IV is updated so that you can
  104. * call the function same function again on the following
  105. * block(s) of data and get the same result as if it was
  106. * encrypted in one call. This allows a "streaming" usage.
  107. * If on the other hand you need to retain the contents of the
  108. * IV, you should either save it manually or use the cipher
  109. * module instead.
  110. *
  111. * \param ctx Blowfish context
  112. * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
  113. * \param length length of the input data
  114. * \param iv initialization vector (updated after use)
  115. * \param input buffer holding the input data
  116. * \param output buffer holding the output data
  117. *
  118. * \return 0 if successful, or
  119. * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH
  120. */
  121. int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
  122. int mode,
  123. size_t length,
  124. unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
  125. const unsigned char *input,
  126. unsigned char *output );
  127. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  128. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  129. /**
  130. * \brief Blowfish CFB buffer encryption/decryption.
  131. *
  132. * \note Upon exit, the content of the IV is updated so that you can
  133. * call the function same function again on the following
  134. * block(s) of data and get the same result as if it was
  135. * encrypted in one call. This allows a "streaming" usage.
  136. * If on the other hand you need to retain the contents of the
  137. * IV, you should either save it manually or use the cipher
  138. * module instead.
  139. *
  140. * \param ctx Blowfish context
  141. * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
  142. * \param length length of the input data
  143. * \param iv_off offset in IV (updated after use)
  144. * \param iv initialization vector (updated after use)
  145. * \param input buffer holding the input data
  146. * \param output buffer holding the output data
  147. *
  148. * \return 0 if successful
  149. */
  150. int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
  151. int mode,
  152. size_t length,
  153. size_t *iv_off,
  154. unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
  155. const unsigned char *input,
  156. unsigned char *output );
  157. #endif /*MBEDTLS_CIPHER_MODE_CFB */
  158. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  159. /**
  160. * \brief Blowfish-CTR buffer encryption/decryption
  161. *
  162. * \warning You must never reuse a nonce value with the same key. Doing so
  163. * would void the encryption for the two messages encrypted with
  164. * the same nonce and key.
  165. *
  166. * There are two common strategies for managing nonces with CTR:
  167. *
  168. * 1. You can handle everything as a single message processed over
  169. * successive calls to this function. In that case, you want to
  170. * set \p nonce_counter and \p nc_off to 0 for the first call, and
  171. * then preserve the values of \p nonce_counter, \p nc_off and \p
  172. * stream_block across calls to this function as they will be
  173. * updated by this function.
  174. *
  175. * With this strategy, you must not encrypt more than 2**64
  176. * blocks of data with the same key.
  177. *
  178. * 2. You can encrypt separate messages by dividing the \p
  179. * nonce_counter buffer in two areas: the first one used for a
  180. * per-message nonce, handled by yourself, and the second one
  181. * updated by this function internally.
  182. *
  183. * For example, you might reserve the first 4 bytes for the
  184. * per-message nonce, and the last 4 bytes for internal use. In that
  185. * case, before calling this function on a new message you need to
  186. * set the first 4 bytes of \p nonce_counter to your chosen nonce
  187. * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
  188. * stream_block to be ignored). That way, you can encrypt at most
  189. * 2**32 messages of up to 2**32 blocks each with the same key.
  190. *
  191. * The per-message nonce (or information sufficient to reconstruct
  192. * it) needs to be communicated with the ciphertext and must be unique.
  193. * The recommended way to ensure uniqueness is to use a message
  194. * counter.
  195. *
  196. * Note that for both stategies, sizes are measured in blocks and
  197. * that a Blowfish block is 8 bytes.
  198. *
  199. * \warning Upon return, \p stream_block contains sensitive data. Its
  200. * content must not be written to insecure storage and should be
  201. * securely discarded as soon as it's no longer needed.
  202. *
  203. * \param ctx Blowfish context
  204. * \param length The length of the data
  205. * \param nc_off The offset in the current stream_block (for resuming
  206. * within current cipher stream). The offset pointer to
  207. * should be 0 at the start of a stream.
  208. * \param nonce_counter The 64-bit nonce and counter.
  209. * \param stream_block The saved stream-block for resuming. Is overwritten
  210. * by the function.
  211. * \param input The input data stream
  212. * \param output The output data stream
  213. *
  214. * \return 0 if successful
  215. */
  216. int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
  217. size_t length,
  218. size_t *nc_off,
  219. unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
  220. unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
  221. const unsigned char *input,
  222. unsigned char *output );
  223. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227. #endif /* blowfish.h */