Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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: GPL-2.0
  9.  *
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License along
  21.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  22.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23.  *
  24.  *  This file is part of mbed TLS (https://tls.mbed.org)
  25.  */
  26. #ifndef MBEDTLS_BLOWFISH_H
  27. #define MBEDTLS_BLOWFISH_H
  28.  
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34.  
  35. #include <stddef.h>
  36. #include <stdint.h>
  37.  
  38. #include "platform_util.h"
  39.  
  40. #define MBEDTLS_BLOWFISH_ENCRYPT     1
  41. #define MBEDTLS_BLOWFISH_DECRYPT     0
  42. #define MBEDTLS_BLOWFISH_MAX_KEY_BITS     448
  43. #define MBEDTLS_BLOWFISH_MIN_KEY_BITS     32
  44. #define MBEDTLS_BLOWFISH_ROUNDS      16         /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */
  45. #define MBEDTLS_BLOWFISH_BLOCKSIZE   8          /* Blowfish uses 64 bit blocks */
  46.  
  47. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  48. #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH   MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0016 )
  49. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  50. #define MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA -0x0016 /**< Bad input data. */
  51.  
  52. #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */
  53.  
  54. /* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used.
  55.  */
  56. #define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED                   -0x0017  /**< Blowfish hardware accelerator failed. */
  57.  
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61.  
  62. #if !defined(MBEDTLS_BLOWFISH_ALT)
  63. // Regular implementation
  64. //
  65.  
  66. /**
  67.  * \brief          Blowfish context structure
  68.  */
  69. typedef struct mbedtls_blowfish_context
  70. {
  71.     uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2];    /*!<  Blowfish round keys    */
  72.     uint32_t S[4][256];                 /*!<  key dependent S-boxes  */
  73. }
  74. mbedtls_blowfish_context;
  75.  
  76. #else  /* MBEDTLS_BLOWFISH_ALT */
  77. #include "blowfish_alt.h"
  78. #endif /* MBEDTLS_BLOWFISH_ALT */
  79.  
  80. /**
  81.  * \brief          Initialize a Blowfish context.
  82.  *
  83.  * \param ctx      The Blowfish context to be initialized.
  84.  *                 This must not be \c NULL.
  85.  */
  86. void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx );
  87.  
  88. /**
  89.  * \brief          Clear a Blowfish context.
  90.  *
  91.  * \param ctx      The Blowfish context to be cleared.
  92.  *                 This may be \c NULL, in which case this function
  93.  *                 returns immediately. If it is not \c NULL, it must
  94.  *                 point to an initialized Blowfish context.
  95.  */
  96. void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx );
  97.  
  98. /**
  99.  * \brief          Perform a Blowfish key schedule operation.
  100.  *
  101.  * \param ctx      The Blowfish context to perform the key schedule on.
  102.  * \param key      The encryption key. This must be a readable buffer of
  103.  *                 length \p keybits Bits.
  104.  * \param keybits  The length of \p key in Bits. This must be between
  105.  *                 \c 32 and \c 448 and a multiple of \c 8.
  106.  *
  107.  * \return         \c 0 if successful.
  108.  * \return         A negative error code on failure.
  109.  */
  110. int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
  111.                      unsigned int keybits );
  112.  
  113. /**
  114.  * \brief          Perform a Blowfish-ECB block encryption/decryption operation.
  115.  *
  116.  * \param ctx      The Blowfish context to use. This must be initialized
  117.  *                 and bound to a key.
  118.  * \param mode     The mode of operation. Possible values are
  119.  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
  120.  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption.
  121.  * \param input    The input block. This must be a readable buffer
  122.  *                 of size \c 8 Bytes.
  123.  * \param output   The output block. This must be a writable buffer
  124.  *                 of size \c 8 Bytes.
  125.  *
  126.  * \return         \c 0 if successful.
  127.  * \return         A negative error code on failure.
  128.  */
  129. int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
  130.                         int mode,
  131.                         const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
  132.                         unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] );
  133.  
  134. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  135. /**
  136.  * \brief          Perform a Blowfish-CBC buffer encryption/decryption operation.
  137.  *
  138.  * \note           Upon exit, the content of the IV is updated so that you can
  139.  *                 call the function same function again on the following
  140.  *                 block(s) of data and get the same result as if it was
  141.  *                 encrypted in one call. This allows a "streaming" usage.
  142.  *                 If on the other hand you need to retain the contents of the
  143.  *                 IV, you should either save it manually or use the cipher
  144.  *                 module instead.
  145.  *
  146.  * \param ctx      The Blowfish context to use. This must be initialized
  147.  *                 and bound to a key.
  148.  * \param mode     The mode of operation. Possible values are
  149.  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
  150.  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption.
  151.  * \param length   The length of the input data in Bytes. This must be
  152.  *                 multiple of \c 8.
  153.  * \param iv       The initialization vector. This must be a read/write buffer
  154.  *                 of length \c 8 Bytes. It is updated by this function.
  155.  * \param input    The input data. This must be a readable buffer of length
  156.  *                 \p length Bytes.
  157.  * \param output   The output data. This must be a writable buffer of length
  158.  *                 \p length Bytes.
  159.  *
  160.  * \return         \c 0 if successful.
  161.  * \return         A negative error code on failure.
  162.  */
  163. int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
  164.                         int mode,
  165.                         size_t length,
  166.                         unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
  167.                         const unsigned char *input,
  168.                         unsigned char *output );
  169. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  170.  
  171. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  172. /**
  173.  * \brief          Perform a Blowfish CFB buffer encryption/decryption operation.
  174.  *
  175.  * \note           Upon exit, the content of the IV is updated so that you can
  176.  *                 call the function same function again on the following
  177.  *                 block(s) of data and get the same result as if it was
  178.  *                 encrypted in one call. This allows a "streaming" usage.
  179.  *                 If on the other hand you need to retain the contents of the
  180.  *                 IV, you should either save it manually or use the cipher
  181.  *                 module instead.
  182.  *
  183.  * \param ctx      The Blowfish context to use. This must be initialized
  184.  *                 and bound to a key.
  185.  * \param mode     The mode of operation. Possible values are
  186.  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or
  187.  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption.
  188.  * \param length   The length of the input data in Bytes.
  189.  * \param iv_off   The offset in the initialiation vector.
  190.  *                 The value pointed to must be smaller than \c 8 Bytes.
  191.  *                 It is updated by this function to support the aforementioned
  192.  *                 streaming usage.
  193.  * \param iv       The initialization vector. This must be a read/write buffer
  194.  *                 of size \c 8 Bytes. It is updated after use.
  195.  * \param input    The input data. This must be a readable buffer of length
  196.  *                 \p length Bytes.
  197.  * \param output   The output data. This must be a writable buffer of length
  198.  *                 \p length Bytes.
  199.  *
  200.  * \return         \c 0 if successful.
  201.  * \return         A negative error code on failure.
  202.  */
  203. int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
  204.                           int mode,
  205.                           size_t length,
  206.                           size_t *iv_off,
  207.                           unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
  208.                           const unsigned char *input,
  209.                           unsigned char *output );
  210. #endif /*MBEDTLS_CIPHER_MODE_CFB */
  211.  
  212. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  213. /**
  214.  * \brief      Perform a Blowfish-CTR buffer encryption/decryption operation.
  215.  *
  216.  * \warning    You must never reuse a nonce value with the same key. Doing so
  217.  *             would void the encryption for the two messages encrypted with
  218.  *             the same nonce and key.
  219.  *
  220.  *             There are two common strategies for managing nonces with CTR:
  221.  *
  222.  *             1. You can handle everything as a single message processed over
  223.  *             successive calls to this function. In that case, you want to
  224.  *             set \p nonce_counter and \p nc_off to 0 for the first call, and
  225.  *             then preserve the values of \p nonce_counter, \p nc_off and \p
  226.  *             stream_block across calls to this function as they will be
  227.  *             updated by this function.
  228.  *
  229.  *             With this strategy, you must not encrypt more than 2**64
  230.  *             blocks of data with the same key.
  231.  *
  232.  *             2. You can encrypt separate messages by dividing the \p
  233.  *             nonce_counter buffer in two areas: the first one used for a
  234.  *             per-message nonce, handled by yourself, and the second one
  235.  *             updated by this function internally.
  236.  *
  237.  *             For example, you might reserve the first 4 bytes for the
  238.  *             per-message nonce, and the last 4 bytes for internal use. In that
  239.  *             case, before calling this function on a new message you need to
  240.  *             set the first 4 bytes of \p nonce_counter to your chosen nonce
  241.  *             value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
  242.  *             stream_block to be ignored). That way, you can encrypt at most
  243.  *             2**32 messages of up to 2**32 blocks each with the same key.
  244.  *
  245.  *             The per-message nonce (or information sufficient to reconstruct
  246.  *             it) needs to be communicated with the ciphertext and must be unique.
  247.  *             The recommended way to ensure uniqueness is to use a message
  248.  *             counter.
  249.  *
  250.  *             Note that for both stategies, sizes are measured in blocks and
  251.  *             that a Blowfish block is 8 bytes.
  252.  *
  253.  * \warning    Upon return, \p stream_block contains sensitive data. Its
  254.  *             content must not be written to insecure storage and should be
  255.  *             securely discarded as soon as it's no longer needed.
  256.  *
  257.  * \param ctx           The Blowfish context to use. This must be initialized
  258.  *                      and bound to a key.
  259.  * \param length        The length of the input data in Bytes.
  260.  * \param nc_off        The offset in the current stream_block (for resuming
  261.  *                      within current cipher stream). The offset pointer
  262.  *                      should be \c 0 at the start of a stream and must be
  263.  *                      smaller than \c 8. It is updated by this function.
  264.  * \param nonce_counter The 64-bit nonce and counter. This must point to a
  265.  *                      read/write buffer of length \c 8 Bytes.
  266.  * \param stream_block  The saved stream-block for resuming. This must point to
  267.  *                      a read/write buffer of length \c 8 Bytes.
  268.  * \param input         The input data. This must be a readable buffer of
  269.  *                      length \p length Bytes.
  270.  * \param output        The output data. This must be a writable buffer of
  271.  *                      length \p length Bytes.
  272.  *
  273.  * \return              \c 0 if successful.
  274.  * \return              A negative error code on failure.
  275.  */
  276. int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
  277.                         size_t length,
  278.                         size_t *nc_off,
  279.                         unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
  280.                         unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
  281.                         const unsigned char *input,
  282.                         unsigned char *output );
  283. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  284.  
  285. #ifdef __cplusplus
  286. }
  287. #endif
  288.  
  289. #endif /* blowfish.h */
  290.