Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * \file aes.h
  3.  *
  4.  * \brief AES 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_AES_H
  28. #define POLARSSL_AES_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 AES_ENCRYPT     1
  40. #define AES_DECRYPT     0
  41.  
  42. #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH                -0x0020  /**< Invalid key length. */
  43. #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH              -0x0022  /**< Invalid data input length. */
  44.  
  45. /**
  46.  * \brief          AES context structure
  47.  */
  48. typedef struct
  49. {
  50.     int nr;                     /*!<  number of rounds  */
  51.     uint32_t *rk;               /*!<  AES round keys    */
  52.     uint32_t buf[68];           /*!<  unaligned data    */
  53. }
  54. aes_context;
  55.  
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59.  
  60. /**
  61.  * \brief          AES key schedule (encryption)
  62.  *
  63.  * \param ctx      AES context to be initialized
  64.  * \param key      encryption key
  65.  * \param keysize  must be 128, 192 or 256
  66.  *
  67.  * \return         0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
  68.  */
  69. int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize );
  70.  
  71. /**
  72.  * \brief          AES key schedule (decryption)
  73.  *
  74.  * \param ctx      AES context to be initialized
  75.  * \param key      decryption key
  76.  * \param keysize  must be 128, 192 or 256
  77.  *
  78.  * \return         0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
  79.  */
  80. int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize );
  81.  
  82. /**
  83.  * \brief          AES-ECB block encryption/decryption
  84.  *
  85.  * \param ctx      AES context
  86.  * \param mode     AES_ENCRYPT or AES_DECRYPT
  87.  * \param input    16-byte input block
  88.  * \param output   16-byte output block
  89.  *
  90.  * \return         0 if successful
  91.  */
  92. int aes_crypt_ecb( aes_context *ctx,
  93.                     int mode,
  94.                     const unsigned char input[16],
  95.                     unsigned char output[16] );
  96.  
  97. /**
  98.  * \brief          AES-CBC buffer encryption/decryption
  99.  *                 Length should be a multiple of the block
  100.  *                 size (16 bytes)
  101.  *
  102.  * \param ctx      AES context
  103.  * \param mode     AES_ENCRYPT or AES_DECRYPT
  104.  * \param length   length of the input data
  105.  * \param iv       initialization vector (updated after use)
  106.  * \param input    buffer holding the input data
  107.  * \param output   buffer holding the output data
  108.  *
  109.  * \return         0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
  110.  */
  111. int aes_crypt_cbc( aes_context *ctx,
  112.                     int mode,
  113.                     size_t length,
  114.                     unsigned char iv[16],
  115.                     const unsigned char *input,
  116.                     unsigned char *output );
  117.  
  118. /**
  119.  * \brief          AES-CFB128 buffer encryption/decryption.
  120.  *
  121.  * Note: Due to the nature of CFB you should use the same key schedule for
  122.  * both encryption and decryption. So a context initialized with
  123.  * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  124.  *
  125.  * both
  126.  * \param ctx      AES context
  127.  * \param mode     AES_ENCRYPT or AES_DECRYPT
  128.  * \param length   length of the input data
  129.  * \param iv_off   offset in IV (updated after use)
  130.  * \param iv       initialization vector (updated after use)
  131.  * \param input    buffer holding the input data
  132.  * \param output   buffer holding the output data
  133.  *
  134.  * \return         0 if successful
  135.  */
  136. int aes_crypt_cfb128( aes_context *ctx,
  137.                        int mode,
  138.                        size_t length,
  139.                        size_t *iv_off,
  140.                        unsigned char iv[16],
  141.                        const unsigned char *input,
  142.                        unsigned char *output );
  143.  
  144. /**
  145.  * \brief               AES-CTR buffer encryption/decryption
  146.  *
  147.  * Warning: You have to keep the maximum use of your counter in mind!
  148.  *
  149.  * Note: Due to the nature of CTR you should use the same key schedule for
  150.  * both encryption and decryption. So a context initialized with
  151.  * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  152.  *
  153.  * \param length        The length of the data
  154.  * \param nc_off        The offset in the current stream_block (for resuming
  155.  *                      within current cipher stream). The offset pointer to
  156.  *                      should be 0 at the start of a stream.
  157.  * \param nonce_counter The 128-bit nonce and counter.
  158.  * \param stream_block  The saved stream-block for resuming. Is overwritten
  159.  *                      by the function.
  160.  * \param input         The input data stream
  161.  * \param output        The output data stream
  162.  *
  163.  * \return         0 if successful
  164.  */
  165. int aes_crypt_ctr( aes_context *ctx,
  166.                        size_t length,
  167.                        size_t *nc_off,
  168.                        unsigned char nonce_counter[16],
  169.                        unsigned char stream_block[16],
  170.                        const unsigned char *input,
  171.                        unsigned char *output );
  172. /**
  173.  * \brief          Checkup routine
  174.  *
  175.  * \return         0 if successful, or 1 if the test failed
  176.  */
  177. int aes_self_test( int verbose );
  178.  
  179. #ifdef __cplusplus
  180. }
  181. #endif
  182.  
  183. #endif /* aes.h */
  184.