blob: 9605066fd9b95794151f34cdf813a2b66770b790 [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 *
Paul Bakkerfab5c822012-02-06 16:45:10 +00008 * Copyright (C) 2006-2012, Brainspark B.V.
Paul Bakker8123e9d2011-01-06 15:37:30 +00009 *
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 Bakker09b1ec62011-07-27 16:28:54 +000035#if defined(_MSC_VER) && !defined(inline)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000036#define inline _inline
Paul Bakker569df2c2011-06-21 07:48:07 +000037#else
Paul Bakker09b1ec62011-07-27 16:28:54 +000038#if defined(__ARMCC_VERSION) && !defined(inline)
Paul Bakker569df2c2011-06-21 07:48:07 +000039#define inline __inline
Paul Bakker74fb74e2011-06-21 13:36:18 +000040#endif /* __ARMCC_VERSION */
Paul Bakker569df2c2011-06-21 07:48:07 +000041#endif /*_MSC_VER */
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000042
Paul Bakker343a8702011-06-09 14:27:58 +000043#define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
Paul Bakkerff61a782011-06-09 15:42:02 +000044#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters to function. */
45#define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
46#define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
47#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
Paul Bakker343a8702011-06-09 14:27:58 +000048
Paul Bakker8123e9d2011-01-06 15:37:30 +000049typedef enum {
50 POLARSSL_CIPHER_ID_NONE = 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +000051 POLARSSL_CIPHER_ID_NULL,
Paul Bakker8123e9d2011-01-06 15:37:30 +000052 POLARSSL_CIPHER_ID_AES,
53 POLARSSL_CIPHER_ID_DES,
54 POLARSSL_CIPHER_ID_3DES,
55 POLARSSL_CIPHER_ID_CAMELLIA,
56} cipher_id_t;
57
58typedef enum {
59 POLARSSL_CIPHER_NONE = 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +000060 POLARSSL_CIPHER_NULL,
Paul Bakker8123e9d2011-01-06 15:37:30 +000061 POLARSSL_CIPHER_AES_128_CBC,
62 POLARSSL_CIPHER_AES_192_CBC,
63 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000064 POLARSSL_CIPHER_AES_128_CFB128,
65 POLARSSL_CIPHER_AES_192_CFB128,
66 POLARSSL_CIPHER_AES_256_CFB128,
67 POLARSSL_CIPHER_AES_128_CTR,
68 POLARSSL_CIPHER_AES_192_CTR,
69 POLARSSL_CIPHER_AES_256_CTR,
70 POLARSSL_CIPHER_CAMELLIA_128_CBC,
71 POLARSSL_CIPHER_CAMELLIA_192_CBC,
72 POLARSSL_CIPHER_CAMELLIA_256_CBC,
73 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
74 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
75 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
76 POLARSSL_CIPHER_CAMELLIA_128_CTR,
77 POLARSSL_CIPHER_CAMELLIA_192_CTR,
78 POLARSSL_CIPHER_CAMELLIA_256_CTR,
Paul Bakker8123e9d2011-01-06 15:37:30 +000079 POLARSSL_CIPHER_DES_CBC,
80 POLARSSL_CIPHER_DES_EDE_CBC,
81 POLARSSL_CIPHER_DES_EDE3_CBC
82} cipher_type_t;
83
84typedef enum {
85 POLARSSL_MODE_NONE = 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +000086 POLARSSL_MODE_NULL,
Paul Bakker8123e9d2011-01-06 15:37:30 +000087 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000088 POLARSSL_MODE_CFB128,
Paul Bakker8123e9d2011-01-06 15:37:30 +000089 POLARSSL_MODE_OFB,
Paul Bakker343a8702011-06-09 14:27:58 +000090 POLARSSL_MODE_CTR,
Paul Bakker8123e9d2011-01-06 15:37:30 +000091} cipher_mode_t;
92
93typedef enum {
Paul Bakkerf7e5bb52011-11-11 10:53:37 +000094 POLARSSL_OPERATION_NONE = -1,
Paul Bakker8123e9d2011-01-06 15:37:30 +000095 POLARSSL_DECRYPT = 0,
96 POLARSSL_ENCRYPT,
97} operation_t;
98
99enum {
100 /** Undefined key length */
101 POLARSSL_KEY_LENGTH_NONE = 0,
Paul Bakker5e18aed2011-11-15 15:38:45 +0000102 /** Key length, in bits (including parity), for DES keys */
103 POLARSSL_KEY_LENGTH_DES = 64,
104 /** Key length, in bits (including parity), for DES in two key EDE */
105 POLARSSL_KEY_LENGTH_DES_EDE = 128,
106 /** Key length, in bits (including parity), for DES in three-key EDE */
107 POLARSSL_KEY_LENGTH_DES_EDE3 = 192,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000108 /** Maximum length of any IV, in bytes */
109 POLARSSL_MAX_IV_LENGTH = 16,
110};
111
112/**
Paul Bakker343a8702011-06-09 14:27:58 +0000113 * Base cipher information. The non-mode specific functions and values.
114 */
115typedef struct {
116
117 /** Base Cipher type (e.g. POLARSSL_CIPHER_ID_AES) */
118 cipher_id_t cipher;
119
120 /** Encrypt using CBC */
121 int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
122 const unsigned char *input, unsigned char *output );
123
124 /** Encrypt using CFB128 */
125 int (*cfb128_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
126 unsigned char *iv, const unsigned char *input, unsigned char *output );
127
128 /** Encrypt using CTR */
129 int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
130 unsigned char *stream_block, const unsigned char *input, unsigned char *output );
131
132 /** Set key for encryption purposes */
133 int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
134
135 /** Set key for decryption purposes */
136 int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
137
138 /** Allocate a new context */
139 void * (*ctx_alloc_func)( void );
140
141 /** Free the given context */
142 void (*ctx_free_func)( void *ctx );
143
144} cipher_base_t;
145
146/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000147 * Cipher information. Allows cipher functions to be called in a generic way.
148 */
149typedef struct {
150 /** Full cipher identifier (e.g. POLARSSL_CIPHER_AES_256_CBC) */
151 cipher_type_t type;
152
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000153 /** Cipher mode (e.g. POLARSSL_MODE_CBC) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000154 cipher_mode_t mode;
155
Paul Bakker5e18aed2011-11-15 15:38:45 +0000156 /** Cipher key length, in bits (default length for variable sized ciphers)
157 * (Includes parity bits for ciphers like DES) */
Paul Bakker23986e52011-04-24 08:57:21 +0000158 unsigned int key_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000159
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000160 /** Name of the cipher */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161 const char * name;
162
163 /** IV size, in bytes */
Paul Bakker23986e52011-04-24 08:57:21 +0000164 unsigned int iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000165
166 /** block size, in bytes */
Paul Bakker23986e52011-04-24 08:57:21 +0000167 unsigned int block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000168
Paul Bakker343a8702011-06-09 14:27:58 +0000169 /** Base cipher information and functions */
170 const cipher_base_t *base;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000171
172} cipher_info_t;
173
174/**
Paul Bakker20281562011-11-11 10:34:04 +0000175 * Generic cipher context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176 */
177typedef struct {
178 /** Information about the associated cipher */
179 const cipher_info_t *cipher_info;
180
181 /** Key length to use */
182 int key_length;
183
184 /** Operation that the context's key has been initialised for */
185 operation_t operation;
186
187 /** Buffer for data that hasn't been encrypted yet */
188 unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH];
189
190 /** Number of bytes that still need processing */
Paul Bakker23986e52011-04-24 08:57:21 +0000191 size_t unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192
Paul Bakker343a8702011-06-09 14:27:58 +0000193 /** Current IV or NONCE_COUNTER for CTR-mode */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194 unsigned char iv[POLARSSL_MAX_IV_LENGTH];
195
196 /** Cipher-specific context */
197 void *cipher_ctx;
198} cipher_context_t;
199
200#ifdef __cplusplus
201extern "C" {
202#endif
203
204/**
Paul Bakker72f62662011-01-16 21:27:44 +0000205 * \brief Returns the list of ciphers supported by the generic cipher module.
206 *
207 * \return a statically allocated array of ciphers, the last entry
208 * is 0.
209 */
210const int *cipher_list( void );
211
212/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213 * \brief Returns the cipher information structure associated
214 * with the given cipher name.
215 *
Paul Bakker23986e52011-04-24 08:57:21 +0000216 * \param cipher_name Name of the cipher to search for.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217 *
218 * \return the cipher information structure associated with the
219 * given cipher_name, or NULL if not found.
220 */
221const cipher_info_t *cipher_info_from_string( const char *cipher_name );
222
223/**
224 * \brief Returns the cipher information structure associated
225 * with the given cipher type.
226 *
227 * \param cipher_type Type of the cipher to search for.
228 *
229 * \return the cipher information structure associated with the
230 * given cipher_type, or NULL if not found.
231 */
232const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
233
234/**
235 * \brief Initialises and fills the cipher context structure with
236 * the appropriate values.
237 *
238 * \param ctx context to initialise. May not be NULL.
239 * \param cipher_info cipher to use.
240 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000241 * \return \c 0 on success,
242 * \c POLARSSL_ERR_CIPHER_BAD_INPUT_DATA on parameter failure,
243 * \c POLARSSL_ERR_CIPHER_ALLOC_FAILED if allocation of the
244 * cipher-specific context failed.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245 */
246int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
247
248/**
249 * \brief Free the cipher-specific context of ctx. Freeing ctx
250 * itself remains the responsibility of the caller.
251 *
252 * \param ctx Free the cipher-specific context
253 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000254 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
255 * parameter verification fails.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256 */
257int cipher_free_ctx( cipher_context_t *ctx );
258
259/**
260 * \brief Returns the block size of the given cipher.
261 *
262 * \param ctx cipher's context. Must have been initialised.
263 *
264 * \return size of the cipher's blocks, or 0 if ctx has not been
265 * initialised.
266 */
Paul Bakker23986e52011-04-24 08:57:21 +0000267static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000268{
269 if( NULL == ctx || NULL == ctx->cipher_info )
270 return 0;
271
272 return ctx->cipher_info->block_size;
273}
274
275/**
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000276 * \brief Returns the mode of operation for the cipher.
277 * (e.g. POLARSSL_MODE_CBC)
278 *
279 * \param ctx cipher's context. Must have been initialised.
280 *
281 * \return mode of operation, or POLARSSL_MODE_NONE if ctx
282 * has not been initialised.
283 */
284static inline cipher_mode_t cipher_get_cipher_mode( const cipher_context_t *ctx )
285{
286 if( NULL == ctx || NULL == ctx->cipher_info )
287 return POLARSSL_MODE_NONE;
288
289 return ctx->cipher_info->mode;
290}
291
292/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000293 * \brief Returns the size of the cipher's IV.
294 *
295 * \param ctx cipher's context. Must have been initialised.
296 *
297 * \return size of the cipher's IV, or 0 if ctx has not been
298 * initialised.
299 */
300static inline int cipher_get_iv_size( const cipher_context_t *ctx )
301{
302 if( NULL == ctx || NULL == ctx->cipher_info )
303 return 0;
304
305 return ctx->cipher_info->iv_size;
306}
307
308/**
309 * \brief Returns the type of the given cipher.
310 *
311 * \param ctx cipher's context. Must have been initialised.
312 *
313 * \return type of the cipher, or POLARSSL_CIPHER_NONE if ctx has
314 * not been initialised.
315 */
316static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
317{
318 if( NULL == ctx || NULL == ctx->cipher_info )
319 return 0;
320
321 return ctx->cipher_info->type;
322}
323
324/**
325 * \brief Returns the name of the given cipher, as a string.
326 *
327 * \param ctx cipher's context. Must have been initialised.
328 *
329 * \return name of the cipher, or NULL if ctx was not initialised.
330 */
331static inline const char *cipher_get_name( const cipher_context_t *ctx )
332{
333 if( NULL == ctx || NULL == ctx->cipher_info )
334 return 0;
335
336 return ctx->cipher_info->name;
337}
338
339/**
340 * \brief Returns the key length of the cipher.
341 *
342 * \param ctx cipher's context. Must have been initialised.
343 *
344 * \return cipher's key length, in bits, or
345 * POLARSSL_KEY_LENGTH_NONE if ctx has not been
346 * initialised.
347 */
348static inline int cipher_get_key_size ( const cipher_context_t *ctx )
349{
350 if( NULL == ctx )
351 return POLARSSL_KEY_LENGTH_NONE;
352
353 return ctx->key_length;
354}
355
356/**
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000357 * \brief Returns the operation of the given cipher.
358 *
359 * \param ctx cipher's context. Must have been initialised.
360 *
361 * \return operation (POLARSSL_ENCRYPT or POLARSSL_DECRYPT),
362 * or POLARSSL_OPERATION_NONE if ctx has not been
363 * initialised.
364 */
365static inline operation_t cipher_get_operation( const cipher_context_t *ctx )
366{
367 if( NULL == ctx || NULL == ctx->cipher_info )
368 return POLARSSL_OPERATION_NONE;
369
370 return ctx->operation;
371}
372
373/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000374 * \brief Set the key to use with the given context.
375 *
376 * \param ctx generic cipher context. May not be NULL. Must have been
377 * initialised using cipher_context_from_type or
Paul Bakker1f14d082011-01-20 16:33:24 +0000378 * cipher_context_from_string.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000379 * \param key The key to use.
380 * \param key_length key length to use, in bits.
381 * \param operation Operation that the key will be used for, either
382 * POLARSSL_ENCRYPT or POLARSSL_DECRYPT.
383 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000384 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
385 * parameter verification fails or a cipher specific
386 * error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000387 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000388int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000389 const operation_t operation );
390
391/**
392 * \brief Reset the given context, setting the IV to iv
393 *
394 * \param ctx generic cipher context
Paul Bakker343a8702011-06-09 14:27:58 +0000395 * \param iv IV to use or NONCE_COUNTER in the case of a CTR-mode cipher
Paul Bakker8123e9d2011-01-06 15:37:30 +0000396 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000397 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
398 * if parameter verification fails.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000399 */
400int cipher_reset( cipher_context_t *ctx, const unsigned char *iv );
401
402/**
403 * \brief Generic cipher update function. Encrypts/decrypts
404 * using the given cipher context. Writes as many block
405 * size'd blocks of data as possible to output. Any data
406 * that cannot be written immediately will either be added
407 * to the next block, or flushed when cipher_final is
408 * called.
409 *
410 * \param ctx generic cipher context
411 * \param input buffer holding the input data
412 * \param ilen length of the input data
413 * \param output buffer for the output data. Should be able to hold at
Paul Bakker1f14d082011-01-20 16:33:24 +0000414 * least ilen + block_size. Cannot be the same buffer as
415 * input!
Paul Bakker8123e9d2011-01-06 15:37:30 +0000416 * \param olen length of the output data, will be filled with the
417 * actual number of bytes written.
418 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000419 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
420 * parameter verification fails,
421 * POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE on an
422 * unsupported mode for a cipher or a cipher specific
423 * error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424 */
Paul Bakker23986e52011-04-24 08:57:21 +0000425int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
426 unsigned char *output, size_t *olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427
428/**
429 * \brief Generic cipher finalisation function. If data still
430 * needs to be flushed from an incomplete block, data
431 * contained within it will be padded with the size of
432 * the last block, and written to the output buffer.
433 *
Paul Bakker20281562011-11-11 10:34:04 +0000434 * \param ctx Generic cipher context
Paul Bakker8123e9d2011-01-06 15:37:30 +0000435 * \param output buffer to write data to. Needs block_size data available.
436 * \param olen length of the data written to the output buffer.
437 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000438 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
439 * parameter verification fails,
440 * POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
441 * expected a full block but was not provided one,
442 * POLARSSL_ERR_CIPHER_INVALID_PADDING on invalid padding
443 * while decrypting or a cipher specific error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000444 */
Paul Bakker23986e52011-04-24 08:57:21 +0000445int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446
447
448/**
449 * \brief Checkup routine
450 *
451 * \return 0 if successful, or 1 if the test failed
452 */
453int cipher_self_test( int verbose );
454
455#ifdef __cplusplus
456}
457#endif
458
459#endif /* POLARSSL_MD_H */