blowfish.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #if !defined(MBEDTLS_BLOWFISH_ALT)
  43. // Regular implementation
  44. //
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  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. /**
  58. * \brief Initialize Blowfish context
  59. *
  60. * \param ctx Blowfish context to be initialized
  61. */
  62. void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
  63. /**
  64. * \brief Clear Blowfish context
  65. *
  66. * \param ctx Blowfish context to be cleared
  67. */
  68. void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
  69. /**
  70. * \brief Blowfish key schedule
  71. *
  72. * \param ctx Blowfish context to be initialized
  73. * \param key encryption key
  74. * \param keybits must be between 32 and 448 bits
  75. *
  76. * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH
  77. */
  78. int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
  79. unsigned int keybits );
  80. /**
  81. * \brief Blowfish-ECB block encryption/decryption
  82. *
  83. * \param ctx Blowfish context
  84. * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
  85. * \param input 8-byte input block
  86. * \param output 8-byte output block
  87. *
  88. * \return 0 if successful
  89. */
  90. int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
  91. int mode,
  92. const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
  93. unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
  94. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  95. /**
  96. * \brief Blowfish-CBC buffer encryption/decryption
  97. * Length should be a multiple of the block
  98. * size (8 bytes)
  99. *
  100. * \note Upon exit, the content of the IV is updated so that you can
  101. * call the function same function again on the following
  102. * block(s) of data and get the same result as if it was
  103. * encrypted in one call. This allows a "streaming" usage.
  104. * If on the other hand you need to retain the contents of the
  105. * IV, you should either save it manually or use the cipher
  106. * module instead.
  107. *
  108. * \param ctx Blowfish context
  109. * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
  110. * \param length length of the input data
  111. * \param iv initialization vector (updated after use)
  112. * \param input buffer holding the input data
  113. * \param output buffer holding the output data
  114. *
  115. * \return 0 if successful, or
  116. * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH
  117. */
  118. int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
  119. int mode,
  120. size_t length,
  121. unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
  122. const unsigned char *input,
  123. unsigned char *output );
  124. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  125. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  126. /**
  127. * \brief Blowfish CFB buffer encryption/decryption.
  128. *
  129. * \note Upon exit, the content of the IV is updated so that you can
  130. * call the function same function again on the following
  131. * block(s) of data and get the same result as if it was
  132. * encrypted in one call. This allows a "streaming" usage.
  133. * If on the other hand you need to retain the contents of the
  134. * IV, you should either save it manually or use the cipher
  135. * module instead.
  136. *
  137. * \param ctx Blowfish context
  138. * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT
  139. * \param length length of the input data
  140. * \param iv_off offset in IV (updated after use)
  141. * \param iv initialization vector (updated after use)
  142. * \param input buffer holding the input data
  143. * \param output buffer holding the output data
  144. *
  145. * \return 0 if successful
  146. */
  147. int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
  148. int mode,
  149. size_t length,
  150. size_t *iv_off,
  151. unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
  152. const unsigned char *input,
  153. unsigned char *output );
  154. #endif /*MBEDTLS_CIPHER_MODE_CFB */
  155. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  156. /**
  157. * \brief Blowfish-CTR buffer encryption/decryption
  158. *
  159. * Warning: You have to keep the maximum use of your counter in mind!
  160. *
  161. * \param ctx Blowfish context
  162. * \param length The length of the data
  163. * \param nc_off The offset in the current stream_block (for resuming
  164. * within current cipher stream). The offset pointer to
  165. * should be 0 at the start of a stream.
  166. * \param nonce_counter The 64-bit nonce and counter.
  167. * \param stream_block The saved stream-block for resuming. Is overwritten
  168. * by the function.
  169. * \param input The input data stream
  170. * \param output The output data stream
  171. *
  172. * \return 0 if successful
  173. */
  174. int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
  175. size_t length,
  176. size_t *nc_off,
  177. unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
  178. unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
  179. const unsigned char *input,
  180. unsigned char *output );
  181. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  182. #ifdef __cplusplus
  183. }
  184. #endif
  185. #else /* MBEDTLS_BLOWFISH_ALT */
  186. #include "blowfish_alt.h"
  187. #endif /* MBEDTLS_BLOWFISH_ALT */
  188. #endif /* blowfish.h */