blowfish.h 7.3 KB

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