Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /**
  2.  * \file des.h
  3.  *
  4.  * \brief DES block cipher
  5.  *
  6.  *  Copyright (C) 2006-2010, Brainspark B.V.
  7.  *
  8.  *  This file is part of PolarSSL (http://www.polarssl.org)
  9.  *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10.  *
  11.  *  All rights reserved.
  12.  *
  13.  *  This program is free software; you can redistribute it and/or modify
  14.  *  it under the terms of the GNU General Public License as published by
  15.  *  the Free Software Foundation; either version 2 of the License, or
  16.  *  (at your option) any later version.
  17.  *
  18.  *  This program is distributed in the hope that it will be useful,
  19.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  *  GNU General Public License for more details.
  22.  *
  23.  *  You should have received a copy of the GNU General Public License along
  24.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  25.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26.  */
  27. #ifndef POLARSSL_DES_H
  28. #define POLARSSL_DES_H
  29.  
  30. #include <string.h>
  31.  
  32. #ifdef _MSC_VER
  33. #include <basetsd.h>
  34. typedef UINT32 uint32_t;
  35. #else
  36. #include <inttypes.h>
  37. #endif
  38.  
  39. #define DES_ENCRYPT     1
  40. #define DES_DECRYPT     0
  41.  
  42. #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH              -0x0032  /**< The data input has an invalid length. */
  43.  
  44. #define DES_KEY_SIZE    8
  45.  
  46. /**
  47.  * \brief          DES context structure
  48.  */
  49. typedef struct
  50. {
  51.     int mode;                   /*!<  encrypt/decrypt   */
  52.     uint32_t sk[32];            /*!<  DES subkeys       */
  53. }
  54. des_context;
  55.  
  56. /**
  57.  * \brief          Triple-DES context structure
  58.  */
  59. typedef struct
  60. {
  61.     int mode;                   /*!<  encrypt/decrypt   */
  62.     uint32_t sk[96];            /*!<  3DES subkeys      */
  63. }
  64. des3_context;
  65.  
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69.  
  70. /**
  71.  * \brief          Set key parity on the given key to odd.
  72.  *
  73.  *                 DES keys are 56 bits long, but each byte is padded with
  74.  *                 a parity bit to allow verification.
  75.  *
  76.  * \param key      8-byte secret key
  77.  */
  78. void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
  79.  
  80. /**
  81.  * \brief          Check that key parity on the given key is odd.
  82.  *
  83.  *                 DES keys are 56 bits long, but each byte is padded with
  84.  *                 a parity bit to allow verification.
  85.  *
  86.  * \param key      8-byte secret key
  87.  *
  88.  * \return         0 is parity was ok, 1 if parity was not correct.
  89.  */
  90. int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
  91.  
  92. /**
  93.  * \brief          Check that key is not a weak or semi-weak DES key
  94.  *
  95.  * \param key      8-byte secret key
  96.  *
  97.  * \return         0 if no weak key was found, 1 if a weak key was identified.
  98.  */
  99. int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
  100.  
  101. /**
  102.  * \brief          DES key schedule (56-bit, encryption)
  103.  *
  104.  * \param ctx      DES context to be initialized
  105.  * \param key      8-byte secret key
  106.  *
  107.  * \return         0
  108.  */
  109. int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
  110.  
  111. /**
  112.  * \brief          DES key schedule (56-bit, decryption)
  113.  *
  114.  * \param ctx      DES context to be initialized
  115.  * \param key      8-byte secret key
  116.  *
  117.  * \return         0
  118.  */
  119. int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
  120.  
  121. /**
  122.  * \brief          Triple-DES key schedule (112-bit, encryption)
  123.  *
  124.  * \param ctx      3DES context to be initialized
  125.  * \param key      16-byte secret key
  126.  *
  127.  * \return         0
  128.  */
  129. int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
  130.  
  131. /**
  132.  * \brief          Triple-DES key schedule (112-bit, decryption)
  133.  *
  134.  * \param ctx      3DES context to be initialized
  135.  * \param key      16-byte secret key
  136.  *
  137.  * \return         0
  138.  */
  139. int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
  140.  
  141. /**
  142.  * \brief          Triple-DES key schedule (168-bit, encryption)
  143.  *
  144.  * \param ctx      3DES context to be initialized
  145.  * \param key      24-byte secret key
  146.  *
  147.  * \return         0
  148.  */
  149. int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
  150.  
  151. /**
  152.  * \brief          Triple-DES key schedule (168-bit, decryption)
  153.  *
  154.  * \param ctx      3DES context to be initialized
  155.  * \param key      24-byte secret key
  156.  *
  157.  * \return         0
  158.  */
  159. int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
  160.  
  161. /**
  162.  * \brief          DES-ECB block encryption/decryption
  163.  *
  164.  * \param ctx      DES context
  165.  * \param input    64-bit input block
  166.  * \param output   64-bit output block
  167.  *
  168.  * \return         0 if successful
  169.  */
  170. int des_crypt_ecb( des_context *ctx,
  171.                     const unsigned char input[8],
  172.                     unsigned char output[8] );
  173.  
  174. /**
  175.  * \brief          DES-CBC buffer encryption/decryption
  176.  *
  177.  * \param ctx      DES context
  178.  * \param mode     DES_ENCRYPT or DES_DECRYPT
  179.  * \param length   length of the input data
  180.  * \param iv       initialization vector (updated after use)
  181.  * \param input    buffer holding the input data
  182.  * \param output   buffer holding the output data
  183.  */
  184. int des_crypt_cbc( des_context *ctx,
  185.                     int mode,
  186.                     size_t length,
  187.                     unsigned char iv[8],
  188.                     const unsigned char *input,
  189.                     unsigned char *output );
  190.  
  191. /**
  192.  * \brief          3DES-ECB block encryption/decryption
  193.  *
  194.  * \param ctx      3DES context
  195.  * \param input    64-bit input block
  196.  * \param output   64-bit output block
  197.  *
  198.  * \return         0 if successful
  199.  */
  200. int des3_crypt_ecb( des3_context *ctx,
  201.                      const unsigned char input[8],
  202.                      unsigned char output[8] );
  203.  
  204. /**
  205.  * \brief          3DES-CBC buffer encryption/decryption
  206.  *
  207.  * \param ctx      3DES context
  208.  * \param mode     DES_ENCRYPT or DES_DECRYPT
  209.  * \param length   length of the input data
  210.  * \param iv       initialization vector (updated after use)
  211.  * \param input    buffer holding the input data
  212.  * \param output   buffer holding the output data
  213.  *
  214.  * \return         0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
  215.  */
  216. int des3_crypt_cbc( des3_context *ctx,
  217.                      int mode,
  218.                      size_t length,
  219.                      unsigned char iv[8],
  220.                      const unsigned char *input,
  221.                      unsigned char *output );
  222.  
  223. /**
  224.  * \brief          Checkup routine
  225.  *
  226.  * \return         0 if successful, or 1 if the test failed
  227.  */
  228. int des_self_test( int verbose );
  229.  
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233.  
  234. #endif /* des.h */
  235.