blob: 18bb7441efba904b435cc51f57897a0c0a71297e [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. */
Paul Bakkerff61a782011-06-09 15:42:02 +000040#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters to function. */
41#define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
42#define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
43#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
Paul Bakker343a8702011-06-09 14:27:58 +000044
Paul Bakker8123e9d2011-01-06 15:37:30 +000045typedef enum {
46 POLARSSL_CIPHER_ID_NONE = 0,
47 POLARSSL_CIPHER_ID_AES,
48 POLARSSL_CIPHER_ID_DES,
49 POLARSSL_CIPHER_ID_3DES,
50 POLARSSL_CIPHER_ID_CAMELLIA,
51} cipher_id_t;
52
53typedef enum {
54 POLARSSL_CIPHER_NONE = 0,
Paul Bakker8123e9d2011-01-06 15:37:30 +000055 POLARSSL_CIPHER_AES_128_CBC,
56 POLARSSL_CIPHER_AES_192_CBC,
57 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000058 POLARSSL_CIPHER_AES_128_CFB128,
59 POLARSSL_CIPHER_AES_192_CFB128,
60 POLARSSL_CIPHER_AES_256_CFB128,
61 POLARSSL_CIPHER_AES_128_CTR,
62 POLARSSL_CIPHER_AES_192_CTR,
63 POLARSSL_CIPHER_AES_256_CTR,
64 POLARSSL_CIPHER_CAMELLIA_128_CBC,
65 POLARSSL_CIPHER_CAMELLIA_192_CBC,
66 POLARSSL_CIPHER_CAMELLIA_256_CBC,
67 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
68 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
69 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
70 POLARSSL_CIPHER_CAMELLIA_128_CTR,
71 POLARSSL_CIPHER_CAMELLIA_192_CTR,
72 POLARSSL_CIPHER_CAMELLIA_256_CTR,
Paul Bakker8123e9d2011-01-06 15:37:30 +000073 POLARSSL_CIPHER_DES_CBC,
74 POLARSSL_CIPHER_DES_EDE_CBC,
75 POLARSSL_CIPHER_DES_EDE3_CBC
76} cipher_type_t;
77
78typedef enum {
79 POLARSSL_MODE_NONE = 0,
80 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000081 POLARSSL_MODE_CFB128,
Paul Bakker8123e9d2011-01-06 15:37:30 +000082 POLARSSL_MODE_OFB,
Paul Bakker343a8702011-06-09 14:27:58 +000083 POLARSSL_MODE_CTR,
Paul Bakker8123e9d2011-01-06 15:37:30 +000084} cipher_mode_t;
85
86typedef enum {
87 POLARSSL_DECRYPT = 0,
88 POLARSSL_ENCRYPT,
89} operation_t;
90
91enum {
92 /** Undefined key length */
93 POLARSSL_KEY_LENGTH_NONE = 0,
94 /** Key length, in bits, for DES keys */
95 POLARSSL_KEY_LENGTH_DES = 56,
96 /** Key length, in bits, for DES in two key EDE */
97 POLARSSL_KEY_LENGTH_DES_EDE = 112,
98 /** Key length, in bits, for DES in three-key EDE */
99 POLARSSL_KEY_LENGTH_DES_EDE3 = 168,
100 /** Maximum length of any IV, in bytes */
101 POLARSSL_MAX_IV_LENGTH = 16,
102};
103
104/**
Paul Bakker343a8702011-06-09 14:27:58 +0000105 * Base cipher information. The non-mode specific functions and values.
106 */
107typedef struct {
108
109 /** Base Cipher type (e.g. POLARSSL_CIPHER_ID_AES) */
110 cipher_id_t cipher;
111
112 /** Encrypt using CBC */
113 int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
114 const unsigned char *input, unsigned char *output );
115
116 /** Encrypt using CFB128 */
117 int (*cfb128_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
118 unsigned char *iv, const unsigned char *input, unsigned char *output );
119
120 /** Encrypt using CTR */
121 int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
122 unsigned char *stream_block, const unsigned char *input, unsigned char *output );
123
124 /** Set key for encryption purposes */
125 int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
126
127 /** Set key for decryption purposes */
128 int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
129
130 /** Allocate a new context */
131 void * (*ctx_alloc_func)( void );
132
133 /** Free the given context */
134 void (*ctx_free_func)( void *ctx );
135
136} cipher_base_t;
137
138/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000139 * Cipher information. Allows cipher functions to be called in a generic way.
140 */
141typedef struct {
142 /** Full cipher identifier (e.g. POLARSSL_CIPHER_AES_256_CBC) */
143 cipher_type_t type;
144
Paul Bakker8123e9d2011-01-06 15:37:30 +0000145 /** Cipher mode (e.g. POLARSSL_CIPHER_MODE_CBC) */
146 cipher_mode_t mode;
147
148 /** Cipher key length, in bits (default length for variable sized ciphers) */
Paul Bakker23986e52011-04-24 08:57:21 +0000149 unsigned int key_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000150
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000151 /** Name of the cipher */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152 const char * name;
153
154 /** IV size, in bytes */
Paul Bakker23986e52011-04-24 08:57:21 +0000155 unsigned int iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156
157 /** block size, in bytes */
Paul Bakker23986e52011-04-24 08:57:21 +0000158 unsigned int block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000159
Paul Bakker343a8702011-06-09 14:27:58 +0000160 /** Base cipher information and functions */
161 const cipher_base_t *base;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000162
163} cipher_info_t;
164
165/**
166 * Generic message digest context.
167 */
168typedef struct {
169 /** Information about the associated cipher */
170 const cipher_info_t *cipher_info;
171
172 /** Key length to use */
173 int key_length;
174
175 /** Operation that the context's key has been initialised for */
176 operation_t operation;
177
178 /** Buffer for data that hasn't been encrypted yet */
179 unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH];
180
181 /** Number of bytes that still need processing */
Paul Bakker23986e52011-04-24 08:57:21 +0000182 size_t unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183
Paul Bakker343a8702011-06-09 14:27:58 +0000184 /** Current IV or NONCE_COUNTER for CTR-mode */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000185 unsigned char iv[POLARSSL_MAX_IV_LENGTH];
186
187 /** Cipher-specific context */
188 void *cipher_ctx;
189} cipher_context_t;
190
191#ifdef __cplusplus
192extern "C" {
193#endif
194
195/**
Paul Bakker72f62662011-01-16 21:27:44 +0000196 * \brief Returns the list of ciphers supported by the generic cipher module.
197 *
198 * \return a statically allocated array of ciphers, the last entry
199 * is 0.
200 */
201const int *cipher_list( void );
202
203/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204 * \brief Returns the cipher information structure associated
205 * with the given cipher name.
206 *
Paul Bakker23986e52011-04-24 08:57:21 +0000207 * \param cipher_name Name of the cipher to search for.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208 *
209 * \return the cipher information structure associated with the
210 * given cipher_name, or NULL if not found.
211 */
212const cipher_info_t *cipher_info_from_string( const char *cipher_name );
213
214/**
215 * \brief Returns the cipher information structure associated
216 * with the given cipher type.
217 *
218 * \param cipher_type Type of the cipher to search for.
219 *
220 * \return the cipher information structure associated with the
221 * given cipher_type, or NULL if not found.
222 */
223const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
224
225/**
226 * \brief Initialises and fills the cipher context structure with
227 * the appropriate values.
228 *
229 * \param ctx context to initialise. May not be NULL.
230 * \param cipher_info cipher to use.
231 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000232 * \return \c 0 on success,
233 * \c POLARSSL_ERR_CIPHER_BAD_INPUT_DATA on parameter failure,
234 * \c POLARSSL_ERR_CIPHER_ALLOC_FAILED if allocation of the
235 * cipher-specific context failed.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000236 */
237int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
238
239/**
240 * \brief Free the cipher-specific context of ctx. Freeing ctx
241 * itself remains the responsibility of the caller.
242 *
243 * \param ctx Free the cipher-specific context
244 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000245 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
246 * parameter verification fails.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247 */
248int cipher_free_ctx( cipher_context_t *ctx );
249
250/**
251 * \brief Returns the block size of the given cipher.
252 *
253 * \param ctx cipher's context. Must have been initialised.
254 *
255 * \return size of the cipher's blocks, or 0 if ctx has not been
256 * initialised.
257 */
Paul Bakker23986e52011-04-24 08:57:21 +0000258static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259{
260 if( NULL == ctx || NULL == ctx->cipher_info )
261 return 0;
262
263 return ctx->cipher_info->block_size;
264}
265
266/**
267 * \brief Returns the size of the cipher's IV.
268 *
269 * \param ctx cipher's context. Must have been initialised.
270 *
271 * \return size of the cipher's IV, or 0 if ctx has not been
272 * initialised.
273 */
274static inline int cipher_get_iv_size( const cipher_context_t *ctx )
275{
276 if( NULL == ctx || NULL == ctx->cipher_info )
277 return 0;
278
279 return ctx->cipher_info->iv_size;
280}
281
282/**
283 * \brief Returns the type of the given cipher.
284 *
285 * \param ctx cipher's context. Must have been initialised.
286 *
287 * \return type of the cipher, or POLARSSL_CIPHER_NONE if ctx has
288 * not been initialised.
289 */
290static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
291{
292 if( NULL == ctx || NULL == ctx->cipher_info )
293 return 0;
294
295 return ctx->cipher_info->type;
296}
297
298/**
299 * \brief Returns the name of the given cipher, as a string.
300 *
301 * \param ctx cipher's context. Must have been initialised.
302 *
303 * \return name of the cipher, or NULL if ctx was not initialised.
304 */
305static inline const char *cipher_get_name( const cipher_context_t *ctx )
306{
307 if( NULL == ctx || NULL == ctx->cipher_info )
308 return 0;
309
310 return ctx->cipher_info->name;
311}
312
313/**
314 * \brief Returns the key length of the cipher.
315 *
316 * \param ctx cipher's context. Must have been initialised.
317 *
318 * \return cipher's key length, in bits, or
319 * POLARSSL_KEY_LENGTH_NONE if ctx has not been
320 * initialised.
321 */
322static inline int cipher_get_key_size ( const cipher_context_t *ctx )
323{
324 if( NULL == ctx )
325 return POLARSSL_KEY_LENGTH_NONE;
326
327 return ctx->key_length;
328}
329
330/**
331 * \brief Set the key to use with the given context.
332 *
333 * \param ctx generic cipher context. May not be NULL. Must have been
334 * initialised using cipher_context_from_type or
Paul Bakker1f14d082011-01-20 16:33:24 +0000335 * cipher_context_from_string.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000336 * \param key The key to use.
337 * \param key_length key length to use, in bits.
338 * \param operation Operation that the key will be used for, either
339 * POLARSSL_ENCRYPT or POLARSSL_DECRYPT.
340 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000341 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
342 * parameter verification fails or a cipher specific
343 * error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000345int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000346 const operation_t operation );
347
348/**
349 * \brief Reset the given context, setting the IV to iv
350 *
351 * \param ctx generic cipher context
Paul Bakker343a8702011-06-09 14:27:58 +0000352 * \param iv IV to use or NONCE_COUNTER in the case of a CTR-mode cipher
Paul Bakker8123e9d2011-01-06 15:37:30 +0000353 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000354 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
355 * if parameter verification fails.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356 */
357int cipher_reset( cipher_context_t *ctx, const unsigned char *iv );
358
359/**
360 * \brief Generic cipher update function. Encrypts/decrypts
361 * using the given cipher context. Writes as many block
362 * size'd blocks of data as possible to output. Any data
363 * that cannot be written immediately will either be added
364 * to the next block, or flushed when cipher_final is
365 * called.
366 *
367 * \param ctx generic cipher context
368 * \param input buffer holding the input data
369 * \param ilen length of the input data
370 * \param output buffer for the output data. Should be able to hold at
Paul Bakker1f14d082011-01-20 16:33:24 +0000371 * least ilen + block_size. Cannot be the same buffer as
372 * input!
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373 * \param olen length of the output data, will be filled with the
374 * actual number of bytes written.
375 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000376 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
377 * parameter verification fails,
378 * POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE on an
379 * unsupported mode for a cipher or a cipher specific
380 * error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000381 */
Paul Bakker23986e52011-04-24 08:57:21 +0000382int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
383 unsigned char *output, size_t *olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384
385/**
386 * \brief Generic cipher finalisation function. If data still
387 * needs to be flushed from an incomplete block, data
388 * contained within it will be padded with the size of
389 * the last block, and written to the output buffer.
390 *
391 * \param ctx Generic message digest context
392 * \param output buffer to write data to. Needs block_size data available.
393 * \param olen length of the data written to the output buffer.
394 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000395 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
396 * parameter verification fails,
397 * POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
398 * expected a full block but was not provided one,
399 * POLARSSL_ERR_CIPHER_INVALID_PADDING on invalid padding
400 * while decrypting or a cipher specific error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000401 */
Paul Bakker23986e52011-04-24 08:57:21 +0000402int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000403
404
405/**
406 * \brief Checkup routine
407 *
408 * \return 0 if successful, or 1 if the test failed
409 */
410int cipher_self_test( int verbose );
411
412#ifdef __cplusplus
413}
414#endif
415
416#endif /* POLARSSL_MD_H */