blob: 1c4fa3b837b4e60b16b3a76c17f573f743f49f5d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file aes.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
Paul Bakker785a9ee2009-01-25 14:15:10 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000021 */
Paul Bakker40e46942009-01-03 21:51:57 +000022#ifndef POLARSSL_AES_H
23#define POLARSSL_AES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000024
25#define AES_ENCRYPT 1
26#define AES_DECRYPT 0
27
Paul Bakker2b222c82009-07-27 21:03:45 +000028#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH 0x0800
29
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 Bakker2b222c82009-07-27 21:03:45 +000054int aes_setkey_enc( aes_context *ctx, 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 Bakker2b222c82009-07-27 21:03:45 +000065int aes_setkey_dec( aes_context *ctx, 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
74 */
75void aes_crypt_ecb( aes_context *ctx,
76 int mode,
77 unsigned char input[16],
78 unsigned char output[16] );
79
80/**
81 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +000082 * Length should be a multiple of the block
83 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +000084 *
85 * \param ctx AES context
86 * \param mode AES_ENCRYPT or AES_DECRYPT
87 * \param length length of the input data
88 * \param iv initialization vector (updated after use)
89 * \param input buffer holding the input data
90 * \param output buffer holding the output data
91 */
92void aes_crypt_cbc( aes_context *ctx,
93 int mode,
94 int length,
95 unsigned char iv[16],
96 unsigned char *input,
97 unsigned char *output );
98
99/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000100 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 *
102 * \param ctx AES context
103 * \param mode AES_ENCRYPT or AES_DECRYPT
104 * \param length length of the input data
105 * \param iv_off offset in IV (updated after use)
106 * \param iv initialization vector (updated after use)
107 * \param input buffer holding the input data
108 * \param output buffer holding the output data
109 */
110void aes_crypt_cfb128( aes_context *ctx,
111 int mode,
112 int length,
113 int *iv_off,
114 unsigned char iv[16],
115 unsigned char *input,
116 unsigned char *output );
117
118/**
119 * \brief Checkup routine
120 *
121 * \return 0 if successful, or 1 if the test failed
122 */
123int aes_self_test( int verbose );
124
125#ifdef __cplusplus
126}
127#endif
128
129#endif /* aes.h */