1 /*
2  *  FIPS-197 compliant AES implementation
3  *
4  *  Copyright (C) 2006-2007  Christophe Devine
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions
8  *  are met:
9  *
10  *    * Redistributions of source code _must_ retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *    * Redistributions in binary form may or may not reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials provided
15  *      with the distribution.
16  *    * Neither the name of XySSL nor the names of its contributors may be
17  *      used to endorse or promote products derived from this software
18  *      without specific prior written permission.
19  *
20  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26  *  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /* $Id: aes.h 8874 2008-07-24 17:28:51Z giles $ */
34 /* AES block cipher implementation from XYSSL */
35 
36 #ifndef XYSSL_AES_H
37 #define XYSSL_AES_H
38 
39 #define AES_ENCRYPT     1
40 #define AES_DECRYPT     0
41 
42 /**
43  * \brief          AES context structure
44  */
45 typedef struct
46 {
47     int nr;                     /*!<  number of rounds  */
48     unsigned long *rk;          /*!<  AES round keys    */
49     unsigned long buf[68];      /*!<  unaligned data    */
50 }
51 aes_context;
52 
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 /**
58  * \brief          AES key schedule (encryption)
59  *
60  * \param ctx      AES context to be initialized
61  * \param key      encryption key
62  * \param keysize  must be 128, 192 or 256
63  */
64 void aes_setkey_enc( aes_context *ctx, const unsigned char *key,
65 			int keysize );
66 
67 /**
68  * \brief          AES key schedule (decryption)
69  *
70  * \param ctx      AES context to be initialized
71  * \param key      decryption key
72  * \param keysize  must be 128, 192 or 256
73  */
74 void aes_setkey_dec( aes_context *ctx, const unsigned char *key,
75 			int keysize );
76 
77 /**
78  * \brief          AES-ECB block encryption/decryption
79  *
80  * \param ctx      AES context
81  * \param mode     AES_ENCRYPT or AES_DECRYPT
82  * \param input    16-byte input block
83  * \param output   16-byte output block
84  */
85 void 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  *
93  * \param ctx      AES context
94  * \param mode     AES_ENCRYPT or AES_DECRYPT
95  * \param length   length of the input data
96  * \param iv       initialization vector (updated after use)
97  * \param input    buffer holding the input data
98  * \param output   buffer holding the output data
99  */
100 void aes_crypt_cbc( aes_context *ctx,
101                     int mode,
102                     int length,
103                     unsigned char iv[16],
104                     const unsigned char *input,
105                     unsigned char *output );
106 
107 /**
108  * \brief          AES-CFB buffer encryption/decryption
109  *
110  * \param ctx      AES context
111  * \param mode     AES_ENCRYPT or AES_DECRYPT
112  * \param length   length of the input data
113  * \param iv_off   offset in IV (updated after use)
114  * \param iv       initialization vector (updated after use)
115  * \param input    buffer holding the input data
116  * \param output   buffer holding the output data
117  */
118 void aes_crypt_cfb( aes_context *ctx,
119                     int mode,
120                     int length,
121                     int *iv_off,
122                     unsigned char iv[16],
123                     const unsigned char *input,
124                     unsigned char *output );
125 
126 /**
127  * \brief          Checkup routine
128  *
129  * \return         0 if successful, or 1 if the test failed
130  */
131 int aes_self_test( int verbose );
132 
133 #ifdef __cplusplus
134 }
135 #endif
136 
137 #endif /* aes.h */
138