blob: 9cb4a92951584f0e9abc919cb3da109b647be243 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file aes.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00008 * 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 Bakker3391b122009-07-28 20:11:54 +000028#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0800
Paul Bakkerf3ccc682010-03-18 21:21:02 +000029#define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0810
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 Bakkerff60ee62010-03-16 21:09:09 +000055int aes_setkey_enc( aes_context *ctx, const 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 Bakkerff60ee62010-03-16 21:09:09 +000066int aes_setkey_dec( aes_context *ctx, const 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
Paul Bakkerf3ccc682010-03-18 21:21:02 +000075 *
Paul Bakker27caa8a2010-03-21 15:43:59 +000076 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000077 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000078int aes_crypt_ecb( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000079 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000080 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +000081 unsigned char output[16] );
82
83/**
84 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +000085 * Length should be a multiple of the block
86 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +000087 *
88 * \param ctx AES context
89 * \param mode AES_ENCRYPT or AES_DECRYPT
90 * \param length length of the input data
91 * \param iv initialization vector (updated after use)
92 * \param input buffer holding the input data
93 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +000094 *
95 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000096 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000097int aes_crypt_cbc( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000098 int mode,
99 int length,
100 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000101 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 unsigned char *output );
103
104/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000105 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 *
107 * \param ctx AES context
108 * \param mode AES_ENCRYPT or AES_DECRYPT
109 * \param length length of the input data
110 * \param iv_off offset in IV (updated after use)
111 * \param iv initialization vector (updated after use)
112 * \param input buffer holding the input data
113 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000114 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000115 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000117int aes_crypt_cfb128( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 int mode,
119 int length,
120 int *iv_off,
121 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000122 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 unsigned char *output );
124
125/**
126 * \brief Checkup routine
127 *
128 * \return 0 if successful, or 1 if the test failed
129 */
130int aes_self_test( int verbose );
131
132#ifdef __cplusplus
133}
134#endif
135
136#endif /* aes.h */