blob: dc971c19eb405508170da1df88dc012484a688bd [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
28/**
29 * \brief AES context structure
30 */
31typedef struct
32{
33 int nr; /*!< number of rounds */
34 unsigned long *rk; /*!< AES round keys */
35 unsigned long buf[68]; /*!< unaligned data */
36}
37aes_context;
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/**
44 * \brief AES key schedule (encryption)
45 *
46 * \param ctx AES context to be initialized
47 * \param key encryption key
48 * \param keysize must be 128, 192 or 256
49 */
50void aes_setkey_enc( aes_context *ctx, unsigned char *key, int keysize );
51
52/**
53 * \brief AES key schedule (decryption)
54 *
55 * \param ctx AES context to be initialized
56 * \param key decryption key
57 * \param keysize must be 128, 192 or 256
58 */
59void aes_setkey_dec( aes_context *ctx, unsigned char *key, int keysize );
60
61/**
62 * \brief AES-ECB block encryption/decryption
63 *
64 * \param ctx AES context
65 * \param mode AES_ENCRYPT or AES_DECRYPT
66 * \param input 16-byte input block
67 * \param output 16-byte output block
68 */
69void aes_crypt_ecb( aes_context *ctx,
70 int mode,
71 unsigned char input[16],
72 unsigned char output[16] );
73
74/**
75 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +000076 * Length should be a multiple of the block
77 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +000078 *
79 * \param ctx AES context
80 * \param mode AES_ENCRYPT or AES_DECRYPT
81 * \param length length of the input data
82 * \param iv initialization vector (updated after use)
83 * \param input buffer holding the input data
84 * \param output buffer holding the output data
85 */
86void aes_crypt_cbc( aes_context *ctx,
87 int mode,
88 int length,
89 unsigned char iv[16],
90 unsigned char *input,
91 unsigned char *output );
92
93/**
Paul Bakker4c067eb2009-05-17 10:25:19 +000094 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +000095 *
96 * \param ctx AES context
97 * \param mode AES_ENCRYPT or AES_DECRYPT
98 * \param length length of the input data
99 * \param iv_off offset in IV (updated after use)
100 * \param iv initialization vector (updated after use)
101 * \param input buffer holding the input data
102 * \param output buffer holding the output data
103 */
104void aes_crypt_cfb128( aes_context *ctx,
105 int mode,
106 int length,
107 int *iv_off,
108 unsigned char iv[16],
109 unsigned char *input,
110 unsigned char *output );
111
112/**
113 * \brief Checkup routine
114 *
115 * \return 0 if successful, or 1 if the test failed
116 */
117int aes_self_test( int verbose );
118
119#ifdef __cplusplus
120}
121#endif
122
123#endif /* aes.h */