blob: 16941c8c8d40a47fc49e2ffa8f4f3f7165f82498 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.h
3 *
4 * \brief Generic cipher wrapper.
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
8 * Copyright (C) 2006-2010, Brainspark B.V.
9 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
30#ifndef POLARSSL_CIPHER_H
31#define POLARSSL_CIPHER_H
32
33#include <string.h>
34
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000035#ifdef _MSC_VER
36#define inline _inline
37#endif
38
Paul Bakker343a8702011-06-09 14:27:58 +000039#define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
40
Paul Bakker8123e9d2011-01-06 15:37:30 +000041typedef enum {
42 POLARSSL_CIPHER_ID_NONE = 0,
43 POLARSSL_CIPHER_ID_AES,
44 POLARSSL_CIPHER_ID_DES,
45 POLARSSL_CIPHER_ID_3DES,
46 POLARSSL_CIPHER_ID_CAMELLIA,
47} cipher_id_t;
48
49typedef enum {
50 POLARSSL_CIPHER_NONE = 0,
Paul Bakker8123e9d2011-01-06 15:37:30 +000051 POLARSSL_CIPHER_AES_128_CBC,
52 POLARSSL_CIPHER_AES_192_CBC,
53 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000054 POLARSSL_CIPHER_AES_128_CFB128,
55 POLARSSL_CIPHER_AES_192_CFB128,
56 POLARSSL_CIPHER_AES_256_CFB128,
57 POLARSSL_CIPHER_AES_128_CTR,
58 POLARSSL_CIPHER_AES_192_CTR,
59 POLARSSL_CIPHER_AES_256_CTR,
60 POLARSSL_CIPHER_CAMELLIA_128_CBC,
61 POLARSSL_CIPHER_CAMELLIA_192_CBC,
62 POLARSSL_CIPHER_CAMELLIA_256_CBC,
63 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
64 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
65 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
66 POLARSSL_CIPHER_CAMELLIA_128_CTR,
67 POLARSSL_CIPHER_CAMELLIA_192_CTR,
68 POLARSSL_CIPHER_CAMELLIA_256_CTR,
Paul Bakker8123e9d2011-01-06 15:37:30 +000069 POLARSSL_CIPHER_DES_CBC,
70 POLARSSL_CIPHER_DES_EDE_CBC,
71 POLARSSL_CIPHER_DES_EDE3_CBC
72} cipher_type_t;
73
74typedef enum {
75 POLARSSL_MODE_NONE = 0,
76 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000077 POLARSSL_MODE_CFB128,
Paul Bakker8123e9d2011-01-06 15:37:30 +000078 POLARSSL_MODE_OFB,
Paul Bakker343a8702011-06-09 14:27:58 +000079 POLARSSL_MODE_CTR,
Paul Bakker8123e9d2011-01-06 15:37:30 +000080} cipher_mode_t;
81
82typedef enum {
83 POLARSSL_DECRYPT = 0,
84 POLARSSL_ENCRYPT,
85} operation_t;
86
87enum {
88 /** Undefined key length */
89 POLARSSL_KEY_LENGTH_NONE = 0,
90 /** Key length, in bits, for DES keys */
91 POLARSSL_KEY_LENGTH_DES = 56,
92 /** Key length, in bits, for DES in two key EDE */
93 POLARSSL_KEY_LENGTH_DES_EDE = 112,
94 /** Key length, in bits, for DES in three-key EDE */
95 POLARSSL_KEY_LENGTH_DES_EDE3 = 168,
96 /** Maximum length of any IV, in bytes */
97 POLARSSL_MAX_IV_LENGTH = 16,
98};
99
100/**
Paul Bakker343a8702011-06-09 14:27:58 +0000101 * Base cipher information. The non-mode specific functions and values.
102 */
103typedef struct {
104
105 /** Base Cipher type (e.g. POLARSSL_CIPHER_ID_AES) */
106 cipher_id_t cipher;
107
108 /** Encrypt using CBC */
109 int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
110 const unsigned char *input, unsigned char *output );
111
112 /** Encrypt using CFB128 */
113 int (*cfb128_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
114 unsigned char *iv, const unsigned char *input, unsigned char *output );
115
116 /** Encrypt using CTR */
117 int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
118 unsigned char *stream_block, const unsigned char *input, unsigned char *output );
119
120 /** Set key for encryption purposes */
121 int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
122
123 /** Set key for decryption purposes */
124 int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
125
126 /** Allocate a new context */
127 void * (*ctx_alloc_func)( void );
128
129 /** Free the given context */
130 void (*ctx_free_func)( void *ctx );
131
132} cipher_base_t;
133
134/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000135 * Cipher information. Allows cipher functions to be called in a generic way.
136 */
137typedef struct {
138 /** Full cipher identifier (e.g. POLARSSL_CIPHER_AES_256_CBC) */
139 cipher_type_t type;
140
Paul Bakker8123e9d2011-01-06 15:37:30 +0000141 /** Cipher mode (e.g. POLARSSL_CIPHER_MODE_CBC) */
142 cipher_mode_t mode;
143
144 /** Cipher key length, in bits (default length for variable sized ciphers) */
Paul Bakker23986e52011-04-24 08:57:21 +0000145 unsigned int key_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000147 /** Name of the cipher */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000148 const char * name;
149
150 /** IV size, in bytes */
Paul Bakker23986e52011-04-24 08:57:21 +0000151 unsigned int iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152
153 /** block size, in bytes */
Paul Bakker23986e52011-04-24 08:57:21 +0000154 unsigned int block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000155
Paul Bakker343a8702011-06-09 14:27:58 +0000156 /** Base cipher information and functions */
157 const cipher_base_t *base;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000158
159} cipher_info_t;
160
161/**
162 * Generic message digest context.
163 */
164typedef struct {
165 /** Information about the associated cipher */
166 const cipher_info_t *cipher_info;
167
168 /** Key length to use */
169 int key_length;
170
171 /** Operation that the context's key has been initialised for */
172 operation_t operation;
173
174 /** Buffer for data that hasn't been encrypted yet */
175 unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH];
176
177 /** Number of bytes that still need processing */
Paul Bakker23986e52011-04-24 08:57:21 +0000178 size_t unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000179
Paul Bakker343a8702011-06-09 14:27:58 +0000180 /** Current IV or NONCE_COUNTER for CTR-mode */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000181 unsigned char iv[POLARSSL_MAX_IV_LENGTH];
182
183 /** Cipher-specific context */
184 void *cipher_ctx;
185} cipher_context_t;
186
187#ifdef __cplusplus
188extern "C" {
189#endif
190
191/**
Paul Bakker72f62662011-01-16 21:27:44 +0000192 * \brief Returns the list of ciphers supported by the generic cipher module.
193 *
194 * \return a statically allocated array of ciphers, the last entry
195 * is 0.
196 */
197const int *cipher_list( void );
198
199/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200 * \brief Returns the cipher information structure associated
201 * with the given cipher name.
202 *
Paul Bakker23986e52011-04-24 08:57:21 +0000203 * \param cipher_name Name of the cipher to search for.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204 *
205 * \return the cipher information structure associated with the
206 * given cipher_name, or NULL if not found.
207 */
208const cipher_info_t *cipher_info_from_string( const char *cipher_name );
209
210/**
211 * \brief Returns the cipher information structure associated
212 * with the given cipher type.
213 *
214 * \param cipher_type Type of the cipher to search for.
215 *
216 * \return the cipher information structure associated with the
217 * given cipher_type, or NULL if not found.
218 */
219const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
220
221/**
222 * \brief Initialises and fills the cipher context structure with
223 * the appropriate values.
224 *
225 * \param ctx context to initialise. May not be NULL.
226 * \param cipher_info cipher to use.
227 *
228 * \return \c 0 on success, \c 1 on parameter failure, \c 2 if
229 * allocation of the cipher-specific context failed.
230 */
231int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
232
233/**
234 * \brief Free the cipher-specific context of ctx. Freeing ctx
235 * itself remains the responsibility of the caller.
236 *
237 * \param ctx Free the cipher-specific context
238 *
239 * \returns 0 on success, 1 if parameter verification fails.
240 */
241int cipher_free_ctx( cipher_context_t *ctx );
242
243/**
244 * \brief Returns the block size of the given cipher.
245 *
246 * \param ctx cipher's context. Must have been initialised.
247 *
248 * \return size of the cipher's blocks, or 0 if ctx has not been
249 * initialised.
250 */
Paul Bakker23986e52011-04-24 08:57:21 +0000251static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252{
253 if( NULL == ctx || NULL == ctx->cipher_info )
254 return 0;
255
256 return ctx->cipher_info->block_size;
257}
258
259/**
260 * \brief Returns the size of the cipher's IV.
261 *
262 * \param ctx cipher's context. Must have been initialised.
263 *
264 * \return size of the cipher's IV, or 0 if ctx has not been
265 * initialised.
266 */
267static inline int cipher_get_iv_size( const cipher_context_t *ctx )
268{
269 if( NULL == ctx || NULL == ctx->cipher_info )
270 return 0;
271
272 return ctx->cipher_info->iv_size;
273}
274
275/**
276 * \brief Returns the type of the given cipher.
277 *
278 * \param ctx cipher's context. Must have been initialised.
279 *
280 * \return type of the cipher, or POLARSSL_CIPHER_NONE if ctx has
281 * not been initialised.
282 */
283static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
284{
285 if( NULL == ctx || NULL == ctx->cipher_info )
286 return 0;
287
288 return ctx->cipher_info->type;
289}
290
291/**
292 * \brief Returns the name of the given cipher, as a string.
293 *
294 * \param ctx cipher's context. Must have been initialised.
295 *
296 * \return name of the cipher, or NULL if ctx was not initialised.
297 */
298static inline const char *cipher_get_name( const cipher_context_t *ctx )
299{
300 if( NULL == ctx || NULL == ctx->cipher_info )
301 return 0;
302
303 return ctx->cipher_info->name;
304}
305
306/**
307 * \brief Returns the key length of the cipher.
308 *
309 * \param ctx cipher's context. Must have been initialised.
310 *
311 * \return cipher's key length, in bits, or
312 * POLARSSL_KEY_LENGTH_NONE if ctx has not been
313 * initialised.
314 */
315static inline int cipher_get_key_size ( const cipher_context_t *ctx )
316{
317 if( NULL == ctx )
318 return POLARSSL_KEY_LENGTH_NONE;
319
320 return ctx->key_length;
321}
322
323/**
324 * \brief Set the key to use with the given context.
325 *
326 * \param ctx generic cipher context. May not be NULL. Must have been
327 * initialised using cipher_context_from_type or
Paul Bakker1f14d082011-01-20 16:33:24 +0000328 * cipher_context_from_string.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000329 * \param key The key to use.
330 * \param key_length key length to use, in bits.
331 * \param operation Operation that the key will be used for, either
332 * POLARSSL_ENCRYPT or POLARSSL_DECRYPT.
333 *
334 * \returns 0 on success, 1 if parameter verification fails.
335 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000336int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000337 const operation_t operation );
338
339/**
340 * \brief Reset the given context, setting the IV to iv
341 *
342 * \param ctx generic cipher context
Paul Bakker343a8702011-06-09 14:27:58 +0000343 * \param iv IV to use or NONCE_COUNTER in the case of a CTR-mode cipher
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344 *
345 * \returns 0 on success, 1 if parameter verification fails.
346 */
347int cipher_reset( cipher_context_t *ctx, const unsigned char *iv );
348
349/**
350 * \brief Generic cipher update function. Encrypts/decrypts
351 * using the given cipher context. Writes as many block
352 * size'd blocks of data as possible to output. Any data
353 * that cannot be written immediately will either be added
354 * to the next block, or flushed when cipher_final is
355 * called.
356 *
357 * \param ctx generic cipher context
358 * \param input buffer holding the input data
359 * \param ilen length of the input data
360 * \param output buffer for the output data. Should be able to hold at
Paul Bakker1f14d082011-01-20 16:33:24 +0000361 * least ilen + block_size. Cannot be the same buffer as
362 * input!
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363 * \param olen length of the output data, will be filled with the
364 * actual number of bytes written.
365 *
366 * \returns 0 on success, 1 if parameter verification fails.
367 */
Paul Bakker23986e52011-04-24 08:57:21 +0000368int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
369 unsigned char *output, size_t *olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000370
371/**
372 * \brief Generic cipher finalisation function. If data still
373 * needs to be flushed from an incomplete block, data
374 * contained within it will be padded with the size of
375 * the last block, and written to the output buffer.
376 *
377 * \param ctx Generic message digest context
378 * \param output buffer to write data to. Needs block_size data available.
379 * \param olen length of the data written to the output buffer.
380 *
381 * \returns 0 on success, 1 if parameter verification fails.
382 */
Paul Bakker23986e52011-04-24 08:57:21 +0000383int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384
385
386/**
387 * \brief Checkup routine
388 *
389 * \return 0 if successful, or 1 if the test failed
390 */
391int cipher_self_test( int verbose );
392
393#ifdef __cplusplus
394}
395#endif
396
397#endif /* POLARSSL_MD_H */