blob: d50cb74b8c5a2261664f14e52b731b4189309d42 [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 Bakker530927b2015-02-13 14:24:10 +01008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakker8123e9d2011-01-06 15:37:30 +00009 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +000010 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27#ifndef POLARSSL_CIPHER_H
28#define POLARSSL_CIPHER_H
29
30#include <string.h>
31
Manuel Pégourié-Gonnard01234052015-10-05 11:40:01 +010032#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
33 !defined(inline) && !defined(__cplusplus)
Paul Bakker569df2c2011-06-21 07:48:07 +000034#define inline __inline
Manuel Pégourié-Gonnardcfd1ba92015-10-05 14:57:01 +010035#endif
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000036
Paul Bakker343a8702011-06-09 14:27:58 +000037#define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
Paul Bakkerff61a782011-06-09 15:42:02 +000038#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters to function. */
39#define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
40#define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
41#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
Paul Bakker343a8702011-06-09 14:27:58 +000042
Paul Bakker8123e9d2011-01-06 15:37:30 +000043typedef enum {
44 POLARSSL_CIPHER_ID_NONE = 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +000045 POLARSSL_CIPHER_ID_NULL,
Paul Bakker8123e9d2011-01-06 15:37:30 +000046 POLARSSL_CIPHER_ID_AES,
47 POLARSSL_CIPHER_ID_DES,
48 POLARSSL_CIPHER_ID_3DES,
49 POLARSSL_CIPHER_ID_CAMELLIA,
Paul Bakker6132d0a2012-07-04 17:10:40 +000050 POLARSSL_CIPHER_ID_BLOWFISH,
Paul Bakker8123e9d2011-01-06 15:37:30 +000051} cipher_id_t;
52
53typedef enum {
54 POLARSSL_CIPHER_NONE = 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +000055 POLARSSL_CIPHER_NULL,
Paul Bakker8123e9d2011-01-06 15:37:30 +000056 POLARSSL_CIPHER_AES_128_CBC,
57 POLARSSL_CIPHER_AES_192_CBC,
58 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000059 POLARSSL_CIPHER_AES_128_CFB128,
60 POLARSSL_CIPHER_AES_192_CFB128,
61 POLARSSL_CIPHER_AES_256_CFB128,
62 POLARSSL_CIPHER_AES_128_CTR,
63 POLARSSL_CIPHER_AES_192_CTR,
64 POLARSSL_CIPHER_AES_256_CTR,
65 POLARSSL_CIPHER_CAMELLIA_128_CBC,
66 POLARSSL_CIPHER_CAMELLIA_192_CBC,
67 POLARSSL_CIPHER_CAMELLIA_256_CBC,
68 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
69 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
70 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
71 POLARSSL_CIPHER_CAMELLIA_128_CTR,
72 POLARSSL_CIPHER_CAMELLIA_192_CTR,
73 POLARSSL_CIPHER_CAMELLIA_256_CTR,
Paul Bakker8123e9d2011-01-06 15:37:30 +000074 POLARSSL_CIPHER_DES_CBC,
75 POLARSSL_CIPHER_DES_EDE_CBC,
Paul Bakker6132d0a2012-07-04 17:10:40 +000076 POLARSSL_CIPHER_DES_EDE3_CBC,
77 POLARSSL_CIPHER_BLOWFISH_CBC,
78 POLARSSL_CIPHER_BLOWFISH_CFB64,
79 POLARSSL_CIPHER_BLOWFISH_CTR,
Paul Bakker8123e9d2011-01-06 15:37:30 +000080} cipher_type_t;
81
82typedef enum {
83 POLARSSL_MODE_NONE = 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +000084 POLARSSL_MODE_NULL,
Paul Bakker8123e9d2011-01-06 15:37:30 +000085 POLARSSL_MODE_CBC,
Paul Bakker6132d0a2012-07-04 17:10:40 +000086 POLARSSL_MODE_CFB,
Paul Bakker8123e9d2011-01-06 15:37:30 +000087 POLARSSL_MODE_OFB,
Paul Bakker343a8702011-06-09 14:27:58 +000088 POLARSSL_MODE_CTR,
Paul Bakker8123e9d2011-01-06 15:37:30 +000089} cipher_mode_t;
90
91typedef enum {
Paul Bakkerf7e5bb52011-11-11 10:53:37 +000092 POLARSSL_OPERATION_NONE = -1,
Paul Bakker8123e9d2011-01-06 15:37:30 +000093 POLARSSL_DECRYPT = 0,
94 POLARSSL_ENCRYPT,
95} operation_t;
96
97enum {
98 /** Undefined key length */
99 POLARSSL_KEY_LENGTH_NONE = 0,
Paul Bakker5e18aed2011-11-15 15:38:45 +0000100 /** Key length, in bits (including parity), for DES keys */
101 POLARSSL_KEY_LENGTH_DES = 64,
102 /** Key length, in bits (including parity), for DES in two key EDE */
103 POLARSSL_KEY_LENGTH_DES_EDE = 128,
104 /** Key length, in bits (including parity), for DES in three-key EDE */
105 POLARSSL_KEY_LENGTH_DES_EDE3 = 192,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000106 /** Maximum length of any IV, in bytes */
107 POLARSSL_MAX_IV_LENGTH = 16,
108};
109
110/**
Paul Bakker343a8702011-06-09 14:27:58 +0000111 * Base cipher information. The non-mode specific functions and values.
112 */
113typedef struct {
114
115 /** Base Cipher type (e.g. POLARSSL_CIPHER_ID_AES) */
116 cipher_id_t cipher;
117
118 /** Encrypt using CBC */
119 int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
120 const unsigned char *input, unsigned char *output );
121
Paul Bakker6132d0a2012-07-04 17:10:40 +0000122 /** Encrypt using CFB (Full length) */
123 int (*cfb_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off,
Paul Bakker343a8702011-06-09 14:27:58 +0000124 unsigned char *iv, const unsigned char *input, unsigned char *output );
125
126 /** Encrypt using CTR */
127 int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
128 unsigned char *stream_block, const unsigned char *input, unsigned char *output );
129
130 /** Set key for encryption purposes */
131 int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);
132
133 /** Set key for decryption purposes */
134 int (*setkey_dec_func)( void *ctx, const unsigned char *key, unsigned int key_length);
135
136 /** Allocate a new context */
137 void * (*ctx_alloc_func)( void );
138
139 /** Free the given context */
140 void (*ctx_free_func)( void *ctx );
141
142} cipher_base_t;
143
144/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000145 * Cipher information. Allows cipher functions to be called in a generic way.
146 */
147typedef struct {
148 /** Full cipher identifier (e.g. POLARSSL_CIPHER_AES_256_CBC) */
149 cipher_type_t type;
150
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000151 /** Cipher mode (e.g. POLARSSL_MODE_CBC) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152 cipher_mode_t mode;
153
Paul Bakker5e18aed2011-11-15 15:38:45 +0000154 /** Cipher key length, in bits (default length for variable sized ciphers)
155 * (Includes parity bits for ciphers like DES) */
Paul Bakker23986e52011-04-24 08:57:21 +0000156 unsigned int key_length;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000157
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000158 /** Name of the cipher */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000159 const char * name;
160
161 /** IV size, in bytes */
Paul Bakker23986e52011-04-24 08:57:21 +0000162 unsigned int iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000163
164 /** block size, in bytes */
Paul Bakker23986e52011-04-24 08:57:21 +0000165 unsigned int block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000166
Paul Bakker343a8702011-06-09 14:27:58 +0000167 /** Base cipher information and functions */
168 const cipher_base_t *base;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000169
170} cipher_info_t;
171
172/**
Paul Bakker20281562011-11-11 10:34:04 +0000173 * Generic cipher context.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000174 */
175typedef struct {
176 /** Information about the associated cipher */
177 const cipher_info_t *cipher_info;
178
179 /** Key length to use */
180 int key_length;
181
182 /** Operation that the context's key has been initialised for */
183 operation_t operation;
184
185 /** Buffer for data that hasn't been encrypted yet */
186 unsigned char unprocessed_data[POLARSSL_MAX_IV_LENGTH];
187
188 /** Number of bytes that still need processing */
Paul Bakker23986e52011-04-24 08:57:21 +0000189 size_t unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190
Paul Bakker343a8702011-06-09 14:27:58 +0000191 /** Current IV or NONCE_COUNTER for CTR-mode */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192 unsigned char iv[POLARSSL_MAX_IV_LENGTH];
193
194 /** Cipher-specific context */
195 void *cipher_ctx;
196} cipher_context_t;
197
198#ifdef __cplusplus
199extern "C" {
200#endif
201
202/**
Paul Bakker72f62662011-01-16 21:27:44 +0000203 * \brief Returns the list of ciphers supported by the generic cipher module.
204 *
205 * \return a statically allocated array of ciphers, the last entry
206 * is 0.
207 */
208const int *cipher_list( void );
209
210/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000211 * \brief Returns the cipher information structure associated
212 * with the given cipher name.
213 *
Paul Bakker23986e52011-04-24 08:57:21 +0000214 * \param cipher_name Name of the cipher to search for.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000215 *
216 * \return the cipher information structure associated with the
217 * given cipher_name, or NULL if not found.
218 */
219const cipher_info_t *cipher_info_from_string( const char *cipher_name );
220
221/**
222 * \brief Returns the cipher information structure associated
223 * with the given cipher type.
224 *
225 * \param cipher_type Type of the cipher to search for.
226 *
227 * \return the cipher information structure associated with the
228 * given cipher_type, or NULL if not found.
229 */
230const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
231
232/**
233 * \brief Initialises and fills the cipher context structure with
234 * the appropriate values.
235 *
236 * \param ctx context to initialise. May not be NULL.
237 * \param cipher_info cipher to use.
238 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000239 * \return \c 0 on success,
240 * \c POLARSSL_ERR_CIPHER_BAD_INPUT_DATA on parameter failure,
241 * \c POLARSSL_ERR_CIPHER_ALLOC_FAILED if allocation of the
242 * cipher-specific context failed.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243 */
244int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info );
245
246/**
247 * \brief Free the cipher-specific context of ctx. Freeing ctx
248 * itself remains the responsibility of the caller.
249 *
250 * \param ctx Free the cipher-specific context
251 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000252 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
253 * parameter verification fails.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000254 */
255int cipher_free_ctx( cipher_context_t *ctx );
256
257/**
258 * \brief Returns the block size of the given cipher.
259 *
260 * \param ctx cipher's context. Must have been initialised.
261 *
262 * \return size of the cipher's blocks, or 0 if ctx has not been
263 * initialised.
264 */
Paul Bakker23986e52011-04-24 08:57:21 +0000265static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266{
267 if( NULL == ctx || NULL == ctx->cipher_info )
268 return 0;
269
270 return ctx->cipher_info->block_size;
271}
272
273/**
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000274 * \brief Returns the mode of operation for the cipher.
275 * (e.g. POLARSSL_MODE_CBC)
276 *
277 * \param ctx cipher's context. Must have been initialised.
278 *
279 * \return mode of operation, or POLARSSL_MODE_NONE if ctx
280 * has not been initialised.
281 */
282static inline cipher_mode_t cipher_get_cipher_mode( const cipher_context_t *ctx )
283{
284 if( NULL == ctx || NULL == ctx->cipher_info )
285 return POLARSSL_MODE_NONE;
286
287 return ctx->cipher_info->mode;
288}
289
290/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000291 * \brief Returns the size of the cipher's IV.
292 *
293 * \param ctx cipher's context. Must have been initialised.
294 *
295 * \return size of the cipher's IV, or 0 if ctx has not been
296 * initialised.
297 */
298static inline int cipher_get_iv_size( const cipher_context_t *ctx )
299{
300 if( NULL == ctx || NULL == ctx->cipher_info )
301 return 0;
302
303 return ctx->cipher_info->iv_size;
304}
305
306/**
307 * \brief Returns the type of the given cipher.
308 *
309 * \param ctx cipher's context. Must have been initialised.
310 *
311 * \return type of the cipher, or POLARSSL_CIPHER_NONE if ctx has
312 * not been initialised.
313 */
314static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx )
315{
316 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakker894dece2012-08-23 08:34:32 +0000317 return POLARSSL_CIPHER_NONE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000318
319 return ctx->cipher_info->type;
320}
321
322/**
323 * \brief Returns the name of the given cipher, as a string.
324 *
325 * \param ctx cipher's context. Must have been initialised.
326 *
327 * \return name of the cipher, or NULL if ctx was not initialised.
328 */
329static inline const char *cipher_get_name( const cipher_context_t *ctx )
330{
331 if( NULL == ctx || NULL == ctx->cipher_info )
332 return 0;
333
334 return ctx->cipher_info->name;
335}
336
337/**
338 * \brief Returns the key length of the cipher.
339 *
340 * \param ctx cipher's context. Must have been initialised.
341 *
342 * \return cipher's key length, in bits, or
343 * POLARSSL_KEY_LENGTH_NONE if ctx has not been
344 * initialised.
345 */
346static inline int cipher_get_key_size ( const cipher_context_t *ctx )
347{
348 if( NULL == ctx )
349 return POLARSSL_KEY_LENGTH_NONE;
350
351 return ctx->key_length;
352}
353
354/**
Paul Bakkerf7e5bb52011-11-11 10:53:37 +0000355 * \brief Returns the operation of the given cipher.
356 *
357 * \param ctx cipher's context. Must have been initialised.
358 *
359 * \return operation (POLARSSL_ENCRYPT or POLARSSL_DECRYPT),
360 * or POLARSSL_OPERATION_NONE if ctx has not been
361 * initialised.
362 */
363static inline operation_t cipher_get_operation( const cipher_context_t *ctx )
364{
365 if( NULL == ctx || NULL == ctx->cipher_info )
366 return POLARSSL_OPERATION_NONE;
367
368 return ctx->operation;
369}
370
371/**
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372 * \brief Set the key to use with the given context.
373 *
374 * \param ctx generic cipher context. May not be NULL. Must have been
375 * initialised using cipher_context_from_type or
Paul Bakker1f14d082011-01-20 16:33:24 +0000376 * cipher_context_from_string.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000377 * \param key The key to use.
378 * \param key_length key length to use, in bits.
379 * \param operation Operation that the key will be used for, either
380 * POLARSSL_ENCRYPT or POLARSSL_DECRYPT.
381 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000382 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
383 * parameter verification fails or a cipher specific
384 * error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385 */
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000386int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000387 const operation_t operation );
388
389/**
390 * \brief Reset the given context, setting the IV to iv
391 *
392 * \param ctx generic cipher context
Paul Bakker343a8702011-06-09 14:27:58 +0000393 * \param iv IV to use or NONCE_COUNTER in the case of a CTR-mode cipher
Paul Bakker8123e9d2011-01-06 15:37:30 +0000394 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000395 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
396 * if parameter verification fails.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397 */
398int cipher_reset( cipher_context_t *ctx, const unsigned char *iv );
399
400/**
401 * \brief Generic cipher update function. Encrypts/decrypts
402 * using the given cipher context. Writes as many block
403 * size'd blocks of data as possible to output. Any data
404 * that cannot be written immediately will either be added
405 * to the next block, or flushed when cipher_final is
406 * called.
407 *
408 * \param ctx generic cipher context
409 * \param input buffer holding the input data
410 * \param ilen length of the input data
411 * \param output buffer for the output data. Should be able to hold at
Paul Bakker1f14d082011-01-20 16:33:24 +0000412 * least ilen + block_size. Cannot be the same buffer as
413 * input!
Paul Bakker8123e9d2011-01-06 15:37:30 +0000414 * \param olen length of the output data, will be filled with the
415 * actual number of bytes written.
416 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000417 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
418 * parameter verification fails,
419 * POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE on an
420 * unsupported mode for a cipher or a cipher specific
421 * error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000422 */
Paul Bakker23986e52011-04-24 08:57:21 +0000423int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
424 unsigned char *output, size_t *olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000425
426/**
427 * \brief Generic cipher finalisation function. If data still
428 * needs to be flushed from an incomplete block, data
429 * contained within it will be padded with the size of
430 * the last block, and written to the output buffer.
431 *
Paul Bakker20281562011-11-11 10:34:04 +0000432 * \param ctx Generic cipher context
Paul Bakker8123e9d2011-01-06 15:37:30 +0000433 * \param output buffer to write data to. Needs block_size data available.
434 * \param olen length of the data written to the output buffer.
435 *
Paul Bakkerff61a782011-06-09 15:42:02 +0000436 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if
437 * parameter verification fails,
438 * POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
439 * expected a full block but was not provided one,
440 * POLARSSL_ERR_CIPHER_INVALID_PADDING on invalid padding
441 * while decrypting or a cipher specific error code.
Paul Bakker8123e9d2011-01-06 15:37:30 +0000442 */
Paul Bakker23986e52011-04-24 08:57:21 +0000443int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000444
445
446/**
447 * \brief Checkup routine
448 *
449 * \return 0 if successful, or 1 if the test failed
450 */
451int cipher_self_test( int verbose );
452
453#ifdef __cplusplus
454}
455#endif
456
457#endif /* POLARSSL_MD_H */