Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * \file camellia.h
  3.  *
  4.  * \brief Camellia 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_CAMELLIA_H
  27. #define MBEDTLS_CAMELLIA_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_CAMELLIA_ENCRYPT     1
  41. #define MBEDTLS_CAMELLIA_DECRYPT     0
  42.  
  43. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  44. #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH   MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0024 )
  45. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  46. #define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024 /**< Bad input data. */
  47.  
  48. #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
  49.  
  50. /* MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED is deprecated and should not be used.
  51.  */
  52. #define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED              -0x0027  /**< Camellia hardware accelerator failed. */
  53.  
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57.  
  58. #if !defined(MBEDTLS_CAMELLIA_ALT)
  59. // Regular implementation
  60. //
  61.  
  62. /**
  63.  * \brief          CAMELLIA context structure
  64.  */
  65. typedef struct mbedtls_camellia_context
  66. {
  67.     int nr;                     /*!<  number of rounds  */
  68.     uint32_t rk[68];            /*!<  CAMELLIA round keys    */
  69. }
  70. mbedtls_camellia_context;
  71.  
  72. #else  /* MBEDTLS_CAMELLIA_ALT */
  73. #include "camellia_alt.h"
  74. #endif /* MBEDTLS_CAMELLIA_ALT */
  75.  
  76. /**
  77.  * \brief          Initialize a CAMELLIA context.
  78.  *
  79.  * \param ctx      The CAMELLIA context to be initialized.
  80.  *                 This must not be \c NULL.
  81.  */
  82. void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
  83.  
  84. /**
  85.  * \brief          Clear a CAMELLIA context.
  86.  *
  87.  * \param ctx      The CAMELLIA context to be cleared. This may be \c NULL,
  88.  *                 in which case this function returns immediately. If it is not
  89.  *                 \c NULL, it must be initialized.
  90.  */
  91. void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
  92.  
  93. /**
  94.  * \brief          Perform a CAMELLIA key schedule operation for encryption.
  95.  *
  96.  * \param ctx      The CAMELLIA context to use. This must be initialized.
  97.  * \param key      The encryption key to use. This must be a readable buffer
  98.  *                 of size \p keybits Bits.
  99.  * \param keybits  The length of \p key in Bits. This must be either \c 128,
  100.  *                 \c 192 or \c 256.
  101.  *
  102.  * \return         \c 0 if successful.
  103.  * \return         A negative error code on failure.
  104.  */
  105. int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx,
  106.                                  const unsigned char *key,
  107.                                  unsigned int keybits );
  108.  
  109. /**
  110.  * \brief          Perform a CAMELLIA key schedule operation for decryption.
  111.  *
  112.  * \param ctx      The CAMELLIA context to use. This must be initialized.
  113.  * \param key      The decryption key. This must be a readable buffer
  114.  *                 of size \p keybits Bits.
  115.  * \param keybits  The length of \p key in Bits. This must be either \c 128,
  116.  *                 \c 192 or \c 256.
  117.  *
  118.  * \return         \c 0 if successful.
  119.  * \return         A negative error code on failure.
  120.  */
  121. int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx,
  122.                                  const unsigned char *key,
  123.                                  unsigned int keybits );
  124.  
  125. /**
  126.  * \brief          Perform a CAMELLIA-ECB block encryption/decryption operation.
  127.  *
  128.  * \param ctx      The CAMELLIA context to use. This must be initialized
  129.  *                 and bound to a key.
  130.  * \param mode     The mode of operation. This must be either
  131.  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
  132.  * \param input    The input block. This must be a readable buffer
  133.  *                 of size \c 16 Bytes.
  134.  * \param output   The output block. This must be a writable buffer
  135.  *                 of size \c 16 Bytes.
  136.  *
  137.  * \return         \c 0 if successful.
  138.  * \return         A negative error code on failure.
  139.  */
  140. int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
  141.                     int mode,
  142.                     const unsigned char input[16],
  143.                     unsigned char output[16] );
  144.  
  145. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  146. /**
  147.  * \brief          Perform a CAMELLIA-CBC buffer encryption/decryption operation.
  148.  *
  149.  * \note           Upon exit, the content of the IV is updated so that you can
  150.  *                 call the function same function again on the following
  151.  *                 block(s) of data and get the same result as if it was
  152.  *                 encrypted in one call. This allows a "streaming" usage.
  153.  *                 If on the other hand you need to retain the contents of the
  154.  *                 IV, you should either save it manually or use the cipher
  155.  *                 module instead.
  156.  *
  157.  * \param ctx      The CAMELLIA context to use. This must be initialized
  158.  *                 and bound to a key.
  159.  * \param mode     The mode of operation. This must be either
  160.  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
  161.  * \param length   The length in Bytes of the input data \p input.
  162.  *                 This must be a multiple of \c 16 Bytes.
  163.  * \param iv       The initialization vector. This must be a read/write buffer
  164.  *                 of length \c 16 Bytes. It is updated to allow streaming
  165.  *                 use as explained above.
  166.  * \param input    The buffer holding the input data. This must point to a
  167.  *                 readable buffer of length \p length Bytes.
  168.  * \param output   The buffer holding the output data. This must point to a
  169.  *                 writable buffer of length \p length Bytes.
  170.  *
  171.  * \return         \c 0 if successful.
  172.  * \return         A negative error code on failure.
  173.  */
  174. int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
  175.                     int mode,
  176.                     size_t length,
  177.                     unsigned char iv[16],
  178.                     const unsigned char *input,
  179.                     unsigned char *output );
  180. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  181.  
  182. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  183. /**
  184.  * \brief          Perform a CAMELLIA-CFB128 buffer encryption/decryption
  185.  *                 operation.
  186.  *
  187.  * \note           Due to the nature of CFB mode, you should use the same
  188.  *                 key for both encryption and decryption. In particular, calls
  189.  *                 to this function should be preceded by a key-schedule via
  190.  *                 mbedtls_camellia_setkey_enc() regardless of whether \p mode
  191.  *                 is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
  192.  *
  193.  * \note           Upon exit, the content of the IV is updated so that you can
  194.  *                 call the function same function again on the following
  195.  *                 block(s) of data and get the same result as if it was
  196.  *                 encrypted in one call. This allows a "streaming" usage.
  197.  *                 If on the other hand you need to retain the contents of the
  198.  *                 IV, you should either save it manually or use the cipher
  199.  *                 module instead.
  200.  *
  201.  * \param ctx      The CAMELLIA context to use. This must be initialized
  202.  *                 and bound to a key.
  203.  * \param mode     The mode of operation. This must be either
  204.  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
  205.  * \param length   The length of the input data \p input. Any value is allowed.
  206.  * \param iv_off   The current offset in the IV. This must be smaller
  207.  *                 than \c 16 Bytes. It is updated after this call to allow
  208.  *                 the aforementioned streaming usage.
  209.  * \param iv       The initialization vector. This must be a read/write buffer
  210.  *                 of length \c 16 Bytes. It is updated after this call to
  211.  *                 allow the aforementioned streaming usage.
  212.  * \param input    The buffer holding the input data. This must be a readable
  213.  *                 buffer of size \p length Bytes.
  214.  * \param output   The buffer to hold the output data. This must be a writable
  215.  *                 buffer of length \p length Bytes.
  216.  *
  217.  * \return         \c 0 if successful.
  218.  * \return         A negative error code on failure.
  219.  */
  220. int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
  221.                        int mode,
  222.                        size_t length,
  223.                        size_t *iv_off,
  224.                        unsigned char iv[16],
  225.                        const unsigned char *input,
  226.                        unsigned char *output );
  227. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  228.  
  229. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  230. /**
  231.  * \brief      Perform a CAMELLIA-CTR buffer encryption/decryption operation.
  232.  *
  233.  * *note       Due to the nature of CTR mode, you should use the same
  234.  *             key for both encryption and decryption. In particular, calls
  235.  *             to this function should be preceded by a key-schedule via
  236.  *             mbedtls_camellia_setkey_enc() regardless of whether \p mode
  237.  *             is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT.
  238.  *
  239.  * \warning    You must never reuse a nonce value with the same key. Doing so
  240.  *             would void the encryption for the two messages encrypted with
  241.  *             the same nonce and key.
  242.  *
  243.  *             There are two common strategies for managing nonces with CTR:
  244.  *
  245.  *             1. You can handle everything as a single message processed over
  246.  *             successive calls to this function. In that case, you want to
  247.  *             set \p nonce_counter and \p nc_off to 0 for the first call, and
  248.  *             then preserve the values of \p nonce_counter, \p nc_off and \p
  249.  *             stream_block across calls to this function as they will be
  250.  *             updated by this function.
  251.  *
  252.  *             With this strategy, you must not encrypt more than 2**128
  253.  *             blocks of data with the same key.
  254.  *
  255.  *             2. You can encrypt separate messages by dividing the \p
  256.  *             nonce_counter buffer in two areas: the first one used for a
  257.  *             per-message nonce, handled by yourself, and the second one
  258.  *             updated by this function internally.
  259.  *
  260.  *             For example, you might reserve the first \c 12 Bytes for the
  261.  *             per-message nonce, and the last \c 4 Bytes for internal use.
  262.  *             In that case, before calling this function on a new message you
  263.  *             need to set the first \c 12 Bytes of \p nonce_counter to your
  264.  *             chosen nonce value, the last four to \c 0, and \p nc_off to \c 0
  265.  *             (which will cause \p stream_block to be ignored). That way, you
  266.  *             can encrypt at most \c 2**96 messages of up to \c 2**32 blocks
  267.  *             each  with the same key.
  268.  *
  269.  *             The per-message nonce (or information sufficient to reconstruct
  270.  *             it) needs to be communicated with the ciphertext and must be
  271.  *             unique. The recommended way to ensure uniqueness is to use a
  272.  *             message counter. An alternative is to generate random nonces,
  273.  *             but this limits the number of messages that can be securely
  274.  *             encrypted: for example, with 96-bit random nonces, you should
  275.  *             not encrypt more than 2**32 messages with the same key.
  276.  *
  277.  *             Note that for both stategies, sizes are measured in blocks and
  278.  *             that a CAMELLIA block is \c 16 Bytes.
  279.  *
  280.  * \warning    Upon return, \p stream_block contains sensitive data. Its
  281.  *             content must not be written to insecure storage and should be
  282.  *             securely discarded as soon as it's no longer needed.
  283.  *
  284.  * \param ctx           The CAMELLIA context to use. This must be initialized
  285.  *                      and bound to a key.
  286.  * \param length        The length of the input data \p input in Bytes.
  287.  *                      Any value is allowed.
  288.  * \param nc_off        The offset in the current \p stream_block (for resuming
  289.  *                      within current cipher stream). The offset pointer to
  290.  *                      should be \c 0 at the start of a stream. It is updated
  291.  *                      at the end of this call.
  292.  * \param nonce_counter The 128-bit nonce and counter. This must be a read/write
  293.  *                      buffer of length \c 16 Bytes.
  294.  * \param stream_block  The saved stream-block for resuming. This must be a
  295.  *                      read/write buffer of length \c 16 Bytes.
  296.  * \param input         The input data stream. This must be a readable buffer of
  297.  *                      size \p length Bytes.
  298.  * \param output        The output data stream. This must be a writable buffer
  299.  *                      of size \p length Bytes.
  300.  *
  301.  * \return              \c 0 if successful.
  302.  * \return              A negative error code on failure.
  303.  */
  304. int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
  305.                        size_t length,
  306.                        size_t *nc_off,
  307.                        unsigned char nonce_counter[16],
  308.                        unsigned char stream_block[16],
  309.                        const unsigned char *input,
  310.                        unsigned char *output );
  311. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  312.  
  313. #if defined(MBEDTLS_SELF_TEST)
  314.  
  315. /**
  316.  * \brief          Checkup routine
  317.  *
  318.  * \return         0 if successful, or 1 if the test failed
  319.  */
  320. int mbedtls_camellia_self_test( int verbose );
  321.  
  322. #endif /* MBEDTLS_SELF_TEST */
  323.  
  324. #ifdef __cplusplus
  325. }
  326. #endif
  327.  
  328. #endif /* camellia.h */
  329.