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