source: trunk/include/polarssl/aes.h @ 1095

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

What are you looking for?