blob: 5576680e71475a81334070ac767bb8865ade43d0 [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.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
Paul Bakker40e46942009-01-03 21:51:57 +000025#ifndef POLARSSL_AES_H
26#define POLARSSL_AES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#define AES_ENCRYPT 1
29#define AES_DECRYPT 0
30
Paul Bakker3391b122009-07-28 20:11:54 +000031#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0800
Paul Bakkerf3ccc682010-03-18 21:21:02 +000032#define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0810
Paul Bakker2b222c82009-07-27 21:03:45 +000033
Paul Bakker5121ce52009-01-03 21:22:43 +000034/**
35 * \brief AES context structure
36 */
37typedef struct
38{
39 int nr; /*!< number of rounds */
40 unsigned long *rk; /*!< AES round keys */
41 unsigned long buf[68]; /*!< unaligned data */
42}
43aes_context;
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49/**
50 * \brief AES key schedule (encryption)
51 *
52 * \param ctx AES context to be initialized
53 * \param key encryption key
54 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000055 *
56 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000057 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000058int aes_setkey_enc( aes_context *ctx, const unsigned char *key, int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60/**
61 * \brief AES key schedule (decryption)
62 *
63 * \param ctx AES context to be initialized
64 * \param key decryption key
65 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000066 *
67 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000068 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000069int aes_setkey_dec( aes_context *ctx, const unsigned char *key, int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +000070
71/**
72 * \brief AES-ECB block encryption/decryption
73 *
74 * \param ctx AES context
75 * \param mode AES_ENCRYPT or AES_DECRYPT
76 * \param input 16-byte input block
77 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +000078 *
Paul Bakker27caa8a2010-03-21 15:43:59 +000079 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000080 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000081int aes_crypt_ecb( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000083 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +000084 unsigned char output[16] );
85
86/**
87 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +000088 * Length should be a multiple of the block
89 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +000090 *
91 * \param ctx AES context
92 * \param mode AES_ENCRYPT or AES_DECRYPT
93 * \param length length of the input data
94 * \param iv initialization vector (updated after use)
95 * \param input buffer holding the input data
96 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +000097 *
98 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000099 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000100int aes_crypt_cbc( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 int mode,
102 int length,
103 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000104 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 unsigned char *output );
106
107/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000108 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 *
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
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000117 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000118 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000120int aes_crypt_cfb128( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 int mode,
122 int length,
123 int *iv_off,
124 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000125 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 unsigned char *output );
127
128/**
129 * \brief Checkup routine
130 *
131 * \return 0 if successful, or 1 if the test failed
132 */
133int aes_self_test( int verbose );
134
135#ifdef __cplusplus
136}
137#endif
138
139#endif /* aes.h */