blob: 8268390eac10a39e04aac6745fa22db6f6354cd9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file aes.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Paul Bakker40e46942009-01-03 21:51:57 +000023#ifndef POLARSSL_AES_H
24#define POLARSSL_AES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
26#define AES_ENCRYPT 1
27#define AES_DECRYPT 0
28
Paul Bakker3391b122009-07-28 20:11:54 +000029#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0800
Paul Bakkerf3ccc682010-03-18 21:21:02 +000030#define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0810
Paul Bakker2b222c82009-07-27 21:03:45 +000031
Paul Bakker5121ce52009-01-03 21:22:43 +000032/**
33 * \brief AES context structure
34 */
35typedef struct
36{
37 int nr; /*!< number of rounds */
38 unsigned long *rk; /*!< AES round keys */
39 unsigned long buf[68]; /*!< unaligned data */
40}
41aes_context;
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47/**
48 * \brief AES key schedule (encryption)
49 *
50 * \param ctx AES context to be initialized
51 * \param key encryption key
52 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000053 *
54 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000056int aes_setkey_enc( aes_context *ctx, const unsigned char *key, int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +000057
58/**
59 * \brief AES key schedule (decryption)
60 *
61 * \param ctx AES context to be initialized
62 * \param key decryption key
63 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000064 *
65 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000066 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000067int aes_setkey_dec( aes_context *ctx, const unsigned char *key, int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +000068
69/**
70 * \brief AES-ECB block encryption/decryption
71 *
72 * \param ctx AES context
73 * \param mode AES_ENCRYPT or AES_DECRYPT
74 * \param input 16-byte input block
75 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +000076 *
Paul Bakker27caa8a2010-03-21 15:43:59 +000077 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000078 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000079int aes_crypt_ecb( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000080 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +000082 unsigned char output[16] );
83
84/**
85 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +000086 * Length should be a multiple of the block
87 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +000088 *
89 * \param ctx AES context
90 * \param mode AES_ENCRYPT or AES_DECRYPT
91 * \param length length of the input data
92 * \param iv initialization vector (updated after use)
93 * \param input buffer holding the input data
94 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +000095 *
96 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000097 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000098int aes_crypt_cbc( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000099 int mode,
100 int length,
101 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000102 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 unsigned char *output );
104
105/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000106 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 *
108 * \param ctx AES context
109 * \param mode AES_ENCRYPT or AES_DECRYPT
110 * \param length length of the input data
111 * \param iv_off offset in IV (updated after use)
112 * \param iv initialization vector (updated after use)
113 * \param input buffer holding the input data
114 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000115 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000116 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000118int aes_crypt_cfb128( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 int mode,
120 int length,
121 int *iv_off,
122 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000123 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 unsigned char *output );
125
126/**
127 * \brief Checkup routine
128 *
129 * \return 0 if successful, or 1 if the test failed
130 */
131int aes_self_test( int verbose );
132
133#ifdef __cplusplus
134}
135#endif
136
137#endif /* aes.h */