blob: b8a358b92e97d79a3256e97e32b18c00b2fcb07a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file aes.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerfc8c4362010-03-21 17:37:16 +00004 * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00005 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Paul Bakker40e46942009-01-03 21:51:57 +000021#ifndef POLARSSL_AES_H
22#define POLARSSL_AES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000023
24#define AES_ENCRYPT 1
25#define AES_DECRYPT 0
26
Paul Bakker3391b122009-07-28 20:11:54 +000027#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0800
Paul Bakkerf3ccc682010-03-18 21:21:02 +000028#define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0810
Paul Bakker2b222c82009-07-27 21:03:45 +000029
Paul Bakker5121ce52009-01-03 21:22:43 +000030/**
31 * \brief AES context structure
32 */
33typedef struct
34{
35 int nr; /*!< number of rounds */
36 unsigned long *rk; /*!< AES round keys */
37 unsigned long buf[68]; /*!< unaligned data */
38}
39aes_context;
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/**
46 * \brief AES key schedule (encryption)
47 *
48 * \param ctx AES context to be initialized
49 * \param key encryption key
50 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000051 *
52 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000053 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000054int aes_setkey_enc( aes_context *ctx, const unsigned char *key, int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +000055
56/**
57 * \brief AES key schedule (decryption)
58 *
59 * \param ctx AES context to be initialized
60 * \param key decryption key
61 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000062 *
63 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000064 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000065int aes_setkey_dec( aes_context *ctx, const unsigned char *key, int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67/**
68 * \brief AES-ECB block encryption/decryption
69 *
70 * \param ctx AES context
71 * \param mode AES_ENCRYPT or AES_DECRYPT
72 * \param input 16-byte input block
73 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +000074 *
Paul Bakker27caa8a2010-03-21 15:43:59 +000075 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000076 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000077int aes_crypt_ecb( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000078 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000079 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +000080 unsigned char output[16] );
81
82/**
83 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +000084 * Length should be a multiple of the block
85 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +000086 *
87 * \param ctx AES context
88 * \param mode AES_ENCRYPT or AES_DECRYPT
89 * \param length length of the input data
90 * \param iv initialization vector (updated after use)
91 * \param input buffer holding the input data
92 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +000093 *
94 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000095 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000096int aes_crypt_cbc( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000097 int mode,
98 int length,
99 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000100 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 unsigned char *output );
102
103/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000104 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 *
106 * \param ctx AES context
107 * \param mode AES_ENCRYPT or AES_DECRYPT
108 * \param length length of the input data
109 * \param iv_off offset in IV (updated after use)
110 * \param iv initialization vector (updated after use)
111 * \param input buffer holding the input data
112 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000113 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000114 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000116int aes_crypt_cfb128( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 int mode,
118 int length,
119 int *iv_off,
120 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000121 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 unsigned char *output );
123
124/**
125 * \brief Checkup routine
126 *
127 * \return 0 if successful, or 1 if the test failed
128 */
129int aes_self_test( int verbose );
130
131#ifdef __cplusplus
132}
133#endif
134
135#endif /* aes.h */