Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
cipher.h
00001 /** 00002 * \file cipher.h 00003 * 00004 * \brief Generic cipher wrapper. 00005 * 00006 * \author Adriaan de Jong <dejong@fox-it.com> 00007 * 00008 * Copyright (C) 2006-2014, Brainspark B.V. 00009 * 00010 * This file is part of PolarSSL (http://www.polarssl.org) 00011 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> 00012 * 00013 * All rights reserved. 00014 * 00015 * This program is free software; you can redistribute it and/or modify 00016 * it under the terms of the GNU General Public License as published by 00017 * the Free Software Foundation; either version 2 of the License, or 00018 * (at your option) any later version. 00019 * 00020 * This program is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * You should have received a copy of the GNU General Public License along 00026 * with this program; if not, write to the Free Software Foundation, Inc., 00027 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00028 */ 00029 00030 #ifndef POLARSSL_CIPHER_H 00031 #define POLARSSL_CIPHER_H 00032 00033 #if !defined(POLARSSL_CONFIG_FILE) 00034 #include "config.h" 00035 #else 00036 #include POLARSSL_CONFIG_FILE 00037 #endif 00038 00039 #if defined(POLARSSL_GCM_C) 00040 #define POLARSSL_CIPHER_MODE_AEAD 00041 #endif 00042 00043 #if defined(POLARSSL_CIPHER_MODE_CBC) 00044 #define POLARSSL_CIPHER_MODE_WITH_PADDING 00045 #endif 00046 00047 #include <string.h> 00048 00049 #if defined(_MSC_VER) && !defined(inline) 00050 #define inline _inline 00051 #else 00052 #if defined(__ARMCC_VERSION) && !defined(inline) 00053 #define inline __inline 00054 #endif /* __ARMCC_VERSION */ 00055 #endif /*_MSC_VER */ 00056 00057 #define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */ 00058 #define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters to function. */ 00059 #define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */ 00060 #define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */ 00061 #define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */ 00062 #define POLARSSL_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */ 00063 00064 #ifdef __cplusplus 00065 extern "C" { 00066 #endif 00067 00068 typedef enum { 00069 POLARSSL_CIPHER_ID_NONE = 0, 00070 POLARSSL_CIPHER_ID_NULL, 00071 POLARSSL_CIPHER_ID_AES, 00072 POLARSSL_CIPHER_ID_DES, 00073 POLARSSL_CIPHER_ID_3DES, 00074 POLARSSL_CIPHER_ID_CAMELLIA, 00075 POLARSSL_CIPHER_ID_BLOWFISH, 00076 POLARSSL_CIPHER_ID_ARC4, 00077 } cipher_id_t; 00078 00079 typedef enum { 00080 POLARSSL_CIPHER_NONE = 0, 00081 POLARSSL_CIPHER_NULL, 00082 POLARSSL_CIPHER_AES_128_ECB, 00083 POLARSSL_CIPHER_AES_192_ECB, 00084 POLARSSL_CIPHER_AES_256_ECB, 00085 POLARSSL_CIPHER_AES_128_CBC, 00086 POLARSSL_CIPHER_AES_192_CBC, 00087 POLARSSL_CIPHER_AES_256_CBC, 00088 POLARSSL_CIPHER_AES_128_CFB128, 00089 POLARSSL_CIPHER_AES_192_CFB128, 00090 POLARSSL_CIPHER_AES_256_CFB128, 00091 POLARSSL_CIPHER_AES_128_CTR, 00092 POLARSSL_CIPHER_AES_192_CTR, 00093 POLARSSL_CIPHER_AES_256_CTR, 00094 POLARSSL_CIPHER_AES_128_GCM, 00095 POLARSSL_CIPHER_AES_192_GCM, 00096 POLARSSL_CIPHER_AES_256_GCM, 00097 POLARSSL_CIPHER_CAMELLIA_128_ECB, 00098 POLARSSL_CIPHER_CAMELLIA_192_ECB, 00099 POLARSSL_CIPHER_CAMELLIA_256_ECB, 00100 POLARSSL_CIPHER_CAMELLIA_128_CBC, 00101 POLARSSL_CIPHER_CAMELLIA_192_CBC, 00102 POLARSSL_CIPHER_CAMELLIA_256_CBC, 00103 POLARSSL_CIPHER_CAMELLIA_128_CFB128, 00104 POLARSSL_CIPHER_CAMELLIA_192_CFB128, 00105 POLARSSL_CIPHER_CAMELLIA_256_CFB128, 00106 POLARSSL_CIPHER_CAMELLIA_128_CTR, 00107 POLARSSL_CIPHER_CAMELLIA_192_CTR, 00108 POLARSSL_CIPHER_CAMELLIA_256_CTR, 00109 POLARSSL_CIPHER_CAMELLIA_128_GCM, 00110 POLARSSL_CIPHER_CAMELLIA_192_GCM, 00111 POLARSSL_CIPHER_CAMELLIA_256_GCM, 00112 POLARSSL_CIPHER_DES_ECB, 00113 POLARSSL_CIPHER_DES_CBC, 00114 POLARSSL_CIPHER_DES_EDE_ECB, 00115 POLARSSL_CIPHER_DES_EDE_CBC, 00116 POLARSSL_CIPHER_DES_EDE3_ECB, 00117 POLARSSL_CIPHER_DES_EDE3_CBC, 00118 POLARSSL_CIPHER_BLOWFISH_ECB, 00119 POLARSSL_CIPHER_BLOWFISH_CBC, 00120 POLARSSL_CIPHER_BLOWFISH_CFB64, 00121 POLARSSL_CIPHER_BLOWFISH_CTR, 00122 POLARSSL_CIPHER_ARC4_128, 00123 } cipher_type_t; 00124 00125 typedef enum { 00126 POLARSSL_MODE_NONE = 0, 00127 POLARSSL_MODE_ECB, 00128 POLARSSL_MODE_CBC, 00129 POLARSSL_MODE_CFB, 00130 POLARSSL_MODE_OFB, 00131 POLARSSL_MODE_CTR, 00132 POLARSSL_MODE_GCM, 00133 POLARSSL_MODE_STREAM, 00134 } cipher_mode_t; 00135 00136 typedef enum { 00137 POLARSSL_PADDING_PKCS7 = 0, /**< PKCS7 padding (default) */ 00138 POLARSSL_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding */ 00139 POLARSSL_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding */ 00140 POLARSSL_PADDING_ZEROS, /**< zero padding (not reversible!) */ 00141 POLARSSL_PADDING_NONE, /**< never pad (full blocks only) */ 00142 } cipher_padding_t ; 00143 00144 typedef enum { 00145 POLARSSL_OPERATION_NONE = -1, 00146 POLARSSL_DECRYPT = 0, 00147 POLARSSL_ENCRYPT, 00148 } operation_t; 00149 00150 enum { 00151 /** Undefined key length */ 00152 POLARSSL_KEY_LENGTH_NONE = 0, 00153 /** Key length, in bits (including parity), for DES keys */ 00154 POLARSSL_KEY_LENGTH_DES = 64, 00155 /** Key length, in bits (including parity), for DES in two key EDE */ 00156 POLARSSL_KEY_LENGTH_DES_EDE = 128, 00157 /** Key length, in bits (including parity), for DES in three-key EDE */ 00158 POLARSSL_KEY_LENGTH_DES_EDE3 = 192, 00159 }; 00160 00161 /** Maximum length of any IV, in bytes */ 00162 #define POLARSSL_MAX_IV_LENGTH 16 00163 /** Maximum block size of any cipher, in bytes */ 00164 #define POLARSSL_MAX_BLOCK_LENGTH 16 00165 00166 /** 00167 * Base cipher information. The non-mode specific functions and values. 00168 */ 00169 typedef struct { 00170 00171 /** Base Cipher type (e.g. POLARSSL_CIPHER_ID_AES) */ 00172 cipher_id_t cipher; 00173 00174 /** Encrypt using ECB */ 00175 int (*ecb_func)( void *ctx, operation_t mode, 00176 const unsigned char *input, unsigned char *output ); 00177 00178 /** Encrypt using CBC */ 00179 int (*cbc_func)( void *ctx, operation_t mode, size_t length, 00180 unsigned char *iv, const unsigned char *input, 00181 unsigned char *output ); 00182 00183 /** Encrypt using CFB (Full length) */ 00184 int (*cfb_func)( void *ctx, operation_t mode, size_t length, size_t *iv_off, 00185 unsigned char *iv, const unsigned char *input, 00186 unsigned char *output ); 00187 00188 /** Encrypt using CTR */ 00189 int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, 00190 unsigned char *nonce_counter, unsigned char *stream_block, 00191 const unsigned char *input, unsigned char *output ); 00192 00193 /** Encrypt using STREAM */ 00194 int (*stream_func)( void *ctx, size_t length, 00195 const unsigned char *input, unsigned char *output ); 00196 00197 /** Set key for encryption purposes */ 00198 int (*setkey_enc_func)( void *ctx, const unsigned char *key, 00199 unsigned int key_length ); 00200 00201 /** Set key for decryption purposes */ 00202 int (*setkey_dec_func)( void *ctx, const unsigned char *key, 00203 unsigned int key_length); 00204 00205 /** Allocate a new context */ 00206 void * (*ctx_alloc_func)( void ); 00207 00208 /** Free the given context */ 00209 void (*ctx_free_func)( void *ctx ); 00210 00211 } cipher_base_t; 00212 00213 /** 00214 * Cipher information. Allows cipher functions to be called in a generic way. 00215 */ 00216 typedef struct { 00217 /** Full cipher identifier (e.g. POLARSSL_CIPHER_AES_256_CBC) */ 00218 cipher_type_t type; 00219 00220 /** Cipher mode (e.g. POLARSSL_MODE_CBC) */ 00221 cipher_mode_t mode; 00222 00223 /** Cipher key length, in bits (default length for variable sized ciphers) 00224 * (Includes parity bits for ciphers like DES) */ 00225 unsigned int key_length; 00226 00227 /** Name of the cipher */ 00228 const char * name; 00229 00230 /** IV/NONCE size, in bytes. 00231 * For cipher that accept many sizes: recommended size */ 00232 unsigned int iv_size; 00233 00234 /** Flag for ciphers that accept many sizes of IV/NONCE */ 00235 int accepts_variable_iv_size; 00236 00237 /** block size, in bytes */ 00238 unsigned int block_size; 00239 00240 /** Base cipher information and functions */ 00241 const cipher_base_t *base; 00242 00243 } cipher_info_t; 00244 00245 /** 00246 * Generic cipher context. 00247 */ 00248 typedef struct { 00249 /** Information about the associated cipher */ 00250 const cipher_info_t *cipher_info; 00251 00252 /** Key length to use */ 00253 int key_length; 00254 00255 /** Operation that the context's key has been initialised for */ 00256 operation_t operation; 00257 00258 /** Padding functions to use, if relevant for cipher mode */ 00259 void (*add_padding)( unsigned char *output, size_t olen, size_t data_len ); 00260 int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len ); 00261 00262 /** Buffer for data that hasn't been encrypted yet */ 00263 unsigned char unprocessed_data[POLARSSL_MAX_BLOCK_LENGTH]; 00264 00265 /** Number of bytes that still need processing */ 00266 size_t unprocessed_len; 00267 00268 /** Current IV or NONCE_COUNTER for CTR-mode */ 00269 unsigned char iv[POLARSSL_MAX_IV_LENGTH]; 00270 00271 /** IV size in bytes (for ciphers with variable-length IVs) */ 00272 size_t iv_size; 00273 00274 /** Cipher-specific context */ 00275 void *cipher_ctx; 00276 } cipher_context_t; 00277 00278 /** 00279 * \brief Returns the list of ciphers supported by the generic cipher module. 00280 * 00281 * \return a statically allocated array of ciphers, the last entry 00282 * is 0. 00283 */ 00284 const int *cipher_list( void ); 00285 00286 /** 00287 * \brief Returns the cipher information structure associated 00288 * with the given cipher name. 00289 * 00290 * \param cipher_name Name of the cipher to search for. 00291 * 00292 * \return the cipher information structure associated with the 00293 * given cipher_name, or NULL if not found. 00294 */ 00295 const cipher_info_t *cipher_info_from_string( const char *cipher_name ); 00296 00297 /** 00298 * \brief Returns the cipher information structure associated 00299 * with the given cipher type. 00300 * 00301 * \param cipher_type Type of the cipher to search for. 00302 * 00303 * \return the cipher information structure associated with the 00304 * given cipher_type, or NULL if not found. 00305 */ 00306 const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type ); 00307 00308 /** 00309 * \brief Returns the cipher information structure associated 00310 * with the given cipher id, key size and mode. 00311 * 00312 * \param cipher_id Id of the cipher to search for 00313 * (e.g. POLARSSL_CIPHER_ID_AES) 00314 * \param key_length Length of the key in bits 00315 * \param mode Cipher mode (e.g. POLARSSL_MODE_CBC) 00316 * 00317 * \return the cipher information structure associated with the 00318 * given cipher_type, or NULL if not found. 00319 */ 00320 const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id, 00321 int key_length, 00322 const cipher_mode_t mode ); 00323 00324 /** 00325 * \brief Initialises and fills the cipher context structure with 00326 * the appropriate values. 00327 * 00328 * \param ctx context to initialise. May not be NULL. 00329 * \param cipher_info cipher to use. 00330 * 00331 * \return 0 on success, 00332 * POLARSSL_ERR_CIPHER_BAD_INPUT_DATA on parameter failure, 00333 * POLARSSL_ERR_CIPHER_ALLOC_FAILED if allocation of the 00334 * cipher-specific context failed. 00335 */ 00336 int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info ); 00337 00338 /** 00339 * \brief Free the cipher-specific context of ctx. Freeing ctx 00340 * itself remains the responsibility of the caller. 00341 * 00342 * \param ctx Free the cipher-specific context 00343 * 00344 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if 00345 * parameter verification fails. 00346 */ 00347 int cipher_free_ctx( cipher_context_t *ctx ); 00348 00349 /** 00350 * \brief Returns the block size of the given cipher. 00351 * 00352 * \param ctx cipher's context. Must have been initialised. 00353 * 00354 * \return size of the cipher's blocks, or 0 if ctx has not been 00355 * initialised. 00356 */ 00357 static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx ) 00358 { 00359 if( NULL == ctx || NULL == ctx->cipher_info ) 00360 return 0; 00361 00362 return ctx->cipher_info->block_size; 00363 } 00364 00365 /** 00366 * \brief Returns the mode of operation for the cipher. 00367 * (e.g. POLARSSL_MODE_CBC) 00368 * 00369 * \param ctx cipher's context. Must have been initialised. 00370 * 00371 * \return mode of operation, or POLARSSL_MODE_NONE if ctx 00372 * has not been initialised. 00373 */ 00374 static inline cipher_mode_t cipher_get_cipher_mode( const cipher_context_t *ctx ) 00375 { 00376 if( NULL == ctx || NULL == ctx->cipher_info ) 00377 return POLARSSL_MODE_NONE; 00378 00379 return ctx->cipher_info->mode; 00380 } 00381 00382 /** 00383 * \brief Returns the size of the cipher's IV/NONCE in bytes. 00384 * 00385 * \param ctx cipher's context. Must have been initialised. 00386 * 00387 * \return If IV has not been set yet: (recommended) IV size 00388 * (0 for ciphers not using IV/NONCE). 00389 * If IV has already been set: actual size. 00390 */ 00391 static inline int cipher_get_iv_size( const cipher_context_t *ctx ) 00392 { 00393 if( NULL == ctx || NULL == ctx->cipher_info ) 00394 return 0; 00395 00396 if( ctx->iv_size != 0 ) 00397 return (int) ctx->iv_size; 00398 00399 return ctx->cipher_info->iv_size; 00400 } 00401 00402 /** 00403 * \brief Returns the type of the given cipher. 00404 * 00405 * \param ctx cipher's context. Must have been initialised. 00406 * 00407 * \return type of the cipher, or POLARSSL_CIPHER_NONE if ctx has 00408 * not been initialised. 00409 */ 00410 static inline cipher_type_t cipher_get_type( const cipher_context_t *ctx ) 00411 { 00412 if( NULL == ctx || NULL == ctx->cipher_info ) 00413 return POLARSSL_CIPHER_NONE; 00414 00415 return ctx->cipher_info->type; 00416 } 00417 00418 /** 00419 * \brief Returns the name of the given cipher, as a string. 00420 * 00421 * \param ctx cipher's context. Must have been initialised. 00422 * 00423 * \return name of the cipher, or NULL if ctx was not initialised. 00424 */ 00425 static inline const char *cipher_get_name( const cipher_context_t *ctx ) 00426 { 00427 if( NULL == ctx || NULL == ctx->cipher_info ) 00428 return 0; 00429 00430 return ctx->cipher_info->name; 00431 } 00432 00433 /** 00434 * \brief Returns the key length of the cipher. 00435 * 00436 * \param ctx cipher's context. Must have been initialised. 00437 * 00438 * \return cipher's key length, in bits, or 00439 * POLARSSL_KEY_LENGTH_NONE if ctx has not been 00440 * initialised. 00441 */ 00442 static inline int cipher_get_key_size ( const cipher_context_t *ctx ) 00443 { 00444 if( NULL == ctx || NULL == ctx->cipher_info ) 00445 return POLARSSL_KEY_LENGTH_NONE; 00446 00447 return ctx->cipher_info->key_length; 00448 } 00449 00450 /** 00451 * \brief Returns the operation of the given cipher. 00452 * 00453 * \param ctx cipher's context. Must have been initialised. 00454 * 00455 * \return operation (POLARSSL_ENCRYPT or POLARSSL_DECRYPT), 00456 * or POLARSSL_OPERATION_NONE if ctx has not been 00457 * initialised. 00458 */ 00459 static inline operation_t cipher_get_operation( const cipher_context_t *ctx ) 00460 { 00461 if( NULL == ctx || NULL == ctx->cipher_info ) 00462 return POLARSSL_OPERATION_NONE; 00463 00464 return ctx->operation; 00465 } 00466 00467 /** 00468 * \brief Set the key to use with the given context. 00469 * 00470 * \param ctx generic cipher context. May not be NULL. Must have been 00471 * initialised using cipher_context_from_type or 00472 * cipher_context_from_string. 00473 * \param key The key to use. 00474 * \param key_length key length to use, in bits. 00475 * \param operation Operation that the key will be used for, either 00476 * POLARSSL_ENCRYPT or POLARSSL_DECRYPT. 00477 * 00478 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if 00479 * parameter verification fails or a cipher specific 00480 * error code. 00481 */ 00482 int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, 00483 int key_length, const operation_t operation ); 00484 00485 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) 00486 /** 00487 * \brief Set padding mode, for cipher modes that use padding. 00488 * (Default: PKCS7 padding.) 00489 * 00490 * \param ctx generic cipher context 00491 * \param mode padding mode 00492 * 00493 * \returns 0 on success, POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE 00494 * if selected padding mode is not supported, or 00495 * POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode 00496 * does not support padding. 00497 */ 00498 int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode ); 00499 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ 00500 00501 /** 00502 * \brief Set the initialization vector (IV) or nonce 00503 * 00504 * \param ctx generic cipher context 00505 * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers) 00506 * \param iv_len IV length for ciphers with variable-size IV; 00507 * discarded by ciphers with fixed-size IV. 00508 * 00509 * \returns O on success, or POLARSSL_ERR_CIPHER_BAD_INPUT_DATA 00510 * 00511 * \note Some ciphers don't use IVs nor NONCE. For these 00512 * ciphers, this function has no effect. 00513 */ 00514 int cipher_set_iv( cipher_context_t *ctx, 00515 const unsigned char *iv, size_t iv_len ); 00516 00517 /** 00518 * \brief Finish preparation of the given context 00519 * 00520 * \param ctx generic cipher context 00521 * 00522 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA 00523 * if parameter verification fails. 00524 */ 00525 int cipher_reset( cipher_context_t *ctx ); 00526 00527 #if defined(POLARSSL_CIPHER_MODE_AEAD) 00528 /** 00529 * \brief Add additional data (for AEAD ciphers). 00530 * This function has no effect for non-AEAD ciphers. 00531 * For AEAD ciphers, it may or may not be called 00532 * repeatedly, and/or interleaved with calls to 00533 * cipher_udpate(), depending on the cipher. 00534 * E.g. for GCM is must be called exactly once, right 00535 * after cipher_reset(). 00536 * 00537 * \param ctx generic cipher context 00538 * \param ad Additional data to use. 00539 * \param ad_len Length of ad. 00540 * 00541 * \returns 0 on success, or a specific error code. 00542 */ 00543 int cipher_update_ad( cipher_context_t *ctx, 00544 const unsigned char *ad, size_t ad_len ); 00545 #endif /* POLARSSL_CIPHER_MODE_AEAD */ 00546 00547 /** 00548 * \brief Generic cipher update function. Encrypts/decrypts 00549 * using the given cipher context. Writes as many block 00550 * size'd blocks of data as possible to output. Any data 00551 * that cannot be written immediately will either be added 00552 * to the next block, or flushed when cipher_final is 00553 * called. 00554 * Exception: for POLARSSL_MODE_ECB, expects single block 00555 * in size (e.g. 16 bytes for AES) 00556 * 00557 * \param ctx generic cipher context 00558 * \param input buffer holding the input data 00559 * \param ilen length of the input data 00560 * \param output buffer for the output data. Should be able to hold at 00561 * least ilen + block_size. Cannot be the same buffer as 00562 * input! 00563 * \param olen length of the output data, will be filled with the 00564 * actual number of bytes written. 00565 * 00566 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if 00567 * parameter verification fails, 00568 * POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE on an 00569 * unsupported mode for a cipher or a cipher specific 00570 * error code. 00571 * 00572 * \note If the underlying cipher is GCM, all calls to this 00573 * function, except the last one before cipher_finish(), 00574 * must have ilen a multiple of the block size. 00575 */ 00576 int cipher_update( cipher_context_t *ctx, const unsigned char *input, 00577 size_t ilen, unsigned char *output, size_t *olen ); 00578 00579 /** 00580 * \brief Generic cipher finalisation function. If data still 00581 * needs to be flushed from an incomplete block, data 00582 * contained within it will be padded with the size of 00583 * the last block, and written to the output buffer. 00584 * 00585 * \param ctx Generic cipher context 00586 * \param output buffer to write data to. Needs block_size available. 00587 * \param olen length of the data written to the output buffer. 00588 * 00589 * \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA if 00590 * parameter verification fails, 00591 * POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption 00592 * expected a full block but was not provided one, 00593 * POLARSSL_ERR_CIPHER_INVALID_PADDING on invalid padding 00594 * while decrypting or a cipher specific error code. 00595 */ 00596 int cipher_finish( cipher_context_t *ctx, 00597 unsigned char *output, size_t *olen ); 00598 00599 #if defined(POLARSSL_CIPHER_MODE_AEAD) 00600 /** 00601 * \brief Write tag for AEAD ciphers. 00602 * No effect for other ciphers. 00603 * Must be called after cipher_finish(). 00604 * 00605 * \param ctx Generic cipher context 00606 * \param tag buffer to write the tag 00607 * \param tag_len Length of the tag to write 00608 * 00609 * \return 0 on success, or a specific error code. 00610 */ 00611 int cipher_write_tag( cipher_context_t *ctx, 00612 unsigned char *tag, size_t tag_len ); 00613 00614 /** 00615 * \brief Check tag for AEAD ciphers. 00616 * No effect for other ciphers. 00617 * Calling time depends on the cipher: 00618 * for GCM, must be called after cipher_finish(). 00619 * 00620 * \param ctx Generic cipher context 00621 * \param tag Buffer holding the tag 00622 * \param tag_len Length of the tag to check 00623 * 00624 * \return 0 on success, or a specific error code. 00625 */ 00626 int cipher_check_tag( cipher_context_t *ctx, 00627 const unsigned char *tag, size_t tag_len ); 00628 #endif /* POLARSSL_CIPHER_MODE_AEAD */ 00629 00630 /** 00631 * \brief Checkup routine 00632 * 00633 * \return 0 if successful, or 1 if the test failed 00634 */ 00635 int cipher_self_test( int verbose ); 00636 00637 #ifdef __cplusplus 00638 } 00639 #endif 00640 00641 #endif /* POLARSSL_CIPHER_H */ 00642 00643
Generated on Tue Jul 12 2022 19:40:15 by
1.7.2