blob: 16721dd032de0b31b2467774c7accb99369b5107 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file aes.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +00004 * \brief AES block cipher
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Paul Bakker90995b52013-06-24 19:20:35 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_AES_H
28#define POLARSSL_AES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker90995b52013-06-24 19:20:35 +020030#include "config.h"
31
Paul Bakker23986e52011-04-24 08:57:21 +000032#include <string.h>
33
Paul Bakker5c2364c2012-10-01 14:41:15 +000034#ifdef _MSC_VER
35#include <basetsd.h>
36typedef UINT32 uint32_t;
37#else
38#include <inttypes.h>
39#endif
40
Paul Bakker5121ce52009-01-03 21:22:43 +000041#define AES_ENCRYPT 1
42#define AES_DECRYPT 0
43
Paul Bakker9d781402011-05-09 16:17:09 +000044#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
45#define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000046
Paul Bakker90995b52013-06-24 19:20:35 +020047#if !defined(POLARSSL_AES_ALT)
48// Regular implementation
49//
50
Paul Bakker407a0da2013-06-27 14:29:21 +020051#ifdef __cplusplus
52extern "C" {
53#endif
54
Paul Bakker5121ce52009-01-03 21:22:43 +000055/**
56 * \brief AES context structure
57 */
58typedef struct
59{
60 int nr; /*!< number of rounds */
Paul Bakker5c2364c2012-10-01 14:41:15 +000061 uint32_t *rk; /*!< AES round keys */
62 uint32_t buf[68]; /*!< unaligned data */
Paul Bakker5121ce52009-01-03 21:22:43 +000063}
64aes_context;
65
Paul Bakker5121ce52009-01-03 21:22:43 +000066/**
67 * \brief AES key schedule (encryption)
68 *
69 * \param ctx AES context to be initialized
70 * \param key encryption key
71 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000072 *
73 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000074 */
Paul Bakker23986e52011-04-24 08:57:21 +000075int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +000076
77/**
78 * \brief AES key schedule (decryption)
79 *
80 * \param ctx AES context to be initialized
81 * \param key decryption key
82 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000083 *
84 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000085 */
Paul Bakker23986e52011-04-24 08:57:21 +000086int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88/**
89 * \brief AES-ECB block encryption/decryption
90 *
91 * \param ctx AES context
92 * \param mode AES_ENCRYPT or AES_DECRYPT
93 * \param input 16-byte input block
94 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +000095 *
Paul Bakker27caa8a2010-03-21 15:43:59 +000096 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000097 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000098int aes_crypt_ecb( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000099 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000100 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 unsigned char output[16] );
102
103/**
104 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000105 * Length should be a multiple of the block
106 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 *
108 * \param ctx AES context
109 * \param mode AES_ENCRYPT or AES_DECRYPT
110 * \param length length of the input data
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 *
115 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000117int aes_crypt_cbc( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000119 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000121 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 unsigned char *output );
123
124/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000125 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000127 * Note: Due to the nature of CFB you should use the same key schedule for
128 * both encryption and decryption. So a context initialized with
129 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
130 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 * \param ctx AES context
132 * \param mode AES_ENCRYPT or AES_DECRYPT
133 * \param length length of the input data
134 * \param iv_off offset in IV (updated after use)
135 * \param iv initialization vector (updated after use)
136 * \param input buffer holding the input data
137 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000138 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000139 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000141int aes_crypt_cfb128( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000143 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000144 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000146 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 unsigned char *output );
148
Paul Bakker9a736322012-11-14 12:39:52 +0000149/**
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000150 * \brief AES-CTR buffer encryption/decryption
151 *
152 * Warning: You have to keep the maximum use of your counter in mind!
153 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000154 * Note: Due to the nature of CTR you should use the same key schedule for
155 * both encryption and decryption. So a context initialized with
156 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
157 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200158 * \param ctx AES context
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000159 * \param length The length of the data
160 * \param nc_off The offset in the current stream_block (for resuming
161 * within current cipher stream). The offset pointer to
162 * should be 0 at the start of a stream.
163 * \param nonce_counter The 128-bit nonce and counter.
164 * \param stream_block The saved stream-block for resuming. Is overwritten
165 * by the function.
166 * \param input The input data stream
167 * \param output The output data stream
168 *
169 * \return 0 if successful
170 */
171int aes_crypt_ctr( aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000172 size_t length,
173 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000174 unsigned char nonce_counter[16],
175 unsigned char stream_block[16],
176 const unsigned char *input,
177 unsigned char *output );
Paul Bakker90995b52013-06-24 19:20:35 +0200178
179#ifdef __cplusplus
180}
181#endif
182
183#else /* POLARSSL_AES_ALT */
184#include "aes_alt.h"
185#endif /* POLARSSL_AES_ALT */
186
187#ifdef __cplusplus
188extern "C" {
189#endif
190
Paul Bakker5121ce52009-01-03 21:22:43 +0000191/**
192 * \brief Checkup routine
193 *
194 * \return 0 if successful, or 1 if the test failed
195 */
196int aes_self_test( int verbose );
197
198#ifdef __cplusplus
199}
200#endif
201
202#endif /* aes.h */